aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/util/VersionedOxmEntities.java
blob: 609ff26f490b2ca5f9a820e92aa601d578d1b488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.util;

import org.eclipse.persistence.dynamic.DynamicType;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.oxm.XPathFragment;
import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.XMLField;
import org.onap.aai.entity.OxmEntityDescriptor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

/**
 * Builds up a representation of the versioned entities in a way that they can be cross referenced
 * in a data-driven way
 *
 * @author DAVEA
 */
public class VersionedOxmEntities {

    private static final String REST_ROOT_ENTITY = "inventory";

    private HashMap<String, Boolean> crossEntityReferenceContainerLookup = new HashMap<>();
    private HashMap<String, CrossEntityReference> crossEntityReferenceLookup = new HashMap<>();
    private Map<String, DynamicType> entityTypeLookup = new LinkedHashMap<>();
    private Map<String, OxmEntityDescriptor> searchableEntityDescriptors = new HashMap<>();
    private Map<String, OxmEntityDescriptor> suggestableEntityDescriptors = new HashMap<>();
    private Map<String, OxmEntityDescriptor> entityAliasDescriptors = new HashMap<>();


    public void initialize(DynamicJAXBContext context) {
        parseOxmContext(context);
        buildCrossEntityReferenceCollections(REST_ROOT_ENTITY, new HashSet<String>());
        populateSearchableDescriptors(context);
    }

    /**
     * The big goal for these methods is to make the processing as generic and model driven as possible.
     * There are only two exceptions to this rule, at the moment.  I needed to hard-coded the top level REST data
     * model entity type, which is "inventory" for now.   And as this class is heavily focused and coupled towards
     * building a version specific set of lookup structures for the "crossEntityReference" model attribute, it possesses
     * knowledge of that attribute whether it exists or not in the DynamicJAXBContext we are currently analyzing.
     * <p>
     * This method will build two collections:
     * <p>
     * 1)  A list of entity types that can have nested entities containing cross entity reference definitions.
     * The purpose of this collection is a fail-fast test when processing UEB events so we can quickly determine if
     * it is necessary to deeply parse the event looking for cross entity reference attributes which not exist.
     * <p>
     * For example, looking at a service-instance <=> inventory path:
     * <p>
     * inventory (true)
     * -> business (true)
     * -> customers  (true)
     * -> customer  (true)
     * -> service-subscriptions (true)
     * -> service-subscription (CER defined here in the model)   (true)
     * -> service-instances    (false)
     * -> service-instance   (false)
     * <p>
     * Because service-subscription contains a model definition of CER, in the first collection all the types in the
     * tree will indicate that it possesses one or more contained entity types with a cross-entity-reference definition.
     * <p>
     * 2)  A lookup for { entityType => CrossEntityReference } so we can quickly access the model definition of a CER
     * for a specific entity type when we begin extracting parent attributes for transposition into nested child entity
     * types.
     *
     * @param entityType
     * @param checked
     * @return
     */
    protected boolean buildCrossEntityReferenceCollections(String entityType, HashSet<String> checked) {

      /*
       * To short-circuit infinite loops, make sure this entityType hasn't
       * already been checked
       */

        if (checked.contains(entityType)) {
            return false;
        } else {
            checked.add(entityType);
        }

        DynamicType parentType = entityTypeLookup.get(entityType);
        DynamicType childType;
        boolean returnValue = false;

        if (parentType == null) {
            return returnValue;
        }

      /*
       * Check if current descriptor contains the cross-entity-reference
       * attribute. If it does not walk the entity model looking for nested
       * entity types that may contain the reference.
       */

        Map<String, String> properties = parentType.getDescriptor().getProperties();
        if (properties != null) {
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                if ("crossEntityReference".equalsIgnoreCase(entry.getKey())) {
                    returnValue = true;
                    CrossEntityReference cer = new CrossEntityReference();
                    cer.initialize(entry.getValue());
                    crossEntityReferenceLookup.put(entityType, cer);
                    //System.out.println("entityType = " + entityType + " contains a CER instance = " + returnValue);
                    // return true;
                }
            }
        }

        Vector<DatabaseField> fields = parentType.getDescriptor().getAllFields();

        if (fields != null) {

            XMLField xmlField;
            for (DatabaseField f : fields) {

                if (f instanceof XMLField) {
                    xmlField = (XMLField) f;
                    XPathFragment xpathFragment = xmlField.getXPathFragment();
                    String entityShortName = xpathFragment.getLocalName();

                    childType = entityTypeLookup.get(entityShortName);

                    if (childType != null) {

                        if (!checked.contains(entityShortName)) {

                            if (buildCrossEntityReferenceCollections(entityShortName, checked)) {
                                returnValue = true;
                            }

                        }

                        checked.add(entityShortName);

                    }

                }

            }

        }

