1 package org.openecomp.sdc.asdctool.impl;
3 import java.util.ArrayList;
4 import java.util.HashMap;
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;
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;
28 public class TitanGraphInitializer {
30 private static Logger logger = LoggerFactory.getLogger(TitanGraphInitializer.class.getName());
31 private static TitanGraph graph;
34 public static boolean createGraph(String titanCfgFile) {
35 logger.info("** createGraph with {}", titanCfgFile);
37 logger.info("createGraph : try to load file {}", titanCfgFile);
38 graph = TitanFactory.open(titanCfgFile);
39 if (graph.isClosed()) {
43 } catch (TitanException e) {
44 logger.info("createGraph : failed to open Titan graph with configuration file: {}", titanCfgFile, e);
48 createIndexesAndDefaults();
50 logger.info("** Titan graph created ");
55 private static boolean isVertexExist(Map<String, Object> properties) {
56 TitanGraphQuery query = graph.query();
58 if (properties != null && !properties.isEmpty()) {
59 for (Map.Entry<String, Object> entry : properties.entrySet()) {
60 query = query.has(entry.getKey(), entry.getValue());
63 Iterable<Vertex> vertecies = query.vertices();
64 java.util.Iterator<Vertex> iterator = vertecies.iterator();
65 if (iterator.hasNext()) {
71 private static void createDefaultAdminUser() {
72 createUser(getDefaultUserAdmin());
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());
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);
107 private static void createVertexIndixes() {
108 logger.info("** createVertexIndixes started");
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();
120 propKey = graphMgt.getPropertyKey(prop.getProperty());
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();
127 graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
133 graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
141 graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();
147 logger.info("** createVertexIndixes ended");
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();
162 logger.info("** createEdgeIndixes ended");
165 private static void createIndexesAndDefaults() {
166 createVertexIndixes();
168 createDefaultAdminUser();