Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / util / PrintGraph.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.be.model.operations.impl.util;
22
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;
34
35 import java.util.*;
36 import java.util.Map.Entry;
37 import java.util.Map;
38 import java.util.Map.Entry;
39 import java.util.Set;
40
41 public class PrintGraph {
42
43     public void printGraphVertices(JanusGraph graph) {
44
45         Iterable<JanusGraphVertex> vertices = graph.query().vertices();
46
47         if (vertices != null) {
48             Iterator<JanusGraphVertex> iterator = vertices.iterator();
49             while (iterator.hasNext()) {
50                 Vertex vertex = iterator.next();
51             }
52
53         }
54         // graph.commit();
55         graph.tx().commit();
56     }
57
58     public void printGraphEdges(JanusGraph graph) {
59         Iterable<JanusGraphEdge> vertices = graph.query().edges();
60
61         if (vertices != null) {
62             Iterator<JanusGraphEdge> iterator = vertices.iterator();
63             while (iterator.hasNext()) {
64                 Edge edge = iterator.next();
65
66             }
67
68         }
69         graph.tx().commit();
70     }
71
72     public String buildGraphForWebgraphWiz(JanusGraph graph) {
73
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();
79
80         if (vertices != null) {
81             Iterator<JanusGraphVertex> iterator = vertices.iterator();
82             while (iterator.hasNext()) {
83                 Vertex vertex = iterator.next();
84
85                 Map<String, Object> properties = getProperties(vertex);
86
87                 String nodeLabel = (String) properties.get(GraphPropertiesDictionary.LABEL.getProperty());
88
89                 String color = getColorByNodeType(nodeLabel);
90
91                 String uid = getNodeIdByLabel(nodeLabel, properties);
92
93                 String nodeRecord = buildNodeRecord(uid, color, properties);
94
95                 builder.append(nodeRecord);
96
97             }
98
99         }
100
101         Iterable<JanusGraphEdge> edges = graph.query().edges();
102
103         if (edges != null) {
104             Iterator<JanusGraphEdge> iterator = edges.iterator();
105             while (iterator.hasNext()) {
106                 Edge edge = iterator.next();
107
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));
114
115                 String edgeLabel = edge.label();
116
117                 String edgeRecord = buildEdgeRecord(fromUid, toUid, edgeLabel, getProperties(edge));
118
119                 builder.append(edgeRecord);
120             }
121
122         }
123
124         builder.append(" } ");
125
126         return builder.toString();
127
128     }
129
130
131     private String buildEdgeRecord(String fromUid, String toUid, String edgeLabel, Map<String, Object> properties) {
132
133         StringBuilder builder = new StringBuilder();
134         // LR_0 -> LR_2 [ label = "SS(B)" ];
135
136         String generatedProps = generateStringFromProperties(properties);
137
138         String color = getEdgeColorByLabel(edgeLabel);
139
140         builder.append("\"" + fromUid + "\"" + " -> " + "\"" + toUid + "\"" + " [ color = " + color + " label = \""
141                 + edgeLabel + "(" + generatedProps + ")\"" + " ] " + "\n");
142
143         return builder.toString();
144     }
145
146     private String getEdgeColorByLabel(String edgeLabel) {
147
148         GraphEdgeLabels edgeLabelEnum = GraphEdgeLabels.getByName(edgeLabel);
149
150         String color = "black";
151
152         switch (edgeLabelEnum) {
153         case PROPERTY:
154             color = "orange";
155             break;
156         case CAPABILITY:
157             break;
158         case DERIVED_FROM:
159             color = "red";
160         default:
161             break;
162         }
163
164         return color;
165     }
166
167     private String generateStringFromProperties(Map<String, Object> properties) {
168
169         StringBuilder builder = new StringBuilder();
170
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 + "__");
176             }
177         }
178         return builder.toString();
179
180     }
181
182     private String buildNodeRecord(String uid, String color, Map<String, Object> properties) {
183
184         StringBuilder builder = new StringBuilder();
185
186         builder.append("\"" + uid + "\"" + " [ ");
187         builder.append("style = \"bold\" ");
188         builder.append(" color = \"" + color + "\"");
189         builder.append("shape = \"Mrecord\" ");
190
191         String label = "";
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;
198             }
199         }
200
201         boolean first = true;
202         for (Entry<String, Object> entry : properties.entrySet()) {
203
204             String key = entry.getKey();
205             String value = entry.getValue().toString();
206
207             if (key.equals(GraphPropertiesDictionary.CONSTRAINTS.getProperty())) {
208                 value = value.replaceAll("[^\\w\\s]", "_");
209             }
210
211             key = padKey(key, maxKeyLength);
212
213             if (first) {
214                 first = false;
215             } else {
216                 label += " | ";
217             }
218             label += " { " + key + " | " + value + " } ";
219         }
220
221         builder.append("label = \"" + label + "\" ");
222         builder.append(" ] ");
223         builder.append(" \n ");
224         return builder.toString();
225     }
226
227     private String getNodeIdByLabel(String nodeLabel, Map<String, Object> properties) {
228
229         NodeTypeEnum typeEnum = NodeTypeEnum.getByName(nodeLabel);
230
231         String uid = null;
232         switch (typeEnum) {
233
234         case User:
235             uid = (String) properties.get(GraphPropertiesDictionary.USERID.getProperty());
236             break;
237         case ServiceCategory:
238         case ResourceCategory:
239         case Tag:
240             uid = (String) properties.get(GraphPropertiesDictionary.NAME.getProperty());
241             break;
242
243         default:
244             uid = (String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
245             break;
246         }
247
248         return uid;
249     }
250
251     private String getColorByNodeType(String nodeLabel) {
252
253         NodeTypeEnum typeEnum = NodeTypeEnum.getByName(nodeLabel);
254
255         String color = "red";
256         switch (typeEnum) {
257         case ServiceCategory:
258             color = "blue";
259             break;
260         case ResourceCategory:
261             color = "blue";
262             break;
263         case Resource:
264             color = "forestgreen";
265             break;
266         case User:
267             color = "green";
268             break;
269         case Capability:
270             color = "lightgreen";
271             break;
272         case CapabilityType:
273             color = "gray";
274             break;
275         case Property:
276             color = "cyan";
277             break;
278         case RelationshipType:
279             color = "darkorchid";
280             break;
281         case Requirement:
282             color = "gold";
283             break;
284         case RequirementImpl:
285             // color = "forestgreen";
286             color = "gold";
287             break;
288         case Service:
289             color = "cyan4";
290             break;
291         case Tag:
292             color = "dimgrey";
293             break;
294         default:
295             break;
296
297         }
298
299         return color;
300     }
301
302     private String padKey(String key, int maxKeyLength) {
303
304         int len = key.length();
305         for (int i = len; i < maxKeyLength; i++) {
306             key += " ";
307         }
308
309         return key;
310     }
311
312     public int getNumberOfVertices(JanusGraph graph) {
313         int counter = 0;
314         Iterable<JanusGraphVertex> vertices = graph.query().vertices();
315
316         if (vertices != null) {
317             Iterator<JanusGraphVertex> iterator = vertices.iterator();
318             while (iterator.hasNext()) {
319                 Vertex vertex = iterator.next();
320                 counter++;
321             }
322         }
323         return counter;
324     }
325
326     public Set<String> getVerticesSet(JanusGraph janusGraph) {
327
328         Set<String> set = new HashSet<>();
329
330         Iterable<JanusGraphVertex> vertices = janusGraph.query().vertices();
331
332         if (vertices != null) {
333             Iterator<JanusGraphVertex> iterator = vertices.iterator();
334             while (iterator.hasNext()) {
335                 Vertex vertex = iterator.next();
336
337                 Map<String, Object> properties = getProperties(vertex);
338
339                 String nodeLabel = (String) properties.get(GraphPropertiesDictionary.LABEL.getProperty());
340
341                 String uid = getNodeIdByLabel(nodeLabel, properties);
342
343                 set.add(uid);
344             }
345         }
346
347         return set;
348
349     }
350
351     public Map<String, Object> getProperties(Element element) {
352
353         Map<String, Object> result = null;
354
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<>();
359
360             for (Entry<String, Property> entry : propertyMap.entrySet()) {
361                 String key = entry.getKey();
362                 Object value = entry.getValue().value();
363
364                 result.put(key, value);
365             }
366         }
367         return result;
368     }
369
370 }