Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / GraphMLDataAnalyzer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.asdctool.impl;
22
23 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
24 import org.apache.poi.ss.usermodel.Row;
25 import org.apache.poi.ss.usermodel.Sheet;
26 import org.apache.poi.ss.usermodel.Workbook;
27 import org.jdom2.Document;
28 import org.jdom2.Element;
29 import org.jdom2.filter.ElementFilter;
30 import org.jdom2.input.SAXBuilder;
31 import org.jdom2.util.IteratorIterable;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.util.ArrayList;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Set;
41
42 public class GraphMLDataAnalyzer {
43
44     private static Logger log = LoggerFactory.getLogger(GraphMLDataAnalyzer.class);
45
46     private static final String[] COMPONENT_SHEET_HEADER = {"uniqueId", "type", "name", "toscaResourceName",
47         "resourceType", "version", "deleted", "hasNonCalculatedReqCap"};
48     private static final String[] COMPONENT_INSTANCES_SHEET_HEADER =
49         {"uniqueId", "name", "originUid", "originType", "containerUid"};
50
51     public String analyzeGraphMLData(String[] args) {
52         String result = null;
53         try {
54             String mlFileLocation = args[0];
55             result = analyzeGraphMLData(mlFileLocation);
56             log.info("Analyzed ML file=" + mlFileLocation + ", XLS result=" + result);
57         } catch (Exception e) {
58             log.error("analyze GraphML Data failed - {}", e);
59             return null;
60         }
61         return result;
62     }
63
64     private String analyzeGraphMLData(String mlFileLocation) throws Exception {
65         // Parse ML file
66         SAXBuilder builder = new SAXBuilder();
67         File xmlFile = new File(mlFileLocation);
68         Document document = builder.build(xmlFile);
69
70         // XLS data file name
71         String outputFile = mlFileLocation.replace(".graphml", ".xls");
72
73         try (Workbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream(outputFile)) {
74             writeComponents(wb, document);
75             writeComponentInstances(wb, document);
76             wb.write(fileOut);
77         } catch (Exception e) {
78             log.error("analyze GraphML Data failed - {}", e);
79         }
80         return outputFile;
81     }
82
83     private void writeComponents(Workbook wb, Document document) {
84         Sheet componentsSheet = wb.createSheet("Components");
85         Row currentRow = componentsSheet.createRow(0);
86         for (int i = 0; i < COMPONENT_SHEET_HEADER.length; i++) {
87             currentRow.createCell(i).setCellValue(COMPONENT_SHEET_HEADER[i]);
88         }
89
90         List<ComponentRow> components = getComponents(document);
91         int rowNum = 1;
92         for (ComponentRow row : components) {
93             currentRow = componentsSheet.createRow(rowNum++);
94             currentRow.createCell(0).setCellValue(row.getUniqueId());
95             currentRow.createCell(1).setCellValue(row.getType());
96             currentRow.createCell(2).setCellValue(row.getName());
97             currentRow.createCell(3).setCellValue(row.getToscaResourceName());
98             currentRow.createCell(4).setCellValue(row.getResourceType());
99             currentRow.createCell(5).setCellValue(row.getVersion());
100             currentRow.createCell(6)
101                 .setCellValue(row.getIsDeleted() != null ? row.getIsDeleted().toString() : "false");
102             currentRow.createCell(7).setCellValue(row.getHasNonCalculatedReqCap());
103         }
104     }
105
106     private void writeComponentInstances(Workbook wb, Document document) {
107         Sheet componentsSheet = wb.createSheet("ComponentInstances");
108         Row currentRow = componentsSheet.createRow(0);
109         for (int i = 0; i < COMPONENT_INSTANCES_SHEET_HEADER.length; i++) {
110             currentRow.createCell(i).setCellValue(COMPONENT_INSTANCES_SHEET_HEADER[i]);
111         }
112         List<ComponentInstanceRow> components = getComponentInstances(document);
113         int rowNum = 1;
114         for (ComponentInstanceRow row : components) {
115             currentRow = componentsSheet.createRow(rowNum++);
116             currentRow.createCell(0).setCellValue(row.getUniqueId());
117             currentRow.createCell(1).setCellValue(row.getName());
118             currentRow.createCell(2).setCellValue(row.getOriginUid());
119             currentRow.createCell(3).setCellValue(row.getOriginType());
120             currentRow.createCell(4).setCellValue(row.getContainerUid());
121         }
122     }
123
124     private List<ComponentRow> getComponents(Document document) {
125         List<ComponentRow> res = new ArrayList<>();
126         Element root = document.getRootElement();
127         ElementFilter filter = new ElementFilter("graph");
128         Element graph = root.getDescendants(filter).next();
129         filter = new ElementFilter("edge");
130         IteratorIterable<Element> edges = graph.getDescendants(filter);
131         Set<String> componentsHavingReqOrCap = new HashSet<>();
132         filter = new ElementFilter("data");
133         for (Element edge : edges) {
134             IteratorIterable<Element> dataNodes = edge.getDescendants(filter);
135             for (Element data : dataNodes) {
136                 String attributeValue = data.getAttributeValue("key");
137                 if ("labelE".equals(attributeValue)) {
138                     String edgeLabel = data.getText();
139                     if ("REQUIREMENT".equals(edgeLabel) || "CAPABILITY".equals(edgeLabel)) {
140                         componentsHavingReqOrCap.add(edge.getAttributeValue("source"));
141                     }
142                 }
143             }
144         }
145
146         filter = new ElementFilter("node");
147         IteratorIterable<Element> nodes = graph.getDescendants(filter);
148         filter = new ElementFilter("data");
149         for (Element element : nodes) {
150             IteratorIterable<Element> dataNodes = element.getDescendants(filter);
151             ComponentRow componentRow = new ComponentRow();
152             boolean isComponent = false;
153             for (Element data : dataNodes) {
154                 String attributeValue = data.getAttributeValue("key");
155                 switch (attributeValue) {
156                     case "nodeLabel":
157                         String nodeLabel = data.getText();
158                         if ("resource".equals(nodeLabel) || "service".equals(nodeLabel)) {
159                             isComponent = true;
160                             componentRow.setType(nodeLabel);
161                             String componentId = element.getAttributeValue("id");
162                             componentRow.setHasNonCalculatedReqCap(componentsHavingReqOrCap.contains(componentId));
163                         }
164                         break;
165                     case "uid":
166                         componentRow.setUniqueId(data.getText());
167                         break;
168                     case "name":
169                         componentRow.setName(data.getText());
170                         break;
171                     case "toscaResourceName":
172                         componentRow.setToscaResourceName(data.getText());
173                         break;
174                     case "resourceType":
175                         componentRow.setResourceType(data.getText());
176                         break;
177                     case "version":
178                         componentRow.setVersion(data.getText());
179                         break;
180                     case "deleted":
181                         componentRow.setIsDeleted(Boolean.parseBoolean(data.getText()));
182                         break;
183                     default:
184                         break;
185                 }
186             }
187             if (isComponent) {
188                 res.add(componentRow);
189             }
190         }
191         return res;
192     }
193
194     private List<ComponentInstanceRow> getComponentInstances(Document document) {
195         List<ComponentInstanceRow> res = new ArrayList<>();
196         Element root = document.getRootElement();
197         ElementFilter filter = new ElementFilter("graph");
198         Element graph = root.getDescendants(filter).next();
199         filter = new ElementFilter("node");
200         IteratorIterable<Element> nodes = graph.getDescendants(filter);
201         filter = new ElementFilter("data");
202         for (Element element : nodes) {
203             IteratorIterable<Element> dataNodes = element.getDescendants(filter);
204             ComponentInstanceRow componentInstRow = new ComponentInstanceRow();
205             boolean isComponentInst = false;
206             for (Element data : dataNodes) {
207                 String attributeValue = data.getAttributeValue("key");
208                 switch (attributeValue) {
209                     case "nodeLabel":
210                         String nodeLabel = data.getText();
211                         if ("resourceInstance".equals(nodeLabel)) {
212                             isComponentInst = true;
213                         }
214                         break;
215                     case "uid":
216                         componentInstRow.setUniqueId(data.getText());
217                         break;
218                     case "name":
219                         componentInstRow.setName(data.getText());
220                         break;
221                     case "originType":
222                         componentInstRow.setOriginType(data.getText());
223                         break;
224                     default:
225                         break;
226                 }
227             }
228             if (isComponentInst) {
229                 // Assuming the uid is in standard form of
230                 // <container>.<origin>.<name>
231                 String uniqueId = componentInstRow.getUniqueId();
232                 if (uniqueId != null) {
233                     String[] split = uniqueId.split("\\.");
234                     if (split.length == 3) {
235                         componentInstRow.setContainerUid(split[0]);
236                         componentInstRow.setOriginUid(split[1]);
237                     }
238                 }
239                 res.add(componentInstRow);
240             }
241         }
242         return res;
243     }
244
245 }