blob: 8962c5f5e9b686ff6c27c103fd89788296dbf6cf [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'''
Dilip kumar Pampana108ff432018-01-08 15:08:21 -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
39leaf = ""
40val = ""
41li = []
42
43if len(sys.argv) < 3:
44 print 'yang2props.py <input yang> <output properties>'
45 sys.exit(2)
46
47with open(sys.argv[1], "r") as ins:
48 for line in ins:
49 # if we see a leaf save the name for later
50 if "leaf " in line:
51 match = re.search(r'leaf (\S+)', line)
52 if match:
53 leaf = match.group(1)
Skip Wonnellfaae86d2018-02-20 14:42:34 -060054
Dilip kumar Pampana108ff432018-01-08 15:08:21 -050055 # if we see enum convert the value to enum format and see if it changed
56 # if the value is different write a property entry
57 if "enum " in line:
58 match = re.search(r'enum "(\S+)";', line)
59 if match:
60 val = match.group(1)
61 enum = to_enum(val)
62
63 # see if converting to enum changed the string
64 if val != enum:
65 property = "yang."+leaf+"."+enum+"="+val
66 if property not in li:
67 li.append( property)
68
69
70# Open output file
71fo = open(sys.argv[2], "wb")
72fo.write("# yang conversion properties \n")
73fo.write("# used to convert Enum back to the original yang value \n")
74fo.write("\n".join(li))
75fo.write("\n")
76
77# Close opend file
78fo.close()
79
Skip Wonnellfaae86d2018-02-20 14:42:34 -060080