PnfReadyEventConsumer implementation

Change-Id: Ia4d9fedb023ef6893e887ff6b5802623649aeeba
Issue-ID: SO-466
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
diff --git a/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/PnfReadyEventHandler.java b/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/PnfReadyEventHandler.java
deleted file mode 100644
index f89b6a7..0000000
--- a/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/PnfReadyEventHandler.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package org.openecomp.mso.bpmn.infrastructure.scripts;
-
-import org.camunda.bpm.engine.delegate.DelegateExecution;
-import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
-import org.openecomp.mso.client.dmaap.DmaapConsumer;
-import org.openecomp.mso.client.sdno.dmaap.PnfReadyEventConsumer;
-
-public class PnfReadyEventHandler {
-
-    private ExceptionUtil exceptionUtil;
-
-    private static final String TOPIC_NAME = "VES event";
-
-    public PnfReadyEventHandler() {
-        exceptionUtil = new ExceptionUtil();
-    }
-
-    public void getPnfReadyEventFromDmaap (DelegateExecution execution) throws Exception {
-        Object correlationIdVar = execution.getVariable("correlationId");
-        if (!(correlationIdVar instanceof String)) {
-            exceptionUtil.buildAndThrowWorkflowException(execution, 500, "correlationId variable is not String type");
-        }
-        String correlationId = (String) correlationIdVar;
-        DmaapConsumer dmaapConsumer = new PnfReadyEventConsumer(correlationId);
-        dmaapConsumer.consume();
-        // TODO inform camunda process that event has been received
-    }
-}
diff --git a/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyConsumer.java b/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyConsumer.java
new file mode 100644
index 0000000..e6019f7
--- /dev/null
+++ b/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyConsumer.java
@@ -0,0 +1,96 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.openecomp.mso.bpmn.infrastructure.pnf.dmaap;
+
+import java.io.IOException;
+import java.net.URI;
+import javax.ws.rs.core.UriBuilder;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.HttpClientBuilder;
+
+public class PnfEventReadyConsumer {
+
+    private static final String JSON_PATH_CORRELATION_ID = "$.pnfRegistrationFields.correlationId";
+    private HttpClient httpClient;
+
+    private String dmaapHost;
+    private int dmaapPort;
+    private String dmaapProtocol;
+    private String dmaapUriPathPrefix;
+    private String dmaapTopicName;
+    private String consumerId;
+    private String consumerGroup;
+
+    public PnfEventReadyConsumer() {
+        httpClient = HttpClientBuilder.create().build();
+    }
+
+    public void notifyWhenPnfReady(String correlationId)
+            throws IOException {
+        HttpGet getRequest = new HttpGet(buildURI(consumerGroup, consumerId));
+        HttpResponse response = httpClient.execute(getRequest);
+        checkIfResponseIsAccepted(response, correlationId);
+    }
+
+    private boolean checkIfResponseIsAccepted(HttpResponse response, String correlationId) {
+        // TODO parse response if contains proper correlationId
+        return false;
+    }
+
+    private URI buildURI(String consumerGroup, String consumerId) {
+        return UriBuilder.fromUri(dmaapUriPathPrefix)
+                .scheme(dmaapProtocol)
+                .host(dmaapHost)
+                .port(dmaapPort).path(dmaapTopicName)
+                .path(consumerGroup).path(consumerId).build();
+    }
+
+    public void setDmaapHost(String dmaapHost) {
+        this.dmaapHost = dmaapHost;
+    }
+
+    public void setDmaapPort(int dmaapPort) {
+        this.dmaapPort = dmaapPort;
+    }
+
+    public void setDmaapProtocol(String dmaapProtocol) {
+        this.dmaapProtocol = dmaapProtocol;
+    }
+
+    public void setDmaapUriPathPrefix(String dmaapUriPathPrefix) {
+        this.dmaapUriPathPrefix = dmaapUriPathPrefix;
+    }
+
+    public void setDmaapTopicName(String dmaapTopicName) {
+        this.dmaapTopicName = dmaapTopicName;
+    }
+
+    public void setConsumerId(String consumerId) {
+        this.consumerId = consumerId;
+    }
+
+    public void setConsumerGroup(String consumerGroup) {
+        this.consumerGroup = consumerGroup;
+    }
+
+}
diff --git a/bpmn/MSOInfrastructureBPMN/src/main/resources/dmaap.properties b/bpmn/MSOInfrastructureBPMN/src/main/resources/dmaap.properties
new file mode 100644
index 0000000..3c4dca4
--- /dev/null
+++ b/bpmn/MSOInfrastructureBPMN/src/main/resources/dmaap.properties
@@ -0,0 +1,7 @@
+dmaapHost=HOSTNAME
+dmaapPort=3905
+dmaapProtocol=http
+dmaapUriPathPrefix = events
+eventReadyTopicName=pnfEventReady
+consumerId=consumerId
+consumerGroup=group
diff --git a/bpmn/MSOInfrastructureBPMN/src/main/resources/processengine.properties b/bpmn/MSOInfrastructureBPMN/src/main/resources/processengine.properties
deleted file mode 100644
index d071fda..0000000
--- a/bpmn/MSOInfrastructureBPMN/src/main/resources/processengine.properties
+++ /dev/null
@@ -1,20 +0,0 @@
-###
-# ============LICENSE_START=======================================================
-# ECOMP MSO
-# ================================================================================
-# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
-# ================================================================================
-# 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=========================================================
-###
-processEngineName=infrastructure
diff --git a/bpmn/MSOInfrastructureBPMN/src/main/webapp/WEB-INF/applicationContext.xml b/bpmn/MSOInfrastructureBPMN/src/main/webapp/WEB-INF/applicationContext.xml
index 46cea5d..f46cd06 100644
--- a/bpmn/MSOInfrastructureBPMN/src/main/webapp/WEB-INF/applicationContext.xml
+++ b/bpmn/MSOInfrastructureBPMN/src/main/webapp/WEB-INF/applicationContext.xml
@@ -14,4 +14,18 @@
     <property name="aaiConnection" ref="aaiConnection"/>

   </bean>

 

