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