ec1c2394860e0644a70b62daadb5dc597e41fd5e
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / db / schema / AuditTitan.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.db.schema;
23
24
25 import java.util.Iterator;
26 import java.util.LinkedHashSet;
27
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29
30 import com.thinkaurelius.titan.core.EdgeLabel;
31 import com.thinkaurelius.titan.core.PropertyKey;
32 import com.thinkaurelius.titan.core.TitanGraph;
33 import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
34 import com.thinkaurelius.titan.core.schema.TitanManagement;
35
36 public class AuditTitan extends Auditor {
37
38         private final TitanGraph graph;
39         
40         /**
41          * Instantiates a new audit titan.
42          *
43          * @param g the g
44          */
45         public AuditTitan (TitanGraph g) {
46                 this.graph = g;
47                 buildSchema();
48         }
49         
50         /**
51          * Builds the schema.
52          */
53         private void buildSchema() {
54                 populateProperties();
55                 populateIndexes();
56                 populateEdgeLabels();
57         }
58         
59         /**
60          * Populate properties.
61          */
62         private void populateProperties() {
63                 TitanManagement mgmt = graph.openManagement();
64                 Iterable<PropertyKey> iterable = mgmt.getRelationTypes(PropertyKey.class);
65                 Iterator<PropertyKey> titanProperties = iterable.iterator();
66                 PropertyKey propKey;
67                 while (titanProperties.hasNext()) {
68                         propKey = titanProperties.next();
69                         DBProperty prop = new DBProperty();
70                         
71                         prop.setName(propKey.name());
72                         prop.setCardinality(propKey.cardinality());
73                         prop.setTypeClass(propKey.dataType());
74                         
75                         this.properties.put(prop.getName(), prop);
76                 }       
77         }
78         
79         /**
80          * Populate indexes.
81          */
82         private void populateIndexes() {
83                 TitanManagement mgmt = graph.openManagement();
84                 Iterable<TitanGraphIndex> iterable = mgmt.getGraphIndexes(Vertex.class);
85                 Iterator<TitanGraphIndex> titanIndexes = iterable.iterator();
86                 TitanGraphIndex titanIndex;
87                 while (titanIndexes.hasNext()) {
88                         titanIndex = titanIndexes.next();
89                         if (titanIndex.isCompositeIndex()) {
90                                 DBIndex index = new DBIndex();
91                                 LinkedHashSet<DBProperty> dbProperties = new LinkedHashSet<>();
92                                 index.setName(titanIndex.name());
93                                 index.setUnique(titanIndex.isUnique());
94                                 PropertyKey[] keys = titanIndex.getFieldKeys();
95                                 for (PropertyKey key : keys) {
96                                         dbProperties.add(this.properties.get(key.name()));
97                                 }
98                                 index.setProperties(dbProperties);
99                                 index.setStatus(titanIndex.getIndexStatus(keys[0]));
100                                 this.indexes.put(index.getName(), index);
101                         }
102                 }       
103         }
104         
105         /**
106          * Populate edge labels.
107          */
108         private void populateEdgeLabels() {
109                 TitanManagement mgmt = graph.openManagement();
110                 Iterable<EdgeLabel> iterable = mgmt.getRelationTypes(EdgeLabel.class);
111                 Iterator<EdgeLabel> titanEdgeLabels = iterable.iterator();
112                 EdgeLabel edgeLabel;
113                 while (titanEdgeLabels.hasNext()) {
114                         edgeLabel = titanEdgeLabels.next();
115                         EdgeProperty edgeProperty = new EdgeProperty();
116                         
117                         edgeProperty.setName(edgeLabel.name());
118                         edgeProperty.setMultiplicity(edgeLabel.multiplicity());
119                         
120                         this.edgeLabels.put(edgeProperty.getName(), edgeProperty);
121                 }       
122         }
123         
124 }