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.titan.TitanOperationStatus;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
33 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
35 import java.io.BufferedOutputStream;
37 import java.io.FileOutputStream;
38 import java.io.OutputStream;
39 import java.util.Iterator;
41 import java.util.UUID;
43 public final class GraphTestUtils {
45 public static GraphVertex createResourceVertex(TitanDao titanDao, Map<GraphPropertyEnum,Object> metadataProps, ResourceTypeEnum type) {
46 GraphVertex vertex = new GraphVertex();
47 if (type == ResourceTypeEnum.VF) {
48 vertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
49 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
51 vertex.setLabel(VertexTypeEnum.NODE_TYPE);
52 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE);
54 String uuid = UUID.randomUUID().toString();
55 vertex.setUniqueId(uuid);
57 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
58 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
59 vertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, type.name());
60 vertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, false);
61 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
62 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
64 titanDao.createVertex(vertex);
69 public static GraphVertex createServiceVertex(TitanDao titanDao, Map<GraphPropertyEnum, Object> metadataProps){
70 GraphVertex vertex = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE);
71 String uuid = UUID.randomUUID().toString();
72 vertex.setUniqueId(uuid);
73 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
74 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
75 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
76 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
77 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
79 titanDao.createVertex(vertex);
84 public static void clearGraph(TitanDao titanDao) {
85 Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
86 TitanGraph graph = graphResult.left().value();
88 Iterable<TitanVertex> vertices = graph.query().vertices();
89 if (vertices != null) {
90 Iterator<TitanVertex> iterator = vertices.iterator();
91 while (iterator.hasNext()) {
92 TitanVertex vertex = iterator.next();
99 public static String exportGraphMl(TitanGraph graph, String outputDirectory) {
100 String result = null;
101 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
103 try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
104 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
108 } catch (Exception e) {
109 graph.tx().rollback();