[SDC] rebase 1710 code
[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 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;
34
35 import java.io.BufferedOutputStream;
36 import java.io.File;
37 import java.io.FileOutputStream;
38 import java.io.OutputStream;
39 import java.util.Iterator;
40 import java.util.Map;
41 import java.util.UUID;
42
43 public final class GraphTestUtils {
44
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);
50         } else {
51             vertex.setLabel(VertexTypeEnum.NODE_TYPE);
52             vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE);
53         }
54         String uuid = UUID.randomUUID().toString();
55         vertex.setUniqueId(uuid);
56
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());
63         }
64         titanDao.createVertex(vertex);
65         titanDao.commit();
66         return vertex;
67     }
68
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());
78         }
79         titanDao.createVertex(vertex);
80         titanDao.commit();
81         return vertex;
82     }
83
84     public static void clearGraph(TitanDao titanDao) {
85         Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
86         TitanGraph graph = graphResult.left().value();
87
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();
93                 vertex.remove();
94             }
95         }
96         titanDao.commit();
97     }
98
99     public static String exportGraphMl(TitanGraph graph, String outputDirectory) {
100         String result = null;
101         String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
102         try {
103             try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
104                 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
105             }
106             result = outputFile;
107             graph.tx().commit();
108         } catch (Exception e) {
109             graph.tx().rollback();
110             e.printStackTrace();
111         }
112         return result;
113     }
114 }