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 System.out.println("Analyzed ML file=" + mlFileLocation + ", XLS result=" + result);
57 } catch (Exception e) {
58 log.info("analyze GraphML Data failed - {}" , e);
64 private String _analyzeGraphMLData(String mlFileLocation) throws Exception {
67 SAXBuilder builder = new SAXBuilder();
68 File xmlFile = new File(mlFileLocation);
69 Document document = (Document) builder.build(xmlFile);
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);
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]);
89 List<ComponentRow> components = getComponents(document);
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());
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]);
110 List<ComponentInstanceRow> components = getComponentInstances(document);
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());
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) {
137 String edgeLabel = data.getText();
138 if (edgeLabel.equals("REQUIREMENT") || edgeLabel.equals("CAPABILITY")) {
139 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 (nodeLabel.equals("resource") || nodeLabel.equals("service")) {
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 (nodeLabel.equals("resourceInstance")) {
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);
245 private class ComponentRow {
247 private String uniqueId;
250 private String toscaResourceName;
251 private String resourceType;
252 private String version;
253 private Boolean isDeleted;
254 private Boolean hasNonCalculatedReqCap;
256 public Boolean getHasNonCalculatedReqCap() {
257 return hasNonCalculatedReqCap;
260 public void setHasNonCalculatedReqCap(Boolean hasNonCalculatedReqCap) {
261 this.hasNonCalculatedReqCap = hasNonCalculatedReqCap;
264 public String getType() {
268 public void setType(String type) {
272 public String getUniqueId() {
276 public void setUniqueId(String uniqueId) {
277 this.uniqueId = uniqueId;
280 public String getName() {
284 public void setName(String name) {
288 public String getToscaResourceName() {
289 return toscaResourceName;
292 public void setToscaResourceName(String toscaResourceName) {
293 this.toscaResourceName = toscaResourceName;
296 public String getResourceType() {
300 public void setResourceType(String resourceType) {
301 this.resourceType = resourceType;
304 public String getVersion() {
308 public void setVersion(String version) {
309 this.version = version;
312 public Boolean getIsDeleted() {
316 public void setIsDeleted(Boolean deleted) {
317 this.isDeleted = deleted;
321 private class ComponentInstanceRow {
322 private String uniqueId;
324 private String originUid;
325 private String originType;
326 private String containerUid;
328 public String getContainerUid() {
332 public void setContainerUid(String containerUid) {
333 this.containerUid = containerUid;
336 public String getUniqueId() {
340 public void setUniqueId(String uniqueId) {
341 this.uniqueId = uniqueId;
344 public String getName() {
348 public void setName(String name) {
352 public String getOriginUid() {
356 public void setOriginUid(String componentUid) {
357 this.originUid = componentUid;
360 public String getOriginType() {
364 public void setOriginType(String originType) {
365 this.originType = originType;