Sync Integ to Master
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsontitan / utils / GraphTestUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsontitan.utils;
22
23 import java.io.BufferedOutputStream;
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.OutputStream;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.UUID;
30
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;
40
41 import com.thinkaurelius.titan.core.TitanGraph;
42 import com.thinkaurelius.titan.core.TitanVertex;
43
44 import fj.data.Either;
45
46 public final class GraphTestUtils {
47
48         public static void createRootCatalogVertex(TitanDao titanDao) {
49                 GraphVertex catalogRootVertex = new GraphVertex(VertexTypeEnum.CATALOG_ROOT);
50                 catalogRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
51                 titanDao.createVertex(catalogRootVertex);
52         }
53
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);
59         } else {
60             vertex.setLabel(VertexTypeEnum.NODE_TYPE);
61             vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE);
62         }
63         String uuid = UUID.randomUUID().toString();
64         vertex.setUniqueId(uuid);
65
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());
72         }
73         titanDao.createVertex(vertex);
74         titanDao.commit();
75         return vertex;
76     }
77
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());
87         }
88         titanDao.createVertex(vertex);
89         titanDao.commit();
90         return vertex;
91     }
92
93     public static void clearGraph(TitanDao titanDao) {
94         Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
95         TitanGraph graph = graphResult.left().value();
96
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();
102                 vertex.remove();
103             }
104         }
105         titanDao.commit();
106     }
107
108     public static String exportGraphMl(TitanGraph graph, String outputDirectory) {
109         String result = null;
110         String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
111         try {
112             try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
113                 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
114             }
115             result = outputFile;
116             graph.tx().commit();
117         } catch (Exception e) {
118             graph.tx().rollback();
119             e.printStackTrace();
120         }
121         return result;
122     }
123 }