2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.asdctool.impl;
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;
36 import java.io.FileOutputStream;
37 import java.util.ArrayList;
38 import java.util.HashSet;
39 import java.util.List;
42 public class GraphMLDataAnalyzer {
44 private static Logger log = LoggerFactory.getLogger(GraphMLDataAnalyzer.class);
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",
51 public String analyzeGraphMLData(String[] args) {
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);
64 private String _analyzeGraphMLData(String mlFileLocation) throws Exception {
66 SAXBuilder builder = new SAXBuilder();
67 File xmlFile = new File(mlFileLocation);
68 Document document = builder.build(xmlFile);
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);
78 log.error("analyze GraphML Data failed - {}" , e);
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]);
90 List<ComponentRow> components = getComponents(document);
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());
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]);
111 List<ComponentInstanceRow> components = getComponentInstances(document);
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());
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"));
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) {
156 String nodeLabel = data.getText();
157 if (nodeLabel.equals("resource") || nodeLabel.equals("service")) {
159 componentRow.setType(nodeLabel);
160 String componentId = element.getAttributeValue("id");
161 componentRow.setHasNonCalculatedReqCap(componentsHavingReqOrCap.contains(componentId));
165 componentRow.setUniqueId(data.getText());
168 componentRow.setName(data.getText());
170 case "toscaResourceName":
171 componentRow.setToscaResourceName(data.getText());
174 componentRow.setResourceType(data.getText());
177 componentRow.setVersion(data.getText());
180 componentRow.setIsDeleted(Boolean.parseBoolean(data.getText()));
187 res.add(componentRow);
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) {
209 String nodeLabel = data.getText();
210 if (nodeLabel.equals("resourceInstance")) {
211 isComponentInst = true;
215 componentInstRow.setUniqueId(data.getText());
218 componentInstRow.setName(data.getText());
221 componentInstRow.setOriginType(data.getText());
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]);
238 res.add(componentInstRow);
244 private class ComponentRow {
246 private String uniqueId;
249 private String toscaResourceName;
250 private String resourceType;
251 private String version;
252 private Boolean isDeleted;
253 private Boolean hasNonCalculatedReqCap;
255 public Boolean getHasNonCalculatedReqCap() {
256 return hasNonCalculatedReqCap;
259 public void setHasNonCalculatedReqCap(Boolean hasNonCalculatedReqCap) {
260 this.hasNonCalculatedReqCap = hasNonCalculatedReqCap;
263 public String getType() {
267 public void setType(String type) {
271 public String getUniqueId() {
275 public void setUniqueId(String uniqueId) {
276 this.uniqueId = uniqueId;
279 public String getName() {
283 public void setName(String name) {
287 public String getToscaResourceName() {
288 return toscaResourceName;
291 public void setToscaResourceName(String toscaResourceName) {
292 this.toscaResourceName = toscaResourceName;
295 public String getResourceType() {
299 public void setResourceType(String resourceType) {
300 this.resourceType = resourceType;
303 public String getVersion() {
307 public void setVersion(String version) {
308 this.version = version;
311 public Boolean getIsDeleted() {
315 public void setIsDeleted(Boolean deleted) {
316 this.isDeleted = deleted;
320 private class ComponentInstanceRow {
321 private String uniqueId;
323 private String originUid;
324 private String originType;
325 private String containerUid;
327 public String getContainerUid() {
331 public void setContainerUid(String containerUid) {
332 this.containerUid = containerUid;
335 public String getUniqueId() {
339 public void setUniqueId(String uniqueId) {
340 this.uniqueId = uniqueId;
343 public String getName() {
347 public void setName(String name) {
351 public String getOriginUid() {
355 public void setOriginUid(String componentUid) {
356 this.originUid = componentUid;
359 public String getOriginType() {
363 public void setOriginType(String originType) {
364 this.originType = originType;