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.w3c.dom.Document;
37 import org.w3c.dom.NodeList;
38 import org.xml.sax.SAXException;
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;
57 public class AuditOXM extends Auditor {
59 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AuditOXM.class);
61 private Set<Introspector> allObjects;
64 * Instantiates a new audit OXM.
66 * @param version the version
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) {
74 final Introspector temp = loader.introspectorFromName(key);
76 this.createDBProperties(temp);
77 } catch (AAIUnknownObjectException e) {
78 LOGGER.warn("Skipping audit for object " + key + " (Unknown Object)", e);
81 for (Introspector temp : allObjects) {
82 this.createDBIndexes(temp);
89 * Gets the all objects.
91 * @param version the version
92 * @return the all objects
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();
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());
106 } catch (ParserConfigurationException e) {
107 // TODO Auto-generated catch block
109 } catch (SAXException e) {
110 // TODO Auto-generated catch block
112 } catch (IOException e) {
113 // TODO Auto-generated catch block
117 result.remove("EdgePropNames");
123 * Creates the DB properties.
125 * @param temp the temp
127 private void createDBProperties(Introspector temp) {
128 Set<String> objectProperties = temp.getProperties();
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;
139 clazz = Class.forName(temp.getGenericType(prop));
140 } catch (ClassNotFoundException e) {
141 clazz = Object.class;
143 dbProperty.setTypeClass(clazz);
144 properties.put(prop, dbProperty);
147 dbProperty.setCardinality(Cardinality.SINGLE);
148 if (temp.isSimpleType(prop)) {
149 Class<?> clazz = null;
151 clazz = Class.forName(temp.getType(prop));
152 } catch (ClassNotFoundException e) {
153 clazz = Object.class;
155 dbProperty.setTypeClass(clazz);
156 properties.put(prop, dbProperty);
165 * Creates the DB indexes.
167 * @param temp the temp
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) {
175 if (namespace == null) {
178 boolean isTopLevel = namespace != "";
179 List<String> unique = Arrays.asList(uniqueProps.split(","));
180 Set<String> indexed = temp.getIndexedProperties();
181 Set<String> keys = temp.getKeys();
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);
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;
202 properties.add(this.properties.get(AAIProperties.NODE_TYPE));
204 for (String key : keys) {
205 properties.add(this.properties.get(key));
207 if (unique.contains(key) && !isUnique) {
211 dbIndex.setUnique(isUnique);
212 dbIndex.setProperties(properties);
213 dbIndex.setStatus(SchemaStatus.ENABLED);
214 this.indexes.put(dbIndex.getName(), dbIndex);
221 * Creates the edge labels.
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
230 for (String item : collection) {
231 label = item.split(",")[0];
234 prop.setMultiplicity(Multiplicity.MULTI);
235 this.edgeLabels.put(label, prop);
240 * Gets the all introspectors.
242 * @return the all introspectors
244 public Set<Introspector> getAllIntrospectors() {
245 return this.allObjects;