asdctool code quality improvements
[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                         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                 Workbook wb = new HSSFWorkbook();
73                 try(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).setCellValue(row.getIsDeleted() != null ? row.getIsDeleted().toString() : "false");
101                         currentRow.createCell(7).setCellValue(row.getHasNonCalculatedReqCap());
102                 }
103         }
104
105         private void writeComponentInstances(Workbook wb, Document document) {
106                 Sheet componentsSheet = wb.createSheet("ComponentInstances");
107                 Row currentRow = componentsSheet.createRow(0);
108                 for (int i = 0; i < COMPONENT_INSTANCES_SHEET_HEADER.length; i++) {
109                         currentRow.createCell(i).setCellValue(COMPONENT_INSTANCES_SHEET_HEADER[i]);
110                 }
111                 List<ComponentInstanceRow> components = getComponentInstances(document);
112                 int rowNum = 1;
113                 for (ComponentInstanceRow row : components) {
114                         currentRow = componentsSheet.createRow(rowNum++);
115                         currentRow.createCell(0).setCellValue(row.getUniqueId());
116                         currentRow.createCell(1).setCellValue(row.getName());
117                         currentRow.createCell(2).setCellValue(row.getOriginUid());
118                         currentRow.createCell(3).setCellValue(row.getOriginType());
119                         currentRow.createCell(4).setCellValue(row.getContainerUid());
120                 }
121         }
122
123         private List<ComponentRow> getComponents(Document document) {
124                 List<ComponentRow> res = new ArrayList<>();
125                 Element root = document.getRootElement();
126                 ElementFilter filter = new ElementFilter("graph");
127                 Element graph = root.getDescendants(filter).next();
128                 filter = new ElementFilter("edge");
129                 IteratorIterable<Element> edges = graph.getDescendants(filter);
130                 Set<String> componentsHavingReqOrCap = new HashSet<>();
131                 filter = new ElementFilter("data");
132                 for (Element edge : edges) {
133                         IteratorIterable<Element> dataNodes = edge.getDescendants(filter);
134                         for (Element data : dataNodes) {
135                                 String attributeValue = data.getAttributeValue("key");
136                                 if( attributeValue.equals("labelE")) {
137                                         String edgeLabel = data.getText();
138                                         if (edgeLabel.equals("REQUIREMENT") || edgeLabel.equals("CAPABILITY")) {
139                                                 componentsHavingReqOrCap.add(edge.getAttributeValue("source"));
140                                         }
141                                 }
142                         }
143                 }
144
145                 filter = new ElementFilter("node");
146                 IteratorIterable<Element> nodes = graph.getDescendants(filter);
147                 filter = new ElementFilter("data");
148                 for (Element element : nodes) {
149                         IteratorIterable<Element> dataNodes = element.getDescendants(filter);
150                         ComponentRow componentRow = new ComponentRow();
151                         boolean isComponent = false;
152                         for (Element data : dataNodes) {
153                                 String attributeValue = data.getAttributeValue("key");
154                                 switch (attributeValue) {
155                                 case "nodeLabel":
156                                         String nodeLabel = data.getText();
157                                         if (nodeLabel.equals("resource") || nodeLabel.equals("service")) {
158                                                 isComponent = true;
159                                                 componentRow.setType(nodeLabel);
160                                                 String componentId = element.getAttributeValue("id");
161                                                 componentRow.setHasNonCalculatedReqCap(componentsHavingReqOrCap.contains(componentId));
162                                         }
163                                         break;
164                                 case "uid":
165                                         componentRow.setUniqueId(data.getText());
166                                         break;
167                                 case "name":
168                                         componentRow.setName(data.getText());
169                                         break;
170                                 case "toscaResourceName":
171                                         componentRow.setToscaResourceName(data.getText());
172                                         break;
173                                 case "resourceType":
174                                         componentRow.setResourceType(data.getText());
175                                         break;
176                                 case "version":
177                                         componentRow.setVersion(data.getText());
178                                         break;
179                                 case "deleted":
180                                         componentRow.setIsDeleted(Boolean.parseBoolean(data.getText()));
181                                         break;
182                                 default:
183                                         break;
184                                 }
185                         }
186                         if (isComponent) {
187                                 res.add(componentRow);
188                         }
189                 }
190                 return res;
191         }
192
193         private List<ComponentInstanceRow> getComponentInstances(Document document) {
194                 List<ComponentInstanceRow> res = new ArrayList<>();
195                 Element root = document.getRootElement();
196                 ElementFilter filter = new ElementFilter("graph");
197                 Element graph = root.getDescendants(filter).next();
198                 filter = new ElementFilter("node");
199                 IteratorIterable<Element> nodes = graph.getDescendants(filter);
200                 filter = new ElementFilter("data");
201                 for (Element element : nodes) {
202                         IteratorIterable<Element> dataNodes = element.getDescendants(filter);
203                         ComponentInstanceRow componentInstRow = new ComponentInstanceRow();
204                         boolean isComponentInst = false;
205                         for (Element data : dataNodes) {
206                                 String attributeValue = data.getAttributeValue("key");
207                                 switch (attributeValue) {
208                                 case "nodeLabel":
209                                         String nodeLabel = data.getText();
210                                         if (nodeLabel.equals("resourceInstance")) {
211                                                 isComponentInst = true;
212                                         }
213                                         break;
214                                 case "uid":
215                                         componentInstRow.setUniqueId(data.getText());
216                                         break;
217                                 case "name":
218                                         componentInstRow.setName(data.getText());
219                                         break;
220                                 case "originType":
221                                         componentInstRow.setOriginType(data.getText());
222                                         break;
223                                 default:
224                                         break;
225                                 }
226                         }
227                         if (isComponentInst) {
228                                 // Assuming the uid is in standard form of
229                                 // <container>.<origin>.<name>
230                                 String uniqueId = componentInstRow.getUniqueId();
231                                 if (uniqueId != null) {
232                                         String[] split = uniqueId.split("\\.");
233                                         if (split.length == 3) {
234                                                 componentInstRow.setContainerUid(split[0]);
235                                                 componentInstRow.setOriginUid(split[1]);
236                                         }
237                                 }
238                                 res.add(componentInstRow);
239                         }
240                 }
241                 return res;
242         }
243
244         private class ComponentRow {
245
246                 private String uniqueId;
247                 private String type;
248                 private String name;
249                 private String toscaResourceName;
250                 private String resourceType;
251                 private String version;
252                 private Boolean isDeleted;
253                 private Boolean hasNonCalculatedReqCap;
254
255                 public Boolean getHasNonCalculatedReqCap() {
256                         return hasNonCalculatedReqCap;
257                 }
258
259                 public void setHasNonCalculatedReqCap(Boolean hasNonCalculatedReqCap) {
260                         this.hasNonCalculatedReqCap = hasNonCalculatedReqCap;
261                 }
262
263                 public String getType() {
264                         return type;
265                 }
266
267                 public void setType(String type) {
268                         this.type = type;
269                 }
270
271                 public String getUniqueId() {
272                         return uniqueId;
273                 }
274
275                 public void setUniqueId(String uniqueId) {
276                         this.uniqueId = uniqueId;
277                 }
278
279                 public String getName() {
280                         return name;
281                 }
282
283                 public void setName(String name) {
284                         this.name = name;
285                 }
286
287                 public String getToscaResourceName() {
288                         return toscaResourceName;
289                 }
290
291                 public void setToscaResourceName(String toscaResourceName) {
292                         this.toscaResourceName = toscaResourceName;
293                 }
294
295                 public String getResourceType() {
296                         return resourceType;
297                 }
298
299                 public void setResourceType(String resourceType) {
300                         this.resourceType = resourceType;
301                 }
302
303                 public String getVersion() {
304                         return version;
305                 }
306
307                 public void setVersion(String version) {
308                         this.version = version;
309                 }
310
311                 public Boolean getIsDeleted() {
312                         return isDeleted;
313                 }
314
315                 public void setIsDeleted(Boolean deleted) {
316                         this.isDeleted = deleted;
317                 }
318         }
319
320         private class ComponentInstanceRow {
321                 private String uniqueId;
322                 private String name;
323                 private String originUid;
324                 private String originType;
325                 private String containerUid;
326
327                 public String getContainerUid() {
328                         return containerUid;
329                 }
330
331                 public void setContainerUid(String containerUid) {
332                         this.containerUid = containerUid;
333                 }
334
335                 public String getUniqueId() {
336                         return uniqueId;
337                 }
338
339                 public void setUniqueId(String uniqueId) {
340                         this.uniqueId = uniqueId;
341                 }
342
343                 public String getName() {
344                         return name;
345                 }
346
347                 public void setName(String name) {
348                         this.name = name;
349                 }
350
351                 public String getOriginUid() {
352                         return originUid;
353                 }
354
355                 public void setOriginUid(String componentUid) {
356                         this.originUid = componentUid;
357                 }
358
359                 public String getOriginType() {
360                         return originType;
361                 }
362
363                 public void setOriginType(String originType) {
364                         this.originType = originType;
365                 }
366         }
367 }