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