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.jsonjanusgraph.utils;
23 import org.janusgraph.core.JanusGraph;
24 import org.janusgraph.core.JanusGraphVertex;
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.janusgraph.JanusGraphDao;
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.janusgraph.JanusGraphOperationStatus;
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(JanusGraphDao janusGraphDao) {
47 GraphVertex catalogRootVertex = new GraphVertex(VertexTypeEnum.CATALOG_ROOT);
48 catalogRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
49 return janusGraphDao.createVertex(catalogRootVertex)
50 .either(v -> v, s -> null);
53 public static GraphVertex createRootArchiveVertex(JanusGraphDao janusGraphDao) {
54 GraphVertex archiveRootVertex = new GraphVertex(VertexTypeEnum.ARCHIVE_ROOT);
55 archiveRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
56 return janusGraphDao.createVertex(archiveRootVertex)
57 .either(v -> v, s -> null);
60 public static GraphVertex createResourceVertex(JanusGraphDao janusGraphDao, 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 janusGraphDao.createVertex(vertex);
80 janusGraphDao.commit();
84 public static GraphVertex createServiceVertex(JanusGraphDao janusGraphDao, 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 janusGraphDao.createVertex(vertex);
95 janusGraphDao.commit();
99 public static void clearGraph(JanusGraphDao janusGraphDao) {
100 Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getJanusGraphClient().getGraph();
101 JanusGraph graph = graphResult.left().value();
103 Iterable<JanusGraphVertex> vertices = graph.query().vertices();
104 if (vertices != null) {
105 Iterator<JanusGraphVertex> iterator = vertices.iterator();
106 while (iterator.hasNext()) {
107 JanusGraphVertex vertex = iterator.next();
111 janusGraphDao.commit();
114 public static String exportGraphMl(JanusGraph 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();