4 # Copyright 2016 RIFT.IO Inc
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
24 import xml
.etree
.ElementTree
as etree
26 from gi
.repository
import (
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
)
42 if descr_type
== "nsd":
43 descr
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd()
45 descr
= VnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd()
47 if input_format
== 'json':
48 json_str
= open(infile
).read()
49 descr
.from_json(model
, json_str
)
51 elif input_format
.strip() == 'xml':
52 tree
= etree
.parse(infile
)
54 xmlstr
= etree
.tostring(root
, encoding
="unicode")
55 descr
.from_xml_v2(model
, xmlstr
)
57 raise("Invalid input format for the descriptor")
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
):
65 shutil
.copy(infile
, dirpath
)
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 )
77 args
= parser
.parse_args()
79 input_format
= args
.format
81 dtype
= args
.descriptor_type
83 print('Reading file {} in format {}'.format(infile
, input_format
))
84 module_list
= ['vld', 'rw-vld']
86 module_list
.extend(['nsd', 'rw-nsd'])
88 module_list
.extend(['vnfd', 'rw-vnfd'])
90 descr
= read_from_file(module_list
, args
.infile
, args
.format
, dtype
)
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
))
99 if __name__
== "__main__":