diff options
Diffstat (limited to 'prh-aai-client/src')
3 files changed, 84 insertions, 68 deletions
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java index 6daf54a1..55dcb398 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java @@ -24,11 +24,18 @@ import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.RESPONSE import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.SERVICE_NAME; import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; + import java.util.Map; +import javax.net.ssl.SSLException; + import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; @@ -36,7 +43,7 @@ import reactor.core.publisher.Mono; public class AaiReactiveWebClient { - private Logger logger = LoggerFactory.getLogger(this.getClass()); + private static final Logger LOGGER = LoggerFactory.getLogger(AaiReactiveWebClient.class); private String aaiUserName; private String aaiUserPassword; @@ -60,8 +67,19 @@ public class AaiReactiveWebClient { * * @return WebClient */ - public WebClient build() { + public WebClient build() throws SSLException { + SslContext sslContext; + sslContext = SslContextBuilder + .forClient() + .trustManager(InsecureTrustManagerFactory.INSTANCE) + .build(); + LOGGER.debug("Setting ssl context"); + return WebClient.builder() + .clientConnector(new ReactorClientHttpConnector(clientOptions -> { + clientOptions.sslContext(sslContext); + clientOptions.disablePool(); + })) .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders)) .filter(basicAuthentication(aaiUserName, aaiUserPassword)) .filter(logRequest()) @@ -72,9 +90,9 @@ public class AaiReactiveWebClient { private ExchangeFilterFunction logRequest() { return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); - logger.info("Request: {} {}", clientRequest.method(), clientRequest.url()); + LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url()); clientRequest.headers() - .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); + .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value))); return Mono.just(clientRequest); }); } @@ -82,7 +100,7 @@ public class AaiReactiveWebClient { private ExchangeFilterFunction logResponse() { return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode())); - logger.info("Response Status {}", clientResponse.statusCode()); + LOGGER.info("Response Status {}", clientResponse.statusCode()); return Mono.just(clientResponse); }); } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java index be6c63e0..358a4524 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java @@ -20,35 +20,32 @@ package org.onap.dcaegen2.services.prh.service.producer; -import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.REQUEST_ID; -import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_INVOCATION_ID; -import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_ONAP_REQUEST_ID; - -import java.net.URI; -import java.net.URISyntaxException; -import java.util.UUID; import org.apache.http.client.utils.URIBuilder; import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; -import org.onap.dcaegen2.services.prh.exceptions.AaiRequestException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; -import org.springframework.http.HttpStatus; -import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.UUID; + +import static org.onap.dcaegen2.services.prh.model.CommonFunctions.createJsonBody; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.*; + public class AaiProducerReactiveHttpClient { + private WebClient webClient; private final String aaiHost; private final String aaiProtocol; private final Integer aaiHostPortNumber; private final String aaiBasePath; - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private WebClient webClient; - + private final String aaiPnfPath; /** * Constructor of AaiProducerReactiveHttpClient. @@ -60,6 +57,7 @@ public class AaiProducerReactiveHttpClient { this.aaiProtocol = configuration.aaiProtocol(); this.aaiHostPortNumber = configuration.aaiPort(); this.aaiBasePath = configuration.aaiBasePath(); + this.aaiPnfPath = configuration.aaiPnfPath(); } /** @@ -68,10 +66,12 @@ public class AaiProducerReactiveHttpClient { * @param consumerDmaapModelMono - object which will be sent to AAI database * @return status code of operation */ - public Mono<Integer> getAaiProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) { - return consumerDmaapModelMono - .doOnNext(consumerDmaapModel -> logger.info("Sending PNF model to AAI {}", consumerDmaapModel)) - .flatMap(this::patchAaiRequest); + public Mono<ClientResponse> getAaiProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) { + try { + return patchAaiRequest(consumerDmaapModelMono); + } catch (URISyntaxException e) { + return Mono.error(e); + } } public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) { @@ -79,26 +79,14 @@ public class AaiProducerReactiveHttpClient { return this; } - private Mono<Integer> patchAaiRequest(ConsumerDmaapModel dmaapModel) { - try { - return webClient.patch() + private Mono<ClientResponse> patchAaiRequest(ConsumerDmaapModel dmaapModel) throws URISyntaxException { + return + webClient.patch() .uri(getUri(dmaapModel.getSourceName())) .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)) .header(X_INVOCATION_ID, UUID.randomUUID().toString()) - .body(BodyInserters.fromObject(dmaapModel)) - .retrieve() - .onStatus( - HttpStatus::is4xxClientError, - clientResponse -> Mono - .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode())) - ) - .onStatus(HttpStatus::is5xxServerError, - clientResponse -> Mono - .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode()))) - .bodyToMono(Integer.class); - } catch (URISyntaxException e) { - return Mono.error(e); - } + .body(Mono.just(createJsonBody(dmaapModel)), String.class) + .exchange(); } URI getUri(String pnfName) throws URISyntaxException { @@ -106,7 +94,7 @@ public class AaiProducerReactiveHttpClient { .setScheme(aaiProtocol) .setHost(aaiHost) .setPort(aaiHostPortNumber) - .setPath(aaiBasePath + "/" + pnfName) + .setPath(aaiBasePath + aaiPnfPath + "/" + pnfName) .build(); } } diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java index 9b0f4fe8..4160f356 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java @@ -22,6 +22,7 @@ package org.onap.dcaegen2.services.prh.service.producer; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; @@ -31,18 +32,19 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; + import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; +import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient.ResponseSpec; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; - class AaiProducerReactiveHttpClientTest { private static final Integer SUCCESS_RESPONSE = 200; @@ -50,16 +52,21 @@ class AaiProducerReactiveHttpClientTest { private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class); private static WebClient webClient = mock(WebClient.class); - private static ConsumerDmaapModel dmaapModel = new ConsumerDmaapModelForUnitTest(); - private static WebClient.RequestBodyUriSpec requestBodyUriSpec; - private static ResponseSpec responseSpec; - private static Map<String, String> aaiHeaders; + private ConsumerDmaapModel dmaapModel = spy(new ConsumerDmaapModelForUnitTest()); + private WebClient.RequestBodyUriSpec requestBodyUriSpec; + private ResponseSpec responseSpec; - @BeforeAll - static void setUp() { - setupHeaders(); + private Map<String, String> aaiHeaders; + private ClientResponse clientResponse; + private Mono<ClientResponse> clientResponseMono; + @BeforeEach + void setUp() { + setupHeaders(); + clientResponse = mock(ClientResponse.class); + clientResponseMono = Mono.just(clientResponse); + when(dmaapModel.getSourceName()).thenReturn("NOKnhfsadhff"); when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); when(aaiConfigurationMock.aaiProtocol()).thenReturn("https"); when(aaiConfigurationMock.aaiPort()).thenReturn(1234); @@ -80,15 +87,6 @@ class AaiProducerReactiveHttpClientTest { responseSpec = mock(ResponseSpec.class); } - private static void setupHeaders() { - aaiHeaders = new HashMap<>(); - aaiHeaders.put("X-FromAppId", "PRH"); - aaiHeaders.put("X-TransactionId", "vv-temp"); - aaiHeaders.put("Accept", "application/json"); - aaiHeaders.put("Real-Time", "true"); - aaiHeaders.put("Content-Type", "application/merge-patch+json"); - } - @Test void getAaiProducerResponse_shouldReturn200() { //given @@ -98,12 +96,11 @@ class AaiProducerReactiveHttpClientTest { mockWebClientDependantObject(); doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class); aaiProducerReactiveHttpClient.createAaiWebClient(webClient); - Mono<Integer> response = aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel)); //then - StepVerifier.create(response).expectSubscription() + StepVerifier.create(aaiProducerReactiveHttpClient.getAaiProducerResponse(dmaapModel)).expectSubscription() .expectNextMatches(results -> { - Assertions.assertEquals(results, expectedResult.block()); + Assertions.assertEquals(results, clientResponse); return true; }).verifyComplete(); } @@ -115,24 +112,37 @@ class AaiProducerReactiveHttpClientTest { //when when(webClient.patch()).thenReturn(requestBodyUriSpec); aaiProducerReactiveHttpClient.createAaiWebClient(webClient); - when(aaiProducerReactiveHttpClient.getUri("pnfName")).thenThrow(URISyntaxException.class); - + doThrow(URISyntaxException.class).when(aaiProducerReactiveHttpClient).getUri(any()); //then StepVerifier.create( aaiProducerReactiveHttpClient.getAaiProducerResponse( - Mono.just(dmaapModel) + dmaapModel )).expectSubscription().expectError(Exception.class).verify(); } + @Test + void getAppropriateUri_whenPassingCorrectedPathForPnf() throws URISyntaxException { + Assertions.assertEquals(aaiProducerReactiveHttpClient.getUri("NOKnhfsadhff"), + URI.create("https://54.45.33.2:1234/aai/v11/network/pnfs/pnf/NOKnhfsadhff")); + } + + + private void setupHeaders() { + aaiHeaders = new HashMap<>(); + aaiHeaders.put("X-FromAppId", "PRH"); + aaiHeaders.put("X-TransactionId", "vv-temp"); + aaiHeaders.put("Accept", "application/json"); + aaiHeaders.put("Real-Time", "true"); + aaiHeaders.put("Content-Type", "application/merge-patch+json"); + } + private void mockWebClientDependantObject() { WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class); when(webClient.patch()).thenReturn(requestBodyUriSpec); when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec); when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec); - when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec); - doReturn(responseSpec).when(requestHeadersSpec).retrieve(); - doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); + when(requestBodyUriSpec.body(any(), (Class<Object>) any())).thenReturn(requestHeadersSpec); + when(requestHeadersSpec.exchange()).thenReturn(clientResponseMono); } - } |