2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.aai.db.schema;
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;
31 import javax.xml.XMLConstants;
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import javax.xml.parsers.ParserConfigurationException;
36 import org.openecomp.aai.serialization.db.EdgeRule;
37 import org.openecomp.aai.serialization.db.EdgeRules;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.NodeList;
40 import org.xml.sax.SAXException;
42 import org.openecomp.aai.db.props.AAIProperties;
43 import org.openecomp.aai.introspection.Introspector;
44 import org.openecomp.aai.introspection.Loader;
45 import org.openecomp.aai.introspection.LoaderFactory;
46 import org.openecomp.aai.introspection.ModelType;
47 import org.openecomp.aai.introspection.Version;
48 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
49 import org.openecomp.aai.schema.enums.ObjectMetadata;
50 import org.openecomp.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)", 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 e) {
108 // TODO Auto-generated catch block
110 } catch (SAXException e) {
111 // TODO Auto-generated catch block
113 } catch (IOException e) {
114 // TODO Auto-generated catch block
118 result.remove("EdgePropNames");
124 * Creates the DB properties.
126 * @param temp the temp
128 private void createDBProperties(Introspector temp) {
129 Set<String> objectProperties = temp.getProperties();
131 for (String prop : objectProperties) {
132 if (!properties.containsKey(prop)) {
133 DBProperty dbProperty = new DBProperty();
134 dbProperty.setName(prop);
135 if (temp.isListType(prop)) {
136 dbProperty.setCardinality(Cardinality.SET);
137 if (temp.isSimpleGenericType(prop)) {
138 Class<?> clazz = null;
140 clazz = Class.forName(temp.getGenericType(prop));
141 } catch (ClassNotFoundException e) {
142 clazz = Object.class;
144 dbProperty.setTypeClass(clazz);
145 properties.put(prop, dbProperty);
148 dbProperty.setCardinality(Cardinality.SINGLE);
149 if (temp.isSimpleType(prop)) {
150 Class<?> clazz = null;
152 clazz = Class.forName(temp.getType(prop));
153 } catch (ClassNotFoundException e) {
154 clazz = Object.class;
156 dbProperty.setTypeClass(clazz);
157 properties.put(prop, dbProperty);
166 * Creates the DB indexes.
168 * @param temp the temp
170 private void createDBIndexes(Introspector temp) {
171 String uniqueProps = temp.getMetadata(ObjectMetadata.UNIQUE_PROPS);
172 String namespace = temp.getMetadata(ObjectMetadata.NAMESPACE);
173 if (uniqueProps == null) {
176 if (namespace == null) {
179 boolean isTopLevel = namespace != "";
180 List<String> unique = Arrays.asList(uniqueProps.split(","));
181 Set<String> indexed = temp.getIndexedProperties();
182 Set<String> keys = temp.getKeys();
184 for (String prop : indexed) {
185 DBIndex dbIndex = new DBIndex();
186 LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
187 if (!this.indexes.containsKey(prop)) {
188 dbIndex.setName(prop);
189 dbIndex.setUnique(unique.contains(prop));
190 properties.add(this.properties.get(prop));
191 dbIndex.setProperties(properties);
192 dbIndex.setStatus(SchemaStatus.ENABLED);
193 this.indexes.put(prop, dbIndex);
196 if (keys.size() > 1 || isTopLevel) {
197 DBIndex dbIndex = new DBIndex();
198 LinkedHashSet<DBProperty> properties = new LinkedHashSet<>();
199 dbIndex.setName("key-for-" + temp.getDbName());
200 if (!this.indexes.containsKey(dbIndex.getName())) {
201 boolean isUnique = false;
203 properties.add(this.properties.get(AAIProperties.NODE_TYPE));
205 for (String key : keys) {
206 properties.add(this.properties.get(key));
208 if (unique.contains(key) && !isUnique) {
212 dbIndex.setUnique(isUnique);
213 dbIndex.setProperties(properties);
214 dbIndex.setStatus(SchemaStatus.ENABLED);
215 this.indexes.put(dbIndex.getName(), dbIndex);
222 * Creates the edge labels.
224 private void createEdgeLabels() {
225 Multimap<String, EdgeRule> edgeRules = EdgeRules.getInstance().getAllRules();
226 for (String key : edgeRules.keySet()) {
227 Collection<EdgeRule> collection = edgeRules.get(key);
228 EdgeProperty prop = new EdgeProperty();
229 //there is only ever one, they used the wrong type for EdgeRules
231 for (EdgeRule item : collection) {
232 label = item.getLabel();
235 prop.setMultiplicity(Multiplicity.MULTI);
236 this.edgeLabels.put(label, prop);
241 * Gets the all introspectors.
243 * @return the all introspectors
245 public Set<Introspector> getAllIntrospectors() {
246 return this.allObjects;