Catalog alignment
[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.jsongraph.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.IS_ABSTRACT, false);
77         for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
78             vertex.addMetadataProperty(prop.getKey(), prop.getValue());
79         }
80         janusGraphDao.createVertex(vertex);
81         janusGraphDao.commit();
82         return vertex;
83     }
84
85     public static GraphVertex createServiceVertex(JanusGraphDao janusGraphDao, Map<GraphPropertyEnum, Object> metadataProps){
86         GraphVertex vertex = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE);
87         String uuid = UUID.randomUUID().toString();
88
89         vertex.setUniqueId(uuid);
90         vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE);
91         vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid);
92         vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
93         vertex.setJsonMetadataField(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
94         vertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
95         for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) {
96             vertex.addMetadataProperty(prop.getKey(), prop.getValue());
97         }
98         janusGraphDao.createVertex(vertex);
99         janusGraphDao.commit();
100         return vertex;
101     }
102
103     public static void clearGraph(JanusGraphDao janusGraphDao) {
104         Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
105         JanusGraph graph = graphResult.left().value();
106
107         Iterable<JanusGraphVertex> vertices = graph.query().vertices();
108         if (vertices != null) {
109             Iterator<JanusGraphVertex> iterator = vertices.iterator();
110             while (iterator.hasNext()) {
111                 JanusGraphVertex vertex = iterator.next();
112                 vertex.remove();
113             }
114         }
115         janusGraphDao.commit();
116     }
117
118     public static String exportGraphMl(JanusGraph graph, String outputDirectory) {
119         String result = null;
120         String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
121         try {
122             try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
123                 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
124             }
125             result = outputFile;
126             graph.tx().commit();
127         } catch (Exception e) {
128             graph.tx().rollback();
129             e.printStackTrace();
130         }
131         return result;
132     }
133 }