0a3650fa10ae9665fa5b6b89f6274ab39690afaf
[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 e) {
110                         // TODO Auto-generated catch block
111                         e.printStackTrace();
112                 } catch (SAXException e) {
113                         // TODO Auto-generated catch block
114                         e.printStackTrace();
115                 } catch (IOException e) {
116                         // TODO Auto-generated catch block
117                         e.printStackTrace();
118                 }
119
120                 result.remove("EdgePropNames");
121                 return result;
122                 
123         }
124         
125         /**
126          * Creates the DB properties.
127          *
128          * @param temp the temp
129          */
130         private void createDBProperties(Introspector temp) {
131                 Set<String> objectProperties = temp.getProperties();
132                 
133                 for (String prop : objectProperties) {
134                         if (!properties.containsKey(prop)) {
135                                 DBProperty dbProperty = new DBProperty();
136                                 dbProperty.setName(prop);
137                                 if (temp.isListType(prop)) {
138                                         dbProperty.setCardinality(Cardinality.SET);
139                                         if (temp.isSimpleGenericType(prop)) {
140                                                 Class<?> clazz = null;
141                                                 try {
142                                                         clazz = Class.forName(temp.getGenericType(prop));
143                                                 } catch (ClassNotFoundException e) {
144                                                         clazz = Object.class;
145                                                 }
146                                                 dbProperty.setTypeClass(clazz);
147                                                 properties.put(prop, dbProperty);
148                                         }
149                                 } else {
150                                         dbProperty.setCardinality(Cardinality.SINGLE);
151                                         if (temp.isSimpleType(prop)) {
152                                                 Class<?> clazz = null;
153                                                 try {
154                                                         clazz = Class.forName(temp.getType(prop));
155                                                 } catch (ClassNotFoundException e) {
156                                                         clazz = Object.class;
157                                                 }
158                                                 dbProperty.setTypeClass(clazz);
159                                                 properties.put(prop, dbProperty);
160                                         }
161                                 }
162                         }
163                 }
164                 
165         }
166         
167         /**
168          * Creates the DB indexes.
169          *
170          * @param temp the temp
171          */
172         private void createDBIndexes(Introspector temp) {
173                 String uniqueProps = temp.getMetadata(ObjectMetadata.UNIQUE_PROPS);
174                 String namespace = temp.getMetadata(ObjectMetadata.NAMESPACE);
175                 if (uniqueProps == null) {
176                         uniqueProps = "";
177                 }
178                 if (namespace == null) {
179                         namespace = "";
180                 }
181                 boolean isTopLevel = namespace != "";
182                 List<String> unique = Arrays.asList(uniqueProps.split(","));
183                 Set<String> indexed = temp.getIndexedProperties();
184                 Set<String> keys = temp.getKeys();
185                 
186                 for (String prop : indexed) {
187                         DBIndex dbIndex = new DBIndex();
188                         LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
189                         if (!this.indexes.containsKey(prop)) {
190                                 dbIndex.setName(prop);
191                                 dbIndex.setUnique(unique.contains(prop));
192                                 properties.add(this.properties.get(prop));
193                                 dbIndex.setProperties(properties);
194                                 dbIndex.setStatus(SchemaStatus.ENABLED);
195                                 this.indexes.put(prop, dbIndex);
196                         }
197                 }
198                 if (keys.size() > 1 || isTopLevel) {
199                         DBIndex dbIndex = new DBIndex();
200                         LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
201                         dbIndex.setName("key-for-" + temp.getDbName());
202                         if (!this.indexes.containsKey(dbIndex.getName())) {
203                                 boolean isUnique = false;
204                                 if (isTopLevel) {
205                                         properties.add(this.properties.get(AAIProperties.NODE_TYPE));
206                                 }
207                                 for (String key : keys) {
208                                         properties.add(this.properties.get(key));
209         
210                                         if (unique.contains(key) && !isUnique) {
211                                                 isUnique = true;
212                                         }
213                                 }
214                                 dbIndex.setUnique(isUnique);
215                                 dbIndex.setProperties(properties);
216                                 dbIndex.setStatus(SchemaStatus.ENABLED);
217                                 this.indexes.put(dbIndex.getName(), dbIndex);
218                         }
219                 }
220
221         }
222         
223         /**
224          * Creates the edge labels.
225          */
226         private void createEdgeLabels() {
227                 Multimap<String, EdgeRule> edgeRules = EdgeRules.getInstance().getAllRules();
228                 for (String key : edgeRules.keySet()) {
229                         Collection<EdgeRule> collection = edgeRules.get(key);
230                         EdgeProperty prop = new EdgeProperty();
231                         //there is only ever one, they used the wrong type for EdgeRules
232                         String label = "";
233                         for (EdgeRule item : collection) {
234                                 label = item.getLabel();
235                         }
236                         prop.setName(label);
237                         prop.setMultiplicity(Multiplicity.MULTI);
238                         this.edgeLabels.put(label, prop);
239                 }
240         }
241         
242         /**
243          * Gets the all introspectors.
244          *
245          * @return the all introspectors
246          */
247         public Set<Introspector> getAllIntrospectors() {
248                 return this.allObjects;
249         }
250         
251 }