| 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 sys |
| 20 | import os |
| 21 | import subprocess |
| 22 | import argparse |
| 23 | import shutil |
| 24 | import xml.etree.ElementTree as etree |
| 25 | |
| 26 | from gi.repository import ( |
| 27 | RwYang, |
| 28 | NsdYang, |
| 29 | RwNsdYang, |
| 30 | VnfdYang, |
| 31 | RwVnfdYang, |
| 32 | VldYang, |
| 33 | RwVldYang |
| 34 | ) |
| 35 | |
| 36 | def read_from_file(module_list, infile, input_format, descr_type): |
| 37 | model = RwYang.Model.create_libncx() |
| 38 | for module in module_list: |
| 39 | model.load_module(module) |
| 40 | |
| 41 | descr = None |
| 42 | if descr_type == "nsd": |
| 43 | descr = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd() |
| 44 | else: |
| 45 | descr = VnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd() |
| 46 | |
| 47 | if input_format == 'json': |
| 48 | json_str = open(infile).read() |
| 49 | descr.from_json(model, json_str) |
| 50 | |
| 51 | elif input_format.strip() == 'xml': |
| 52 | tree = etree.parse(infile) |
| 53 | root = tree.getroot() |
| 54 | xmlstr = etree.tostring(root, encoding="unicode") |
| 55 | descr.from_xml_v2(model, xmlstr) |
| 56 | else: |
| 57 | raise("Invalid input format for the descriptor") |
| 58 | |
| 59 | return descr |
| 60 | |
| 61 | def write_to_file(name, outdir, infile, descr_type): |
| 62 | dirpath = os.path.join(outdir, name, descr_type) |
| 63 | if not os.path.exists(dirpath): |
| 64 | os.makedirs(dirpath) |
| 65 | shutil.copy(infile, dirpath) |
| 66 | |
| 67 | def main(argv=sys.argv[1:]): |
| 68 | global outdir, output_format |
| 69 | parser = argparse.ArgumentParser() |
| 70 | parser.add_argument('-i', '--infile', required=True, |
| 71 | type=lambda x: os.path.isfile(x) and x or parser.error("INFILE does not exist")) |
| 72 | parser.add_argument('-o', '--outdir', default=".", |
| 73 | type=lambda x: os.path.isdir(x) and x or parser.error("OUTDIR does not exist")) |
| 74 | parser.add_argument('-f', '--format', choices=['json', 'xml'], required=True) |
| 75 | parser.add_argument('-t', '--descriptor-type', choices=['nsd', 'vnfd'], required=True ) |
| 76 | |
| 77 | args = parser.parse_args() |
| 78 | infile = args.infile |
| 79 | input_format = args.format |
| 80 | outdir = args.outdir |
| 81 | dtype = args.descriptor_type |
| 82 | |
| 83 | print('Reading file {} in format {}'.format(infile, input_format)) |
| 84 | module_list = ['vld', 'rw-vld'] |
| 85 | if dtype == 'nsd': |
| 86 | module_list.extend(['nsd', 'rw-nsd']) |
| 87 | else: |
| 88 | module_list.extend(['vnfd', 'rw-vnfd']) |
| 89 | |
| 90 | descr = read_from_file(module_list, args.infile, args.format, dtype) |
| 91 | |
| 92 | print("Creating %s descriptor for {}".format(dtype.upper(), descr.name)) |
| 93 | write_to_file(descr.name, outdir, infile, dtype) |
| 94 | status = subprocess.call(os.path.join(os.environ["RIFT_INSTALL"], |
| 95 | "/usr/rift/toolchain/cmake/bin/generate_descriptor_pkg.sh %s %s" % (outdir, descr.name)), shell=True) |
| 96 | print("Status of %s descriptor package creation is: %s" % (dtype.upper(), status)) |
| 97 | |
| 98 | |
| 99 | if __name__ == "__main__": |
| 100 | main() |
| 101 | |
| 102 | |