2 * ============LICENSE_START=======================================================
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
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 package org.onap.aai.db.schema;
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;
30 import javax.xml.XMLConstants;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
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;
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;
58 public class AuditOXM extends Auditor {
60 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AuditOXM.class);
62 private Set<Introspector> allObjects;
65 * Instantiates a new audit OXM.
67 * @param version the version
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) {
75 final Introspector temp = loader.introspectorFromName(key);
77 this.createDBProperties(temp);
78 } catch (AAIUnknownObjectException e) {
79 LOGGER.warn("Skipping audit for object " + key + " (Unknown Object) " + LogFormatTools.getStackTop(e));
82 for (Introspector temp : allObjects) {
83 this.createDBIndexes(temp);
90 * Gets the all objects.
92 * @param version the version
93 * @return the all objects
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();
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());
107 } catch (ParserConfigurationException | SAXException | IOException e) {
108 LOGGER.error(e.getMessage());
111 result.remove("EdgePropNames");
117 * Creates the DB properties.
119 * @param temp the temp
121 private void createDBProperties(Introspector temp) {
122 Set<String> objectProperties = temp.getProperties();
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;
133 clazz = Class.forName(temp.getGenericType(prop));
134 } catch (ClassNotFoundException e) {
135 clazz = Object.class;
137 dbProperty.setTypeClass(clazz);
138 properties.put(prop, dbProperty);
141 dbProperty.setCardinality(Cardinality.SINGLE);
142 if (temp.isSimpleType(prop)) {
143 Class<?> clazz = null;
145 clazz = Class.forName(temp.getType(prop));
146 } catch (ClassNotFoundException e) {
147 clazz = Object.class;
149 dbProperty.setTypeClass(clazz);
150 properties.put(prop, dbProperty);
159 * Creates the DB indexes.
161 * @param temp the temp
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) {
169 if (namespace == null) {
172 boolean isTopLevel = namespace != "";
173 List<String> unique = Arrays.asList(uniqueProps.split(","));
174 Set<String> indexed = temp.getIndexedProperties();
175 Set<String> keys = temp.getKeys();
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);
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;
196 properties.add(this.properties.get(AAIProperties.NODE_TYPE));
198 for (String key : keys) {
199 properties.add(this.properties.get(key));
201 if (unique.contains(key) && !isUnique) {
205 dbIndex.setUnique(isUnique);
206 dbIndex.setProperties(properties);
207 dbIndex.setStatus(SchemaStatus.ENABLED);
208 this.indexes.put(dbIndex.getName(), dbIndex);
215 * Creates the edge labels.
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
224 for (EdgeRule item : collection) {
225 label = item.getLabel();
228 prop.setMultiplicity(Multiplicity.MULTI);
229 this.edgeLabels.put(label, prop);
234 * Gets the all introspectors.
236 * @return the all introspectors
238 public Set<Introspector> getAllIntrospectors() {
239 return this.allObjects;