Update the license for 2017-2018 license
[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             Writer file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v12.json"));
144             Map<String, List<EdgeRuleBean>> wrappedRules = new HashMap<>();
145                 wrappedRules.put("wrappedRules", rulesToWriteV12);
146                 template.process(wrappedRules, file);
147                 file.close();
148
149                 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v7.json"));
150             wrappedRules = new HashMap<>();
151                 wrappedRules.put("wrappedRules", rulesToWriteV7);
152                 template.process(wrappedRules, file);
153                 file.close();
154
155                 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v8.json"));
156             wrappedRules = new HashMap<>();
157                 wrappedRules.put("wrappedRules", rulesToWriteV8);
158                 template.process(wrappedRules, file);
159                 file.close();
160
161
162                 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v9.json"));
163             wrappedRules = new HashMap<>();
164                 wrappedRules.put("wrappedRules", rulesToWriteV9);
165                 template.process(wrappedRules, file);
166                 file.close();
167
168                 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v10.json"));
169             wrappedRules = new HashMap<>();
170                 wrappedRules.put("wrappedRules", rulesToWriteV10);
171                 template.process(wrappedRules, file);
172                 file.close();
173
174                 file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v11.json"));
175             wrappedRules = new HashMap<>();
176                 wrappedRules.put("wrappedRules", rulesToWriteV11);
177                 template.process(wrappedRules, file);
178                 file.close();
179
180         } catch(Exception ex){
181             ex.printStackTrace();
182         }
183
184
185     }
186
187     private static Map<String, Integer> retrieveHeaderMap(String line){
188
189         if(line == null)
190             throw new NullPointerException();
191
192         String[] columnNames = line.split(",");
193
194         Map<String, Integer> map = new HashMap<String, Integer>();
195
196         int index = 0;
197
198         for(String columnName : columnNames){
199                 map.put(columnName, index++);
200         }
201
202         return map;
203     }
204
205 }