2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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=========================================================
20 package org.onap.aai.db.schema;
22 import java.util.ArrayList;
23 import java.util.List;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.onap.aai.introspection.Version;
31 import org.janusgraph.core.PropertyKey;
32 import org.janusgraph.core.JanusGraph;
33 import org.janusgraph.core.schema.SchemaStatus;
34 import org.janusgraph.core.schema.JanusGraphIndex;
35 import org.janusgraph.core.schema.JanusGraphManagement;
36 import org.janusgraph.core.schema.JanusGraphManagement.IndexBuilder;
38 public class ManageJanusGraphSchema {
40 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ManageJanusGraphSchema.class);
42 private JanusGraphManagement graphMgmt;
43 private JanusGraph graph;
44 private List<DBProperty> aaiProperties;
45 private List<DBIndex> aaiIndexes;
46 private List<EdgeProperty> aaiEdgeProperties;
47 private Auditor oxmInfo = null;
48 private Auditor graphInfo = null;
51 * Instantiates a new manage JanusGraph schema.
53 * @param graph the graph
55 public ManageJanusGraphSchema(final JanusGraph graph) {
57 oxmInfo = AuditorFactory.getOXMAuditor(Version.v8);
58 graphInfo = AuditorFactory.getGraphAuditor(graph);
65 public void buildSchema() {
67 this.graphMgmt = graph.openManagement();
68 aaiProperties = new ArrayList<>();
69 aaiEdgeProperties = new ArrayList<>();
70 aaiIndexes = new ArrayList<>();
71 aaiProperties.addAll(oxmInfo.getAuditDoc().getProperties());
72 aaiIndexes.addAll(oxmInfo.getAuditDoc().getIndexes());
73 aaiEdgeProperties.addAll(oxmInfo.getAuditDoc().getEdgeLabels());
78 } catch (Exception e) {
79 logger.error(e.getMessage(),e);
86 * Creates the property keys.
88 private void createPropertyKeys() {
90 for (DBProperty prop : aaiProperties) {
91 this.createProperty(graphMgmt, prop);
97 * Creates the indexes.
99 private void createIndexes() {
101 for (DBIndex index : aaiIndexes) {
102 Set<DBProperty> props = index.getProperties();
103 boolean isChanged = false;
104 boolean isNew = false;
105 List<PropertyKey> keyList = new ArrayList<>();
106 for (DBProperty prop : props) {
107 keyList.add(graphMgmt.getPropertyKey(prop.getName()));
109 if (graphMgmt.containsGraphIndex(index.getName())) {
110 JanusGraphIndex janusGraphIndex = graphMgmt.getGraphIndex(index.getName());
111 PropertyKey[] dbKeys = janusGraphIndex.getFieldKeys();
112 if (dbKeys.length != keyList.size()) {
116 for (PropertyKey key : keyList) {
117 if (!dbKeys[i].equals(key)) {
127 if (!keyList.isEmpty()) {
128 this.createIndex(graphMgmt, index.getName(), keyList, index.isUnique(), isNew, isChanged);
133 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is
134 // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One"
135 // We use the same edge-label for edges between many different types of nodes and our internal
136 // multiplicty definitions depends on which two types of nodes are being connected.
138 * Creates the edge labels.
140 private void createEdgeLabels() {
143 for (EdgeProperty prop : aaiEdgeProperties) {
145 if (graphMgmt.containsEdgeLabel(prop.getName())) {
148 graphMgmt.makeEdgeLabel(prop.getName()).multiplicity(prop.getMultiplicity()).make();
157 * Creates the property.
159 * @param mgmt the mgmt
160 * @param prop the prop
162 private void createProperty(JanusGraphManagement mgmt, DBProperty prop) {
163 if (mgmt.containsPropertyKey(prop.getName())) {
164 PropertyKey key = mgmt.getPropertyKey(prop.getName());
165 boolean isChanged = false;
166 if (!prop.getCardinality().equals(key.cardinality())) {
169 if (!prop.getTypeClass().equals(key.dataType())) {
174 this.replaceProperty(prop);
177 //create a new property key
178 logger.info("Key: " + prop.getName() + " not found - adding");
179 mgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()).make();
186 * @param mgmt the mgmt
187 * @param indexName the index name
188 * @param keys the keys
189 * @param isUnique the is unique
190 * @param isNew the is new
191 * @param isChanged the is changed
193 private void createIndex(JanusGraphManagement mgmt, String indexName, List<PropertyKey> keys, boolean isUnique, boolean isNew, boolean isChanged) {
195 if (isNew || isChanged) {
198 IndexBuilder builder = mgmt.buildIndex(indexName,Vertex.class);
199 for (PropertyKey k : keys) {
205 builder.buildCompositeIndex();
206 logger.info("Built index for " + indexName + " with keys: " + keys);
212 * Wait for completion.
214 * @param name the name
215 * @throws InterruptedException the interrupted exception
217 private void waitForCompletion(String name) throws InterruptedException {
219 boolean registered = false;
220 long before = System.currentTimeMillis();
221 while (!registered) {
223 JanusGraphManagement mgmt = graph.openManagement();
224 JanusGraphIndex idx = mgmt.getGraphIndex(name);
226 for (PropertyKey k : idx.getFieldKeys()) {
227 SchemaStatus s = idx.getIndexStatus(k);
228 registered &= s.equals(SchemaStatus.REGISTERED);
232 logger.info("Index REGISTERED in " + (System.currentTimeMillis() - before) + " ms");
240 private void replaceProperty(DBProperty key) {
247 * @param index the index
249 public void updateIndex(DBIndex index) {
251 JanusGraphManagement mgmt = graph.openManagement();
252 List<PropertyKey> keys = new ArrayList<>();
253 boolean isNew = false;
254 boolean isChanged = false;
255 for (DBProperty prop : index.getProperties()) {
256 createProperty(mgmt, prop);
257 keys.add(mgmt.getPropertyKey(prop.getName()));
259 if (mgmt.containsGraphIndex(index.getName())) {
260 logger.info("index already exists");
267 this.createIndex(mgmt, index.getName(), keys, index.isUnique(), isNew, isChanged);