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