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 com.thinkaurelius.titan.core.PropertyKey;
32 import com.thinkaurelius.titan.core.TitanGraph;
33 import com.thinkaurelius.titan.core.schema.SchemaStatus;
34 import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
35 import com.thinkaurelius.titan.core.schema.TitanManagement;
36 import com.thinkaurelius.titan.core.schema.TitanManagement.IndexBuilder;
38 public class ManageTitanSchema {
40 private static final EELFLogger logger = EELFManager.getInstance().getLogger(AuditOXM.class);
42 private TitanManagement graphMgmt;
43 private TitanGraph 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 titan schema.
53 * @param graph the graph
55 public ManageTitanSchema(final TitanGraph 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() {
91 for (DBProperty prop : aaiProperties) {
93 if (graphMgmt.containsPropertyKey(prop.getName())) {
94 PropertyKey key = graphMgmt.getPropertyKey(prop.getName());
95 boolean isChanged = false;
96 if (!prop.getCardinality().equals(key.cardinality())) {
99 if (!prop.getTypeClass().equals(key.dataType())) {
104 this.replaceProperty(prop);
107 //create a new property key
108 System.out.println("Key: " + prop.getName() + " not found - adding");
109 graphMgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()).make();
116 * Creates the indexes.
118 private void createIndexes() {
120 for (DBIndex index : aaiIndexes) {
121 Set<DBProperty> props = index.getProperties();
122 boolean isChanged = false;
123 boolean isNew = false;
124 List<PropertyKey> keyList = new ArrayList<>();
125 for (DBProperty prop : props) {
126 keyList.add(graphMgmt.getPropertyKey(prop.getName()));
128 if (graphMgmt.containsGraphIndex(index.getName())) {
129 TitanGraphIndex titanIndex = graphMgmt.getGraphIndex(index.getName());
130 PropertyKey[] dbKeys = titanIndex.getFieldKeys();
131 if (dbKeys.length != keyList.size()) {
135 for (PropertyKey key : keyList) {
136 if (!dbKeys[i].equals(key)) {
146 if (keyList.size() > 0) {
147 this.createIndex(graphMgmt, index.getName(), keyList, index.isUnique(), isNew, isChanged);
152 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is
153 // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One"
154 // We use the same edge-label for edges between many different types of nodes and our internal
155 // multiplicty definitions depends on which two types of nodes are being connected.
157 * Creates the edge labels.
159 private void createEdgeLabels() {
162 for (EdgeProperty prop : aaiEdgeProperties) {
164 if (graphMgmt.containsEdgeLabel(prop.getName())) {
167 graphMgmt.makeEdgeLabel(prop.getName()).multiplicity(prop.getMultiplicity()).make();
176 * Creates the property.
178 * @param mgmt the mgmt
179 * @param prop the prop
181 private void createProperty(TitanManagement mgmt, DBProperty prop) {
182 if (mgmt.containsPropertyKey(prop.getName())) {
183 PropertyKey key = mgmt.getPropertyKey(prop.getName());
184 boolean isChanged = false;
185 if (!prop.getCardinality().equals(key.cardinality())) {
188 if (!prop.getTypeClass().equals(key.dataType())) {
193 this.replaceProperty(prop);
196 //create a new property key
197 System.out.println("Key: " + prop.getName() + " not found - adding");
198 mgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()).make();
205 * @param mgmt the mgmt
206 * @param indexName the index name
207 * @param keys the keys
208 * @param isUnique the is unique
209 * @param isNew the is new
210 * @param isChanged the is changed
212 private void createIndex(TitanManagement mgmt, String indexName, List<PropertyKey> keys, boolean isUnique, boolean isNew, boolean isChanged) {
215 System.out.println("Changing index: " + indexName);
216 TitanGraphIndex oldIndex = mgmt.getGraphIndex(indexName);
217 mgmt.updateIndex(oldIndex, SchemaAction.DISABLE_INDEX);
219 //cannot remove indexes
220 //graphMgmt.updateIndex(oldIndex, SchemaAction.REMOVE_INDEX);
222 if (isNew || isChanged) {
225 IndexBuilder builder = mgmt.buildIndex(indexName,Vertex.class);
226 for (PropertyKey k : keys) {
232 builder.buildCompositeIndex();
233 System.out.println("Built index for " + indexName + " with keys: " + keys);
238 //mgmt = graph.asAdmin().getManagementSystem();
239 //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REGISTER_INDEX);
243 //waitForCompletion(indexName);
244 //TitanIndexRepair.hbaseRepair(AAIConstants.AAI_CONFIG_FILENAME, indexName, "");
245 } catch (Exception e) {
246 graph.tx().rollback();
248 logger.error(e.getMessage(),e);
251 //mgmt = graph.asAdmin().getManagementSystem();
252 //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REINDEX);
254 //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.ENABLE_INDEX);
262 * Wait for completion.
264 * @param name the name
265 * @throws InterruptedException the interrupted exception
267 private void waitForCompletion(String name) throws InterruptedException {
269 boolean registered = false;
270 long before = System.currentTimeMillis();
271 while (!registered) {
273 TitanManagement mgmt = graph.openManagement();
274 TitanGraphIndex idx = mgmt.getGraphIndex(name);
276 for (PropertyKey k : idx.getFieldKeys()) {
277 SchemaStatus s = idx.getIndexStatus(k);
278 registered &= s.equals(SchemaStatus.REGISTERED);
282 System.out.println("Index REGISTERED in " + (System.currentTimeMillis() - before) + " ms");
290 private void replaceProperty(DBProperty key) {
300 * @param index the index
302 public void updateIndex(DBIndex index) {
304 TitanManagement mgmt = graph.openManagement();
305 List<PropertyKey> keys = new ArrayList<>();
306 boolean isNew = false;
307 boolean isChanged = false;
308 for (DBProperty prop : index.getProperties()) {
309 createProperty(mgmt, prop);
310 keys.add(mgmt.getPropertyKey(prop.getName()));
312 if (mgmt.containsGraphIndex(index.getName())) {
313 System.out.println("index already exists");
320 this.createIndex(mgmt, index.getName(), keys, index.isUnique(), isNew, isChanged);