diff options
Diffstat (limited to 'bpmn')
6 files changed, 206 insertions, 35 deletions
diff --git a/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/JsonUtilForCorrelationId.java b/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/JsonUtilForCorrelationId.java new file mode 100644 index 0000000000..2853899ae2 --- /dev/null +++ b/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/JsonUtilForCorrelationId.java @@ -0,0 +1,71 @@ +/*- + * ============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 com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.Spliterator; + +final class JsonUtilForCorrelationId { + + private static final String JSON_HEADER = "pnfRegistrationFields"; + private static final String JSON_CORRELATION_ID_FIELD_NAME = "correlationId"; + + static List<String> parseJsonToGelAllCorrelationId(String json) { + List<String> list = new ArrayList<>(); + JsonElement je = new JsonParser().parse(json); + if (je.isJsonObject()) { + getCorrelationIdFromJsonObject(je.getAsJsonObject()).ifPresent(corr -> list.add(corr)); + } else { + JsonArray array = je.getAsJsonArray(); + Spliterator<JsonElement> spliterator = array.spliterator(); + spliterator.forEachRemaining(jsonElement -> { + parseJsonElementToJsonObject(jsonElement) + .ifPresent(jsonObject -> getCorrelationIdFromJsonObject(jsonObject) + .ifPresent(correlationId -> list.add(correlationId))); + }); + } + return list; + } + + private static Optional<JsonObject> parseJsonElementToJsonObject(JsonElement jsonElement) { + if (jsonElement.isJsonObject()) { + return Optional.ofNullable(jsonElement.getAsJsonObject()); + } + return Optional.ofNullable(new JsonParser().parse(jsonElement.getAsString()).getAsJsonObject()); + } + + private static Optional<String> getCorrelationIdFromJsonObject(JsonObject jsonObject) { + if (jsonObject.has(JSON_HEADER)) { + JsonObject jo = jsonObject.getAsJsonObject(JSON_HEADER); + if (jo.has(JSON_CORRELATION_ID_FIELD_NAME)) { + return Optional.ofNullable(jo.get(JSON_CORRELATION_ID_FIELD_NAME).getAsString()); + } + } + return Optional.empty(); + } + +} diff --git a/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java b/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java index 830574bad4..2c7309def4 100644 --- a/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java +++ b/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java @@ -22,8 +22,9 @@ package org.openecomp.mso.bpmn.infrastructure.pnf.dmaap; import java.io.IOException; import java.net.URI; +import java.util.Collections; +import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -34,14 +35,14 @@ import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; -import org.openecomp.mso.jsonpath.JsonPathUtil; +import org.openecomp.mso.bpmn.core.PropertyConfiguration; import org.openecomp.mso.logger.MsoLogger; +import org.openecomp.mso.logger.MsoLogger.Catalog; public class PnfEventReadyDmaapClient implements DmaapClient { - private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA); + private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(Catalog.GENERAL); - private static final String JSON_PATH_CORRELATION_ID = "$.pnfRegistrationFields.correlationId"; private HttpClient httpClient; private String dmaapHost; private int dmaapPort; @@ -56,18 +57,20 @@ public class PnfEventReadyDmaapClient implements DmaapClient { private int dmaapClientDelayInSeconds; private volatile boolean dmaapThreadListenerIsRunning; - public PnfEventReadyDmaapClient() { + public void init() { httpClient = HttpClientBuilder.create().build(); pnfCorrelationIdToThreadMap = new ConcurrentHashMap<>(); + dmaapHost = PropertyConfiguration.getInstance().getProperties(PropertyConfiguration.MSO_BPMN_URN_PROPERTIES) + .get("dmaapHost"); + dmaapPort = Integer.parseInt(PropertyConfiguration.getInstance() + .getProperties(PropertyConfiguration.MSO_BPMN_URN_PROPERTIES).get("dmaapPort")); executor = null; - } - - public void init() { getRequest = new HttpGet(buildURI()); } @Override public synchronized void registerForUpdate(String correlationId, Runnable informConsumer) { + LOGGER.debug("registering for pnf ready dmaap event for correlation id: " + correlationId); pnfCorrelationIdToThreadMap.put(correlationId, informConsumer); if (!dmaapThreadListenerIsRunning) { startDmaapThreadListener(); @@ -76,6 +79,7 @@ public class PnfEventReadyDmaapClient implements DmaapClient { @Override public synchronized Runnable unregister(String correlationId) { + LOGGER.debug("unregistering from pnf ready dmaap event for correlation id: " + correlationId); Runnable runnable = pnfCorrelationIdToThreadMap.remove(correlationId); if (pnfCorrelationIdToThreadMap.isEmpty()) { stopDmaapThreadListener(); @@ -108,14 +112,6 @@ public class PnfEventReadyDmaapClient implements DmaapClient { .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; } @@ -146,25 +142,26 @@ public class PnfEventReadyDmaapClient implements DmaapClient { public void run() { try { HttpResponse response = httpClient.execute(getRequest); - getCorrelationIdFromResponse(response).ifPresent(this::informAboutPnfReadyIfCorrelationIdFound); + getCorrelationIdListFromResponse(response).forEach(this::informAboutPnfReadyIfCorrelationIdFound); } catch (IOException e) { LOGGER.error("Exception caught during sending rest request to dmaap for listening event topic", e); } } - private Optional<String> getCorrelationIdFromResponse(HttpResponse response) throws IOException { + private List<String> getCorrelationIdListFromResponse(HttpResponse response) throws IOException { if (response.getStatusLine().getStatusCode() == 200) { String responseString = EntityUtils.toString(response.getEntity(), "UTF-8"); if (responseString != null) { - return JsonPathUtil.getInstance().locateResult(responseString, JSON_PATH_CORRELATION_ID); + return JsonUtilForCorrelationId.parseJsonToGelAllCorrelationId(responseString); } } - return Optional.empty(); + return Collections.emptyList(); } private synchronized void informAboutPnfReadyIfCorrelationIdFound(String correlationId) { Runnable runnable = unregister(correlationId); if (runnable != null) { + LOGGER.debug("pnf ready event got from dmaap for correlationId: " + correlationId); runnable.run(); } } diff --git a/bpmn/MSOInfrastructureBPMN/src/main/resources/dmaap.properties b/bpmn/MSOInfrastructureBPMN/src/main/resources/dmaap.properties index 6807a24ea9..a1286b056c 100644 --- a/bpmn/MSOInfrastructureBPMN/src/main/resources/dmaap.properties +++ b/bpmn/MSOInfrastructureBPMN/src/main/resources/dmaap.properties @@ -1,11 +1,8 @@ -host=HOSTNAME -port=3905 protocol=http uriPathPrefix = events -eventReadyTopicName=pnfEventReady +eventReadyTopicName=unauthenticated.PNF_READY consumerId=consumerId consumerGroup=group -clientThreadInitialDelayInSeconds=1 clientThreadDelayInSeconds=5 pnfDefaultTimeout=P14D diff --git a/bpmn/MSOInfrastructureBPMN/src/main/webapp/WEB-INF/applicationContext.xml b/bpmn/MSOInfrastructureBPMN/src/main/webapp/WEB-INF/applicationContext.xml index 7a0aa60bb3..1753ba1919 100644 --- a/bpmn/MSOInfrastructureBPMN/src/main/webapp/WEB-INF/applicationContext.xml +++ b/bpmn/MSOInfrastructureBPMN/src/main/webapp/WEB-INF/applicationContext.xml @@ -24,8 +24,6 @@ <bean id="pnfEventReadyDmaapClient" class="org.openecomp.mso.bpmn.infrastructure.pnf.dmaap.PnfEventReadyDmaapClient"
init-method="init">
- <property name="dmaapHost" value="${host}"/>
- <property name="dmaapPort" value="${port}"/>
<property name="dmaapProtocol" value="${protocol}"/>
<property name="dmaapUriPathPrefix" value="${uriPathPrefix}"/>
<property name="dmaapTopicName" value="${eventReadyTopicName}"/>
diff --git a/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/JsonUtilForCorrelationIdTest.java b/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/JsonUtilForCorrelationIdTest.java new file mode 100644 index 0000000000..95de883dab --- /dev/null +++ b/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/JsonUtilForCorrelationIdTest.java @@ -0,0 +1,76 @@ +/*- + * ============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 java.util.List; +import org.junit.Test; + +public class JsonUtilForCorrelationIdTest { + + private static final String JSON_EXAMPLE_WITH_CORRELATION_ID = "[\n" + + " {\n" + + " \"pnfRegistrationFields\" : {\n" + + " \"correlationId\" : \"corrTest1\",\n" + + " \"value\" : \"value1\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"pnfRegistrationFields\" : {\n" + + " \"correlationId\" : \"corrTest2\",\n" + + " \"value\" : \"value2\"\n" + + " }\n" + + " }\n" + + "]"; + + private static final String JSON_EXAMPLE_WITH_CORRELATION_ID2 = "{\"pnfRegistrationFields\":{\"correlationId\":\"corrTest3\"}}"; + private static final String JSON_EXAMPLE_WITH_CORRELATION_ID3 = "[\"\\{\\\"pnfRegistrationFields\\\":" + + "{\\\"correlationId\\\":\\\"corrTest4\\\"}}\", \"\\{\\\"pnfRegistrationFields\\\":" + + "{\\\"correlationId\\\":\\\"corrTest5\\\"}}\"]"; + private static final String JSON_EXAMPLE_WITH_CORRELATION_ID4 = "{\"header\":{\"key\":\"value\"}}"; + + @Test + public void parseJsonSuccessful() { + List<String> expectedResult = JsonUtilForCorrelationId + .parseJsonToGelAllCorrelationId(JSON_EXAMPLE_WITH_CORRELATION_ID); + assertThat(expectedResult).containsExactly("corrTest1", "corrTest2"); + + List<String> expectedResult2 = JsonUtilForCorrelationId + .parseJsonToGelAllCorrelationId(JSON_EXAMPLE_WITH_CORRELATION_ID2); + assertThat(expectedResult2).containsExactly("corrTest3"); + } + + @Test + public void parseJsonWithEscapeCharacters_Successful() { + List<String> expectedResult = JsonUtilForCorrelationId + .parseJsonToGelAllCorrelationId(JSON_EXAMPLE_WITH_CORRELATION_ID3); + assertThat(expectedResult).containsExactly("corrTest4", "corrTest5"); + } + + @Test + public void parseJson_emptyListReturnedWhenNothingFound() { + List<String> expectedResult = JsonUtilForCorrelationId + .parseJsonToGelAllCorrelationId(JSON_EXAMPLE_WITH_CORRELATION_ID4); + assertThat(expectedResult).isEmpty(); + } + +} diff --git a/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java b/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java index 393730ed43..6ded47d5fa 100644 --- a/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java +++ b/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java @@ -30,6 +30,7 @@ import static org.mockito.Mockito.when; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; @@ -42,15 +43,36 @@ import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHttpResponse; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; +import org.openecomp.mso.bpmn.core.PropertyConfiguration; import org.openecomp.mso.bpmn.infrastructure.pnf.dmaap.PnfEventReadyDmaapClient.DmaapTopicListenerThread; - +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({PropertyConfiguration.class}) +@PowerMockIgnore("javax.net.ssl.*") public class PnfEventReadyDmaapClientTest { private static final String CORRELATION_ID = "corrTestId"; private static final String CORRELATION_ID_NOT_FOUND_IN_MAP = "otherCorrId"; - private static final String JSON_EXAMPLE_WITH_CORRELATION_ID = - "{\"pnfRegistrationFields\":{\"correlationId\":\"%s\"}}"; + private static final String JSON_EXAMPLE_WITH_CORRELATION_ID = "[\n" + + " {\n" + + " \"pnfRegistrationFields\" : {\n" + + " \"correlationId\" : \"%s\",\n" + + " \"value\" : \"value1\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"pnfRegistrationFields\" : {\n" + + " \"correlationId\" : \"corr\",\n" + + " \"value\" : \"value2\"\n" + + " }\n" + + " }\n" + + "]"; private static final String JSON_EXAMPLE_WITH_NO_CORRELATION_ID = "{\"pnfRegistrationFields\":{\"field\":\"value\"}}"; @@ -70,9 +92,12 @@ public class PnfEventReadyDmaapClientTest { @Before public void init() throws NoSuchFieldException, IllegalAccessException { + PowerMockito.mockStatic(PropertyConfiguration.class); + PropertyConfiguration propertyConfigurationMock = mock(PropertyConfiguration.class); + PowerMockito.when(PropertyConfiguration.getInstance()).thenReturn(propertyConfigurationMock); + when(propertyConfigurationMock.getProperties(PropertyConfiguration.MSO_BPMN_URN_PROPERTIES)).thenReturn( + createProperties()); testedObject = new PnfEventReadyDmaapClient(); - testedObject.setDmaapHost(HOST); - testedObject.setDmaapPort(PORT); testedObject.setDmaapProtocol(PROTOCOL); testedObject.setDmaapUriPathPrefix(URI_PATH_PREFIX); testedObject.setDmaapTopicName(EVENT_TOPIC_TEST); @@ -91,8 +116,8 @@ public class PnfEventReadyDmaapClientTest { * Test run method, where the are following conditions: * <p> - DmaapThreadListener is running, flag is set to true * <p> - map is filled with one entry with the key that we get from response - * <p> run method should invoke thread from map to notify camunda process, remove element from the map (map is empty) - * and shutdown the executor because of empty map + * <p> run method should invoke thread from map to notify camunda process, remove element from the map (map is + * empty) and shutdown the executor because of empty map */ @Test public void correlationIdIsFoundInHttpResponse_notifyAboutPnfReady() @@ -113,8 +138,8 @@ public class PnfEventReadyDmaapClientTest { * Test run method, where the are following conditions: * <p> - DmaapThreadListener is running, flag is set to true * <p> - map is filled with one entry with the correlationId that does not match to correlationId - * taken from http response. run method should not do anything with the map not run any thread to - * notify camunda process + * taken from http response. run method should not do anything with the map not run any thread to notify camunda + * process */ @Test public void correlationIdIsFoundInHttpResponse_NotFoundInMap() @@ -164,6 +189,13 @@ public class PnfEventReadyDmaapClientTest { threadRunFlag.setAccessible(false); } + private Map<String, String> createProperties() { + Map<String, String> map = new HashMap<>(); + map.put("dmaapHost", HOST); + map.put("dmaapPort", String.valueOf(PORT)); + return map; + } + private HttpResponse createResponse(String json) throws UnsupportedEncodingException { HttpEntity entity = new StringEntity(json); ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1); |