Move common code to router-core from DR
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / policy / EntityEventPolicy.java
index f92eb17..6276abd 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017 Amdocs
+ * 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.
@@ -17,8 +17,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
 package org.onap.aai.datarouter.policy;
 
@@ -39,26 +37,27 @@ 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.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;
@@ -76,20 +75,20 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
 public class EntityEventPolicy implements Processor {
 
   public static final String additionalInfo = "Response of AAIEntityEventPolicy";
-  private static final String entitySearchSchema = "entitysearch_schema.json";
-  private static final String topographicalSearchSchema   = "topographysearch_schema.json";
+  private static final String ENTITY_SEARCH_SCHEMA = "entitysearch_schema.json";
+  private static final String TOPOGRAPHICAL_SEARCH_SCHEMA = "topographysearch_schema.json";
   private Collection<ExternalOxmModelProcessor> externalOxmModelProcessors;
 
-  private final String EVENT_HEADER = "event-header";
-  private final String ENTITY_HEADER = "entity";
-  private final String ACTION_CREATE = "create";
-  private final String ACTION_DELETE = "delete";
-  private final String ACTION_UPDATE = "update";
-  private final String PROCESS_AAI_EVENT = "Process AAI Event";
-  private final String TOPO_LAT = "latitude";
-  private final String TOPO_LONG = "longitude";
+  private static final String EVENT_HEADER = "event-header";
+  private static final String ENTITY_HEADER = "entity";
+  private static final String ACTION_CREATE = "create";
+  private static final String ACTION_DELETE = "delete";
+  private static final String ACTION_UPDATE = "update";
+  private static final String PROCESS_AAI_EVENT = "Process AAI Event";
+  private static final String TOPO_LAT = "latitude";
+  private static final String TOPO_LONG = "longitude";
 
-  private final List<String> SUPPORTED_ACTIONS =
+  private static final List<String> SUPPORTED_ACTIONS =
       Arrays.asList(ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE);
 
   Map<String, DynamicJAXBContext> oxmVersionContextMap = new HashMap<>();
@@ -130,6 +129,7 @@ public class EntityEventPolicy implements Processor {
     entitySearchIndex        = config.getSearchEntitySearchIndex();
     topographicalSearchIndex = config.getSearchTopographySearchIndex();
     aggregateGenericVnfIndex = config.getSearchAggregationVnfIndex();
+    autosuggestIndex            = config.getSearchEntityAutoSuggestIndex();
        
     // Instantiate the agent that we will use for interacting with the Search Service.
     searchAgent = new SearchServiceAgent(config.getSearchCertName(),
@@ -173,8 +173,8 @@ public class EntityEventPolicy implements Processor {
   public void startup() {
     
     // Create the indexes in the search service if they do not already exist.
-    searchAgent.createSearchIndex(entitySearchIndex, entitySearchSchema);
-    searchAgent.createSearchIndex(topographicalSearchIndex, topographicalSearchSchema);
+    searchAgent.createSearchIndex(entitySearchIndex, ENTITY_SEARCH_SCHEMA);
+    searchAgent.createSearchIndex(topographicalSearchIndex, TOPOGRAPHICAL_SEARCH_SCHEMA);
     
     logger.info(EntityEventPolicyMsgs.ENTITY_EVENT_POLICY_REGISTERED);
   }
@@ -203,8 +203,8 @@ public class EntityEventPolicy implements Processor {
   }
 
   public void returnWithError(Exchange exchange, String payload, String errorMsg){
-    logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE, errorMsg);
-    logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE, errorMsg, payload);
+    logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE, errorMsg);
+    logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE, errorMsg, payload);
     setResponse(exchange, ResponseType.FAILURE, additionalInfo);
   }
   
