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;
24 import fj.data.Either;
25 import org.apache.tinkerpop.gremlin.structure.Vertex;
26 import org.janusgraph.core.InvalidElementException;
27 import org.janusgraph.core.InvalidIDException;
28 import org.janusgraph.core.JanusGraph;
29 import org.janusgraph.core.JanusGraphConfigurationException;
30 import org.janusgraph.core.JanusGraphFactory;
31 import org.janusgraph.core.PropertyKey;
32 import org.janusgraph.core.QueryException;
33 import org.janusgraph.core.SchemaViolationException;
34 import org.janusgraph.core.schema.ConsistencyModifier;
35 import org.janusgraph.core.schema.JanusGraphIndex;
36 import org.janusgraph.core.schema.JanusGraphManagement;
37 import org.janusgraph.diskstorage.BackendException;
38 import org.janusgraph.diskstorage.ResourceUnavailableException;
39 import org.janusgraph.diskstorage.locking.PermanentLockingException;
40 import org.janusgraph.graphdb.database.idassigner.IDPoolExhaustedException;
41 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
42 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
43 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Component;
48 import javax.annotation.PostConstruct;
49 import java.util.ArrayList;
50 import java.util.HashMap;
52 @Component("janusgraph-client")
53 public class InMemoryJanusGraphClient extends JanusGraphClient {
56 private static final Logger logger = LoggerFactory.getLogger(InMemoryJanusGraphClient.class);
58 private static final String OK = "GOOD";
60 private JanusGraph graph;
62 public InMemoryJanusGraphClient() {
64 logger.info("** JanusGraphClient created");
68 public JanusGraphOperationStatus createGraph() {
70 logger.info("** createGraph started **");
71 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
72 createJanusGraphSchema();
74 logger.info("** in memory graph created");
75 return JanusGraphOperationStatus.OK;
80 public void cleanupGraph() {
85 JanusGraphFactory.drop(graph);
86 } catch (BackendException e) {
92 public JanusGraphOperationStatus createGraph(String janusGraphCfgFile) {
93 logger.info("** open graph with {} started", janusGraphCfgFile);
95 logger.info("openGraph : try to load file {}", janusGraphCfgFile);
96 graph = JanusGraphFactory.open(janusGraphCfgFile);
97 if (graph.isClosed()) {
98 logger.error("janusgraph graph was not initialized");
99 return JanusGraphOperationStatus.NOT_CREATED;
102 } catch (Exception e) {
104 logger.info("createGraph : failed to open JanusGraph graph with configuration file: {}", janusGraphCfgFile, e);
105 return JanusGraphOperationStatus.NOT_CONNECTED;
108 logger.info("** JanusGraph graph created ");
110 return JanusGraphOperationStatus.OK;
114 public Either<JanusGraph, JanusGraphOperationStatus> getGraph() {
116 return Either.left(graph);
118 return Either.right(JanusGraphOperationStatus.NOT_CREATED);
122 public JanusGraphOperationStatus commit() {
126 return JanusGraphOperationStatus.OK;
127 } catch (Exception e) {
128 return handleJanusGraphException(e);
131 return JanusGraphOperationStatus.NOT_CREATED;
135 public JanusGraphOperationStatus rollback() {
139 graph.tx().rollback();
140 return JanusGraphOperationStatus.OK;
141 } catch (Exception e) {
142 return handleJanusGraphException(e);
145 return JanusGraphOperationStatus.NOT_CREATED;
149 public static JanusGraphOperationStatus handleJanusGraphException(Exception e) {
150 if (e instanceof JanusGraphConfigurationException) {
151 return JanusGraphOperationStatus.JANUSGRAPH_CONFIGURATION;
153 if (e instanceof SchemaViolationException) {
154 return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION;
156 if (e instanceof PermanentLockingException) {
157 return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION;
159 if (e instanceof IDPoolExhaustedException) {
160 return JanusGraphOperationStatus.GENERAL_ERROR;
162 if (e instanceof InvalidElementException) {
163 return JanusGraphOperationStatus.INVALID_ELEMENT;
165 if (e instanceof InvalidIDException) {
166 return JanusGraphOperationStatus.INVALID_ID;
168 if (e instanceof QueryException) {
169 return JanusGraphOperationStatus.INVALID_QUERY;
171 if (e instanceof ResourceUnavailableException) {
172 return JanusGraphOperationStatus.RESOURCE_UNAVAILABLE;
174 if (e instanceof IllegalArgumentException) {
175 // TODO check the error message??
176 return JanusGraphOperationStatus.ILLEGAL_ARGUMENT;
179 return JanusGraphOperationStatus.GENERAL_ERROR;
182 public boolean getHealth() {
186 private boolean isGraphOpen() {
191 private static final String JANUSGRAPH_HEALTH_CHECK_STR = "janusGraphHealthCheck";
194 private void createJanusGraphSchema() {
196 JanusGraphManagement graphMgt = graph.openManagement();
197 JanusGraphIndex index = null;
198 for (GraphPropertiesDictionary prop : GraphPropertiesDictionary.values()) {
199 PropertyKey propKey = null;
200 if (!graphMgt.containsPropertyKey(prop.getProperty())) {
201 Class<?> clazz = prop.getClazz();
202 if (!ArrayList.class.getName().equals(clazz.getName()) && !HashMap.class.getName().equals(clazz.getName())) {
203 propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
206 propKey = graphMgt.getPropertyKey(prop.getProperty());
208 if (prop.isIndexed()) {
209 if (!graphMgt.containsGraphIndex(prop.getProperty())) {
210 if (prop.isUnique()) {
211 index = graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).unique().buildCompositeIndex();
213 graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
219 graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
227 graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();