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.components.path.utils;
23 import fj.data.Either;
24 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
25 import org.janusgraph.core.JanusGraph;
26 import org.janusgraph.core.JanusGraphVertex;
27 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
29 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
30 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
32 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
33 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
34 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
35 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
37 import java.io.BufferedOutputStream;
39 import java.io.FileOutputStream;
40 import java.io.OutputStream;
41 import java.util.Iterator;
43 import java.util.UUID;
45 public final class GraphTestUtils {
47 public static GraphVertex createRootCatalogVertex(JanusGraphDao janusGraphDao) {
48 GraphVertex catalogRootVertex = new GraphVertex(VertexTypeEnum.CATALOG_ROOT);
49 catalogRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
50 return janusGraphDao.createVertex(catalogRootVertex)
51 .either(v -> v, s -> null);
54 public static GraphVertex createRootArchiveVertex(JanusGraphDao janusGraphDao) {
55 GraphVertex archiveRootVertex = new GraphVertex(VertexTypeEnum.ARCHIVE_ROOT);
56 archiveRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
57 return janusGraphDao.createVertex(archiveRootVertex)
58 .either(v -> v, s -> null);
61 public static GraphVertex createResourceVertex(JanusGraphDao janusGraphDao, Map<GraphPropertyEnum,Object> metadataProps, ResourceTypeEnum type) {
62 GraphVertex vertex = new GraphVertex();
63 if (type == ResourceTypeEnum.VF) {
64 vertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
65 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
67 vertex.setLabel(VertexTypeEnum.NODE_TYPE);
68 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE);
70 String uuid = UUID.randomUUID().toString();
71 vertex.setUniqueId(uuid);
73 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
74 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
75 vertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, type.name());
76 vertex.addMetadataProperty(GraphPropertyEnum.MODEL, type.name());
77 vertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, false);
78 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
79 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
81 janusGraphDao.createVertex(vertex);
82 janusGraphDao.commit();
86 public static GraphVertex createServiceVertex(JanusGraphDao janusGraphDao, Map<GraphPropertyEnum, Object> metadataProps){
87 GraphVertex vertex = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE);
88 String uuid = UUID.randomUUID().toString();
90 vertex.setUniqueId(uuid);
91 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
92 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
93 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
94 vertex.setJsonMetadataField(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
95 vertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
96 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
97 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
99 janusGraphDao.createVertex(vertex);
100 janusGraphDao.commit();
104 public static void clearGraph(JanusGraphDao janusGraphDao) {
105 Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
106 JanusGraph graph = graphResult.left().value();
108 Iterable<JanusGraphVertex> vertices = graph.query().vertices();
109 if (vertices != null) {
110 Iterator<JanusGraphVertex> iterator = vertices.iterator();
111 while (iterator.hasNext()) {
112 JanusGraphVertex vertex = iterator.next();
116 janusGraphDao.commit();
119 public static String exportGraphMl(JanusGraph graph, String outputDirectory) {
120 String result = null;
121 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
123 try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
124 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
128 } catch (Exception e) {
129 graph.tx().rollback();