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 com.thinkaurelius.titan.core.TitanGraph;
24 import com.thinkaurelius.titan.core.TitanVertex;
25 import fj.data.Either;
26 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
27 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
28 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
29 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
30 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
31 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
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.ResourceTypeEnum;
36 import java.io.BufferedOutputStream;
38 import java.io.FileOutputStream;
39 import java.io.OutputStream;
40 import java.util.Iterator;
42 import java.util.UUID;
44 public final class GraphTestUtils {
46 public static GraphVertex createRootCatalogVertex(TitanDao titanDao) {
47 GraphVertex catalogRootVertex = new GraphVertex(VertexTypeEnum.CATALOG_ROOT);
48 catalogRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
49 return titanDao.createVertex(catalogRootVertex)
50 .either(v -> v, s -> null);
53 public static GraphVertex createRootArchiveVertex(TitanDao titanDao) {
54 GraphVertex archiveRootVertex = new GraphVertex(VertexTypeEnum.ARCHIVE_ROOT);
55 archiveRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
56 return titanDao.createVertex(archiveRootVertex)
57 .either(v -> v, s -> null);
60 public static GraphVertex createResourceVertex(TitanDao titanDao, Map<GraphPropertyEnum,Object> metadataProps, ResourceTypeEnum type) {
61 GraphVertex vertex = new GraphVertex();
62 if (type == ResourceTypeEnum.VF) {
63 vertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
64 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
66 vertex.setLabel(VertexTypeEnum.NODE_TYPE);
67 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE);
69 String uuid = UUID.randomUUID().toString();
70 vertex.setUniqueId(uuid);
72 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
73 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
74 vertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, type.name());
75 vertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, false);
76 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
77 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
79 titanDao.createVertex(vertex);
84 public static GraphVertex createServiceVertex(TitanDao titanDao, Map<GraphPropertyEnum, Object> metadataProps){
85 GraphVertex vertex = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE);
86 String uuid = UUID.randomUUID().toString();
87 vertex.setUniqueId(uuid);
88 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
89 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
90 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
91 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
92 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
94 titanDao.createVertex(vertex);
99 public static void clearGraph(TitanDao titanDao) {
100 Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
101 TitanGraph graph = graphResult.left().value();
103 Iterable<TitanVertex> vertices = graph.query().vertices();
104 if (vertices != null) {
105 Iterator<TitanVertex> iterator = vertices.iterator();
106 while (iterator.hasNext()) {
107 TitanVertex vertex = iterator.next();
114 public static String exportGraphMl(TitanGraph graph, String outputDirectory) {
115 String result = null;
116 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
118 try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
119 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
123 } catch (Exception e) {
124 graph.tx().rollback();