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 org.janusgraph.core.*;
25 import org.janusgraph.core.schema.ConsistencyModifier;
26 import org.janusgraph.core.schema.JanusGraphIndex;
27 import org.janusgraph.core.schema.JanusGraphManagement;
28 import org.janusgraph.core.util.JanusGraphCleanup;
29 import org.janusgraph.diskstorage.BackendException;
30 import org.janusgraph.diskstorage.ResourceUnavailableException;
31 import org.janusgraph.diskstorage.locking.PermanentLockingException;
32 import org.janusgraph.graphdb.database.idassigner.IDPoolExhaustedException;
33 import fj.data.Either;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.openecomp.sdc.be.dao.JanusGraphClientStrategy;
36 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
37 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
38 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Component;
43 import javax.annotation.PostConstruct;
44 import java.util.ArrayList;
45 import java.util.HashMap;
47 @Component("janusgraph-client")
48 public class InMemoryJanusGraphClient extends JanusGraphClient {
51 private static final Logger logger = LoggerFactory.getLogger(InMemoryJanusGraphClient.class);
53 private static final String OK = "GOOD";
55 public InMemoryJanusGraphClient() {
59 private JanusGraph graph;
60 JanusGraphClientStrategy janusGraphClientStrategy;
62 public InMemoryJanusGraphClient(JanusGraphClientStrategy janusGraphClientStrategy) {
64 this.janusGraphClientStrategy = janusGraphClientStrategy;
65 logger.info("** JanusGraphClient created");
69 public JanusGraphOperationStatus createGraph() {
71 logger.info("** createGraph started **");
72 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
73 createJanusGraphSchema();
75 logger.info("** in memory graph created");
76 return JanusGraphOperationStatus.OK;
81 public void cleanupGraph() {
86 JanusGraphFactory.drop(graph);
87 } catch (BackendException e) {
93 public JanusGraphOperationStatus createGraph(String janusGraphCfgFile) {
94 logger.info("** open graph with {} started", janusGraphCfgFile);
96 logger.info("openGraph : try to load file {}", janusGraphCfgFile);
97 graph = JanusGraphFactory.open(janusGraphCfgFile);
98 if (graph.isClosed()) {
99 logger.error("janusgraph graph was not initialized");
100 return JanusGraphOperationStatus.NOT_CREATED;
103 } catch (Exception e) {
105 logger.info("createGraph : failed to open JanusGraph graph with configuration file: {}", janusGraphCfgFile, e);
106 return JanusGraphOperationStatus.NOT_CONNECTED;
109 logger.info("** JanusGraph graph created ");
111 return JanusGraphOperationStatus.OK;
115 public Either<JanusGraph, JanusGraphOperationStatus> getGraph() {
117 return Either.left(graph);
119 return Either.right(JanusGraphOperationStatus.NOT_CREATED);
123 public JanusGraphOperationStatus commit() {
127 return JanusGraphOperationStatus.OK;
128 } catch (Exception e) {
129 return handleJanusGraphException(e);
132 return JanusGraphOperationStatus.NOT_CREATED;
136 public JanusGraphOperationStatus rollback() {
140 graph.tx().rollback();
141 return JanusGraphOperationStatus.OK;
142 } catch (Exception e) {
143 return handleJanusGraphException(e);
146 return JanusGraphOperationStatus.NOT_CREATED;
150 public static JanusGraphOperationStatus handleJanusGraphException(Exception e) {
151 if (e instanceof JanusGraphConfigurationException) {
152 return JanusGraphOperationStatus.JANUSGRAPH_CONFIGURATION;
154 if (e instanceof SchemaViolationException) {
155 return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION;
157 if (e instanceof PermanentLockingException) {
158 return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION;
160 if (e instanceof IDPoolExhaustedException) {
161 return JanusGraphOperationStatus.GENERAL_ERROR;
163 if (e instanceof InvalidElementException) {
164 return JanusGraphOperationStatus.INVALID_ELEMENT;
166 if (e instanceof InvalidIDException) {
167 return JanusGraphOperationStatus.INVALID_ID;
169 if (e instanceof QueryException) {
170 return JanusGraphOperationStatus.INVALID_QUERY;
172 if (e instanceof ResourceUnavailableException) {
173 return JanusGraphOperationStatus.RESOURCE_UNAVAILABLE;
175 if (e instanceof IllegalArgumentException) {
176 // TODO check the error message??
177 return JanusGraphOperationStatus.ILLEGAL_ARGUMENT;
180 return JanusGraphOperationStatus.GENERAL_ERROR;
183 public boolean getHealth() {
187 private boolean isGraphOpen() {
192 private static final String JANUSGRAPH_HEALTH_CHECK_STR = "janusGraphHealthCheck";
195 private void createJanusGraphSchema() {
197 JanusGraphManagement graphMgt = graph.openManagement();
198 JanusGraphIndex index = null;
199 for (GraphPropertiesDictionary prop : GraphPropertiesDictionary.values()) {
200 PropertyKey propKey = null;
201 if (!graphMgt.containsPropertyKey(prop.getProperty())) {
202 Class<?> clazz = prop.getClazz();
203 if (!ArrayList.class.getName().equals(clazz.getName()) && !HashMap.class.getName().equals(clazz.getName())) {
204 propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
207 propKey = graphMgt.getPropertyKey(prop.getProperty());
209 if (prop.isIndexed()) {
210 if (!graphMgt.containsGraphIndex(prop.getProperty())) {
211 if (prop.isUnique()) {
212 index = graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).unique().buildCompositeIndex();
214 graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
220 graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
228 graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();