2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.path.beans;
23 import org.janusgraph.core.PropertyKey;
24 import org.janusgraph.core.JanusGraph;
25 import org.janusgraph.core.JanusGraphQuery;
26 import org.janusgraph.core.schema.ConsistencyModifier;
27 import org.janusgraph.core.schema.JanusGraphIndex;
28 import org.janusgraph.core.schema.JanusGraphManagement;
29 import org.apache.tinkerpop.gremlin.structure.Edge;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
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;
44 import java.util.ArrayList;
45 import java.util.HashMap;
48 public class JanusGraphTestSetup {
51 private static final Logger logger = LoggerFactory.getLogger(JanusGraphTestSetup.class);
53 private static JanusGraph graph;
55 public static boolean createGraph( JanusGraph graph) {
56 JanusGraphTestSetup.graph = graph;
57 createIndexesAndDefaults();
59 logger.info("** JanusGraph graph created ");
64 private static boolean isVertexExist(Map<String, Object> properties) {
65 JanusGraphQuery query = graph.query();
67 if (properties != null && !properties.isEmpty()) {
68 for (Map.Entry<String, Object> entry : properties.entrySet()) {
69 query = query.has(entry.getKey(), entry.getValue());
72 Iterable<Vertex> vertecies = query.vertices();
73 java.util.Iterator<Vertex> iterator = vertecies.iterator();
74 if (iterator.hasNext()) {
80 private static void createDefaultAdminUser() {
81 createUser(getDefaultUserAdmin());
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());
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);
116 private static void createVertexIndixes() {
117 logger.info("** createVertexIndixes started");
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();
129 propKey = graphMgt.getPropertyKey(prop.getProperty());
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();
136 graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
142 graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
150 graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();
156 logger.info("** createVertexIndixes ended");
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();
171 logger.info("** createEdgeIndixes ended");
174 private static void createIndexesAndDefaults() {
175 createVertexIndixes();
177 createDefaultAdminUser();
178 createRootCatalogVertex();
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());