        crossEntityReferenceContainerLookup.put(entityType, Boolean.valueOf(returnValue));
        return returnValue;
    }

    private void populateSearchableDescriptors(DynamicJAXBContext oxmContext) {
        List<Descriptor> descriptorsList = oxmContext.getXMLContext().getDescriptors();
        OxmEntityDescriptor newOxmEntity;

        for (Descriptor desc : descriptorsList) {

            DynamicType entity = (DynamicType) oxmContext.getDynamicType(desc.getAlias());

            //LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
            String primaryKeyAttributeNames = null;

            //Not all fields have key attributes
            if (desc.getPrimaryKeyFields() != null) {
                primaryKeyAttributeNames = desc.getPrimaryKeyFields()
                        .toString().replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", "");
            }

            String entityName = desc.getDefaultRootElement();

            Map<String, String> properties = entity.getDescriptor().getProperties();
            if (properties != null) {
                for (Map.Entry<String, String> entry : properties.entrySet()) {
                    if ("searchable".equalsIgnoreCase(entry.getKey())) {
                  
                  /*
                   * we can do all the work here, we don't have a create additional collections for 
                   * subsequent passes
                   */
                        newOxmEntity = new OxmEntityDescriptor();
                        newOxmEntity.setEntityName(entityName);
                        newOxmEntity
                                .setPrimaryKeyAttributeName(Arrays.asList(primaryKeyAttributeNames.split(",")));
                        newOxmEntity.setSearchableAttributes(Arrays.asList(entry.getValue().split(",")));
                        searchableEntityDescriptors.put(entityName, newOxmEntity);
                    } else if ("containsSuggestibleProps".equalsIgnoreCase(entry.getKey())) {
                        newOxmEntity = new OxmEntityDescriptor();
                        newOxmEntity.setEntityName(entityName);
                        newOxmEntity.setSuggestableEntity(true);
                        Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
                        List<String> listOfSuggestableAttributes = new ArrayList<>();

                        for (DatabaseMapping descMap : descriptorMaps) {
                            if (descMap.isAbstractDirectMapping()) {

                                if (descMap.getProperties().get("suggestibleOnSearch") != null) {
                                    String suggestableOnSearchString = String.valueOf(
                                            descMap.getProperties().get("suggestibleOnSearch"));

                                    boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);

                                    if (isSuggestibleOnSearch) {
                         /* Grab attribute types for suggestion */
                                        String attributeName = descMap.getField().getName()
                                                .replaceAll("/text\\(\\)", "");
                                        listOfSuggestableAttributes.add(attributeName);
                                    }
                                }
                            }
                        }
                        newOxmEntity.setSuggestableAttributes(listOfSuggestableAttributes);
                        suggestableEntityDescriptors.put(entityName, newOxmEntity);
                    } else if ("suggestionAliases".equalsIgnoreCase(entry.getKey())) {
                        newOxmEntity = new OxmEntityDescriptor();
                        newOxmEntity.setEntityName(entityName);
                        newOxmEntity.setAlias(Arrays.asList(entry.getValue().split(",")));
                        entityAliasDescriptors.put(entityName, newOxmEntity);
                    }
                }
            }

        }

    }

    public Map<String, OxmEntityDescriptor> getSearchableEntityDescriptors() {
        return searchableEntityDescriptors;
    }

    public OxmEntityDescriptor getSearchableEntityDescriptor(String entityType) {
        return searchableEntityDescriptors.get(entityType);
    }


    public HashMap<String, Boolean> getCrossEntityReferenceContainers() {
        return crossEntityReferenceContainerLookup;
    }

    public HashMap<String, CrossEntityReference> getCrossEntityReferences() {
        return crossEntityReferenceLookup;
    }


    private void parseOxmContext(DynamicJAXBContext oxmContext) {
        List<Descriptor> descriptorsList = oxmContext.getXMLContext().getDescriptors();

        for (Descriptor desc : descriptorsList) {

            DynamicType entity = (DynamicType) oxmContext.getDynamicType(desc.getAlias());

            String entityName = desc.getDefaultRootElement();

            entityTypeLookup.put(entityName, entity);

        }

    }

    public boolean entityModelContainsCrossEntityReference(String containerEntityType) {
        Boolean v = crossEntityReferenceContainerLookup.get(containerEntityType);

        if (v == null) {
            return false;
        }

        return v;
    }

    public boolean entityContainsCrossEntityReference(String entityType) {
        return crossEntityReferenceLookup.get(entityType) != null;
    }

    public CrossEntityReference getCrossEntityReference(String entityType) {
        return crossEntityReferenceLookup.get(entityType);
    }

    public Map<String, OxmEntityDescriptor> getSuggestableEntityDescriptors() {
        return suggestableEntityDescriptors;
    }

    public void setSuggestableEntityDescriptors(
            Map<String, OxmEntityDescriptor> suggestableEntityDescriptors) {
        this.suggestableEntityDescriptors = suggestableEntityDescriptors;
    }

    public Map<String, OxmEntityDescriptor> getEntityAliasDescriptors() {
        return entityAliasDescriptors;
    }

    public void setEntityAliasDescriptors(Map<String, OxmEntityDescriptor> entityAliasDescriptors) {
        this.entityAliasDescriptors = entityAliasDescriptors;
    }

    public void extractEntities(String entityType, DynamicJAXBContext context, Collection<DynamicType> entities) {


    }

    public String dumpCrossEntityReferenceContainers() {

        Set<String> keys = crossEntityReferenceContainerLookup.keySet();
        StringBuilder sb = new StringBuilder(128);

        for (String key : keys) {

            if (crossEntityReferenceContainerLookup.get(key)) {
                sb.append("\n").append("Entity-Type = '" + key + "' contains a Cross-Entity-Reference.");
            }
        }


        return sb.toString();

    }
    
    public Map<String, DynamicType> getEntityTypeLookup() {
      return entityTypeLookup;
    }

}