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.onap.aai.sparky.config.oxm;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.LinkedHashMap;
34 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.Vector;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
41 import org.eclipse.persistence.dynamic.DynamicType;
42 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
43 import org.eclipse.persistence.jaxb.JAXBContextProperties;
44 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
45 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
46 import org.eclipse.persistence.mappings.DatabaseMapping;
47 import org.onap.aai.sparky.logging.AaiUiMsgs;
48 import org.onap.aai.sparky.synchronizer.entity.SuggestionSearchEntity;
49 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
50 import org.onap.aai.cl.api.Logger;
51 import org.onap.aai.cl.eelf.LoggerFactory;
54 * The Class OxmModelLoader.
56 public class OxmModelLoader {
58 private static OxmModelLoader instance;
60 private static final Logger LOG = LoggerFactory.getInstance().getLogger(OxmModelLoader.class);
62 private Map<String, HashMap<String, String>> oxmModel =
63 new LinkedHashMap<String, HashMap<String, String>>();
65 private Map<String, DynamicType> entityTypeLookup = new LinkedHashMap<String, DynamicType>();
67 private Map<String, HashMap<String, String>> searchableOxmModel =
68 new LinkedHashMap<String, HashMap<String, String>>();
70 private Map<String, HashMap<String, String>> crossReferenceEntityOxmModel =
71 new LinkedHashMap<String, HashMap<String, String>>();
73 private Map<String, HashMap<String, String>> geoEntityOxmModel =
74 new LinkedHashMap<String, HashMap<String, String>>();
76 private Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel =
77 new LinkedHashMap<String, HashMap<String, String>>();
79 private Map<String, OxmEntityDescriptor> entityDescriptors =
80 new HashMap<String, OxmEntityDescriptor>();
82 private Map<String, OxmEntityDescriptor> searchableEntityDescriptors =
83 new HashMap<String, OxmEntityDescriptor>();
85 private Map<String, OxmEntityDescriptor> crossReferenceEntityDescriptors =
86 new HashMap<String, OxmEntityDescriptor>();
88 private Map<String, OxmEntityDescriptor> geoEntityDescriptors =
89 new HashMap<String, OxmEntityDescriptor>();
91 private Map<String, OxmEntityDescriptor> suggestionSearchEntityDescriptors =
92 new HashMap<String, OxmEntityDescriptor>();
94 public static OxmModelLoader getInstance() {
95 if (instance == null) {
96 instance = new OxmModelLoader();
97 LOG.info(AaiUiMsgs.INITIALIZE_OXM_MODEL_LOADER);
98 instance.loadModels();
106 * Instantiates a new oxm model loader.
108 public OxmModelLoader() {
115 private void loadModels() {
116 // find latest version of OXM file in folder
117 String version = findLatestOxmVersion();
118 if (version == null) {
119 LOG.error(AaiUiMsgs.OXM_FILE_NOT_FOUND, TierSupportUiConstants.CONFIG_OXM_LOCATION);
123 // load the latest version based on file name
131 * @param version the version
133 public void loadModel(String version) {
134 String fileName = loadOxmFileName(version);
136 try (FileInputStream inputStream = new FileInputStream(new File(fileName))) {
137 Map<String, Object> properties = new HashMap<String, Object>();
138 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
140 final DynamicJAXBContext oxmContext = DynamicJAXBContextFactory
141 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
142 parseOxmContext(oxmContext);
143 // populateSearchableOxmModel();
144 LOG.info(AaiUiMsgs.OXM_LOAD_SUCCESS);
146 } catch (FileNotFoundException fnf) {
147 LOG.info(AaiUiMsgs.OXM_READ_ERROR_NONVERBOSE);
148 LOG.error(AaiUiMsgs.OXM_READ_ERROR_VERBOSE, fileName);
149 } catch (Exception exc) {
150 LOG.info(AaiUiMsgs.OXM_PARSE_ERROR_NONVERBOSE);
151 LOG.error(AaiUiMsgs.OXM_PARSE_ERROR_VERBOSE, fileName, exc.getMessage());
156 * Parses the oxm context.
158 * @param oxmContext the oxm context
160 private void parseOxmContext(DynamicJAXBContext oxmContext) {
161 @SuppressWarnings("rawtypes")
162 List<Descriptor> descriptorsList = oxmContext.getXMLContext().getDescriptors();
164 for (@SuppressWarnings("rawtypes")
165 Descriptor desc : descriptorsList) {
167 DynamicType entity = oxmContext.getDynamicType(desc.getAlias());
169 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
171 // Not all fields have key attributes
172 if (desc.getPrimaryKeyFields() != null) {
173 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
174 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
177 String entityName = desc.getDefaultRootElement();
179 entityTypeLookup.put(entityName, entity);
182 oxmProperties.put("entityName", entityName);
184 Map<String, String> properties = entity.getDescriptor().getProperties();
185 if (properties != null) {
186 for (Map.Entry<String, String> entry : properties.entrySet()) {
188 if (entry.getKey().equalsIgnoreCase("searchable")) {
189 oxmProperties.put("searchableAttributes", entry.getValue());
190 } else if (entry.getKey().equalsIgnoreCase("crossEntityReference")) {
191 oxmProperties.put("crossEntityReference", entry.getValue());
192 } else if (entry.getKey().equalsIgnoreCase("geoLat")) {
193 if (entry.getValue().length() > 0) {
194 oxmProperties.put("geoLat", entry.getValue());
196 } else if (entry.getKey().equalsIgnoreCase("geoLong")) {
197 if (entry.getValue().length() > 0) {
198 oxmProperties.put("geoLong", entry.getValue());
200 } else if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) {
202 oxmProperties.put("containsSuggestibleProps", "true");
204 Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
205 List<String> listOfSuggestableAttributes = new ArrayList<String>();
207 for (DatabaseMapping descMap : descriptorMaps) {
208 if (descMap.isAbstractDirectMapping()) {
210 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
211 String suggestableOnSearchString =
212 String.valueOf(descMap.getProperties().get("suggestibleOnSearch"));
214 boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
216 if (isSuggestibleOnSearch) {
217 /* Grab attribute types for suggestion */
218 String attributeName =
219 descMap.getField().getName().replaceAll("/text\\(\\)", "");
220 listOfSuggestableAttributes.add(attributeName);
222 if (descMap.getProperties().get("suggestionVerbs") != null) {
223 String suggestionVerbsString =
224 String.valueOf(descMap.getProperties().get("suggestionVerbs"));
226 oxmProperties.put("suggestionVerbs", suggestionVerbsString);
232 if (!listOfSuggestableAttributes.isEmpty()) {
233 oxmProperties.put("suggestibleAttributes",
234 String.join(",", listOfSuggestableAttributes));
236 } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) {
237 oxmProperties.put("suggestionAliases", entry.getValue());
242 oxmModel.put(entityName, oxmProperties);
244 // Add all searchable entity types for reserve lookup
245 if (oxmProperties.containsKey("searchableAttributes")) {
246 searchableOxmModel.put(entityName, oxmProperties);
249 if (oxmProperties.containsKey("crossEntityReference")) {
250 crossReferenceEntityOxmModel.put(entityName, oxmProperties);
253 if (oxmProperties.containsKey("geoLat") && oxmProperties.containsKey("geoLong")) {
254 geoEntityOxmModel.put(entityName, oxmProperties);
257 if (oxmProperties.containsKey("containsSuggestibleProps")) {
258 suggestionSearchEntityOxmModel.put(entityName, oxmProperties);
262 for (Entry<String, HashMap<String, String>> entityModel : oxmModel.entrySet()) {
263 HashMap<String, String> attribute = entityModel.getValue();
264 OxmEntityDescriptor entity = new OxmEntityDescriptor();
265 entity.setEntityName(attribute.get("entityName"));
266 if (attribute.containsKey("primaryKeyAttributeNames")) {
268 entity.setPrimaryKeyAttributeName(
269 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
270 if (attribute.containsKey("searchableAttributes")) {
271 entity.setSearchableAttributes(
272 Arrays.asList(attribute.get("searchableAttributes").split(",")));
273 } else if (attribute.containsKey("crossEntityReference")) {
274 List<String> crossEntityRefTokens =
275 Arrays.asList(attribute.get("crossEntityReference").split(","));
277 if (crossEntityRefTokens.size() >= 2) {
278 CrossEntityReference entityRef = new CrossEntityReference();
279 entityRef.setTargetEntityType(crossEntityRefTokens.get(0));
281 for (int i = 1; i < crossEntityRefTokens.size(); i++) {
282 entityRef.addReferenceAttribute(crossEntityRefTokens.get(i));
285 entity.setCrossEntityReference(entityRef);
287 LOG.error(AaiUiMsgs.OXM_PROP_DEF_ERR_CROSS_ENTITY_REF, attribute.get("entityName"),
288 attribute.get("crossEntityReference"));
292 if (attribute.containsKey("geoLat") || attribute.containsKey("geoLong")) {
293 entity.setGeoLatName(attribute.get("geoLat"));
294 entity.setGeoLongName(attribute.get("geoLong"));
297 if (attribute.containsKey("suggestionVerbs")) {
298 String entityName = attribute.get("entityName");
299 SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity(this);
300 suggestionSearchEntity.setEntityType(entityName);
302 entity.setSuggestionSearchEntity(suggestionSearchEntity);
305 entityDescriptors.put(attribute.get("entityName"), entity);
310 for (Entry<String, HashMap<String, String>> searchableModel : searchableOxmModel.entrySet()) {
311 HashMap<String, String> attribute = searchableModel.getValue();
312 OxmEntityDescriptor entity = new OxmEntityDescriptor();
313 entity.setEntityName(attribute.get("entityName"));
314 entity.setPrimaryKeyAttributeName(
315 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
317 .setSearchableAttributes(Arrays.asList(attribute.get("searchableAttributes").split(",")));
318 searchableEntityDescriptors.put(attribute.get("entityName"), entity);
321 for (Entry<String, HashMap<String, String>> geoEntityModel : geoEntityOxmModel.entrySet()) {
322 HashMap<String, String> attribute = geoEntityModel.getValue();
323 OxmEntityDescriptor entity = new OxmEntityDescriptor();
324 entity.setEntityName(attribute.get("entityName"));
325 entity.setPrimaryKeyAttributeName(
326 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
327 entity.setGeoLatName(attribute.get("geoLat"));
328 entity.setGeoLongName(attribute.get("geoLong"));
329 geoEntityDescriptors.put(attribute.get("entityName"), entity);
332 for (Entry<String, HashMap<String, String>> crossRefModel : crossReferenceEntityOxmModel
334 HashMap<String, String> attribute = crossRefModel.getValue();
335 OxmEntityDescriptor entity = new OxmEntityDescriptor();
336 entity.setEntityName(attribute.get("entityName"));
337 entity.setPrimaryKeyAttributeName(
338 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
341 List<String> crossEntityRefTokens =
342 Arrays.asList(attribute.get("crossEntityReference").split(","));
344 if (crossEntityRefTokens.size() >= 2) {
345 CrossEntityReference entityRef = new CrossEntityReference();
346 entityRef.setTargetEntityType(crossEntityRefTokens.get(0));
348 for (int i = 1; i < crossEntityRefTokens.size(); i++) {
349 entityRef.addReferenceAttribute(crossEntityRefTokens.get(i));
352 entity.setCrossEntityReference(entityRef);
354 crossReferenceEntityDescriptors.put(attribute.get("entityName"), entity);
357 for (Entry<String, HashMap<String, String>> suggestionEntityModel : suggestionSearchEntityOxmModel
359 HashMap<String, String> attribute = suggestionEntityModel.getValue();
361 String entityName = attribute.get("entityName");
362 SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity(this);
363 suggestionSearchEntity.setEntityType(entityName);
365 if (attribute.get("suggestionVerbs") != null) {
366 suggestionSearchEntity.setSuggestionConnectorWords(
367 Arrays.asList(attribute.get("suggestionVerbs").split(",")));
370 if (attribute.get("suggestionAliases") != null) {
371 suggestionSearchEntity
372 .setSuggestionAliases(Arrays.asList(attribute.get("suggestionAliases").split(",")));
375 if (attribute.get("suggestibleAttributes") != null) {
376 suggestionSearchEntity.setSuggestionPropertyTypes(
377 Arrays.asList(attribute.get("suggestibleAttributes").split(",")));
380 OxmEntityDescriptor entity = new OxmEntityDescriptor();
381 entity.setSuggestionSearchEntity(suggestionSearchEntity);
382 entity.setEntityName(entityName);
384 if (attribute.get("primaryKeyAttributeNames") != null) {
385 entity.setPrimaryKeyAttributeName(
386 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
389 suggestionSearchEntityDescriptors.put(entityName, entity);
394 * Find latest oxm version.
398 public String findLatestOxmVersion() {
399 File[] listOxmFiles = loadOxmFolder().listFiles();
401 if (listOxmFiles == null) {
405 Integer latestVersion = -1;
407 Pattern oxmFileNamePattern = Pattern.compile("^aai_oxm_v([0-9]*).xml");
409 for (File file : listOxmFiles) {
411 String fileName = file.getName();
412 Matcher matcher = oxmFileNamePattern.matcher(fileName);
413 if (matcher.matches()) {
414 if (latestVersion <= Integer.parseInt(matcher.group(1))) {
415 latestVersion = Integer.parseInt(matcher.group(1));
421 if (latestVersion != -1) {
422 return "v" + latestVersion.toString();
434 public File loadOxmFolder() {
435 return new File(TierSupportUiConstants.CONFIG_OXM_LOCATION);
439 * Load oxm file name.
441 * @param version the version
444 public String loadOxmFileName(String version) {
445 return new String(TierSupportUiConstants.CONFIG_OXM_LOCATION + "aai_oxm_" + version + ".xml");
449 * Get the original representation of the OXM Model
451 public Map<String, HashMap<String, String>> getOxmModel() {
456 * Get the searchable raw map entity types
458 public Map<String, HashMap<String, String>> getSearchableOxmModel() {
459 return searchableOxmModel;
462 public Map<String, HashMap<String, String>> getCrossReferenceEntityOxmModel() {
463 return crossReferenceEntityOxmModel;
466 public Map<String, OxmEntityDescriptor> getEntityDescriptors() {
467 return entityDescriptors;
471 * Gets the entity descriptor.
473 * @param type the type
474 * @return the entity descriptor
476 public OxmEntityDescriptor getEntityDescriptor(String type) {
477 return entityDescriptors.get(type);
480 public Map<String, OxmEntityDescriptor> getSearchableEntityDescriptors() {
481 return searchableEntityDescriptors;
485 * Gets the searchable entity descriptor.
487 * @param entityType the entity type
488 * @return the searchable entity descriptor
490 public OxmEntityDescriptor getSearchableEntityDescriptor(String entityType) {
491 return searchableEntityDescriptors.get(entityType);
494 public Map<String, OxmEntityDescriptor> getCrossReferenceEntityDescriptors() {
495 return crossReferenceEntityDescriptors;
498 public Map<String, OxmEntityDescriptor> getGeoEntityDescriptors() {
499 return geoEntityDescriptors;
502 public Map<String, OxmEntityDescriptor> getSuggestionSearchEntityDescriptors() {
503 return suggestionSearchEntityDescriptors;