| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # |
| 4 | # Copyright 2016 RIFT.IO Inc |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
| 19 | import argparse |
| 20 | import logging |
| 21 | import rift.auto.proxy |
| 22 | import rift.vcs |
| 23 | import sys |
| 24 | |
| 25 | import gi |
| 26 | gi.require_version('RwYang', '1.0') |
| 27 | |
| 28 | from gi.repository import NsdYang, VldYang, VnfdYang, RwYang |
| 29 | |
| 30 | logging.basicConfig(level=logging.DEBUG) |
| 31 | logger = logging.getLogger(__name__) |
| 32 | |
| 33 | model = RwYang.Model.create_libncx() |
| 34 | model.load_schema_ypbc(VldYang.get_schema()) |
| 35 | model.load_schema_ypbc(NsdYang.get_schema()) |
| 36 | model.load_schema_ypbc(VnfdYang.get_schema()) |
| 37 | |
| 38 | |
| 39 | def configure_vld(proxy, vld_xml_hdl): |
| 40 | vld_xml = vld_xml_hdl.read() |
| 41 | logger.debug("Attempting to deserialize XML into VLD protobuf: %s", vld_xml) |
| 42 | vld = VldYang.YangData_Vld_VldCatalog_Vld() |
| 43 | vld.from_xml_v2(model, vld_xml) |
| 44 | |
| 45 | logger.debug("Sending VLD to netconf: %s", vld) |
| 46 | proxy.merge_config(vld.to_xml_v2(model)) |
| 47 | |
| 48 | |
| 49 | def configure_vnfd(proxy, vnfd_xml_hdl): |
| 50 | vnfd_xml = vnfd_xml_hdl.read() |
| 51 | logger.debug("Attempting to deserialize XML into VNFD protobuf: %s", vnfd_xml) |
| 52 | vnfd = VnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd() |
| 53 | vnfd.from_xml_v2(model, vnfd_xml) |
| 54 | |
| 55 | logger.debug("Sending VNFD to netconf: %s", vnfd) |
| 56 | proxy.merge_config(vnfd.to_xml_v2(model)) |
| 57 | |
| 58 | |
| 59 | def configure_nsd(proxy, nsd_xml_hdl): |
| 60 | nsd_xml = nsd_xml_hdl.read() |
| 61 | logger.debug("Attempting to deserialize XML into NSD protobuf: %s", nsd_xml) |
| 62 | nsd = NsdYang.YangData_Nsd_NsdCatalog_Nsd() |
| 63 | nsd.from_xml_v2(model, nsd_xml) |
| 64 | |
| 65 | logger.debug("Sending NSD to netconf: %s", nsd) |
| 66 | proxy.merge_config(nsd.to_xml_v2(model)) |
| 67 | |
| 68 | |
| 69 | def parse_args(argv=sys.argv[1:]): |
| 70 | """Create a parser which includes all generic demo arguments and parse args |
| 71 | |
| 72 | Arguments: |
| 73 | argv - arguments to be parsed |
| 74 | |
| 75 | Returns: List of parsed arguments |
| 76 | """ |
| 77 | |
| 78 | parser = argparse.ArgumentParser() |
| 79 | parser.add_argument( |
| 80 | '--confd-host', |
| 81 | default="127.0.0.1", |
| 82 | help="Hostname or IP where the confd netconf server is running.") |
| 83 | |
| 84 | parser.add_argument( |
| 85 | '--vld-xml-file', |
| 86 | action="append", |
| 87 | default=[], |
| 88 | type=argparse.FileType(), |
| 89 | help="VLD XML File Path", |
| 90 | ) |
| 91 | |
| 92 | parser.add_argument( |
| 93 | '--vnfd-xml-file', |
| 94 | action="append", |
| 95 | default=[], |
| 96 | type=argparse.FileType(), |
| 97 | help="VNFD XML File Path", |
| 98 | ) |
| 99 | |
| 100 | parser.add_argument( |
| 101 | '--nsd-xml-file', |
| 102 | action="append", |
| 103 | default=[], |
| 104 | type=argparse.FileType(), |
| 105 | help="VNFD XML File Path", |
| 106 | ) |
| 107 | |
| 108 | parser.add_argument( |
| 109 | '-v', '--verbose', |
| 110 | action='store_true', |
| 111 | help="Logging is normally set to an INFO level. When this flag " |
| 112 | "is used logging is set to DEBUG. ") |
| 113 | |
| 114 | args = parser.parse_args(argv) |
| 115 | |
| 116 | return args |
| 117 | |
| 118 | |
| 119 | def connect(args): |
| 120 | # Initialize Netconf Management Proxy |
| 121 | mgmt_proxy = rift.auto.proxy.NetconfProxy(args.confd_host) |
| 122 | mgmt_proxy.connect() |
| 123 | |
| 124 | # Ensure system started |
| 125 | vcs_component_info = rift.vcs.mgmt.VcsComponentInfo(mgmt_proxy) |
| 126 | vcs_component_info.wait_until_system_started() |
| 127 | |
| 128 | return mgmt_proxy |
| 129 | |
| 130 | |
| 131 | def main(): |
| 132 | args = parse_args() |
| 133 | proxy = connect(args) |
| 134 | for xml_file in args.vnfd_xml_file: |
| 135 | configure_vnfd(proxy, xml_file) |
| 136 | |
| 137 | for xml_file in args.vld_xml_file: |
| 138 | configure_vld(proxy, xml_file) |
| 139 | |
| 140 | for xml_file in args.nsd_xml_file: |
| 141 | configure_nsd(proxy, xml_file) |
| 142 | |
| 143 | |
| 144 | if __name__ == "__main__": |
| 145 | main() |
| 146 | |