0c1399bc51946ee17c103110adc15cd8516aebcb
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / path / 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.components.path.utils;
22
23 import fj.data.Either;
24 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
25 import org.janusgraph.core.JanusGraph;
26 import org.janusgraph.core.JanusGraphVertex;
27 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
29 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
30 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
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.JsonPresentationFields;
35 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
36
37 import java.io.BufferedOutputStream;
38 import java.io.File;
39 import java.io.FileOutputStream;
40 import java.io.OutputStream;
41 import java.util.Iterator;
42 import java.util.Map;
43 import java.util.UUID;
44
45 public final class GraphTestUtils {
46
47     public static GraphVertex createRootCatalogVertex(JanusGraphDao janusGraphDao) {
48         GraphVertex catalogRootVertex = new GraphVertex(VertexTypeEnum.CATALOG_ROOT);
49         catalogRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
50         return janusGraphDao.createVertex(catalogRootVertex)
51                 .either(v -> v, s -> null);
52     }
53
54     public static GraphVertex createRootArchiveVertex(JanusGraphDao janusGraphDao) {
55         GraphVertex archiveRootVertex = new GraphVertex(VertexTypeEnum.ARCHIVE_ROOT);
56         archiveRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
57         return janusGraphDao.createVertex(archiveRootVertex)
58                 .either(v -> v, s -> null);
59     }
60
61     public static GraphVertex createResourceVertex(JanusGraphDao janusGraphDao, Map<GraphPropertyEnum,Object> metadataProps, ResourceTypeEnum type) {
62         GraphVertex vertex = new GraphVertex();
63         if (type == ResourceTypeEnum.VF) {
64             vertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
65             vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
66         } else {
67             vertex.setLabel(VertexTypeEnum.NODE_TYPE);
68             vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE);
69         }
70         String uuid = UUID.randomUUID().toString();
71         vertex.setUniqueId(uuid);
72
73         vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
74         vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
75         vertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, type.name());
76         vertex.addMetadataProperty(GraphPropertyEnum.MODEL, type.name());
77         vertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, false);
78         for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
79             vertex.addMetadataProperty(prop.getKey(), prop.getValue());
80         }
81         janusGraphDao.createVertex(vertex);
82         janusGraphDao.commit();
83         return vertex;
84     }
85
86     public static GraphVertex createServiceVertex(JanusGraphDao janusGraphDao, Map<GraphPropertyEnum, Object> metadataProps){
87         GraphVertex vertex = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE);
88         String uuid = UUID.randomUUID().toString();
89
90         vertex.setUniqueId(uuid);
91         vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
92         vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
93         vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
94         vertex.setJsonMetadataField(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
95         vertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
96         for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
97             vertex.addMetadataProperty(prop.getKey(), prop.getValue());
98         }
99         janusGraphDao.createVertex(vertex);
100         janusGraphDao.commit();
101         return vertex;
102     }
103
104     public static void clearGraph(JanusGraphDao janusGraphDao) {
105         Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
106         JanusGraph graph = graphResult.left().value();
107
108         Iterable<JanusGraphVertex> vertices = graph.query().vertices();
109         if (vertices != null) {
110             Iterator<JanusGraphVertex> iterator = vertices.iterator();
111             while (iterator.hasNext()) {
112                 JanusGraphVertex vertex = iterator.next();
113                 vertex.remove();
114             }
115         }
116         janusGraphDao.commit();
117     }
118
119     public static String exportGraphMl(JanusGraph graph, String outputDirectory) {
120         String result = null;
121         String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
122         try {
123             try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
124                 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
125             }
126             result = outputFile;
127             graph.tx().commit();
128         } catch (Exception e) {
129             graph.tx().rollback();
130             e.printStackTrace();
131         }
132         return result;
133     }
134 }