From 8e3ee2eab19c9b2db2423567418ab28b65ff8aec Mon Sep 17 00:00:00 2001 From: Lukasz Muszkieta Date: Fri, 6 Apr 2018 14:50:21 +0200 Subject: PnfReadyEventConsumer implementation Change-Id: Ia4d9fedb023ef6893e887ff6b5802623649aeeba Issue-ID: SO-466 Signed-off-by: Lukasz Muszkieta --- .../scripts/PnfReadyEventHandler.java | 28 ------- .../pnf/dmaap/PnfEventReadyConsumer.java | 96 ++++++++++++++++++++++ .../src/main/resources/dmaap.properties | 7 ++ .../src/main/resources/processengine.properties | 20 ----- .../src/main/webapp/WEB-INF/applicationContext.xml | 14 ++++ .../pnf/dmaap/PnfEventReadyConsumerTest.java | 68 +++++++++++++++ .../src/test/resources/dmaapTest.properties | 7 ++ .../springConfig_PnfEventReadyConsumer.xml | 19 +++++ 8 files changed, 211 insertions(+), 48 deletions(-) delete mode 100644 bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/PnfReadyEventHandler.java create mode 100644 bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyConsumer.java create mode 100644 bpmn/MSOInfrastructureBPMN/src/main/resources/dmaap.properties delete mode 100644 bpmn/MSOInfrastructureBPMN/src/main/resources/processengine.properties create mode 100644 bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyConsumerTest.java create mode 100644 bpmn/MSOInfrastructureBPMN/src/test/resources/dmaapTest.properties create mode 100644 bpmn/MSOInfrastructureBPMN/src/test/resources/springConfig_PnfEventReadyConsumer.xml (limited to 'bpmn/MSOInfrastructureBPMN') 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 f89b6a7a58..0000000000 --- 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 0000000000..e6019f73f0 --- /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 0000000000..3c4dca49cf --- /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 d071fdabf8..0000000000 --- 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 46cea5da63..f46cd065ed 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 @@ + + + + + + + + + + + + + + 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 0000000000..2f6a00db66 --- /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 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 0000000000..a8df15c5df --- /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 0000000000..5abee9dfd9 --- /dev/null +++ b/bpmn/MSOInfrastructureBPMN/src/test/resources/springConfig_PnfEventReadyConsumer.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + -- cgit 1.2.3-korg