add the gap event transformer 71/55071/2
authorrenealr <reneal.rogers@amdocs.com>
Tue, 19 Jun 2018 14:50:41 +0000 (10:50 -0400)
committerrenealr <reneal.rogers@amdocs.com>
Tue, 19 Jun 2018 15:40:30 +0000 (11:40 -0400)
add the gap event transformer and add stub data for test cases

Issue-ID: AAI-1230

Change-Id: I95f6f050ca4d2d7ea3a288e05f69c6be66a9222b
Signed-off-by: renealr <reneal.rogers@amdocs.com>
src/main/bin/start.sh
src/main/java/org/onap/aai/datarouter/policy/GapEventTransformer.java [new file with mode: 0644]
src/test/java/org/onap/aai/datarouter/entity/SuggestionSearchEntityTest.java [moved from src/test/java/org/openecomp/datarouter/entity/SuggestionSearchEntityTest.java with 99% similarity]
src/test/java/org/onap/aai/datarouter/policy/GapEventTransformerTest.java [new file with mode: 0644]
src/test/resources/gap_event.json [new file with mode: 0644]
src/test/resources/gap_event_wrong.json [new file with mode: 0644]

index ced887a..bb219b8 100644 (file)
@@ -2,7 +2,7 @@
 
 BASEDIR="/opt/app/data-router/"
 AJSC_HOME="$BASEDIR"
-AJSC_CONF_HOME="$AJSC_HOME/bundleconfig/"
+
 
 if [ -z "$CONFIG_HOME" ]; then
         echo "CONFIG_HOME must be set in order to start up process"
@@ -12,35 +12,15 @@ fi
 if [ -z "$KEY_STORE_PASSWORD" ]; then
        echo "KEY_STORE_PASSWORD must be set in order to start up process"
        exit 1
-else
-       ## Extract java jar to DEOBFUSCATE the password.
-       CURR_D=`pwd`
-       cd $BASEDIR
-       jar xf data-router.jar
-       sudo java -cp ./BOOT-INF/lib/jetty-util-9.4.8.v20171121.jar org.eclipse.jetty.util.security.Password $KEY_STORE_PASSWORD > pass.txt 2>> pass.txt
-       PASS=`sed "2q;d" pass.txt`
-       sudo rm pass.txt
-       cd $CURR_D
 fi
 
-## tomcat_keystore to p12
-keytool -importkeystore -noprompt -deststorepass $PASS -destkeypass $PASS -srckeystore $BASEDIR/config/auth/tomcat_keystore -destkeystore $BASEDIR/config/auth/onap.p12 -deststoretype PKCS12 -srcstorepass $PASS
-
-## import into cacerts
-sudo keytool -importkeystore -noprompt -deststorepass changeit -destkeypass changeit -destkeystore /$JAVA_HOME/jre/lib/security/cacerts -srckeystore $BASEDIR/config/auth/onap.p12 -srcstoretype PKCS12 -srcstorepass $PASS -alias tomcat
-
-PROPS="-DBASEDIR=$BASEDIR"
 PROPS="-DAJSC_HOME=$AJSC_HOME"
-PROPS="$PROPS -DAJSC_CONF_HOME=$AJSC_CONF_HOME"
 PROPS="$PROPS -Dlogging.config=$BASEDIR/bundleconfig/etc/logback.xml"
-PROPS="$PROPS -DAJSC_SHARED_CONFIG=$AJSC_CONF_HOME"
-PROPS="$PROPS -DAJSC_SERVICE_NAMESPACE=data-router"
-PROPS="$PROPS -DAJSC_SERVICE_VERSION=v1"
-PROPS="$PROPS -Dserver.port=9502"
 PROPS="$PROPS -DCONFIG_HOME=$CONFIG_HOME"
-JVM_MAX_HEAP=${MAX_HEAP:-1024}
+PROPS="$PROPS -DKEY_STORE_PASSWORD=$KEY_STORE_PASSWORD"
+
 
