Sync the latest code changes
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.restcore.util;
23
24 import freemarker.template.Configuration;
25 import freemarker.template.Template;
26 import freemarker.template.TemplateException;
27
28 import java.io.*;
29 import java.util.*;
30
31 import org.onap.aai.serialization.db.EdgeRules;
32 import org.onap.aai.introspection.Version;
33
34 public class GenerateEdgeRules {
35
36     public static void main(String[] args) throws IOException, TemplateException {
37
38         String filename = "/edgeLabelMigration.csv";
39         InputStream inputStream = GenerateEdgeRules.class.getResourceAsStream(filename);
40         Map<String, Integer> headers = new HashMap<>();
41
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<>();
48
49         ArrayList <String> rulesWeAlreadyHave = new ArrayList <String>();
50
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);
55
56         try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
57
58             String line = null;
59             int rowNum = 0;
60             while ((line = reader.readLine()) != null) {
61                 if (rowNum == 0) {
62                     headers = retrieveHeaderMap(line);
63                 }
64                 else {
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")];
70
71                     String nodeA = columns[headers.get("new from")];
72                     data.setFrom(nodeA);
73                     String nodeB = columns[headers.get("new to")];
74                     data.setTo(nodeB);
75
76                     String edgeLabel = columns[headers.get("new label")];
77                     data.setLabel( edgeLabel );
78
79
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. ");
87                     }
88                     else if( rulesWeAlreadyHave.contains(key2) ){
89                         throw new Exception ("Duplicate rule found for [" + key2 + "] -- please fix the CSV file. ");
90                     }
91                     else {
92                         rulesWeAlreadyHave.add(key1);
93                         rulesWeAlreadyHave.add(key2);
94                     }
95
96                     String direction = columns[headers.get("new direction")];
97                     data.setDirection(direction);
98
99                     String multiplicity = columns[headers.get("new multiplicity")];
100                     data.setMultiplicity(multiplicity);
101
102                     String lineage = columns[headers.get("new contains-other-v")];
103                     data.setLineage(lineage);
104
105                     String deleteOtherV = columns[headers.get("new delete-other-v")];
106                     data.setDeleteOtherV(deleteOtherV);
107
108                     String svcInfra = columns[headers.get("new SVC-INFRA")];
109                     data.setSvcInfra(svcInfra);
110
111                     String prevDel = columns[headers.get("new prevent-delete")];
112                     data.setPreventDelete(prevDel);
113
114                     String defaultVal = columns[headers.get("new default")];
115                     if( defaultVal.equals("T") ){
116                         data.setDefault("true");
117                     }
118                     else if( defaultVal.equals("F") ){
119                         data.setDefault("false");
120                     }
121
122                     rulesToWriteV12.add(data);
123
124                     if( rulesV8.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
125                         rulesToWriteV8.add(data);
126                     }
127
128                     if( rulesV9.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
129                         rulesToWriteV9.add(data);
130                     }
131
132                     if( rulesV10.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
133                         rulesToWriteV10.add(data);
134                     }
135
136                     if( rulesV11.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){
137                         rulesToWriteV11.add(data);
138                     }
139                 }
140                 ++rowNum;
141             }
142
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);
149                 file.close();
150
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);
155                 file.close();
156
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);
161                 file.close();
162
163
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);
168                 file.close();
169
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);
174                 file.close();
175
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);
180                 file.close();
181
182         } catch(Exception ex){
183             ex.printStackTrace();
184         }
185
186
187     }
188
189     private static Map<String, Integer> retrieveHeaderMap(String line){
190
191         if(line == null)
192             throw new NullPointerException();
193
194         String[] columnNames = line.split(",");
195
196         Map<String, Integer> map = new HashMap<String, Integer>();
197
198         int index = 0;
199
200         for(String columnName : columnNames){
201                 map.put(columnName, index++);
202         }
203
204         return map;
205     }
206
207 }