Sonar Bug Fix- for EntityEventPolicy.java
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / policy / EntityEventPolicy.java
index 793588f..ec76c95 100644 (file)
@@ -4,6 +4,7 @@
  * ================================================================================
  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
  * Copyright © 2017-2018 Amdocs
+ * Modifications Copyright (C) 2019 IBM
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -37,26 +38,28 @@ import org.apache.camel.Processor;
 import org.eclipse.persistence.dynamic.DynamicType;
 import org.eclipse.persistence.internal.helper.DatabaseField;
 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
+import org.eclipse.persistence.oxm.MediaType;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.onap.aai.datarouter.entity.AaiEventEntity;
 import org.onap.aai.datarouter.entity.AggregationEntity;
 import org.onap.aai.datarouter.entity.DocumentStoreDataEntity;
-import org.onap.aai.datarouter.entity.OxmEntityDescriptor;
 import org.onap.aai.datarouter.entity.SuggestionSearchEntity;
 import org.onap.aai.datarouter.entity.TopographicalEntity;
 import org.onap.aai.datarouter.entity.UebEventHeader;
 import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
-import org.onap.aai.datarouter.util.CrossEntityReference;
-import org.onap.aai.datarouter.util.EntityOxmReferenceHelper;
-import org.onap.aai.datarouter.util.ExternalOxmModelProcessor;
 import org.onap.aai.datarouter.util.NodeUtils;
-import org.onap.aai.datarouter.util.OxmModelLoader;
 import org.onap.aai.datarouter.util.RouterServiceUtil;
 import org.onap.aai.datarouter.util.SearchServiceAgent;
 import org.onap.aai.datarouter.util.SearchSuggestionPermutation;
-import org.onap.aai.datarouter.util.Version;
-import org.onap.aai.datarouter.util.VersionedOxmEntities;
+import org.onap.aai.entity.OxmEntityDescriptor;
+import org.onap.aai.util.CrossEntityReference;
+import org.onap.aai.util.EntityOxmReferenceHelper;
+import org.onap.aai.util.ExternalOxmModelProcessor;
+import org.onap.aai.schema.OxmModelLoader;
+import org.onap.aai.setup.SchemaVersions;
+import org.onap.aai.util.Version;
+import org.onap.aai.util.VersionedOxmEntities;
 import org.onap.aai.cl.api.Logger;
 import org.onap.aai.cl.eelf.LoggerFactory;
 import org.onap.aai.cl.mdc.MdcContext;
@@ -142,7 +145,7 @@ public class EntityEventPolicy implements Processor {
     this.externalOxmModelProcessors = new ArrayList<>();
     this.externalOxmModelProcessors.add(EntityOxmReferenceHelper.getInstance());
     OxmModelLoader.registerExternalOxmModelProcessors(externalOxmModelProcessors);
-    OxmModelLoader.loadModels();
+    OxmModelLoader.loadModels(config.getSchemaVersions(), config.getSchemaLocationsBean());
     oxmVersionContextMap = OxmModelLoader.getVersionContextMap();
     parseLatestOxmVersion();
   }
@@ -214,7 +217,7 @@ public class EntityEventPolicy implements Processor {
 
     String uebPayload = exchange.getIn().getBody().toString();
 
-    JsonNode uebAsJson =null;
+    JsonNode uebAsJson = null;
     ObjectMapper mapper = new ObjectMapper();
     try{
       uebAsJson = mapper.readTree(uebPayload);
@@ -251,7 +254,7 @@ public class EntityEventPolicy implements Processor {
       return;
     }
 
-    DynamicJAXBContext oxmJaxbContext = loadOxmContext(oxmVersion.toLowerCase());
+    DynamicJAXBContext oxmJaxbContext = loadOxmContext(oxmVersion);
     if (oxmJaxbContext == null) {
       logger.error(EntityEventPolicyMsgs.OXM_VERSION_NOT_SUPPORTED, oxmVersion);
       logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE, "OXM version mismatch",
@@ -313,11 +316,7 @@ public class EntityEventPolicy implements Processor {
 
 
     // Process for building AaiEventEntity object
-    String[] entityTypeArr = entityType.split("-");
-    String oxmEntityType = "";
-    for (String entityWord : entityTypeArr) {
-      oxmEntityType += entityWord.substring(0, 1).toUpperCase() + entityWord.substring(1);
-    }
+    String oxmEntityType = new OxmEntityTypeConverter().convert(entityType);
 
     List<String> searchableAttr =
         getOxmAttributes(uebPayload, oxmJaxbContext, oxmEntityType, entityType, "searchable");
@@ -389,7 +388,7 @@ public class EntityEventPolicy implements Processor {
      * Use the versioned OXM Entity class to get access to cross-entity reference helper collections
      */
     VersionedOxmEntities oxmEntities =
-        EntityOxmReferenceHelper.getInstance().getVersionedOxmEntities(Version.valueOf(oxmVersion));
+        EntityOxmReferenceHelper.getInstance().getVersionedOxmEntities(Version.valueOf(oxmVersion.toLowerCase()));
 
     /**
      * NOTES:
@@ -412,7 +411,8 @@ public class EntityEventPolicy implements Processor {
 
       JSONObject entityJsonObject = getUebEntity(uebPayload);
 
-      JsonNode entityJsonNode = convertToJsonNode(entityJsonObject.toString());
+      JsonNode entityJsonNode = 
+                 convertToJsonNode(entityJsonObject !=null ? entityJsonObject.toString() : "");
       
       String parentEntityType = entityType;
       
@@ -909,7 +909,7 @@ public class EntityEventPolicy implements Processor {
         if (!sourceObject.isEmpty()) {
           JsonNode node = sourceObject.get(0);
           final String sourceCer = NodeUtils.extractFieldValueFromObject(node, 
-              "crossEntityReferenceValues");
+              "crossReferenceEntityValues");
           String newCer = aaiEventEntity.getCrossReferenceEntityValues();
           boolean hasNewCer = true;
           if (sourceCer != null && sourceCer.length() > 0){ // already has CER
@@ -922,7 +922,7 @@ public class EntityEventPolicy implements Processor {
           
           if (hasNewCer){
             // Do the PUT with new CER
-            ((ObjectNode)node).put("crossEntityReferenceValues", newCer);
+            ((ObjectNode)node).put("crossReferenceEntityValues", newCer);
             jsonPayload = NodeUtils.convertObjectToJson(node, false);
             searchAgent.putDocument(entitySearchIndex, entityId, jsonPayload, headers);
           }
@@ -1000,6 +1000,13 @@ public class EntityEventPolicy implements Processor {
             logger.error(EntityEventPolicyMsgs.NO_ETAG_AVAILABLE_FAILURE, index,
                 entityId);
           }
+          
+          /*
+           * The Spring-Boot version of the search-data-service rejects the DELETE operation unless
+           * we specify a Content-Type.
+           */
+
+          headers.put("Content-Type", Arrays.asList(MediaType.APPLICATION_JSON.getMediaType()));
 
           searchAgent.deleteDocument(index, eventEntity.getId(), headers);
         } else {