copy_config.py revision c85493b5
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_SOURCE_TEMPLATE = """<source xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">%s</source>"""
21
22
23def _copy_config(config_filename, host='localhost', port=2831, username='admin', password='admin'):
24    with manager.connect(host=host, port=port, username=username, password=password, hostkey_verify=False) as m:
25        logger.info("Connected to HC")
26        with open(config_filename, 'r') as f:
27            ret = m.copy_config(target='candidate', source=_SOURCE_TEMPLATE % f.read())
28            logger.debug("CopyConfig successful:\n%s" % ret)
29        ret = m.commit()
30        logger.debug("Commit successful:\n%s", ret)
31
32
33if __name__ == '__main__':
34    argparser = argparse.ArgumentParser(description="Configures VPP using <copy-config> RPC")
35    argparser.add_argument('config_filename', help="name of XML file with <config> element")
36    argparser.add_argument('--verbose', help="increase output verbosity", action="store_true")
37    args = argparser.parse_args()
38
39    logger = logging.getLogger("hc2vpp.examples.copy_config")
40    if args.verbose:
41        logging.basicConfig(level=logging.DEBUG)
42    else:
43        logging.basicConfig(level=logging.WARNING)
44
45    _copy_config(args.config_filename)
46