-echo $CLASSPATH
+JVM_MAX_HEAP=${MAX_HEAP:-1024}
 
 cd ${MICRO_HOME}
 jar uf0 $MICRO_HOME/data-router.jar BOOT-INF/lib/*
diff --git a/src/main/java/org/onap/aai/datarouter/policy/GapEventTransformer.java b/src/main/java/org/onap/aai/datarouter/policy/GapEventTransformer.java
new file mode 100644 (file)
index 0000000..6565fc2
--- /dev/null
@@ -0,0 +1,105 @@
+/**
+ * ============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.datarouter.policy;
+
+import java.io.FileNotFoundException;
+
+import org.apache.camel.Exchange;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.onap.aai.cl.api.Logger;
+import org.onap.aai.cl.eelf.LoggerFactory;
+import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
+
+
+
+public class GapEventTransformer {
+  protected Logger logger;
+  protected static final String additionalInfo = "Response of GapEvent to transform";
+
+  protected enum ResponseType {
+    SUCCESS, PARTIAL_SUCCESS, FAILURE;
+  }
+
+  public GapEventTransformer() throws FileNotFoundException {
+    LoggerFactory loggerFactoryInstance = LoggerFactory.getInstance();
+    logger = loggerFactoryInstance.getLogger(AbstractSpikeEntityEventProcessor.class.getName());
+  }
+
+
+  public void process(Exchange exchange) throws Exception {
+
+    String payload = getExchangeBody(exchange);
+    JSONObject newPayload = transformToSpikePattern(payload);
+    exchange.getOut().setBody(newPayload.toString());
+
+  }
+
+  protected JSONObject transformToSpikePattern(String payload) {
+    JSONObject payloadJson = new JSONObject(payload);
+    JSONObject payloadEntity = payloadJson.getJSONObject("entity");
+    JSONObject payloadProperties = payloadEntity.getJSONObject("properties");
+    String payloadId = payloadEntity.getString("id");
+    String payloadType = payloadEntity.getString("type");
+
+    JSONObject newPayload = new JSONObject();
+    newPayload.put("operation", "UPDATE");
+    JSONObject newPayloadVertex = new JSONObject();
+    newPayloadVertex.put("key", payloadId);
+    newPayloadVertex.put("type", payloadType);
+    newPayloadVertex.put("properties", payloadProperties);
+    newPayload.put("vertex", newPayloadVertex);
+    return newPayload;
+  }
+
+  protected String getExchangeBody(Exchange exchange) {
+    String uebPayload = exchange.getIn().getBody().toString();
+    if (uebPayload == null || !isJSONValid(uebPayload)) {
+      uebPayload = exchange.getIn().getBody(String.class);
+      if (uebPayload == null || !isJSONValid(uebPayload)) {
+        returnWithError(exchange, uebPayload, "Invalid Payload");
+        return null;
+      }
+    }
+    return uebPayload;
+  }
+
+  private boolean isJSONValid(String test) {
+    try {
+      new JSONObject(test);
+    } catch (JSONException ex) {
+      return false;
+    }
+    return true;
+  }
+
+  protected void returnWithError(Exchange exchange, String payload, String errorMsg) {
+    logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE, errorMsg);
+    logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE, errorMsg, payload);
+    setResponse(exchange, ResponseType.FAILURE, additionalInfo);
+  }
+
+  protected void setResponse(Exchange exchange, ResponseType responseType, String additionalInfo) {
+
+    exchange.getOut().setHeader("ResponseType", responseType.toString());
+    exchange.getOut().setBody(additionalInfo);
+  }
+}
@@ -18,7 +18,7 @@
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
-package org.openecomp.datarouter.entity;
+package org.onap.aai.datarouter.entity;
 
 import static org.junit.Assert.assertEquals;
 
diff --git a/src/test/java/org/onap/aai/datarouter/policy/GapEventTransformerTest.java b/src/test/java/org/onap/aai/datarouter/policy/GapEventTransformerTest.java
new file mode 100644 (file)
index 0000000..355756b
--- /dev/null
@@ -0,0 +1,68 @@
+/**
+ * ============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.datarouter.policy;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.FileInputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
+
+
+
+public class GapEventTransformerTest {
+  GapEventTransformer policy;
+  String eventJson;
+  String eventJson2;
+
+  @SuppressWarnings("unchecked")
+  @Before
+  public void init() throws Exception {
+    policy = new GapEventTransformer();
+    FileInputStream event = new FileInputStream(new File("src/test/resources/gap_event.json"));
+    eventJson = IOUtils.toString(event, "UTF-8");
+    FileInputStream event2 =
+        new FileInputStream(new File("src/test/resources/gap_event_wrong.json"));
+    eventJson2 = IOUtils.toString(event2, "UTF-8");
+
+  }
+
+  @Test
+  public void testTransform_success() throws Exception {
+    JSONObject newPayloadJson = policy.transformToSpikePattern(eventJson.toString());
+    JSONObject payloadEntity = newPayloadJson.getJSONObject("vertex");
+    assertTrue(newPayloadJson.has("vertex"));
+    assertTrue(newPayloadJson.has("operation"));
+    assertEquals(newPayloadJson.get("operation"), "UPDATE");
+
+
+  }
+
+  @Test(expected = Exception.class)
+  public void testTransform_badPayload_fail() throws Exception {
+    policy.transformToSpikePattern(eventJson2.toString());
+  }
+
+}
diff --git a/src/test/resources/gap_event.json b/src/test/resources/gap_event.json
new file mode 100644 (file)
index 0000000..05e8fc3
--- /dev/null
@@ -0,0 +1,50 @@
+{  
+   "event-header":{  
+      "entity-type":"pserver",
+      "top-entity-type":"pserver",
+      "entity-link":"/services/inventory/v11/pservers?properties=all",
+      "event-type":"${crud.event.type.api}",
+      "domain":"dev",
+      "topic-name":"CRUD-DATA-EXPORT-SDWAN",
+      "event-id":"7445ecab-40fc-46a9-a595-42d940488b02"
+   },
+   "entity":{  
+      "in":[  
+         {  
+            "id":"9387d038-1e4f-4a08-ac5e-97c09530bbd8",
+            "source":"services/inventory/v11/vserver/ad7c5ad8-142f-4cd1-b38a-11fd7d98239d",
+            "type":"tosca.relationships.HostedOn",
+            "url":"services/inventory/relationships/v12/tosca.relationships.HostedOn/9387d038-1e4f-4a08-ac5e-97c09530bbd8"
+         }
+      ],
+      "id":"441d5a1e-a107-4fc3-b885-ccf683e3513b",
+      "type":"pserver",
+      "url":"services/inventory/v11/pserver/441d5a1e-a107-4fc3-b885-ccf683e3513b",
+      "properties":{  
+         "aai-last-mod-ts":"1522935511411",
+         "equip-type":"Rack Server",
+         "equip-vendor":"HP",
+         "serial-number":"S4MPLEDDD456D",
+         "pserver-id":"Pserver-AMT-8",
+         "aai-created-ts":"1522935511411",
+         "source-of-truth":"RO",
+         "number-of-cpus":10,
+         "aai-node-type":"pserver",
+         "hostname":"10_aaingenvnf-pserver-fw-8",
+         "equip-model":"PowerEdge R930",
+         "in-maint":false,
+         "resource-version":"64684545",
+         "disk-in-gigabytes":146,
+         "ram-in-megabytes":62914556,
+         "last-mod-source-of-truth":"RO"
+      },
+      "out":[  
+         {  
+            "id":"bfa7ec57-cfa6-4bda-b1dc-a11a192bacaa",
+            "type":"org.onap.relationships.inventory.LocatedIn",
+            "url":"services/inventory/relationships/v12/org.onap.relationships.inventory.LocatedIn/bfa7ec57-cfa6-4bda-b1dc-a11a192bacaa",
+            "target":"services/inventory/v11/complex/90564f64-d3c4-47f8-847d-553497f6b04a"
+         }
+      ]
+   }
+}
\ No newline at end of file
diff --git a/src/test/resources/gap_event_wrong.json b/src/test/resources/gap_event_wrong.json
new file mode 100644 (file)
index 0000000..bf4135f
--- /dev/null
@@ -0,0 +1,50 @@
+{  
+   "event-header":{  
+      "entity-type":"pserver",
+      "top-entity-type":"pserver",
+      "entity-link":"/services/inventory/v11/pservers?properties=all",
+      "event-type":"${crud.event.type.api}",
+      "domain":"dev",
+      "topic-name":"CRUD-DATA-EXPORT-SDWAN",
+      "event-id":"7445ecab-40fc-46a9-a595-42d940488b02"
+   },
+   "entity2":{  
+      "in":[  
+         {  
+            "id":"9387d038-1e4f-4a08-ac5e-97c09530bbd8",
+            "source":"services/inventory/v11/vserver/ad7c5ad8-142f-4cd1-b38a-11fd7d98239d",
+            "type":"tosca.relationships.HostedOn",
+            "url":"services/inventory/relationships/v12/tosca.relationships.HostedOn/9387d038-1e4f-4a08-ac5e-97c09530bbd8"
+         }
+      ],
+      "id":"441d5a1e-a107-4fc3-b885-ccf683e3513b",
+      "type":"pserver",
+      "url":"services/inventory/v11/pserver/441d5a1e-a107-4fc3-b885-ccf683e3513b",
+      "properties":{  
+         "aai-last-mod-ts":"1522935511411",
+         "equip-type":"Rack Server",
+         "equip-vendor":"HP",
+         "serial-number":"S4MPLEDDD456D",
+         "pserver-id":"Pserver-AMT-8",
+         "aai-created-ts":"1522935511411",
+         "source-of-truth":"RO",
+         "number-of-cpus":10,
+         "aai-node-type":"pserver",
+         "hostname":"10_aaingenvnf-pserver-fw-8",
+         "equip-model":"PowerEdge R930",
+         "in-maint":false,
+         "resource-version":"64684545",
+         "disk-in-gigabytes":146,
+         "ram-in-megabytes":62914556,
+         "last-mod-source-of-truth":"RO"
+      },
+      "out":[  
+         {  
+            "id":"bfa7ec57-cfa6-4bda-b1dc-a11a192bacaa",
+            "type":"org.onap.relationships.inventory.LocatedIn",
+            "url":"services/inventory/relationships/v12/org.onap.relationships.inventory.LocatedIn/bfa7ec57-cfa6-4bda-b1dc-a11a192bacaa",
+            "target":"services/inventory/v11/complex/90564f64-d3c4-47f8-847d-553497f6b04a"
+         }
+      ]
+   }
+}
\ No newline at end of file