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