2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.restcore.util;
24 import freemarker.template.Configuration;
25 import freemarker.template.Template;
26 import freemarker.template.TemplateException;
31 import org.onap.aai.serialization.db.EdgeRules;
32 import org.onap.aai.introspection.Version;
34 public class GenerateEdgeRules {
36 public static void main(String[] args) throws IOException, TemplateException {
38 String filename = "/edgeLabelMigration.csv";
39 InputStream inputStream = GenerateEdgeRules.class.getResourceAsStream(filename);
40 Map<String, Integer> headers = new HashMap<>();
42 List<EdgeRuleBean> rulesToWriteV12 = new ArrayList<>();
43 List<EdgeRuleBean> rulesToWriteV7 = new ArrayList<>();
44 List<EdgeRuleBean> rulesToWriteV8 = new ArrayList<>();
45 List<EdgeRuleBean> rulesToWriteV9 = new ArrayList<>();
46 List<EdgeRuleBean> rulesToWriteV10 = new ArrayList<>();
47 List<EdgeRuleBean> rulesToWriteV11 = new ArrayList<>();
49 ArrayList <String> rulesWeAlreadyHave = new ArrayList <String>();
51 EdgeRules rulesV8 = EdgeRules.getInstance(Version.v8);
52 EdgeRules rulesV9 = EdgeRules.getInstance(Version.v9);
53 EdgeRules rulesV10 = EdgeRules.getInstance(Version.v10);
54 EdgeRules rulesV11 = EdgeRules.getInstance(Version.v11);
56 try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
60 while ((line = reader.readLine()) != null) {
62 headers = retrieveHeaderMap(line);
65 EdgeRuleBean data = new EdgeRuleBean();
66 String[] columns = line.split(",");
67 String oldNodeA = columns[headers.get("from")];
68 String oldNodeB = columns[headers.get("to")];
69 String oldEdgeLabel = columns[headers.get("label")];
71 String nodeA = columns[headers.get("new from")];
73 String nodeB = columns[headers.get("new to")];
76 String edgeLabel = columns[headers.get("new label")];
77 data.setLabel( edgeLabel );
80 // Note: it is assumed that if we know the two NodeTypes and the edgeLabel, we can
81 // uniquely identify an edgeRule -- so if that key is found twice, it is a
82 // problem with our CSV file. Note -we check with the nodeTypes in both directions.
83 String key1 = nodeA + "|" + nodeB + "|" + edgeLabel;
84 String key2 = nodeB + "|" + nodeA + "|" + edgeLabel;
85 if( rulesWeAlreadyHave.contains(key1) ){
86 throw new Exception ("Duplicate rule found for [" + key1 + "] -- please fix the CSV file. ");
88 else if( rulesWeAlreadyHave.contains(key2) ){
89 throw new Exception ("Duplicate rule found for [" + key2 + "] -- please fix the CSV file. ");
92 rulesWeAlreadyHave.add(key1);
93 rulesWeAlreadyHave.add(key2);
96 String direction = columns[headers.get("new direction")];
97 data.setDirection(direction);
99 String multiplicity = columns[headers.get("new multiplicity")];
100 data.setMultiplicity(multiplicity);
102 String lineage = columns[headers.get("new contains-other-v")];
103 data.setLineage(lineage);
105 String deleteOtherV = columns[headers.get("new delete-other-v")];
106 data.setDeleteOtherV(deleteOtherV);
108 String svcInfra = columns[headers.get("new SVC-INFRA")];
109 data.setSvcInfra(svcInfra);
111 String prevDel = columns[headers.get("new prevent-delete")];
112 data.setPreventDelete(prevDel);
114 String defaultVal = columns[headers.get("new default")];
115 if( defaultVal.equals("T") ){
116 data.setDefault("true");
118 else if( defaultVal.equals("F") ){
119 data.setDefault("false");
122 rulesToWriteV12.add(data);
124 if( rulesV8.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
125 rulesToWriteV8.add(data);
128 if( rulesV9.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
129 rulesToWriteV9.add(data);
132 if( rulesV10.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
133 rulesToWriteV10.add(data);
136 if( rulesV11.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
137 rulesToWriteV11.add(data);
143 Configuration configuration = new Configuration();
144 Template template = configuration.getTemplate("src/main/resources/edgerulesTemplate.ftlh");
145 Writer file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v12.json"));
146 Map<String, List<EdgeRuleBean>> wrappedRules = new HashMap<>();
147 wrappedRules.put("wrappedRules", rulesToWriteV12);
148 template.process(wrappedRules, file);
151 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v7.json"));
152 wrappedRules = new HashMap<>();
153 wrappedRules.put("wrappedRules", rulesToWriteV7);
154 template.process(wrappedRules, file);
157 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v8.json"));
158 wrappedRules = new HashMap<>();
159 wrappedRules.put("wrappedRules", rulesToWriteV8);
160 template.process(wrappedRules, file);
164 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v9.json"));
165 wrappedRules = new HashMap<>();
166 wrappedRules.put("wrappedRules", rulesToWriteV9);
167 template.process(wrappedRules, file);
170 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v10.json"));
171 wrappedRules = new HashMap<>();
172 wrappedRules.put("wrappedRules", rulesToWriteV10);
173 template.process(wrappedRules, file);
176 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v11.json"));
177 wrappedRules = new HashMap<>();
178 wrappedRules.put("wrappedRules", rulesToWriteV11);
179 template.process(wrappedRules, file);
182 } catch(Exception ex){
183 ex.printStackTrace();
189 private static Map<String, Integer> retrieveHeaderMap(String line){
192 throw new NullPointerException();
194 String[] columnNames = line.split(",");
196 Map<String, Integer> map = new HashMap<String, Integer>();
200 for(String columnName : columnNames){
201 map.put(columnName, index++);