+  <bean id="pnfEventReadyConsumer" class="org.openecomp.mso.bpmn.infrastructure.pnf.dmaap.PnfEventReadyConsumer">

+    <property name="dmaapHost" value="${dmaapHost}" />

+    <property name="dmaapPort" value="${dmaapPort}"/>

+    <property name="dmaapProtocol" value="${dmaapProtocol}"/>

+    <property name="dmaapUriPathPrefix" value="${dmaapUriPathPrefix}"/>

+    <property name="dmaapTopicName" value="${eventReadyTopicName}"/>

+    <property name= "consumerGroup" value="${consumerGroup}"/>

+    <property name="consumerId" value="${consumerId}"/>

+  </bean>

+

+  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

+    <property name="locations" value="classpath:dmaap.properties"/>

+  </bean>

+

 </beans>

diff --git a/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyConsumerTest.java b/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyConsumerTest.java
new file mode 100644
index 0000000..2f6a00d
--- /dev/null
+++ b/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyConsumerTest.java
@@ -0,0 +1,68 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.openecomp.mso.bpmn.infrastructure.pnf.dmaap;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.lang.reflect.Field;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@ContextConfiguration({"classpath:springConfig_PnfEventReadyConsumer.xml"})
+public class PnfEventReadyConsumerTest {
+
+    @Autowired
+    private PnfEventReadyConsumer pnfEventReadyConsumer;
+
+    private HttpClient httpClientMock;
+
+    @Before
+    public void init() throws NoSuchFieldException, IllegalAccessException {
+        httpClientMock = mock(HttpClient.class);
+        setPrivateField();
+    }
+
+    @Test
+    public void restClientInvokesWithProperURI() throws Exception {
+        ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
+        pnfEventReadyConsumer.notifyWhenPnfReady("correlationId");
+        verify(httpClientMock).execute(captor1.capture());
+        assertThat(captor1.getValue().getURI()).hasHost("hostTest").hasPort(1234).hasScheme("http")
+                .hasPath("/eventsForTesting/eventTopicTest/consumerGroupTest/consumerTestId");
+    }
+
+    private void setPrivateField() throws NoSuchFieldException, IllegalAccessException {
+        Field field = pnfEventReadyConsumer.getClass().getDeclaredField("httpClient");
+        field.setAccessible(true);
+        field.set(pnfEventReadyConsumer, httpClientMock);
+    }
+
+}
diff --git a/bpmn/MSOInfrastructureBPMN/src/test/resources/dmaapTest.properties b/bpmn/MSOInfrastructureBPMN/src/test/resources/dmaapTest.properties
new file mode 100644
index 0000000..a8df15c
--- /dev/null
+++ b/bpmn/MSOInfrastructureBPMN/src/test/resources/dmaapTest.properties
@@ -0,0 +1,7 @@
+dmaapHost=hostTest
+dmaapPort=1234
+dmaapProtocol=http
+dmaapUriPathPrefix = eventsForTesting
+eventReadyTopicName=eventTopicTest
+consumerId=consumerTestId
+consumerGroup=consumerGroupTest
\ No newline at end of file
diff --git a/bpmn/MSOInfrastructureBPMN/src/test/resources/springConfig_PnfEventReadyConsumer.xml b/bpmn/MSOInfrastructureBPMN/src/test/resources/springConfig_PnfEventReadyConsumer.xml
new file mode 100644
index 0000000..5abee9d
--- /dev/null
+++ b/bpmn/MSOInfrastructureBPMN/src/test/resources/springConfig_PnfEventReadyConsumer.xml
@@ -0,0 +1,19 @@
+<beans xmlns="http://www.springframework.org/schema/beans"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://www.springframework.org/schema/beans
+                         http://www.springframework.org/schema/beans/spring-beans.xsd">
+  <bean id="pnfEventReadyConsumer" class="org.openecomp.mso.bpmn.infrastructure.pnf.dmaap.PnfEventReadyConsumer">
+    <property name="dmaapHost" value="${dmaapHost}" />
+    <property name="dmaapPort" value="${dmaapPort}"/>
+    <property name="dmaapProtocol" value="${dmaapProtocol}"/>
+    <property name="dmaapUriPathPrefix" value="${dmaapUriPathPrefix}"/>
+    <property name="dmaapTopicName" value="${eventReadyTopicName}"/>
+    <property name= "consumerGroup" value="${consumerGroup}"/>
+    <property name="consumerId" value="${consumerId}"/>
+  </bean>
+
+  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+    <property name="locations" value="classpath:dmaapTest.properties"/>
+  </bean>
+
+</beans>