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