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 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();
73 vertex.setUniqueId(uuid);
74 vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
75 vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
76 vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
77 for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
78 vertex.addMetadataProperty(prop.getKey(), prop.getValue());
80 titanDao.createVertex(vertex);
85 public static void clearGraph(TitanDao titanDao) {
86 Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
87 TitanGraph graph = graphResult.left().value();
89 Iterable<TitanVertex> vertices = graph.query().vertices();
90 if (vertices != null) {
91 Iterator<TitanVertex> iterator = vertices.iterator();
92 while (iterator.hasNext()) {
93 TitanVertex vertex = iterator.next();
100 public static String exportGraphMl(TitanGraph graph, String outputDirectory) {
101 String result = null;
102 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
104 try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
105 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
109 } catch (Exception e) {
110 graph.tx().rollback();