re base code
[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 = { "uniqueId", "name", "originUid", "originType",
49                         "containerUid" };
50
51         public String analyzeGraphMLData(String[] args) {
52                 String result = null;
53                 try {
54                         String mlFileLocation = args[0];
55                         result = _analyzeGraphMLData(mlFileLocation);
56                         System.out.println("Analyzed ML file=" + mlFileLocation + ", XLS result=" + result);
57                 } catch (Exception e) {
58                         log.info("analyze GraphML Data failed - {}" , e);
59                         return null;
60                 }
61                 return result;
62         }
63
64         private String _analyzeGraphMLData(String mlFileLocation) throws Exception {
65
66                 // Parse ML file
67                 SAXBuilder builder = new SAXBuilder();
68                 File xmlFile = new File(mlFileLocation);
69                 Document document = (Document) builder.build(xmlFile);
70
71                 // XLS data file name
72                 String outputFile = mlFileLocation.replace(".graphml", ".xls");
73                 Workbook wb = new HSSFWorkbook();
74                 FileOutputStream fileOut = new FileOutputStream(outputFile);
75                 writeComponents(wb, document);
76                 writeComponentInstances(wb, document);
77                 wb.write(fileOut);
78                 fileOut.close();
79                 return outputFile;
80         }
81
82         private void writeComponents(Workbook wb, Document document) {
83                 Sheet componentsSheet = wb.createSheet("Components");
84                 Row currentRow = componentsSheet.createRow(0);
85                 for (int i = 0; i < COMPONENT_SHEET_HEADER.length; i++) {
86                         currentRow.createCell(i).setCellValue(COMPONENT_SHEET_HEADER[i]);
87                 }
88
89                 List<ComponentRow> components = getComponents(document);
90                 int rowNum = 1;
91                 for (ComponentRow row : components) {
92                         currentRow = componentsSheet.createRow(rowNum++);
93                         currentRow.createCell(0).setCellValue(row.getUniqueId());
94                         currentRow.createCell(1).setCellValue(row.getType());
95                         currentRow.createCell(2).setCellValue(row.getName());
96                         currentRow.createCell(3).setCellValue(row.getToscaResourceName());
97                         currentRow.createCell(4).setCellValue(row.getResourceType());
98                         currentRow.createCell(5).setCellValue(row.getVersion());
99                         currentRow.createCell(6).setCellValue(row.getIsDeleted() != null ? row.getIsDeleted().toString() : "false");
100                         currentRow.createCell(7).setCellValue(row.getHasNonCalculatedReqCap());
101                 }
102         }
103
104         private void writeComponentInstances(Workbook wb, Document document) {
105                 Sheet componentsSheet = wb.createSheet("ComponentInstances");
106                 Row currentRow = componentsSheet.createRow(0);
107                 for (int i = 0; i < COMPONENT_INSTANCES_SHEET_HEADER.length; i++) {
108                         currentRow.createCell(i).setCellValue(COMPONENT_INSTANCES_SHEET_HEADER[i]);
109                 }
110                 List<ComponentInstanceRow> components = getComponentInstances(document);
111                 int rowNum = 1;
112                 for (ComponentInstanceRow row : components) {
113                         currentRow = componentsSheet.createRow(rowNum++);
114                         currentRow.createCell(0).setCellValue(row.getUniqueId());
115                         currentRow.createCell(1).setCellValue(row.getName());
116                         currentRow.createCell(2).setCellValue(row.getOriginUid());
117                         currentRow.createCell(3).setCellValue(row.getOriginType());
118                         currentRow.createCell(4).setCellValue(row.getContainerUid());
119                 }
120         }
121
122         private List<ComponentRow> getComponents(Document document) {
123                 List<ComponentRow> res = new ArrayList<>();
124                 Element root = document.getRootElement();
125                 ElementFilter filter = new ElementFilter("graph");
126                 Element graph = root.getDescendants(filter).next();
127                 filter = new ElementFilter("edge");
128                 IteratorIterable<Element> edges = graph.getDescendants(filter);
129                 Set<String> componentsHavingReqOrCap = new HashSet<>();
130                 filter = new ElementFilter("data");
131                 for (Element edge : edges) {
132                         IteratorIterable<Element> dataNodes = edge.getDescendants(filter);
133                         for (Element data : dataNodes) {
134                                 String attributeValue = data.getAttributeValue("key");
135                                 switch (attributeValue) {
136                                 case "labelE":
137                                         String edgeLabel = data.getText();
138                                         if (edgeLabel.equals("REQUIREMENT") || edgeLabel.equals("CAPABILITY")) {
139                                                 componentsHavingReqOrCap.add(edge.getAttributeValue("source"));
140                                         }
141                                         break;
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 (nodeLabel.equals("resource") || nodeLabel.equals("service")) {
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 (nodeLabel.equals("resourceInstance")) {
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         private class ComponentRow {
246
247                 private String uniqueId;
248                 private String type;
249                 private String name;
250                 private String toscaResourceName;
251                 private String resourceType;
252                 private String version;
253                 private Boolean isDeleted;
254                 private Boolean hasNonCalculatedReqCap;
255
256                 public Boolean getHasNonCalculatedReqCap() {
257                         return hasNonCalculatedReqCap;
258                 }
259
260                 public void setHasNonCalculatedReqCap(Boolean hasNonCalculatedReqCap) {
261                         this.hasNonCalculatedReqCap = hasNonCalculatedReqCap;
262                 }
263
264                 public String getType() {
265                         return type;
266                 }
267
268                 public void setType(String type) {
269                         this.type = type;
270                 }
271
272                 public String getUniqueId() {
273                         return uniqueId;
274                 }
275
276                 public void setUniqueId(String uniqueId) {
277                         this.uniqueId = uniqueId;
278                 }
279
280                 public String getName() {
281                         return name;
282                 }
283
284                 public void setName(String name) {
285                         this.name = name;
286                 }
287
288                 public String getToscaResourceName() {
289                         return toscaResourceName;
290                 }
291
292                 public void setToscaResourceName(String toscaResourceName) {
293                         this.toscaResourceName = toscaResourceName;
294                 }
295
296                 public String getResourceType() {
297                         return resourceType;
298                 }
299
300                 public void setResourceType(String resourceType) {
301                         this.resourceType = resourceType;
302                 }
303
304                 public String getVersion() {
305                         return version;
306                 }
307
308                 public void setVersion(String version) {
309                         this.version = version;
310                 }
311
312                 public Boolean getIsDeleted() {
313                         return isDeleted;
314                 }
315
316                 public void setIsDeleted(Boolean deleted) {
317                         this.isDeleted = deleted;
318                 }
319         }
320
321         private class ComponentInstanceRow {
322                 private String uniqueId;
323                 private String name;
324                 private String originUid;
325                 private String originType;
326                 private String containerUid;
327
328                 public String getContainerUid() {
329                         return containerUid;
330                 }
331
332                 public void setContainerUid(String containerUid) {
333                         this.containerUid = containerUid;
334                 }
335
336                 public String getUniqueId() {
337                         return uniqueId;
338                 }
339
340                 public void setUniqueId(String uniqueId) {
341                         this.uniqueId = uniqueId;
342                 }
343
344                 public String getName() {
345                         return name;
346                 }
347
348                 public void setName(String name) {
349                         this.name = name;
350                 }
351
352                 public String getOriginUid() {
353                         return originUid;
354                 }
355
356                 public void setOriginUid(String componentUid) {
357                         this.originUid = componentUid;
358                 }
359
360                 public String getOriginType() {
361                         return originType;
362                 }
363
364                 public void setOriginType(String originType) {
365                         this.originType = originType;
366                 }
367         }
368 }