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