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 java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.LinkedHashMap;
31 import java.util.List;
34 import java.util.Vector;
36 import org.eclipse.persistence.dynamic.DynamicType;
37 import org.eclipse.persistence.internal.helper.DatabaseField;
38 import org.eclipse.persistence.internal.oxm.XPathFragment;
39 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
41 import org.eclipse.persistence.mappings.DatabaseMapping;
42 import org.eclipse.persistence.oxm.XMLField;
43 import org.openecomp.datarouter.entity.OxmEntityDescriptor;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Builds up a representation of the versioned entities in a way that they can be cross referenced
49 * in a data-driven way
52 public class VersionedOxmEntities {
54 private static final Logger logger = LoggerFactory.getLogger(VersionedOxmEntities.class);
55 private static final String REST_ROOT_ENTITY = "inventory";
57 private HashMap<String,Boolean> crossEntityReferenceContainerLookup = new HashMap<String,Boolean>();
58 private HashMap<String,CrossEntityReference> crossEntityReferenceLookup = new HashMap<String,CrossEntityReference>();
59 private Map<String,DynamicType> entityTypeLookup = new LinkedHashMap<String,DynamicType>();
60 private Map<String, OxmEntityDescriptor> searchableEntityDescriptors = new HashMap<String, OxmEntityDescriptor>();
61 private Map<String, OxmEntityDescriptor> suggestableEntityDescriptors = new HashMap<String, OxmEntityDescriptor>();
62 private Map<String, OxmEntityDescriptor> entityAliasDescriptors = new HashMap<String, OxmEntityDescriptor>();
65 public void initialize(DynamicJAXBContext context) {
66 parseOxmContext(context);
67 buildCrossEntityReferenceCollections(REST_ROOT_ENTITY, new HashSet<String>());
68 populateSearchableDescriptors(context);
72 * The big goal for these methods is to make the processing as generic and model driven as possible. There are only two
73 * exceptions to this rule, at the moment. I needed to hard-coded the top level REST data model entity type, which is
74 * "inventory" for now. And as this class is heavily focused and coupled towards building a version specific set of
75 * lookup structures for the "crossEntityReference" model attribute, it possesses knowledge of that attribute whether it
76 * exists or not in the DynamicJAXBContext we are currently analyzing.
78 * This method will build two collections:
80 * 1) A list of entity types that can have nested entities containing cross entity reference definitions. The purpose
81 * of this collection is a fail-fast test when processing UEB events so we can quickly determine if it is necessary
82 * to deeply parse the event looking for cross entity reference attributes which not exist.
84 * For example, looking at a service-instance <=> inventory path:
90 * -> service-subscriptions (true)
91 * -> service-subscription (CER defined here in the model) (true)
92 * -> service-instances (false)
93 * -> service-instance (false)
95 * Because service-subscription contains a model definition of CER, in the first collection all the types in the tree will
96 * indicate that it possesses one or more contained entity types with a cross-entity-reference definition.
98 * 2) A lookup for { entityType => CrossEntityReference } so we can quickly access the model definition of a CER for
99 * a specific entity type when we begin extracting parent attributes for transposition into nested child entity types.
106 protected boolean buildCrossEntityReferenceCollections(String entityType, HashSet<String> checked) {
109 * To short-circuit infinite loops, make sure this entityType hasn't
110 * already been checked
113 if(checked.contains(entityType)) {
117 checked.add(entityType);
120 DynamicType parentType = entityTypeLookup.get(entityType);
121 DynamicType childType = null;
122 boolean returnValue = false;
124 if(parentType == null) {
129 * Check if current descriptor contains the cross-entity-reference
130 * attribute. If it does not walk the entity model looking for nested
131 * entity types that may contain the reference.
134 Map<String, String> properties = parentType.getDescriptor().getProperties();
135 if(properties != null) {
136 for(Map.Entry<String, String> entry : properties.entrySet()) {
137 if(entry.getKey().equalsIgnoreCase("crossEntityReference")) {
139 CrossEntityReference cer = new CrossEntityReference();
140 cer.initialize(entry.getValue());
141 crossEntityReferenceLookup.put( entityType, cer);
142 //System.out.println("entityType = " + entityType + " contains a CER instance = " + returnValue);
148 Vector<DatabaseField> fields = parentType.getDescriptor().getAllFields();
152 XMLField xmlField = null;
153 for(DatabaseField f : fields) {
155 if(f instanceof XMLField) {
156 xmlField = (XMLField)f;
157 XPathFragment xpathFragment = xmlField.getXPathFragment();
158 String entityShortName = xpathFragment.getLocalName();
160 childType = entityTypeLookup.get(entityShortName);
162 if(childType != null) {
164 if(!checked.contains(entityShortName)) {
166 if(buildCrossEntityReferenceCollections(entityShortName,checked)) {
172 checked.add(entityShortName);
182 crossEntityReferenceContainerLookup.put(entityType, Boolean.valueOf(returnValue));
186 private void populateSearchableDescriptors(DynamicJAXBContext oxmContext) {
187 List<Descriptor> descriptorsList = oxmContext.getXMLContext().getDescriptors();
188 OxmEntityDescriptor newOxmEntity = null;
190 for (Descriptor desc: descriptorsList) {
192 DynamicType entity = (DynamicType) oxmContext.getDynamicType(desc.getAlias());
194 //LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
195 String primaryKeyAttributeNames = null;
197 //Not all fields have key attributes
198 if (desc.getPrimaryKeyFields() != null) {
199 primaryKeyAttributeNames = desc.getPrimaryKeyFields().toString().replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", "");
202 String entityName = desc.getDefaultRootElement();
204 Map<String, String> properties = entity.getDescriptor().getProperties();
205 if (properties != null) {
206 for (Map.Entry<String, String> entry : properties.entrySet()) {
207 if (entry.getKey().equalsIgnoreCase("searchable")) {
210 * we can do all the work here, we don't have a create additional collections for
213 newOxmEntity = new OxmEntityDescriptor();
214 newOxmEntity.setEntityName(entityName);
215 newOxmEntity.setPrimaryKeyAttributeName(Arrays.asList(primaryKeyAttributeNames.split(",")));
216 newOxmEntity.setSearchableAttributes(Arrays.asList(entry.getValue().split(",")));
217 searchableEntityDescriptors.put(entityName, newOxmEntity);
218 } else if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) {
219 newOxmEntity = new OxmEntityDescriptor();
220 newOxmEntity.setEntityName(entityName);
221 newOxmEntity.setSuggestableEntity(true);
222 Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
223 List<String> listOfSuggestableAttributes = new ArrayList<String>();
225 for (DatabaseMapping descMap : descriptorMaps) {
226 if (descMap.isAbstractDirectMapping()) {
228 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
229 String suggestableOnSearchString = String.valueOf(
230 descMap.getProperties().get("suggestibleOnSearch"));
232 boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
234 if (isSuggestibleOnSearch) {
235 /* Grab attribute types for suggestion */
236 String attributeName = descMap.getField().getName()
237 .replaceAll("/text\\(\\)", "");
238 listOfSuggestableAttributes.add(attributeName);
243 newOxmEntity.setSuggestableAttributes(listOfSuggestableAttributes);
244 suggestableEntityDescriptors.put(entityName, newOxmEntity);
245 } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) {
246 newOxmEntity = new OxmEntityDescriptor();
247 newOxmEntity.setEntityName(entityName);
248 newOxmEntity.setAlias(Arrays.asList(entry.getValue().split(",")));
249 entityAliasDescriptors.put(entityName, newOxmEntity);
258 public Map<String, OxmEntityDescriptor> getSearchableEntityDescriptors() {
259 return searchableEntityDescriptors;
262 public OxmEntityDescriptor getSearchableEntityDescriptor(String entityType) {
263 return searchableEntityDescriptors.get(entityType);
267 public HashMap<String,Boolean> getCrossEntityReferenceContainers() {
268 return crossEntityReferenceContainerLookup;
271 public HashMap<String,CrossEntityReference> getCrossEntityReferences() {
272 return crossEntityReferenceLookup;
276 private void parseOxmContext(DynamicJAXBContext oxmContext) {
277 List<Descriptor> descriptorsList = oxmContext.getXMLContext().getDescriptors();
279 for(Descriptor desc : descriptorsList) {
281 DynamicType entity = (DynamicType)oxmContext.getDynamicType(desc.getAlias());
283 String entityName = desc.getDefaultRootElement();
285 entityTypeLookup.put(entityName, entity);
291 public boolean entityModelContainsCrossEntityReference(String containerEntityType) {
292 Boolean v = crossEntityReferenceContainerLookup.get(containerEntityType);
301 public boolean entityContainsCrossEntityReference(String entityType) {
302 return (crossEntityReferenceLookup.get(entityType) != null);
305 public CrossEntityReference getCrossEntityReference(String entityType) {
306 return crossEntityReferenceLookup.get(entityType);
309 public Map<String, OxmEntityDescriptor> getSuggestableEntityDescriptors() {
310 return suggestableEntityDescriptors;
313 public void setSuggestableEntityDescriptors(
314 Map<String, OxmEntityDescriptor> suggestableEntityDescriptors) {
315 this.suggestableEntityDescriptors = suggestableEntityDescriptors;
318 public Map<String, OxmEntityDescriptor> getEntityAliasDescriptors() {
319 return entityAliasDescriptors;
322 public void setEntityAliasDescriptors(Map<String, OxmEntityDescriptor> entityAliasDescriptors) {
323 this.entityAliasDescriptors = entityAliasDescriptors;
326 public void extractEntities(String entityType, DynamicJAXBContext context, Collection<DynamicType> entities) {
333 public String dumpCrossEntityReferenceContainers() {
335 Set<String> keys = crossEntityReferenceContainerLookup.keySet();
336 StringBuilder sb = new StringBuilder(128);
338 for ( String key : keys ) {
340 if ( crossEntityReferenceContainerLookup.get(key) ) {
341 sb.append("\n").append("Entity-Type = '" + key + "' contains a Cross-Entity-Reference.");
346 return sb.toString();