Update the license for 2017-2018 license
[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-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 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 import javax.xml.XMLConstants;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34
35 import org.onap.aai.serialization.db.EdgeRule;
36 import org.onap.aai.serialization.db.EdgeRules;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.NodeList;
39 import org.xml.sax.SAXException;
40
41 import org.onap.aai.db.props.AAIProperties;
42 import org.onap.aai.introspection.Introspector;
43 import org.onap.aai.introspection.Loader;
44 import org.onap.aai.introspection.LoaderFactory;
45 import org.onap.aai.introspection.ModelType;
46 import org.onap.aai.introspection.Version;
47 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
48 import org.onap.aai.logging.LogFormatTools;
49 import org.onap.aai.schema.enums.ObjectMetadata;
50 import org.onap.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) " + LogFormatTools.getStackTop(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 | SAXException | IOException e) {
108                         LOGGER.error(e.getMessage());
109                 }
110
111                 result.remove("EdgePropNames");
112                 return result;
113                 
114         }
115         
116         /**
117          * Creates the DB properties.
118          *
119          * @param temp the temp
120          */
121         private void createDBProperties(Introspector temp) {
122                 Set<String> objectProperties = temp.getProperties();
123                 
124                 for (String prop : objectProperties) {
125                         if (!properties.containsKey(prop)) {
126                                 DBProperty dbProperty = new DBProperty();
127                                 dbProperty.setName(prop);
128                                 if (temp.isListType(prop)) {
129                                         dbProperty.setCardinality(Cardinality.SET);
130                                         if (temp.isSimpleGenericType(prop)) {
131                                                 Class<?> clazz = null;
132                                                 try {
133                                                         clazz = Class.forName(temp.getGenericType(prop));
134                                                 } catch (ClassNotFoundException e) {
135                                                         clazz = Object.class;
136                                                 }
137                                                 dbProperty.setTypeClass(clazz);
138                                                 properties.put(prop, dbProperty);
139                                         }
140                                 } else {
141                                         dbProperty.setCardinality(Cardinality.SINGLE);
142                                         if (temp.isSimpleType(prop)) {
143                                                 Class<?> clazz = null;
144                                                 try {
145                                                         clazz = Class.forName(temp.getType(prop));
146                                                 } catch (ClassNotFoundException e) {
147                                                         clazz = Object.class;
148                                                 }
149                                                 dbProperty.setTypeClass(clazz);
150                                                 properties.put(prop, dbProperty);
151                                         }
152                                 }
153                         }
154                 }
155                 
156         }
157         
158         /**
159          * Creates the DB indexes.
160          *
161          * @param temp the temp
162          */
163         private void createDBIndexes(Introspector temp) {
164                 String uniqueProps = temp.getMetadata(ObjectMetadata.UNIQUE_PROPS);
165                 String namespace = temp.getMetadata(ObjectMetadata.NAMESPACE);
166                 if (uniqueProps == null) {
167                         uniqueProps = "";
168                 }
169                 if (namespace == null) {
170                         namespace = "";
171                 }
172                 boolean isTopLevel = namespace != "";
173                 List<String> unique = Arrays.asList(uniqueProps.split(","));
174                 Set<String> indexed = temp.getIndexedProperties();
175                 Set<String> keys = temp.getKeys();
176                 
177                 for (String prop : indexed) {
178                         DBIndex dbIndex = new DBIndex();
179                         LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
180                         if (!this.indexes.containsKey(prop)) {
181                                 dbIndex.setName(prop);
182                                 dbIndex.setUnique(unique.contains(prop));
183                                 properties.add(this.properties.get(prop));
184                                 dbIndex.setProperties(properties);
185                                 dbIndex.setStatus(SchemaStatus.ENABLED);
186                                 this.indexes.put(prop, dbIndex);
187                         }
188                 }
189                 if (keys.size() > 1 || isTopLevel) {
190                         DBIndex dbIndex = new DBIndex();
191                         LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
192                         dbIndex.setName("key-for-" + temp.getDbName());
193                         if (!this.indexes.containsKey(dbIndex.getName())) {
194                                 boolean isUnique = false;
195                                 if (isTopLevel) {
196                                         properties.add(this.properties.get(AAIProperties.NODE_TYPE));
197                                 }
198                                 for (String key : keys) {
199                                         properties.add(this.properties.get(key));
200         
201                                         if (unique.contains(key) && !isUnique) {
202                                                 isUnique = true;
203                                         }
204                                 }
205                                 dbIndex.setUnique(isUnique);
206                                 dbIndex.setProperties(properties);
207                                 dbIndex.setStatus(SchemaStatus.ENABLED);
208                                 this.indexes.put(dbIndex.getName(), dbIndex);
209                         }
210                 }
211
212         }
213         
214         /**
215          * Creates the edge labels.
216          */
217         private void createEdgeLabels() {
218                 Multimap<String, EdgeRule> edgeRules = EdgeRules.getInstance().getAllRules();
219                 for (String key : edgeRules.keySet()) {
220                         Collection<EdgeRule> collection = edgeRules.get(key);
221                         EdgeProperty prop = new EdgeProperty();
222                         //there is only ever one, they used the wrong type for EdgeRules
223                         String label = "";
224                         for (EdgeRule item : collection) {
225                                 label = item.getLabel();
226                         }
227                         prop.setName(label);
228                         prop.setMultiplicity(Multiplicity.MULTI);
229                         this.edgeLabels.put(label, prop);
230                 }
231         }
232         
233         /**
234          * Gets the all introspectors.
235          *
236          * @return the all introspectors
237          */
238         public Set<Introspector> getAllIntrospectors() {
239                 return this.allObjects;
240         }
241         
242 }