2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.db.schema;
24 import java.util.ArrayList;
25 import java.util.List;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.onap.aai.introspection.Version;
33 import com.thinkaurelius.titan.core.PropertyKey;
34 import com.thinkaurelius.titan.core.TitanGraph;
35 import com.thinkaurelius.titan.core.schema.SchemaStatus;
36 import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
37 import com.thinkaurelius.titan.core.schema.TitanManagement;
38 import com.thinkaurelius.titan.core.schema.TitanManagement.IndexBuilder;
40 public class ManageTitanSchema {
42 private static final EELFLogger logger = EELFManager.getInstance().getLogger(AuditOXM.class);
44 private TitanManagement graphMgmt;
45 private TitanGraph graph;
46 private List<DBProperty> aaiProperties;
47 private List<DBIndex> aaiIndexes;
48 private List<EdgeProperty> aaiEdgeProperties;
49 private Auditor oxmInfo = null;
50 private Auditor graphInfo = null;
53 * Instantiates a new manage titan schema.
55 * @param graph the graph
57 public ManageTitanSchema(final TitanGraph graph) {
59 oxmInfo = AuditorFactory.getOXMAuditor(Version.v8);
60 graphInfo = AuditorFactory.getGraphAuditor(graph);
67 public void buildSchema() {
69 this.graphMgmt = graph.openManagement();
70 aaiProperties = new ArrayList<>();
71 aaiEdgeProperties = new ArrayList<>();
72 aaiIndexes = new ArrayList<>();
73 aaiProperties.addAll(oxmInfo.getAuditDoc().getProperties());
74 aaiIndexes.addAll(oxmInfo.getAuditDoc().getIndexes());
75 aaiEdgeProperties.addAll(oxmInfo.getAuditDoc().getEdgeLabels());
80 } catch (Exception e) {
81 logger.error(e.getMessage(),e);
88 * Creates the property keys.
90 private void createPropertyKeys() {
93 for (DBProperty prop : aaiProperties) {
95 if (graphMgmt.containsPropertyKey(prop.getName())) {
96 PropertyKey key = graphMgmt.getPropertyKey(prop.getName());
97 boolean isChanged = false;
98 if (!prop.getCardinality().equals(key.cardinality())) {
101 if (!prop.getTypeClass().equals(key.dataType())) {
106 this.replaceProperty(prop);
109 //create a new property key
110 System.out.println("Key: " + prop.getName() + " not found - adding");
111 graphMgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()).make();
118 * Creates the indexes.
120 private void createIndexes() {
122 for (DBIndex index : aaiIndexes) {
123 Set<DBProperty> props = index.getProperties();
124 boolean isChanged = false;
125 boolean isNew = false;
126 List<PropertyKey> keyList = new ArrayList<>();
127 for (DBProperty prop : props) {
128 keyList.add(graphMgmt.getPropertyKey(prop.getName()));
130 if (graphMgmt.containsGraphIndex(index.getName())) {
131 TitanGraphIndex titanIndex = graphMgmt.getGraphIndex(index.getName());
132 PropertyKey[] dbKeys = titanIndex.getFieldKeys();
133 if (dbKeys.length != keyList.size()) {
137 for (PropertyKey key : keyList) {
138 if (!dbKeys[i].equals(key)) {
148 if (keyList.size() > 0) {
149 this.createIndex(graphMgmt, index.getName(), keyList, index.isUnique(), isNew, isChanged);
154 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is
155 // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One"
156 // We use the same edge-label for edges between many different types of nodes and our internal
157 // multiplicty definitions depends on which two types of nodes are being connected.
159 * Creates the edge labels.
161 private void createEdgeLabels() {
164 for (EdgeProperty prop : aaiEdgeProperties) {
166 if (graphMgmt.containsEdgeLabel(prop.getName())) {
169 graphMgmt.makeEdgeLabel(prop.getName()).multiplicity(prop.getMultiplicity()).make();
178 * Creates the property.
180 * @param mgmt the mgmt
181 * @param prop the prop
183 private void createProperty(TitanManagement mgmt, DBProperty prop) {
184 if (mgmt.containsPropertyKey(prop.getName())) {
185 PropertyKey key = mgmt.getPropertyKey(prop.getName());
186 boolean isChanged = false;
187 if (!prop.getCardinality().equals(key.cardinality())) {
190 if (!prop.getTypeClass().equals(key.dataType())) {
195 this.replaceProperty(prop);
198 //create a new property key
199 System.out.println("Key: " + prop.getName() + " not found - adding");
200 mgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()).make();
207 * @param mgmt the mgmt
208 * @param indexName the index name
209 * @param keys the keys
210 * @param isUnique the is unique
211 * @param isNew the is new
212 * @param isChanged the is changed
214 private void createIndex(TitanManagement mgmt, String indexName, List<PropertyKey> keys, boolean isUnique, boolean isNew, boolean isChanged) {
217 System.out.println("Changing index: " + indexName);
218 TitanGraphIndex oldIndex = mgmt.getGraphIndex(indexName);
219 mgmt.updateIndex(oldIndex, SchemaAction.DISABLE_INDEX);
221 //cannot remove indexes
222 //graphMgmt.updateIndex(oldIndex, SchemaAction.REMOVE_INDEX);
224 if (isNew || isChanged) {
227 IndexBuilder builder = mgmt.buildIndex(indexName,Vertex.class);
228 for (PropertyKey k : keys) {
234 builder.buildCompositeIndex();
235 System.out.println("Built index for " + indexName + " with keys: " + keys);
240 //mgmt = graph.asAdmin().getManagementSystem();
241 //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REGISTER_INDEX);
245 //waitForCompletion(indexName);
246 //TitanIndexRepair.hbaseRepair(AAIConstants.AAI_CONFIG_FILENAME, indexName, "");
247 } catch (Exception e) {
248 graph.tx().rollback();
250 logger.error(e.getMessage(),e);
253 //mgmt = graph.asAdmin().getManagementSystem();
254 //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REINDEX);
256 //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.ENABLE_INDEX);
264 * Wait for completion.
266 * @param name the name
267 * @throws InterruptedException the interrupted exception
269 private void waitForCompletion(String name) throws InterruptedException {
271 boolean registered = false;
272 long before = System.currentTimeMillis();
273 while (!registered) {
275 TitanManagement mgmt = graph.openManagement();
276 TitanGraphIndex idx = mgmt.getGraphIndex(name);
278 for (PropertyKey k : idx.getFieldKeys()) {
279 SchemaStatus s = idx.getIndexStatus(k);
280 registered &= s.equals(SchemaStatus.REGISTERED);
284 System.out.println("Index REGISTERED in " + (System.currentTimeMillis() - before) + " ms");
292 private void replaceProperty(DBProperty key) {
302 * @param index the index
304 public void updateIndex(DBIndex index) {
306 TitanManagement mgmt = graph.openManagement();
307 List<PropertyKey> keys = new ArrayList<>();
308 boolean isNew = false;
309 boolean isChanged = false;
310 for (DBProperty prop : index.getProperties()) {
311 createProperty(mgmt, prop);
312 keys.add(mgmt.getPropertyKey(prop.getName()));
314 if (mgmt.containsGraphIndex(index.getName())) {
315 System.out.println("index already exists");
322 this.createIndex(mgmt, index.getName(), keys, index.isUnique(), isNew, isChanged);