2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.datarouter.util;
25 import org.eclipse.persistence.dynamic.DynamicType;
26 import org.eclipse.persistence.internal.helper.DatabaseField;
27 import org.eclipse.persistence.internal.oxm.XPathFragment;
28 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
29 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
30 import org.eclipse.persistence.mappings.DatabaseMapping;
31 import org.eclipse.persistence.oxm.XMLField;
32 import org.onap.aai.datarouter.entity.OxmEntityDescriptor;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.LinkedHashMap;
40 import java.util.List;
43 import java.util.Vector;
46 * Builds up a representation of the versioned entities in a way that they can be cross referenced
47 * in a data-driven way
51 public class VersionedOxmEntities {
53 private static final String REST_ROOT_ENTITY = "inventory";
55 private HashMap<String, Boolean> crossEntityReferenceContainerLookup = new HashMap<>();
56 private HashMap<String, CrossEntityReference> crossEntityReferenceLookup = new HashMap<>();
57 private Map<String, DynamicType> entityTypeLookup = new LinkedHashMap<>();
58 private Map<String, OxmEntityDescriptor> searchableEntityDescriptors = new HashMap<>();
59 private Map<String, OxmEntityDescriptor> suggestableEntityDescriptors = new HashMap<>();
60 private Map<String, OxmEntityDescriptor> entityAliasDescriptors = new HashMap<>();
63 public void initialize(DynamicJAXBContext context) {
64 parseOxmContext(context);
65 buildCrossEntityReferenceCollections(REST_ROOT_ENTITY, new HashSet<String>());
66 populateSearchableDescriptors(context);
70 * The big goal for these methods is to make the processing as generic and model driven as possible.
71 * There are only two exceptions to this rule, at the moment. I needed to hard-coded the top level REST data
72 * model entity type, which is "inventory" for now. And as this class is heavily focused and coupled towards
73 * building a version specific set of lookup structures for the "crossEntityReference" model attribute, it possesses
74 * knowledge of that attribute whether it exists or not in the DynamicJAXBContext we are currently analyzing.
76 * This method will build two collections:
78 * 1) A list of entity types that can have nested entities containing cross entity reference definitions.
79 * The purpose of this collection is a fail-fast test when processing UEB events so we can quickly determine if
80 * it is necessary to deeply parse the event looking for cross entity reference attributes which not exist.
82 * For example, looking at a service-instance <=> inventory path:
88 * -> service-subscriptions (true)
89 * -> service-subscription (CER defined here in the model) (true)
90 * -> service-instances (false)
91 * -> service-instance (false)
93 * Because service-subscription contains a model definition of CER, in the first collection all the types in the
94 * tree will indicate that it possesses one or more contained entity types with a cross-entity-reference definition.
96 * 2) A lookup for { entityType => CrossEntityReference } so we can quickly access the model definition of a CER
97 * for a specific entity type when we begin extracting parent attributes for transposition into nested child entity
104 protected boolean buildCrossEntityReferenceCollections(String entityType, HashSet<String> checked) {
107 * To short-circuit infinite loops, make sure this entityType hasn't
108 * already been checked
111 if (checked.contains(entityType)) {
114 checked.add(entityType);
117 DynamicType parentType = entityTypeLookup.get(entityType);
118 DynamicType childType;
119 boolean returnValue = false;
121 if (parentType == null) {
126 * Check if current descriptor contains the cross-entity-reference
127 * attribute. If it does not walk the entity model looking for nested
128 * entity types that may contain the reference.
131 Map<String, String> properties = parentType.getDescriptor().getProperties();
132 if (properties != null) {
133 for (Map.Entry<String, String> entry : properties.entrySet()) {
134 if ("crossEntityReference".equalsIgnoreCase(entry.getKey())) {
136 CrossEntityReference cer = new CrossEntityReference();
137 cer.initialize(entry.getValue());
138 crossEntityReferenceLookup.put(entityType, cer);
139 //System.out.println("entityType = " + entityType + " contains a CER instance = " + returnValue);
145 Vector<DatabaseField> fields = parentType.getDescriptor().getAllFields();
147 if (fields != null) {
150 for (DatabaseField f : fields) {
152 if (f instanceof XMLField) {
153 xmlField = (XMLField) f;
154 XPathFragment xpathFragment = xmlField.getXPathFragment();
155 String entityShortName = xpathFragment.getLocalName();
157 childType = entityTypeLookup.get(entityShortName);
159 if (childType != null) {
161 if (!checked.contains(entityShortName)) {
163 if (buildCrossEntityReferenceCollections(entityShortName, checked)) {
169 checked.add(entityShortName);
179 crossEntityReferenceContainerLookup.put(entityType, Boolean.valueOf(returnValue));
183 private void populateSearchableDescriptors(DynamicJAXBContext oxmContext) {
184 List<Descriptor> descriptorsList = oxmContext.getXMLContext().getDescriptors();
185 OxmEntityDescriptor newOxmEntity;
187 for (Descriptor desc : descriptorsList) {
189 DynamicType entity = (DynamicType) oxmContext.getDynamicType(desc.getAlias());
191 //LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
192 String primaryKeyAttributeNames = null;
194 //Not all fields have key attributes
195 if (desc.getPrimaryKeyFields() != null) {
196 primaryKeyAttributeNames = desc.getPrimaryKeyFields()
197 .toString().replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", "");
200 String entityName = desc.getDefaultRootElement();
202 Map<String, String> properties = entity.getDescriptor().getProperties();
203 if (properties != null) {
204 for (Map.Entry<String, String> entry : properties.entrySet()) {
205 if ("searchable".equalsIgnoreCase(entry.getKey())) {
208 * we can do all the work here, we don't have a create additional collections for
211 newOxmEntity = new OxmEntityDescriptor();
212 newOxmEntity.setEntityName(entityName);
214 .setPrimaryKeyAttributeName(Arrays.asList(primaryKeyAttributeNames.split(",")));
215 newOxmEntity.setSearchableAttributes(Arrays.asList(entry.getValue().split(",")));
216 searchableEntityDescriptors.put(entityName, newOxmEntity);
217 } else if ("containsSuggestibleProps".equalsIgnoreCase(entry.getKey())) {
218 newOxmEntity = new OxmEntityDescriptor();
219 newOxmEntity.setEntityName(entityName);
220 newOxmEntity.setSuggestableEntity(true);
221 Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
222 List<String> listOfSuggestableAttributes = new ArrayList<>();
224 for (DatabaseMapping descMap : descriptorMaps) {
225 if (descMap.isAbstractDirectMapping()) {
227 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
228 String suggestableOnSearchString = String.valueOf(
229 descMap.getProperties().get("suggestibleOnSearch"));
231 boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
233 if (isSuggestibleOnSearch) {
234 /* Grab attribute types for suggestion */
235 String attributeName = descMap.getField().getName()
236 .replaceAll("/text\\(\\)", "");
237 listOfSuggestableAttributes.add(attributeName);
242 newOxmEntity.setSuggestableAttributes(listOfSuggestableAttributes);
243 suggestableEntityDescriptors.put(entityName, newOxmEntity);
244 } else if ("suggestionAliases".equalsIgnoreCase(entry.getKey())) {
245 newOxmEntity = new OxmEntityDescriptor();
246 newOxmEntity.setEntityName(entityName);
247 newOxmEntity.setAlias(Arrays.asList(entry.getValue().split(",")));
248 entityAliasDescriptors.put(entityName, newOxmEntity);
257 public Map<String, OxmEntityDescriptor> getSearchableEntityDescriptors() {
258 return searchableEntityDescriptors;
261 public OxmEntityDescriptor getSearchableEntityDescriptor(String entityType) {
262 return searchableEntityDescriptors.get(entityType);
266 public HashMap<String, Boolean> getCrossEntityReferenceContainers() {
267 return crossEntityReferenceContainerLookup;
270 public HashMap<String, CrossEntityReference> getCrossEntityReferences() {
271 return crossEntityReferenceLookup;
275 private void parseOxmContext(DynamicJAXBContext oxmContext) {
276 List<Descriptor> descriptorsList = oxmContext.getXMLContext().getDescriptors();
278 for (Descriptor desc : descriptorsList) {
280 DynamicType entity = (DynamicType) oxmContext.getDynamicType(desc.getAlias());
282 String entityName = desc.getDefaultRootElement();
284 entityTypeLookup.put(entityName, entity);
290 public boolean entityModelContainsCrossEntityReference(String containerEntityType) {
291 Boolean v = crossEntityReferenceContainerLookup.get(containerEntityType);
300 public boolean entityContainsCrossEntityReference(String entityType) {
301 return crossEntityReferenceLookup.get(entityType) != null;
304 public CrossEntityReference getCrossEntityReference(String entityType) {
305 return crossEntityReferenceLookup.get(entityType);
308 public Map<String, OxmEntityDescriptor> getSuggestableEntityDescriptors() {
309 return suggestableEntityDescriptors;
312 public void setSuggestableEntityDescriptors(
313 Map<String, OxmEntityDescriptor> suggestableEntityDescriptors) {
314 this.suggestableEntityDescriptors = suggestableEntityDescriptors;
317 public Map<String, OxmEntityDescriptor> getEntityAliasDescriptors() {
318 return entityAliasDescriptors;
321 public void setEntityAliasDescriptors(Map<String, OxmEntityDescriptor> entityAliasDescriptors) {
322 this.entityAliasDescriptors = entityAliasDescriptors;
325 public void extractEntities(String entityType, DynamicJAXBContext context, Collection<DynamicType> entities) {
330 public String dumpCrossEntityReferenceContainers() {
332 Set<String> keys = crossEntityReferenceContainerLookup.keySet();
333 StringBuilder sb = new StringBuilder(128);
335 for (String key : keys) {
337 if (crossEntityReferenceContainerLookup.get(key)) {
338 sb.append("\n").append("Entity-Type = '" + key + "' contains a Cross-Entity-Reference.");
343 return sb.toString();