@@ -243,19 +243,19 @@ public class EntityEventPolicy implements Processor {
     // Get src domain from header; discard event if not originated from same domain
     String payloadSrcDomain = eventHeader.getDomain();
     if (payloadSrcDomain == null || !payloadSrcDomain.equalsIgnoreCase(this.srcDomain)) {
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Unrecognized source domain '" + payloadSrcDomain + "'", uebPayload);
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Unrecognized source domain '" + payloadSrcDomain + "'");
 
       setResponse(exchange, ResponseType.SUCCESS, additionalInfo);
       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_AAI_EVENT_VERBOSE, "OXM version mismatch",
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE, "OXM version mismatch",
           uebPayload);
 
       setResponse(exchange, ResponseType.FAILURE, additionalInfo);
@@ -264,9 +264,9 @@ public class EntityEventPolicy implements Processor {
 
     String action = eventHeader.getAction();
     if (action == null || !SUPPORTED_ACTIONS.contains(action.toLowerCase())) {
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Unrecognized action '" + action + "'", uebPayload);
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Unrecognized action '" + action + "'");
 
       setResponse(exchange, ResponseType.FAILURE, additionalInfo);
@@ -275,9 +275,9 @@ public class EntityEventPolicy implements Processor {
 
     String entityType = eventHeader.getEntityType();
     if (entityType == null) {
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Payload header missing entity type", uebPayload);
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Payload header missing entity type");
 
       setResponse(exchange, ResponseType.FAILURE, additionalInfo);
@@ -286,9 +286,9 @@ public class EntityEventPolicy implements Processor {
 
     String topEntityType = eventHeader.getTopEntityType();
     if (topEntityType == null) {
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Payload header missing top entity type", uebPayload);
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Payload header top missing entity type");
 
       setResponse(exchange, ResponseType.FAILURE, additionalInfo);
@@ -297,9 +297,9 @@ public class EntityEventPolicy implements Processor {
 
     String entityLink = eventHeader.getEntityLink();
     if (entityLink == null) {
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Payload header missing entity link", uebPayload);
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Payload header missing entity link");
 
       setResponse(exchange, ResponseType.FAILURE, additionalInfo);
@@ -307,25 +307,21 @@ public class EntityEventPolicy implements Processor {
     }
 
     // log the fact that all data are in good shape
-    logger.info(EntityEventPolicyMsgs.PROCESS_AAI_ENTITY_EVENT_POLICY_NONVERBOSE, action,
+    logger.info(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_NONVERBOSE, action,
         entityType);
-    logger.debug(EntityEventPolicyMsgs.PROCESS_AAI_ENTITY_EVENT_POLICY_VERBOSE, action, entityType,
+    logger.debug(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_VERBOSE, action, entityType,
         uebPayload);
 
 
     // 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");
     if (searchableAttr == null) {
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Searchable attribute not found for payload entity type '" + entityType + "'");
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Searchable attribute not found for payload entity type '" + entityType + "'",
           uebPayload);
 
@@ -337,9 +333,9 @@ public class EntityEventPolicy implements Processor {
         getEntityPrimaryKeyFieldName(oxmJaxbContext, uebPayload, oxmEntityType, entityType);
     String entityPrimaryKeyFieldValue = lookupValueUsingKey(uebPayload, entityPrimaryKeyFieldName);
     if (entityPrimaryKeyFieldValue == null || entityPrimaryKeyFieldValue.isEmpty()) {
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Payload missing primary key attribute");
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Payload missing primary key attribute", uebPayload);
 
       setResponse(exchange, ResponseType.FAILURE, additionalInfo);
@@ -358,9 +354,9 @@ public class EntityEventPolicy implements Processor {
     aaiEventEntity.setLink(entityLink);
 
     if (!getSearchTags(aaiEventEntity, searchableAttr, uebPayload, action)) {
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Payload missing searchable attribute for entity type '" + entityType + "'");
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Payload missing searchable attribute for entity type '" + entityType + "'", uebPayload);
 
       setResponse(exchange, ResponseType.FAILURE, additionalInfo);
@@ -372,9 +368,9 @@ public class EntityEventPolicy implements Processor {
       aaiEventEntity.deriveFields();
 
     } catch (NoSuchAlgorithmException e) {
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Cannot create unique SHA digest");
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
           "Cannot create unique SHA digest", uebPayload);
 
       setResponse(exchange, ResponseType.FAILURE, additionalInfo);
@@ -390,7 +386,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:
@@ -507,7 +503,7 @@ public class EntityEventPolicy implements Processor {
                     updateCerInEntity(entityToSync);
 
                   } catch (NoSuchAlgorithmException e) {
-                    e.printStackTrace();
+                    logger.debug(e.getMessage());
                   }
                 }
               } else {
@@ -814,9 +810,9 @@ public class EntityEventPolicy implements Processor {
     try {
       uebJsonObj = new JSONObject(payload);
     } catch (JSONException e) {
-      logger.debug(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_VERBOSE,
-          "Payload has invalid JSON Format", payload.toString());
-      logger.error(EntityEventPolicyMsgs.DISCARD_AAI_EVENT_NONVERBOSE,
+      logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE,
+          "Payload has invalid JSON Format", payload);
+      logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE,
           "Payload has invalid JSON Format");
       return null;
     }
@@ -824,8 +820,7 @@ public class EntityEventPolicy implements Processor {
     if (uebJsonObj.has(ENTITY_HEADER)) {
       return uebJsonObj.getJSONObject(ENTITY_HEADER);
     } else {
-      logger.debug(EntityEventPolicyMsgs.FAILED_TO_PARSE_UEB_PAYLOAD, ENTITY_HEADER + " missing",
-          payload.toString());
+      logger.debug(EntityEventPolicyMsgs.FAILED_TO_PARSE_UEB_PAYLOAD, ENTITY_HEADER + " missing", payload);
       logger.error(EntityEventPolicyMsgs.FAILED_TO_PARSE_UEB_PAYLOAD, ENTITY_HEADER + " missing");
       return null;
     }
@@ -847,8 +842,6 @@ public class EntityEventPolicy implements Processor {
         primaryKeyValues.add(pkeyValue);
         primaryKeyNames.add(keyName);
       } else {
-        // logger.warn("getPopulatedDocument(), pKeyValue is null for entityType = " +
-        // resultDescriptor.getEntityName());
         logger.error(EntityEventPolicyMsgs.PRIMARY_KEY_NULL_FOR_ENTITY_TYPE,
             resultDescriptor.getEntityName());
       }
@@ -902,7 +895,7 @@ public class EntityEventPolicy implements Processor {
           headers.put(Headers.IF_MATCH, etag);
         } else {
           logger.error(EntityEventPolicyMsgs.NO_ETAG_AVAILABLE_FAILURE,
-                         entitySearchIndex, entityId);
+                  entitySearchIndex, entityId);
         }
         
         ArrayList<JsonNode> sourceObject = new ArrayList<>();
@@ -967,8 +960,6 @@ public class EntityEventPolicy implements Processor {
 
       String entityId = eventEntity.getId();
 
-      // System.out.println("aaiEventEntity as json = " + aaiEventEntity.getAsJson());
-
       if ((action.equalsIgnoreCase(ACTION_CREATE) && entityId != null)
           || action.equalsIgnoreCase(ACTION_UPDATE)) {
 
@@ -1006,6 +997,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 {