2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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 package org.onap.aai.restcore.util;
22 import freemarker.template.Configuration;
23 import freemarker.template.Template;
24 import freemarker.template.TemplateException;
29 import org.onap.aai.serialization.db.EdgeRules;
30 import org.onap.aai.introspection.Version;
32 public class GenerateEdgeRules {
34 public static void main(String[] args) throws IOException, TemplateException {
36 String filename = "/edgeLabelMigration.csv";
37 InputStream inputStream = GenerateEdgeRules.class.getResourceAsStream(filename);
38 Map<String, Integer> headers = new HashMap<>();
40 List<EdgeRuleBean> rulesToWriteV12 = new ArrayList<>();
41 List<EdgeRuleBean> rulesToWriteV7 = new ArrayList<>();
42 List<EdgeRuleBean> rulesToWriteV8 = new ArrayList<>();
43 List<EdgeRuleBean> rulesToWriteV9 = new ArrayList<>();
44 List<EdgeRuleBean> rulesToWriteV10 = new ArrayList<>();
45 List<EdgeRuleBean> rulesToWriteV11 = new ArrayList<>();
47 ArrayList <String> rulesWeAlreadyHave = new ArrayList <String>();
49 EdgeRules rulesV8 = EdgeRules.getInstance(Version.v8);
50 EdgeRules rulesV9 = EdgeRules.getInstance(Version.v9);
51 EdgeRules rulesV10 = EdgeRules.getInstance(Version.v10);
52 EdgeRules rulesV11 = EdgeRules.getInstance(Version.v11);
54 try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
58 while ((line = reader.readLine()) != null) {
60 headers = retrieveHeaderMap(line);
63 EdgeRuleBean data = new EdgeRuleBean();
64 String[] columns = line.split(",");
65 String oldNodeA = columns[headers.get("from")];
66 String oldNodeB = columns[headers.get("to")];
67 String oldEdgeLabel = columns[headers.get("label")];
69 String nodeA = columns[headers.get("new from")];
71 String nodeB = columns[headers.get("new to")];
74 String edgeLabel = columns[headers.get("new label")];
75 data.setLabel( edgeLabel );
78 // Note: it is assumed that if we know the two NodeTypes and the edgeLabel, we can
79 // uniquely identify an edgeRule -- so if that key is found twice, it is a
80 // problem with our CSV file. Note -we check with the nodeTypes in both directions.
81 String key1 = nodeA + "|" + nodeB + "|" + edgeLabel;
82 String key2 = nodeB + "|" + nodeA + "|" + edgeLabel;
83 if( rulesWeAlreadyHave.contains(key1) ){
84 throw new Exception ("Duplicate rule found for [" + key1 + "] -- please fix the CSV file. ");
86 else if( rulesWeAlreadyHave.contains(key2) ){
87 throw new Exception ("Duplicate rule found for [" + key2 + "] -- please fix the CSV file. ");
90 rulesWeAlreadyHave.add(key1);
91 rulesWeAlreadyHave.add(key2);
94 String direction = columns[headers.get("new direction")];
95 data.setDirection(direction);
97 String multiplicity = columns[headers.get("new multiplicity")];
98 data.setMultiplicity(multiplicity);
100 String lineage = columns[headers.get("new contains-other-v")];
101 data.setLineage(lineage);
103 String deleteOtherV = columns[headers.get("new delete-other-v")];
104 data.setDeleteOtherV(deleteOtherV);
106 String svcInfra = columns[headers.get("new SVC-INFRA")];
107 data.setSvcInfra(svcInfra);
109 String prevDel = columns[headers.get("new prevent-delete")];
110 data.setPreventDelete(prevDel);
112 String defaultVal = columns[headers.get("new default")];
113 if( defaultVal.equals("T") ){
114 data.setDefault("true");
116 else if( defaultVal.equals("F") ){
117 data.setDefault("false");
120 rulesToWriteV12.add(data);
122 if( rulesV8.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
123 rulesToWriteV8.add(data);
126 if( rulesV9.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
127 rulesToWriteV9.add(data);
130 if( rulesV10.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
131 rulesToWriteV10.add(data);
134 if( rulesV11.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
135 rulesToWriteV11.add(data);
141 Configuration configuration = new Configuration();
142 Template template = configuration.getTemplate("src/main/resources/edgerulesTemplate.ftlh");
144 saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v12.json", template, rulesToWriteV12);
145 saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v7.json", template, rulesToWriteV7);
146 saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v8.json", template, rulesToWriteV8);
147 saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v9.json", template, rulesToWriteV9);
148 saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v10.json", template, rulesToWriteV10);
149 saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v11.json", template, rulesToWriteV11);
151 } catch(Exception ex){
152 ex.printStackTrace();
158 private static void saveRulesIntoTheFile(String filePath, Template fileTemplate, List<EdgeRuleBean> rulesToWrite)
159 throws IOException, TemplateException {
162 try (Writer file = new FileWriter(new File(filePath))) {
163 Map<String, List<EdgeRuleBean>> wrappedRules = new HashMap<>();
164 wrappedRules.put("wrappedRules", rulesToWrite);
165 fileTemplate.process(wrappedRules, file);
169 private static Map<String, Integer> retrieveHeaderMap(String line){
172 throw new NullPointerException();
174 String[] columnNames = line.split(",");
176 Map<String, Integer> map = new HashMap<String, Integer>();
180 for(String columnName : columnNames){
181 map.put(columnName, index++);