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 =
49 {"uniqueId", "name", "originUid", "originType", "containerUid"};
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");
73 try (Workbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream(outputFile)) {
74 writeComponents(wb, document);
75 writeComponentInstances(wb, document);
77 } catch (Exception e) {
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)
101 .setCellValue(row.getIsDeleted() != null ? row.getIsDeleted().toString() : "false");
102 currentRow.createCell(7).setCellValue(row.getHasNonCalculatedReqCap());
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]);
112 List<ComponentInstanceRow> components = getComponentInstances(document);
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());
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"));
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) {
157 String nodeLabel = data.getText();
158 if ("resource".equals(nodeLabel) || "service".equals(nodeLabel)) {
160 componentRow.setType(nodeLabel);
161 String componentId = element.getAttributeValue("id");
162 componentRow.setHasNonCalculatedReqCap(componentsHavingReqOrCap.contains(componentId));
166 componentRow.setUniqueId(data.getText());
169 componentRow.setName(data.getText());
171 case "toscaResourceName":
172 componentRow.setToscaResourceName(data.getText());
175 componentRow.setResourceType(data.getText());
178 componentRow.setVersion(data.getText());
181 componentRow.setIsDeleted(Boolean.parseBoolean(data.getText()));
188 res.add(componentRow);
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) {
210 String nodeLabel = data.getText();
211 if ("resourceInstance".equals(nodeLabel)) {
212 isComponentInst = true;
216 componentInstRow.setUniqueId(data.getText());
219 componentInstRow.setName(data.getText());
222 componentInstRow.setOriginType(data.getText());
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]);
239 res.add(componentInstRow);