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 | SAXException | IOException e) {
110 LOGGER.error(e.getMessage());
113 result.remove("EdgePropNames");
119 * Creates the DB properties.
121 * @param temp the temp
123 private void createDBProperties(Introspector temp) {
124 Set<String> objectProperties = temp.getProperties();
126 for (String prop : objectProperties) {
127 if (!properties.containsKey(prop)) {
128 DBProperty dbProperty = new DBProperty();
129 dbProperty.setName(prop);
130 if (temp.isListType(prop)) {
131 dbProperty.setCardinality(Cardinality.SET);
132 if (temp.isSimpleGenericType(prop)) {
133 Class<?> clazz = null;
135 clazz = Class.forName(temp.getGenericType(prop));
136 } catch (ClassNotFoundException e) {
137 clazz = Object.class;
139 dbProperty.setTypeClass(clazz);
140 properties.put(prop, dbProperty);
143 dbProperty.setCardinality(Cardinality.SINGLE);
144 if (temp.isSimpleType(prop)) {
145 Class<?> clazz = null;
147 clazz = Class.forName(temp.getType(prop));
148 } catch (ClassNotFoundException e) {
149 clazz = Object.class;
151 dbProperty.setTypeClass(clazz);
152 properties.put(prop, dbProperty);
161 * Creates the DB indexes.
163 * @param temp the temp
165 private void createDBIndexes(Introspector temp) {
166 String uniqueProps = temp.getMetadata(ObjectMetadata.UNIQUE_PROPS);
167 String namespace = temp.getMetadata(ObjectMetadata.NAMESPACE);
168 if (uniqueProps == null) {
171 if (namespace == null) {
174 boolean isTopLevel = namespace != "";
175 List<String> unique = Arrays.asList(uniqueProps.split(","));
176 Set<String> indexed = temp.getIndexedProperties();
177 Set<String> keys = temp.getKeys();
179 for (String prop : indexed) {
180 DBIndex dbIndex = new DBIndex();
181 LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
182 if (!this.indexes.containsKey(prop)) {
183 dbIndex.setName(prop);
184 dbIndex.setUnique(unique.contains(prop));
185 properties.add(this.properties.get(prop));
186 dbIndex.setProperties(properties);
187 dbIndex.setStatus(SchemaStatus.ENABLED);
188 this.indexes.put(prop, dbIndex);
191 if (keys.size() > 1 || isTopLevel) {
192 DBIndex dbIndex = new DBIndex();
193 LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
194 dbIndex.setName("key-for-" + temp.getDbName());
195 if (!this.indexes.containsKey(dbIndex.getName())) {
196 boolean isUnique = false;
198 properties.add(this.properties.get(AAIProperties.NODE_TYPE));
200 for (String key : keys) {
201 properties.add(this.properties.get(key));
203 if (unique.contains(key) && !isUnique) {
207 dbIndex.setUnique(isUnique);
208 dbIndex.setProperties(properties);
209 dbIndex.setStatus(SchemaStatus.ENABLED);
210 this.indexes.put(dbIndex.getName(), dbIndex);
217 * Creates the edge labels.
219 private void createEdgeLabels() {
220 Multimap<String, EdgeRule> edgeRules = EdgeRules.getInstance().getAllRules();
221 for (String key : edgeRules.keySet()) {
222 Collection<EdgeRule> collection = edgeRules.get(key);
223 EdgeProperty prop = new EdgeProperty();
224 //there is only ever one, they used the wrong type for EdgeRules
226 for (EdgeRule item : collection) {
227 label = item.getLabel();
230 prop.setMultiplicity(Multiplicity.MULTI);
231 this.edgeLabels.put(label, prop);
236 * Gets the all introspectors.
238 * @return the all introspectors
240 public Set<Introspector> getAllIntrospectors() {
241 return this.allObjects;