Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / path / beans / JanusGraphTestSetup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.beans;
22
23 import org.apache.tinkerpop.gremlin.structure.Edge;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.janusgraph.core.JanusGraph;
26 import org.janusgraph.core.JanusGraphQuery;
27 import org.janusgraph.core.PropertyKey;
28 import org.janusgraph.core.schema.ConsistencyModifier;
29 import org.janusgraph.core.schema.JanusGraphIndex;
30 import org.janusgraph.core.schema.JanusGraphManagement;
31 import org.openecomp.sdc.be.dao.graph.datatype.ActionEnum;
32 import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
33 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
34 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
35 import org.openecomp.sdc.be.dao.neo4j.GraphEdgePropertiesDictionary;
36 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
37 import org.openecomp.sdc.be.dao.utils.UserStatusEnum;
38 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
39 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
40 import org.openecomp.sdc.be.resources.data.UserData;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.Map;
47
48 public class JanusGraphTestSetup {
49
50
51     private static final Logger logger = LoggerFactory.getLogger(JanusGraphTestSetup.class);
52
53     private static JanusGraph graph;
54
55     public static boolean createGraph(  JanusGraph graph) {
56         JanusGraphTestSetup.graph = graph;
57         createIndexesAndDefaults();
58
59         logger.info("** JanusGraph graph created ");
60
61         return true;
62     }
63
64     private static boolean isVertexExist(Map<String, Object> properties) {
65         JanusGraphQuery query = graph.query();
66
67         if (properties != null && !properties.isEmpty()) {
68             for (Map.Entry<String, Object> entry : properties.entrySet()) {
69                 query = query.has(entry.getKey(), entry.getValue());
70             }
71         }
72         Iterable<Vertex> vertecies = query.vertices();
73         java.util.Iterator<Vertex> iterator = vertecies.iterator();
74         if (iterator.hasNext()) {
75             return true;
76         }
77         return false;
78     }
79
80     private static void createDefaultAdminUser() {
81         createUser(getDefaultUserAdmin());
82         graph.tx().commit();
83
84     }
85
86     private static void createUser(UserData user) {
87         Map<String, Object> checkedProperties = new HashMap<>();
88         checkedProperties.put(GraphPropertiesDictionary.USERID.getProperty(), user.getUserId());
89         checkedProperties.put(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.User.getName());
90         Map<String, Object> properties = null;
91         if (!isVertexExist(checkedProperties)) {
92             Vertex vertex = graph.addVertex();
93             vertex.property(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.User.getName());
94             properties = user.toGraphMap();
95             for (Map.Entry<String, Object> entry : properties.entrySet()) {
96                 vertex.property(entry.getKey(), entry.getValue());
97             }
98         }
99     }
100
101     private static UserData getDefaultUserAdmin() {
102         UserData userData = new UserData();
103         userData.setAction(ActionEnum.Create);
104         userData.setElementType(GraphElementTypeEnum.Node);
105         userData.setUserId("jh0003");
106         userData.setEmail("admin@sdc.com");
107         userData.setFirstName("Jimmy");
108         userData.setLastName("Hendrix");
109         userData.setRole("ADMIN");
110         userData.setStatus(UserStatusEnum.ACTIVE.name());
111         userData.setLastLoginTime(0L);
112         return userData;
113     }
114
115
116     private static void createVertexIndixes() {
117         logger.info("** createVertexIndixes started");
118
119         JanusGraphManagement graphMgt = graph.openManagement();
120         JanusGraphIndex index = null;
121         for (GraphPropertiesDictionary prop : GraphPropertiesDictionary.values()) {
122             PropertyKey propKey = null;
123             if (!graphMgt.containsPropertyKey(prop.getProperty())) {
124                 Class<?> clazz = prop.getClazz();
125                 if (!ArrayList.class.getName().equals(clazz.getName()) && !HashMap.class.getName().equals(clazz.getName())) {
126                     propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
127                 }
128             } else {
129                 propKey = graphMgt.getPropertyKey(prop.getProperty());
130             }
131             if (prop.isIndexed()) {
132                 if (!graphMgt.containsGraphIndex(prop.getProperty())) {
133                     if (prop.isUnique()) {
134                         index = graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).unique().buildCompositeIndex();
135
136                         graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
137                         // only
138                         // one
139                         // name
140                         // per
141                         // vertex
142                         graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
143                         // name
144                         // uniqueness
145                         // in
146                         // the
147                         // graph
148
149                     } else {
150                         graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();
151                     }
152                 }
153             }
154         }
155         graphMgt.commit();
156         logger.info("** createVertexIndixes ended");
157
158     }
159
160     private static void createEdgeIndixes() {
161         logger.info("** createEdgeIndixes started");
162         JanusGraphManagement graphMgt = graph.openManagement();
163         for (GraphEdgePropertiesDictionary prop : GraphEdgePropertiesDictionary.values()) {
164             if (!graphMgt.containsGraphIndex(prop.getProperty())) {
165                 PropertyKey propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
166                 graphMgt.buildIndex(prop.getProperty(), Edge.class).addKey(propKey).buildCompositeIndex();
167
168             }
169         }
170         graphMgt.commit();
171         logger.info("** createEdgeIndixes ended");
172     }
173
174     private static void createIndexesAndDefaults() {
175         createVertexIndixes();
176         createEdgeIndixes();
177         createDefaultAdminUser();
178         createRootCatalogVertex();
179     }
180     private static void createRootCatalogVertex(){
181         Vertex vertex = graph.addVertex();
182         vertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty(), IdBuilderUtils.generateUniqueId());
183         vertex.property(GraphPropertyEnum.LABEL.getProperty(), VertexTypeEnum.CATALOG_ROOT.getName());
184         graph.tx().commit();
185     }
186 }