blob: dd948c99f821be25bb4b86f7ee37d7e8d23d8fe7 [file] [log] [blame]
Skip Wonnellfaae86d2018-02-20 14:42:34 -06001'''
2/*-
3* ============LICENSE_START=======================================================
4* ONAP : APPC
5* ================================================================================
6* Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
7* =============================================================================
8* Licensed under the Apache License, Version 2.0 (the "License");
9* you may not use this file except in compliance with the License.
10* You may obtain a copy of the License at
11*
12* http://www.apache.org/licenses/LICENSE-2.0
13*
14* Unless required by applicable law or agreed to in writing, software
15* distributed under the License is distributed on an "AS IS" BASIS,
16* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17* See the License for the specific language governing permissions and
18* limitations under the License.
19*
20* ECOMP is a trademark and service mark of AT&T Intellectual Property.
21* ============LICENSE_END=========================================================
22*/
23'''
Skip Wonnellab6c2c02017-08-14 17:47:10 -050024#!/usr/bin/python
25
26import re
27import sys
28
29
30# Convert word from foo-bar to FooBar
31# words begining with a digit will be converted to _digit
32def to_enum(s):
33 if s[0].isdigit():
34 s = "_" + s
35 else:
36 s = s[0].upper() + s[1:]
37 return re.sub(r'(?!^)-([a-zA-Z])', lambda m: m.group(1).upper(), s)
38
Ryan Youngb96311a2018-04-06 17:32:41 -040039
Skip Wonnellab6c2c02017-08-14 17:47:10 -050040leaf = ""
41val = ""
42li = []
43
44if len(sys.argv) < 3:
Ryan Youngb96311a2018-04-06 17:32:41 -040045 print('yang2props.py <input yang> <output properties>')
Skip Wonnellab6c2c02017-08-14 17:47:10 -050046 sys.exit(2)
47
48with open(sys.argv[1], "r") as ins:
49 for line in ins:
50 # if we see a leaf save the name for later
51 if "leaf " in line:
Ryan Youngb96311a2018-04-06 17:32:41 -040052 match = re.search(r'leaf (\S+)', line)
53 if match:
54 leaf = match.group(1)
55
Skip Wonnellab6c2c02017-08-14 17:47:10 -050056 # if we see enum convert the value to enum format and see if it changed
57 # if the value is different write a property entry
58 if "enum " in line:
Ryan Youngb96311a2018-04-06 17:32:41 -040059 match = re.search(r'enum "(\S+)";', line)
60 if match:
Skip Wonnellab6c2c02017-08-14 17:47:10 -050061 val = match.group(1)
Ryan Youngb96311a2018-04-06 17:32:41 -040062 enum = to_enum(val)
Skip Wonnellab6c2c02017-08-14 17:47:10 -050063 # see if converting to enum changed the string
Ryan Youngb96311a2018-04-06 17:32:41 -040064 if val != enum:
65 property = "yang." + leaf + "." + enum + "=" + val
66 if property not in li:
67 li.append(property)
Skip Wonnellab6c2c02017-08-14 17:47:10 -050068# Open output file
Ryan Youngb96311a2018-04-06 17:32:41 -040069fo = open(sys.argv[2], "w")
Skip Wonnellab6c2c02017-08-14 17:47:10 -050070fo.write("# yang conversion properties \n")
71fo.write("# used to convert Enum back to the original yang value \n")
72fo.write("\n".join(li))
73fo.write("\n")
74
75# Close opend file
76fo.close()
Skip Wonnellab6c2c02017-08-14 17:47:10 -050077