Constraint validation - Policy Properties in a Service
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / 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.jsonjanusgraph.utils;
22
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;
35
36 import java.io.BufferedOutputStream;
37 import java.io.File;
38 import java.io.FileOutputStream;
39 import java.io.OutputStream;
40 import java.util.Iterator;
41 import java.util.Map;
42 import java.util.UUID;
43
44 public final class GraphTestUtils {
45
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);
51     }
52
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);
58     }
59
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);
65         } else {
66             vertex.setLabel(VertexTypeEnum.NODE_TYPE);
67             vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE);
68         }
69         String uuid = UUID.randomUUID().toString();
70         vertex.setUniqueId(uuid);
71
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());
78         }
79         janusGraphDao.createVertex(vertex);
80         janusGraphDao.commit();
81         return vertex;
82     }
83
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());
93         }
94         janusGraphDao.createVertex(vertex);
95         janusGraphDao.commit();
96         return vertex;
97     }
98
99     public static void clearGraph(JanusGraphDao janusGraphDao) {
100         Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
101         JanusGraph graph = graphResult.left().value();
102
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();
108                 vertex.remove();
109             }
110         }
111         janusGraphDao.commit();
112     }
113
114     public static String exportGraphMl(JanusGraph graph, String outputDirectory) {
115         String result = null;
116         String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
117         try {
118             try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
119                 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
120             }
121             result = outputFile;
122             graph.tx().commit();
123         } catch (Exception e) {
124             graph.tx().rollback();
125             e.printStackTrace();
126         }
127         return result;
128     }
129 }