2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.asdctool.impl;
23 import com.thinkaurelius.titan.core.*;
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 import org.apache.tinkerpop.gremlin.structure.Edge;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.openecomp.sdc.be.dao.graph.datatype.ActionEnum;
30 import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
32 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
33 import org.openecomp.sdc.be.dao.neo4j.GraphEdgePropertiesDictionary;
34 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
35 import org.openecomp.sdc.be.dao.utils.UserStatusEnum;
36 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
37 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
38 import org.openecomp.sdc.be.resources.data.UserData;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.util.ArrayList;
43 import java.util.HashMap;
46 public class TitanGraphInitializer {
48 private static Logger logger = LoggerFactory.getLogger(TitanGraphInitializer.class.getName());
49 private static TitanGraph graph;
51 public static boolean createGraph(String titanCfgFile) {
52 logger.info("** createGraph with {}", titanCfgFile);
54 logger.info("createGraph : try to load file {}", titanCfgFile);
55 graph = TitanFactory.open(titanCfgFile);
56 if (graph.isClosed()) {
60 } catch (TitanException e) {
61 logger.info("createGraph : failed to open Titan graph with configuration file: {}", titanCfgFile, e);
65 createIndexesAndDefaults();
67 logger.info("** Titan graph created ");
72 private static boolean isVertexExist(Map<String, Object> properties) {
73 TitanGraphQuery query = graph.query();
75 if (properties != null && !properties.isEmpty()) {
76 for (Map.Entry<String, Object> entry : properties.entrySet()) {
77 query = query.has(entry.getKey(), entry.getValue());
80 Iterable<Vertex> vertecies = query.vertices();
81 java.util.Iterator<Vertex> iterator = vertecies.iterator();
82 if (iterator.hasNext()) {
88 private static boolean isVertexNotExist(Map<String, Object> properties) {
89 return !isVertexExist(properties);
92 private static void createDefaultAdminUser() {
93 createUser(getDefaultUserAdmin());
98 private static void createUser(UserData user) {
99 Map<String, Object> checkedProperties = new HashMap<>();
100 checkedProperties.put(GraphPropertiesDictionary.USERID.getProperty(), user.getUserId());
101 checkedProperties.put(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.User.getName());
102 Map<String, Object> properties = null;
103 if (!isVertexExist(checkedProperties)) {
104 Vertex vertex = graph.addVertex();
105 vertex.property(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.User.getName());
106 properties = user.toGraphMap();
107 for (Map.Entry<String, Object> entry : properties.entrySet()) {
108 vertex.property(entry.getKey(), entry.getValue());
113 private static UserData getDefaultUserAdmin() {
114 UserData userData = new UserData();
115 userData.setAction(ActionEnum.Create);
116 userData.setElementType(GraphElementTypeEnum.Node);
117 userData.setUserId("jh0003");
118 userData.setEmail("admin@sdc.com");
119 userData.setFirstName("Jimmy");
120 userData.setLastName("Hendrix");
121 userData.setRole("ADMIN");
122 userData.setStatus(UserStatusEnum.ACTIVE.name());
123 userData.setLastLoginTime(0L);
127 private static void createVertexIndixes() {
128 logger.info("** createVertexIndixes started");
130 TitanManagement graphMgt = graph.openManagement();
131 TitanGraphIndex index = null;
132 for (GraphPropertiesDictionary prop : GraphPropertiesDictionary.values()) {
133 PropertyKey propKey = null;
134 if (!graphMgt.containsPropertyKey(prop.getProperty())) {
135 Class<?> clazz = prop.getClazz();
136 if (!clazz.isAssignableFrom(ArrayList.class) && !clazz.isAssignableFrom(HashMap.class)) {
137 propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
140 propKey = graphMgt.getPropertyKey(prop.getProperty());
142 if (prop.isIndexed()) {
143 if (!graphMgt.containsGraphIndex(prop.getProperty())) {
144 if (prop.isUnique()) {
145 index = graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).unique().buildCompositeIndex();
147 graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
153 graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
161 graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();
167 logger.info("** createVertexIndixes ended");
171 private static void createEdgeIndixes() {
172 logger.info("** createEdgeIndixes started");
173 TitanManagement graphMgt = graph.openManagement();
174 for (GraphEdgePropertiesDictionary prop : GraphEdgePropertiesDictionary.values()) {
175 if (!graphMgt.containsGraphIndex(prop.getProperty())) {
176 PropertyKey propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
177 graphMgt.buildIndex(prop.getProperty(), Edge.class).addKey(propKey).buildCompositeIndex();
182 logger.info("** createEdgeIndixes ended");
185 private static void createIndexesAndDefaults() {
186 createVertexIndixes();
188 createDefaultAdminUser();
189 createRootVertex(VertexTypeEnum.CATALOG_ROOT);
190 createRootVertex(VertexTypeEnum.ARCHIVE_ROOT);
193 private static void createRootVertex(VertexTypeEnum vertexTypeEnum) {
194 Map<String, Object> checkedProperties = new HashMap<>();
195 checkedProperties.put(GraphPropertiesDictionary.LABEL.getProperty(), vertexTypeEnum.getName());
196 if (isVertexNotExist(checkedProperties)) {
197 Vertex vertex = graph.addVertex();
198 vertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty(), IdBuilderUtils.generateUniqueId());
199 vertex.property(GraphPropertyEnum.LABEL.getProperty(), vertexTypeEnum.getName());