[CCSDK-987]Create GR Toolkit
[ccsdk/sli/plugins.git] / grToolkit / model / scripts / python / yang2props.py
1 #!/usr/bin/python
2
3 ###
4 # ============LICENSE_START=======================================================
5 # openECOMP : SDN-C
6 # ================================================================================
7 # Copyright (C) 2018 AT&T Intellectual Property. All rights
8 #                       reserved.
9 # ================================================================================
10 # Licensed under the Apache License, Version 2.0 (the "License");
11 # you may not use this file except in compliance with the License.
12 # You may obtain a copy of the License at
13 #
14 #      http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS,
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 # See the License for the specific language governing permissions and
20 # limitations under the License.
21 # ============LICENSE_END=========================================================
22 ###
23
24 import re
25 import sys
26
27
28 # Convert word from foo-bar to FooBar
29 # words begining with a digit will be converted to _digit
30 def to_enum(s):
31     if s[0].isdigit():
32         s = "_" + s
33     else:
34         s = s[0].upper() + s[1:]
35     return re.sub(r'(?!^)-([a-zA-Z])', lambda m: m.group(1).upper(), s)
36
37 leaf = ""
38 val = ""
39 li = []
40
41 if len(sys.argv) < 3:
42      print 'yang2props.py <input yang> <output properties>'
43      sys.exit(2)
44
45 with open(sys.argv[1], "r") as ins:
46     for line in ins:
47         # if we see a leaf save the name for later
48         if "leaf " in line:
49             match = re.search(r'leaf (\S+)', line)
50             if match:
51                  leaf = match.group(1)
52       
53         # if we see enum convert the value to enum format and see if it changed
54         # if the value is different write a property entry
55         if "enum " in line:
56             match = re.search(r'enum "(\S+)";', line)
57             if match:
58                 val = match.group(1)
59                 enum = to_enum(val)
60
61                 # see if converting to enum changed the string
62                 if val != enum:
63                     property = "yang."+leaf+"."+enum+"="+val
64                     if property not in li:
65                         li.append( property)
66
67
68 # Open output file
69 fo = open(sys.argv[2], "wb")
70 fo.write("# yang conversion properties \n")
71 fo.write("# used to convert Enum back to the original yang value \n")
72 fo.write("\n".join(li))
73 fo.write("\n")
74
75 # Close opend file
76 fo.close()
77
78