Design document for policy2tosca patch set-2
[parser.git] / yang2tosca / tosca_translator.py
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 #    http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10 # implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 import os
15 import sys
16 import getopt
17 import argparse
18 from lxml import etree
19
20
21 def tosca_translator(file_name):
22     if os.path.exists(file_name):
23         var = file_name
24         file_format = var.split('.')
25         if len(file_format) == 2:
26             if file_format[1] == 'yaml' or file_format[1] == 'yang':
27                 print("Its a yaml file")
28                 os.system('pyang -f yin '+var + ' -o ' + file_format[0]+'.xml')
29             elif file_format[1] == 'xml':
30                 print ("Its a "+file_format[1]+" file")
31
32             else:
33                 print ("File format not supported exiting script.")
34                 exit()
35         else:
36             print ("File format not supported exiting script.")
37             exit()
38         tree = etree.parse(file_format[0]+'.xml')
39
40         doc = tree.getroot()
41
42         xslt_root = etree.parse('tosca_transformer.xslt')
43
44         transform = etree.XSLT(xslt_root)
45
46         result_tree = transform(doc)
47         output_file = file_format[0]+'_tosca.yaml'
48
49         f = open(output_file, "w")
50         f.write(str(result_tree))
51         f.close()
52         print ("TOSCA file generated with name "+output_file)
53     else:
54         print ("File does not exist, exiting the script")
55         exit()
56 if __name__ == '__main__':
57     parser = argparse.ArgumentParser(add_help=True)
58     parser.add_argument('-n', '--filename', dest='filename', required=True,
59                         help='Please enter the YANG file name: ')
60     args = parser.parse_args()
61     file_name = str(args.filename)
62     tosca_translator(file_name)