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