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