2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.db.schema;
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;
32 import javax.xml.XMLConstants;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
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;
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;
60 public class AuditOXM extends Auditor {
62 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AuditOXM.class);
64 private Set<Introspector> allObjects;
67 * Instantiates a new audit OXM.
69 * @param version the version
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) {
77 final Introspector temp = loader.introspectorFromName(key);
79 this.createDBProperties(temp);
80 } catch (AAIUnknownObjectException e) {
81 LOGGER.warn("Skipping audit for object " + key + " (Unknown Object) " + LogFormatTools.getStackTop(e));
84 for (Introspector temp : allObjects) {
85 this.createDBIndexes(temp);
92 * Gets the all objects.
94 * @param version the version
95 * @return the all objects
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();
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());
109 } catch (ParserConfigurationException e) {
110 // TODO Auto-generated catch block
112 } catch (SAXException e) {
113 // TODO Auto-generated catch block
115 } catch (IOException e) {
116 // TODO Auto-generated catch block
120 result.remove("EdgePropNames");
126 * Creates the DB properties.
128 * @param temp the temp
130 private void createDBProperties(Introspector temp) {
131 Set<String> objectProperties = temp.getProperties();
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;
142 clazz = Class.forName(temp.getGenericType(prop));
143 } catch (ClassNotFoundException e) {
144 clazz = Object.class;
146 dbProperty.setTypeClass(clazz);
147 properties.put(prop, dbProperty);
150 dbProperty.setCardinality(Cardinality.SINGLE);
151 if (temp.isSimpleType(prop)) {
152 Class<?> clazz = null;
154 clazz = Class.forName(temp.getType(prop));
155 } catch (ClassNotFoundException e) {
156 clazz = Object.class;
158 dbProperty.setTypeClass(clazz);
159 properties.put(prop, dbProperty);
168 * Creates the DB indexes.
170 * @param temp the temp
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) {
178 if (namespace == null) {
181 boolean isTopLevel = namespace != "";
182 List<String> unique = Arrays.asList(uniqueProps.split(","));
183 Set<String> indexed = temp.getIndexedProperties();
184 Set<String> keys = temp.getKeys();
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);
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;
205 properties.add(this.properties.get(AAIProperties.NODE_TYPE));
207 for (String key : keys) {
208 properties.add(this.properties.get(key));
210 if (unique.contains(key) && !isUnique) {
214 dbIndex.setUnique(isUnique);
215 dbIndex.setProperties(properties);
216 dbIndex.setStatus(SchemaStatus.ENABLED);
217 this.indexes.put(dbIndex.getName(), dbIndex);
224 * Creates the edge labels.
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
233 for (EdgeRule item : collection) {
234 label = item.getLabel();
237 prop.setMultiplicity(Multiplicity.MULTI);
238 this.edgeLabels.put(label, prop);
243 * Gets the all introspectors.
245 * @return the all introspectors
247 public Set<Introspector> getAllIntrospectors() {
248 return this.allObjects;