Merge "Fix sonar issues in SchemaGenerator"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / restcore / util / GenerateEdgeRules.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.restcore.util;
21
22 import freemarker.template.Configuration;
23 import freemarker.template.Template;
24 import freemarker.template.TemplateException;
25
26 import java.io.*;
27 import java.util.*;
28
29 import org.onap.aai.serialization.db.EdgeRules;
30 import org.onap.aai.introspection.Version;
31
32 public class GenerateEdgeRules {
33
34     public static void main(String[] args) throws IOException, TemplateException {
35
36         String filename = "/edgeLabelMigration.csv";
37         InputStream inputStream = GenerateEdgeRules.class.getResourceAsStream(filename);
38         Map<String, Integer> headers = new HashMap<>();
39
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<>();
46
47         ArrayList <String> rulesWeAlreadyHave = new ArrayList <String>();
48
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);
53
54         try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
55
56             String line = null;
57             int rowNum = 0;
58             while ((line = reader.readLine()) != null) {
59                 if (rowNum == 0) {
60                     headers = retrieveHeaderMap(line);
61                 }
62                 else {
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")];
68
69                     String nodeA = columns[headers.get("new from")];
70                     data.setFrom(nodeA);
71                     String nodeB = columns[headers.get("new to")];
72                     data.setTo(nodeB);
73
74                     String edgeLabel = columns[headers.get("new label")];
75                     data.setLabel( edgeLabel );
76
77
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. ");
85                     }
86                     else if( rulesWeAlreadyHave.contains(key2) ){
87                         throw new Exception ("Duplicate rule found for [" + key2 + "] -- please fix the CSV file. ");
88                     }
89                     else {
90                         rulesWeAlreadyHave.add(key1);
91                         rulesWeAlreadyHave.add(key2);
92                     }
93
94                     String direction = columns[headers.get("new direction")];
95                     data.setDirection(direction);
96
97                     String multiplicity = columns[headers.get("new multiplicity")];
98                     data.setMultiplicity(multiplicity);
99
100                     String lineage = columns[headers.get("new contains-other-v")];
101                     data.setLineage(lineage);
102
103                     String deleteOtherV = columns[headers.get("new delete-other-v")];
104                     data.setDeleteOtherV(deleteOtherV);
105
106                     String svcInfra = columns[headers.get("new SVC-INFRA")];
107                     data.setSvcInfra(svcInfra);
108
109                     String prevDel = columns[headers.get("new prevent-delete")];
110                     data.setPreventDelete(prevDel);
111
112                     String defaultVal = columns[headers.get("new default")];
113                     if( defaultVal.equals("T") ){
114                         data.setDefault("true");
115                     }
116                     else if( defaultVal.equals("F") ){
117                         data.setDefault("false");
118                     }
119
120                     rulesToWriteV12.add(data);
121
122                     if( rulesV8.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
123                         rulesToWriteV8.add(data);
124                     }
125
126                     if( rulesV9.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
127                         rulesToWriteV9.add(data);
128                     }
129
130                     if( rulesV10.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
131                         rulesToWriteV10.add(data);
132                     }
133
134                     if( rulesV11.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
135                         rulesToWriteV11.add(data);
136                     }
137                 }
138                 ++rowNum;
139             }
140
141             Configuration configuration = new Configuration();
142             Template template = configuration.getTemplate("src/main/resources/edgerulesTemplate.ftlh");
143
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);
150
151         } catch(Exception ex){
152             ex.printStackTrace();
153         }
154
155
156     }
157
158     private static void saveRulesIntoTheFile(String filePath, Template fileTemplate, List<EdgeRuleBean> rulesToWrite)
159         throws IOException, TemplateException {
160
161
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);
166         }
167     }
168
169     private static Map<String, Integer> retrieveHeaderMap(String line){
170
171         if(line == null)
172             throw new NullPointerException();
173
174         String[] columnNames = line.split(",");
175
176         Map<String, Integer> map = new HashMap<String, Integer>();
177
178         int index = 0;
179
180         for(String columnName : columnNames){
181                 map.put(columnName, index++);
182         }
183
184         return map;
185     }
186
187 }