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.be.model.operations.impl.util;
23 import org.janusgraph.core.JanusGraphEdge;
24 import org.janusgraph.core.JanusGraph;
25 import org.janusgraph.core.JanusGraphVertex;
26 import org.apache.tinkerpop.gremlin.structure.Edge;
27 import org.apache.tinkerpop.gremlin.structure.Element;
28 import org.apache.tinkerpop.gremlin.structure.Property;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
31 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
32 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
33 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
36 import java.util.Map.Entry;
38 import java.util.Map.Entry;
41 public class PrintGraph {
43 public void printGraphVertices(JanusGraph graph) {
45 Iterable<JanusGraphVertex> vertices = graph.query().vertices();
47 if (vertices != null) {
48 Iterator<JanusGraphVertex> iterator = vertices.iterator();
49 while (iterator.hasNext()) {
50 Vertex vertex = iterator.next();
58 public void printGraphEdges(JanusGraph graph) {
59 Iterable<JanusGraphEdge> vertices = graph.query().edges();
61 if (vertices != null) {
62 Iterator<JanusGraphEdge> iterator = vertices.iterator();
63 while (iterator.hasNext()) {
64 Edge edge = iterator.next();
72 public String buildGraphForWebgraphWiz(JanusGraph graph) {
74 StringBuilder builder = new StringBuilder();
75 builder.append("digraph finite_state_machine {\n");
76 builder.append("rankdir=LR;\n");
77 builder.append("size=\"15,10\" \n");
78 Iterable<JanusGraphVertex> vertices = graph.query().vertices();
80 if (vertices != null) {
81 Iterator<JanusGraphVertex> iterator = vertices.iterator();
82 while (iterator.hasNext()) {
83 Vertex vertex = iterator.next();
85 Map<String, Object> properties = getProperties(vertex);
87 String nodeLabel = (String) properties.get(GraphPropertiesDictionary.LABEL.getProperty());
89 String color = getColorByNodeType(nodeLabel);
91 String uid = getNodeIdByLabel(nodeLabel, properties);
93 String nodeRecord = buildNodeRecord(uid, color, properties);
95 builder.append(nodeRecord);
101 Iterable<JanusGraphEdge> edges = graph.query().edges();
104 Iterator<JanusGraphEdge> iterator = edges.iterator();
105 while (iterator.hasNext()) {
106 Edge edge = iterator.next();
108 Vertex vertexFrom = edge.outVertex();
109 Vertex vertexTo = edge.inVertex();
110 String fromUid = getNodeIdByLabel(vertexFrom.value(GraphPropertiesDictionary.LABEL.getProperty()),
111 getProperties(vertexFrom));
112 String toUid = getNodeIdByLabel(vertexTo.value(GraphPropertiesDictionary.LABEL.getProperty()),
113 getProperties(vertexTo));
115 String edgeLabel = edge.label();
117 String edgeRecord = buildEdgeRecord(fromUid, toUid, edgeLabel, getProperties(edge));
119 builder.append(edgeRecord);
124 builder.append(" } ");
126 return builder.toString();
131 private String buildEdgeRecord(String fromUid, String toUid, String edgeLabel, Map<String, Object> properties) {
133 StringBuilder builder = new StringBuilder();
134 // LR_0 -> LR_2 [ label = "SS(B)" ];
136 String generatedProps = generateStringFromProperties(properties);
138 String color = getEdgeColorByLabel(edgeLabel);
140 builder.append("\"" + fromUid + "\"" + " -> " + "\"" + toUid + "\"" + " [ color = " + color + " label = \""
141 + edgeLabel + "(" + generatedProps + ")\"" + " ] " + "\n");
143 return builder.toString();
146 private String getEdgeColorByLabel(String edgeLabel) {
148 GraphEdgeLabels edgeLabelEnum = GraphEdgeLabels.getByName(edgeLabel);
150 String color = "black";
152 switch (edgeLabelEnum) {
167 private String generateStringFromProperties(Map<String, Object> properties) {
169 StringBuilder builder = new StringBuilder();
171 if (properties != null) {
172 for (Entry<String, Object> entry : properties.entrySet()) {
173 String key = entry.getKey();
174 String value = entry.getValue().toString();
175 builder.append(key + "=" + value + "__");
178 return builder.toString();
182 private String buildNodeRecord(String uid, String color, Map<String, Object> properties) {
184 StringBuilder builder = new StringBuilder();
186 builder.append("\"" + uid + "\"" + " [ ");
187 builder.append("style = \"bold\" ");
188 builder.append(" color = \"" + color + "\"");
189 builder.append("shape = \"Mrecord\" ");
192 int maxKeyLength = 0;
193 for (Entry<String, Object> entry1 : properties.entrySet()) {
194 String key = entry1.getKey();
195 int keyLength = key.length();
196 if (keyLength > maxKeyLength) {
197 maxKeyLength = keyLength;
201 boolean first = true;
202 for (Entry<String, Object> entry : properties.entrySet()) {
204 String key = entry.getKey();
205 String value = entry.getValue().toString();
207 if (key.equals(GraphPropertiesDictionary.CONSTRAINTS.getProperty())) {
208 value = value.replaceAll("[^\\w\\s]", "_");
211 key = padKey(key, maxKeyLength);
218 label += " { " + key + " | " + value + " } ";
221 builder.append("label = \"" + label + "\" ");
222 builder.append(" ] ");
223 builder.append(" \n ");
224 return builder.toString();
227 private String getNodeIdByLabel(String nodeLabel, Map<String, Object> properties) {
229 NodeTypeEnum typeEnum = NodeTypeEnum.getByName(nodeLabel);
235 uid = (String) properties.get(GraphPropertiesDictionary.USERID.getProperty());
237 case ServiceCategory:
238 case ResourceCategory:
240 uid = (String) properties.get(GraphPropertiesDictionary.NAME.getProperty());
244 uid = (String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
251 private String getColorByNodeType(String nodeLabel) {
253 NodeTypeEnum typeEnum = NodeTypeEnum.getByName(nodeLabel);
255 String color = "red";
257 case ServiceCategory:
260 case ResourceCategory:
264 color = "forestgreen";
270 color = "lightgreen";
278 case RelationshipType:
279 color = "darkorchid";
284 case RequirementImpl:
285 // color = "forestgreen";
302 private String padKey(String key, int maxKeyLength) {
304 int len = key.length();
305 for (int i = len; i < maxKeyLength; i++) {
312 public int getNumberOfVertices(JanusGraph graph) {
314 Iterable<JanusGraphVertex> vertices = graph.query().vertices();
316 if (vertices != null) {
317 Iterator<JanusGraphVertex> iterator = vertices.iterator();
318 while (iterator.hasNext()) {
319 Vertex vertex = iterator.next();
326 public Set<String> getVerticesSet(JanusGraph janusGraph) {
328 Set<String> set = new HashSet<>();
330 Iterable<JanusGraphVertex> vertices = janusGraph.query().vertices();
332 if (vertices != null) {
333 Iterator<JanusGraphVertex> iterator = vertices.iterator();
334 while (iterator.hasNext()) {
335 Vertex vertex = iterator.next();
337 Map<String, Object> properties = getProperties(vertex);
339 String nodeLabel = (String) properties.get(GraphPropertiesDictionary.LABEL.getProperty());
341 String uid = getNodeIdByLabel(nodeLabel, properties);
351 public Map<String, Object> getProperties(Element element) {
353 Map<String, Object> result = null;
355 if (element.keys() != null && element.keys().size() > 0) {
356 Map<String, Property> propertyMap = ElementHelper.propertyMap(element,
357 element.keys().toArray(new String[element.keys().size()]));
358 result = new HashMap<>();
360 for (Entry<String, Property> entry : propertyMap.entrySet()) {
361 String key = entry.getKey();
362 Object value = entry.getValue().value();
364 result.put(key, value);