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.jsontitan.utils;
23 import java.io.BufferedOutputStream;
25 import java.io.FileOutputStream;
26 import java.io.OutputStream;
27 import java.util.Iterator;
29 import java.util.UUID;
31 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
32 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
33 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
34 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
36 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
39 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
41 import com.thinkaurelius.titan.core.TitanGraph;
42 import com.thinkaurelius.titan.core.TitanVertex;
44 import fj.data.Either;
46 public final class GraphTestUtils {
48 public static void createRootCatalogVertex(TitanDao titanDao) {
49 GraphVertex catalogRootVertex = new GraphVertex(VertexTypeEnum.CATALOG_ROOT);
50 catalogRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
51 titanDao.createVertex(catalogRootVertex);
54 public static GraphVertex createResourceVertex(TitanDao titanDao, Map<GraphPropertyEnum,Object> metadataProps, ResourceTypeEnum type) {
55 GraphVertex vertex = new GraphVertex();
56 if (type == ResourceTypeEnum.VF) {
57 vertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
58 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
60 vertex.setLabel(VertexTypeEnum.NODE_TYPE);
61 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE);
63 String uuid = UUID.randomUUID().toString();
64 vertex.setUniqueId(uuid);
66 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
67 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
68 vertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, type.name());
69 vertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, false);
70 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
71 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
73 titanDao.createVertex(vertex);
78 public static GraphVertex createServiceVertex(TitanDao titanDao, Map<GraphPropertyEnum, Object> metadataProps){
79 GraphVertex vertex = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE);
80 String uuid = UUID.randomUUID().toString();
81 vertex.setUniqueId(uuid);
82 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
83 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
84 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
85 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
86 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
88 titanDao.createVertex(vertex);
93 public static void clearGraph(TitanDao titanDao) {
94 Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
95 TitanGraph graph = graphResult.left().value();
97 Iterable<TitanVertex> vertices = graph.query().vertices();
98 if (vertices != null) {
99 Iterator<TitanVertex> iterator = vertices.iterator();
100 while (iterator.hasNext()) {
101 TitanVertex vertex = iterator.next();
108 public static String exportGraphMl(TitanGraph graph, String outputDirectory) {
109 String result = null;
110 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
112 try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
113 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
117 } catch (Exception e) {
118 graph.tx().rollback();