Increase code coverage for aai-common repo to 30%
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / db / schema / AuditOXM.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.io.IOException;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.LinkedHashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import javax.xml.XMLConstants;
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import javax.xml.parsers.ParserConfigurationException;
35
36 import org.openecomp.aai.serialization.db.EdgeRule;
37 import org.openecomp.aai.serialization.db.EdgeRules;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.NodeList;
40 import org.xml.sax.SAXException;
41
42 import org.openecomp.aai.db.props.AAIProperties;
43 import org.openecomp.aai.introspection.Introspector;
44 import org.openecomp.aai.introspection.Loader;
45 import org.openecomp.aai.introspection.LoaderFactory;
46 import org.openecomp.aai.introspection.ModelType;
47 import org.openecomp.aai.introspection.Version;
48 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
49 import org.openecomp.aai.schema.enums.ObjectMetadata;
50 import org.openecomp.aai.util.AAIConstants;
51 import com.att.eelf.configuration.EELFLogger;
52 import com.att.eelf.configuration.EELFManager;
53 import com.google.common.collect.Multimap;
54 import com.thinkaurelius.titan.core.Cardinality;
55 import com.thinkaurelius.titan.core.Multiplicity;
56 import com.thinkaurelius.titan.core.schema.SchemaStatus;
57
58 public class AuditOXM extends Auditor {
59
60         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AuditOXM.class);
61
62         private Set<Introspector> allObjects;
63         
64         /**
65          * Instantiates a new audit OXM.
66          *
67          * @param version the version
68          */
69         public AuditOXM(Version version) {
70                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
71                 Set<String> objectNames = getAllObjects(version);
72                 allObjects = new HashSet<>();
73                 for (String key : objectNames) {
74                         try {
75                                 final Introspector temp = loader.introspectorFromName(key);
76                                 allObjects.add(temp);
77                                 this.createDBProperties(temp);
78                         } catch (AAIUnknownObjectException e) {
79                                 LOGGER.warn("Skipping audit for object " + key + " (Unknown Object)", e);
80                         }
81                 }
82                 for (Introspector temp : allObjects) {
83                         this.createDBIndexes(temp);
84                 }
85                 createEdgeLabels();
86                 
87         }
88
89         /**
90          * Gets the all objects.
91          *
92          * @param version the version
93          * @return the all objects
94          */
95         private Set<String> getAllObjects(Version version) {
96                 String fileName = AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + version.toString() + ".xml";
97                 Set<String> result = new HashSet<>();
98                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
99                 try {
100                         docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
101                         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
102                         Document doc = docBuilder.parse(fileName);
103                         NodeList list = doc.getElementsByTagName("java-type");
104                         for (int i = 0; i < list.getLength(); i++) {
105                                 result.add(list.item(i).getAttributes().getNamedItem("name").getNodeValue());
106                         }
107                 } catch (ParserConfigurationException e) {
108                         // TODO Auto-generated catch block
109                         e.printStackTrace();
110                 } catch (SAXException e) {
111                         // TODO Auto-generated catch block
112                         e.printStackTrace();
113                 } catch (IOException e) {
114                         // TODO Auto-generated catch block
115                         e.printStackTrace();
116                 }
117
118                 result.remove("EdgePropNames");
119                 return result;
120                 
121         }
122         
123         /**
124          * Creates the DB properties.
125          *
126          * @param temp the temp
127          */
128         private void createDBProperties(Introspector temp) {
129                 Set<String> objectProperties = temp.getProperties();
130                 
131                 for (String prop : objectProperties) {
132                         if (!properties.containsKey(prop)) {
133                                 DBProperty dbProperty = new DBProperty();
134                                 dbProperty.setName(prop);
135                                 if (temp.isListType(prop)) {
136                                         dbProperty.setCardinality(Cardinality.SET);
137                                         if (temp.isSimpleGenericType(prop)) {
138                                                 Class<?> clazz = null;
139                                                 try {
140                                                         clazz = Class.forName(temp.getGenericType(prop));
141                                                 } catch (ClassNotFoundException e) {
142                                                         clazz = Object.class;
143                                                 }
144                                                 dbProperty.setTypeClass(clazz);
145                                                 properties.put(prop, dbProperty);
146                                         }
147                                 } else {
148                                         dbProperty.setCardinality(Cardinality.SINGLE);
149                                         if (temp.isSimpleType(prop)) {
150                                                 Class<?> clazz = null;
151                                                 try {
152                                                         clazz = Class.forName(temp.getType(prop));
153                                                 } catch (ClassNotFoundException e) {
154                                                         clazz = Object.class;
155                                                 }
156                                                 dbProperty.setTypeClass(clazz);
157                                                 properties.put(prop, dbProperty);
158                                         }
159                                 }
160                         }
161                 }
162                 
163         }
164         
165         /**
166          * Creates the DB indexes.
167          *
168          * @param temp the temp
169          */
170         private void createDBIndexes(Introspector temp) {
171                 String uniqueProps = temp.getMetadata(ObjectMetadata.UNIQUE_PROPS);
172                 String namespace = temp.getMetadata(ObjectMetadata.NAMESPACE);
173                 if (uniqueProps == null) {
174                         uniqueProps = "";
175                 }
176                 if (namespace == null) {
177                         namespace = "";
178                 }
179                 boolean isTopLevel = namespace != "";
180                 List<String> unique = Arrays.asList(uniqueProps.split(","));
181                 Set<String> indexed = temp.getIndexedProperties();
182                 Set<String> keys = temp.getKeys();
183                 
184                 for (String prop : indexed) {
185                         DBIndex dbIndex = new DBIndex();
186                         LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
187                         if (!this.indexes.containsKey(prop)) {
188                                 dbIndex.setName(prop);
189                                 dbIndex.setUnique(unique.contains(prop));
190                                 properties.add(this.properties.get(prop));
191                                 dbIndex.setProperties(properties);
192                                 dbIndex.setStatus(SchemaStatus.ENABLED);
193                                 this.indexes.put(prop, dbIndex);
194                         }
195                 }
196                 if (keys.size() > 1 || isTopLevel) {
197                         DBIndex dbIndex = new DBIndex();
198                         LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
199                         dbIndex.setName("key-for-" + temp.getDbName());
200                         if (!this.indexes.containsKey(dbIndex.getName())) {
201                                 boolean isUnique = false;
202                                 if (isTopLevel) {
203                                         properties.add(this.properties.get(AAIProperties.NODE_TYPE));
204                                 }
205                                 for (String key : keys) {
206                                         properties.add(this.properties.get(key));
207         
208                                         if (unique.contains(key) && !isUnique) {
209                                                 isUnique = true;
210                                         }
211                                 }
212                                 dbIndex.setUnique(isUnique);
213                                 dbIndex.setProperties(properties);
214                                 dbIndex.setStatus(SchemaStatus.ENABLED);
215                                 this.indexes.put(dbIndex.getName(), dbIndex);
216                         }
217                 }
218
219         }
220         
221         /**
222          * Creates the edge labels.
223          */
224         private void createEdgeLabels() {
225                 Multimap<String, EdgeRule> edgeRules = EdgeRules.getInstance().getAllRules();
226                 for (String key : edgeRules.keySet()) {
227                         Collection<EdgeRule> collection = edgeRules.get(key);
228                         EdgeProperty prop = new EdgeProperty();
229                         //there is only ever one, they used the wrong type for EdgeRules
230                         String label = "";
231                         for (EdgeRule item : collection) {
232                                 label = item.getLabel();
233                         }
234                         prop.setName(label);
235                         prop.setMultiplicity(Multiplicity.MULTI);
236                         this.edgeLabels.put(label, prop);
237                 }
238         }
239         
240         /**
241          * Gets the all introspectors.
242          *
243          * @return the all introspectors
244          */
245         public Set<Introspector> getAllIntrospectors() {
246                 return this.allObjects;
247         }
248         
249 }