Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / restcore / util / GenerateEdgeRules.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.restcore.util;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import freemarker.template.Configuration;
26 import freemarker.template.Template;
27 import freemarker.template.TemplateException;
28
29 import java.io.*;
30 import java.util.*;
31
32 public class GenerateEdgeRules {
33
34     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(GenerateEdgeRules.class);
35
36     public static void main(String[] args) throws IOException, TemplateException {
37
38         String filename = "/AAI8032.csv";
39         InputStream inputStream = GenerateEdgeRules.class.getResourceAsStream(filename);
40         Map<String, Integer> headers = new HashMap<>();
41         Map<String, Object> edgeRulesMap = new TreeMap<String, Object>();
42         List<Map<String, String>> edgeRules = new ArrayList<>();
43
44         try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
45
46             String line = null;
47
48             int rowNum = 0;
49
50             // Retrieve the header line to map the indexes to their column names
51
52             while ((line = reader.readLine()) != null) {
53
54                 if (rowNum == 0) {
55                     headers = retrieveHeaderMap(line);
56                 } else {
57                     String[] columns = line.split(",");
58
59                     String originalNode = columns[headers.get("Orig NodeA|NodeB")];
60                     String finalNode = columns[headers.get("Final NodeA|NodeB")];
61                     String originalEdge = columns[headers.get("Orig EdgeLabel")];
62                     String finalEdge = columns[headers.get("Final EdgeLabel")];
63
64                     String lineage = columns[headers.get("Final Lineage")];
65                     String originalParent = columns[headers.get("Orig ParentOf")];
66                     String usesResource = columns[headers.get("Revised UsesResource")];
67                     String hasDelTarget = columns[headers.get("Revised hasDelTarget")];
68                     String svcInfra = columns[headers.get("Final SVC-INFRA")];
69                     String svcInfraRev = "";
70
71                     if(usesResource.equals("T"))
72                         usesResource = "true";
73                     else if(usesResource.equals("F"))
74                         usesResource = "false";
75
76                     if (hasDelTarget.equals("T") || hasDelTarget.equals("AB")) {
77                         hasDelTarget = "true";
78                     } else if (hasDelTarget.equals("F")) {
79                         hasDelTarget = "false";
80                     }
81
82                     if (svcInfra.equals("T")) {
83                         svcInfra = "true";
84                     } else if (svcInfra.equals("F")) {
85                         svcInfra = "false";
86                     } else if (svcInfra.equals("R")) {
87                         svcInfra = "reverse";
88                     }
89
90                     if (originalParent.equals("T")) {
91                         if (lineage.trim().equalsIgnoreCase("CHILD")) {
92                             lineage = "true";
93                         } else if (lineage.trim().equalsIgnoreCase("PARENT")) {
94                             lineage = "reverse";
95                         }
96                     } else {
97                         lineage = "false";
98                     }
99
100                     Map<String, String> edgeMap = new HashMap<String, String>();
101
102                     edgeMap.put("lineage", lineage);
103                     edgeMap.put("usesResource", usesResource);
104                     edgeMap.put("hasDelTarget", hasDelTarget);
105                     edgeMap.put("SVC-INFRA", svcInfra);
106                     edgeMap.put("SVC-INFRA-REV", svcInfraRev);
107                     edgeMap.put("nodes", finalNode);
108                     edgeMap.put("edge", finalEdge);
109                     edgeMap.put("direction", columns[headers.get("Orig Direction")]);
110                     edgeMap.put("multiplicity", columns[headers.get("Revised Multiplicity")]);
111
112                     edgeRules.add(edgeMap);
113
114                 }
115                 ++rowNum;
116             }
117         } catch(Exception ex){
118             ex.printStackTrace();
119         }
120
121         edgeRulesMap.put("edgeRules", edgeRules);
122
123         Collections.sort(edgeRules, new Comparator<Map<String, String>>() {
124             @Override
125             public int compare(Map<String, String> o1, Map<String, String> o2) {
126                 return o1.get("nodes").compareTo(o2.get("nodes"));
127             }
128         });
129
130         Configuration configuration = new Configuration();
131         Template template = configuration.getTemplate("ajsc-aai/src/main/resources/EdgeRules.ftl");
132         Writer file = new FileWriter(new File("ajsc-aai/src/main/resources" + "/" + "EdgeRules.txt"));
133         template.process(edgeRulesMap, file);
134     }
135
136     private static Map<String, Integer> retrieveHeaderMap(String line){
137
138         if(line == null)
139             throw new NullPointerException();
140
141         String[] columnNames = line.split(",");
142
143         Map<String, Integer> map = new HashMap<String, Integer>();
144
145         int index = 0;
146
147         for(String columnName : columnNames){
148             map.put(columnName, index++);
149         }
150
151         return map;
152     }
153
154 }