edit_config.py revision 16d190d2
1#!/usr/bin/env python2
2#
3# Copyright (c) 2018 Cisco and/or its affiliates.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at:
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import logging
18from ncclient import manager
19
20
21def _edit_config(config_filename, host='localhost', port=2831, username='admin', password='admin',
22                 validate=False, commit=False):
23    with manager.connect(host=host, port=port, username=username, password=password, hostkey_verify=False) as m:
24        logger.info("Connected to HC")
25        with open(config_filename, 'r') as f:
26            ret = m.edit_config(config=f.read())
27            logger.debug("EditConfig successful:\n%s" % ret)
28            validate = m.validate()
29            logger.debug("Validate successful:\n%s" % validate)
30            commit = m.commit()
31            logger.debug("Commit successful:\n%s" % commit)
32
33if __name__ == '__main__':
34    logger = logging.getLogger("hc2vpp.examples.edit_config")
35    logging.basicConfig(level=logging.WARNING)
36    argparser = argparse.ArgumentParser(description="Configures VPP using <edit-config> RPC")
37    argparser.add_argument('config_filename', help="name of XML file with <config> element")
38    argparser.add_argument('-v', '--validate', help="sends <validate> RPC is <edit-config> was successful",
39                           action="store_true")
40    argparser.add_argument('-c', '--commit', help="commits candidate configuration",
41                           action="store_true")
42    args = argparser.parse_args()
43    _edit_config(args.config_filename, validate=args.validate, commit=args.commit)
44