diff options
Diffstat (limited to 'rest-services/aai-client')
7 files changed, 375 insertions, 2 deletions
diff --git a/rest-services/aai-client/pom.xml b/rest-services/aai-client/pom.xml index a57e6b79..2da34f37 100644 --- a/rest-services/aai-client/pom.xml +++ b/rest-services/aai-client/pom.xml @@ -23,8 +23,9 @@ <dependency> <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId> <artifactId>common-dependency</artifactId> - <version>1.1.2-SNAPSHOT</version> + <version>${project.version}</version> </dependency> + <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webflux</artifactId> diff --git a/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/AaiHttpClientFactory.java b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/AaiHttpClientFactory.java new file mode 100644 index 00000000..903528b9 --- /dev/null +++ b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/AaiHttpClientFactory.java @@ -0,0 +1,115 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018-2019 NOKIA 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.onap.dcaegen2.services.sdk.rest.services.aai.client.service; + +import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.ssl.SslContext; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.AaiHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.get.AaiHttpGetClient; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.patch.AaiHttpPatchClient; +import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder; +import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Mono; +import reactor.netty.Connection; +import reactor.netty.http.client.HttpClient; +import reactor.netty.http.client.HttpClientRequest; +import reactor.netty.http.client.HttpClientResponse; + +import javax.net.ssl.SSLException; +import java.util.Base64; +import java.util.Map; +import java.util.function.BiConsumer; + +public class AaiHttpClientFactory { + + private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class); + + private final AaiClientConfiguration configuration; + private final SslFactory sslFactory; + + + public AaiHttpClientFactory(SslFactory sslFactory, AaiClientConfiguration configuration) { + this.configuration = configuration; + this.sslFactory = sslFactory; + } + + public AaiHttpClient<String> get() throws SSLException { + return new AaiHttpGetClient(configuration).createAaiHttpClient(build()); + } + + public AaiHttpClient<Integer> patch(JsonBodyBuilder jsonBodyBuilder) throws SSLException { + return new AaiHttpPatchClient(configuration, jsonBodyBuilder).createAaiHttpClient(build()); + } + + private HttpClient build() throws SSLException { + LOGGER.debug("Setting ssl context"); + + SslContext sslContext = createSslContext(); + + return HttpClient.create() + .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)) + .headers(this::settingHeaders) + .doOnRequest(logRequest()) + .doOnResponse(logResponse()); + } + + private SslContext createSslContext() throws SSLException { + if (configuration.enableAaiCertAuth()) { + return sslFactory.createSecureContext( + configuration.keyStorePath(), + configuration.keyStorePasswordPath(), + configuration.trustStorePath(), + configuration.trustStorePasswordPath() + ); + } + return sslFactory.createInsecureContext(); + } + + private HttpHeaders settingHeaders(HttpHeaders httpHeaders) { + httpHeaders.add("Authorization", "Basic " + performBasicAuthentication()); + for(Map.Entry<String,String> header : configuration.aaiHeaders().entrySet()) + httpHeaders.add(header.getKey(), header.getValue()); + return httpHeaders; + } + + private String performBasicAuthentication() { + return Base64.getEncoder().encodeToString( + (configuration.aaiUserName() + ":" + configuration.aaiUserPassword()).getBytes() + ); + } + + private static BiConsumer<HttpClientRequest, Connection> logRequest() { + return (httpClientRequest, connection) -> { + LOGGER.info("Request: {} {}", httpClientRequest.method(), httpClientRequest.uri()); + httpClientRequest.requestHeaders().forEach(stringStringEntry -> + LOGGER.info("{}={}", stringStringEntry.getKey(), stringStringEntry.getValue()) + ); + }; + } + + private static BiConsumer<? super HttpClientResponse, ? super Connection> logResponse() { + return (httpClientResponse, connection) -> + LOGGER.info("ResponseStatus {}", httpClientResponse.status().code()); + } +} diff --git a/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/AaiReactiveWebClientFactory.java b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/AaiReactiveWebClientFactory.java index 0ed4fb4b..85ba8ea5 100644 --- a/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/AaiReactiveWebClientFactory.java +++ b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/AaiReactiveWebClientFactory.java @@ -21,8 +21,8 @@ package org.onap.dcaegen2.services.sdk.rest.services.aai.client.service; import io.netty.handler.ssl.SslContext; -import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,6 +31,7 @@ import org.springframework.http.client.reactive.ClientHttpConnector; 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; import reactor.netty.http.client.HttpClient; diff --git a/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/AaiHttpClient.java b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/AaiHttpClient.java new file mode 100644 index 00000000..c88ca0ea --- /dev/null +++ b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/AaiHttpClient.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018-2019 NOKIA 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.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http; + +import org.onap.dcaegen2.services.sdk.rest.services.model.AaiModel; +import reactor.core.publisher.Mono; + +public interface AaiHttpClient<T> { + Mono<T> getAaiResponse(AaiModel aaiModel); +} diff --git a/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/get/AaiHttpGetClient.java b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/get/AaiHttpGetClient.java new file mode 100644 index 00000000..7feffddc --- /dev/null +++ b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/get/AaiHttpGetClient.java @@ -0,0 +1,63 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018-2019 NOKIA 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.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.get; + +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.AaiHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.model.AaiModel; +import org.onap.dcaegen2.services.sdk.rest.services.uri.URI; +import reactor.core.publisher.Mono; +import reactor.netty.http.client.HttpClient; + + +public final class AaiHttpGetClient implements AaiHttpClient<String> { + + private HttpClient httpClient; + private final AaiClientConfiguration configuration; + + + public AaiHttpGetClient(AaiClientConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public Mono<String> getAaiResponse(AaiModel aaiModel) { + return httpClient + .baseUrl(getUri(aaiModel.getCorrelationId())) + .get() + .responseContent() + .aggregate() + .asString(); + } + + public AaiHttpGetClient createAaiHttpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + String getUri(String pnfName) { + return new URI.URIBuilder() + .scheme(configuration.aaiProtocol()) + .host(configuration.aaiHost()) + .port(configuration.aaiPort()) + .path(configuration.aaiBasePath() + configuration.aaiPnfPath() + "/" + pnfName).build().toString(); + } +} diff --git a/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/patch/AaiHttpPatchClient.java b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/patch/AaiHttpPatchClient.java new file mode 100644 index 00000000..51000b09 --- /dev/null +++ b/rest-services/aai-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/patch/AaiHttpPatchClient.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA 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.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.patch; + +import io.netty.handler.codec.http.HttpHeaders; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.AaiHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.model.AaiModel; +import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder; +import org.onap.dcaegen2.services.sdk.rest.services.uri.URI; +import org.slf4j.MDC; +import reactor.core.publisher.Mono; +import reactor.netty.ByteBufFlux; +import reactor.netty.http.client.HttpClient; + +import java.util.UUID; +import java.util.function.Consumer; + +import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.REQUEST_ID; +import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_INVOCATION_ID; +import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_ONAP_REQUEST_ID; + +public final class AaiHttpPatchClient implements AaiHttpClient<Integer> { + + private HttpClient httpClient; + private final AaiClientConfiguration configuration; + private final JsonBodyBuilder jsonBodyBuilder; + + + public AaiHttpPatchClient(final AaiClientConfiguration configuration, JsonBodyBuilder jsonBodyBuilder) { + this.configuration = configuration; + this.jsonBodyBuilder = jsonBodyBuilder; + } + + + public Mono<Integer> getAaiResponse(AaiModel aaiModel) { + return httpClient + .headers(addHeaders()) + .baseUrl(getUri(aaiModel.getCorrelationId())) + .patch() + .send(ByteBufFlux.fromString(Mono.just(jsonBodyBuilder.createJsonBody(aaiModel)))) + .responseSingle((res, content) -> Mono.just(res.status().code())); + } + + public AaiHttpPatchClient createAaiHttpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + String getUri(String pnfName) { + return new URI.URIBuilder() + .scheme(configuration.aaiProtocol()) + .host(configuration.aaiHost()) + .port(configuration.aaiPort()) + .path(configuration.aaiBasePath() + configuration.aaiPnfPath() + "/" + pnfName).build().toString(); + } + + private Consumer<? super HttpHeaders> addHeaders() { + return h -> { + h.add(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)); + h.add(X_INVOCATION_ID, UUID.randomUUID().toString()); + }; + } +} diff --git a/rest-services/aai-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/get/AaiHttpGetClientTest.java b/rest-services/aai-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/get/AaiHttpGetClientTest.java new file mode 100644 index 00000000..41a532d9 --- /dev/null +++ b/rest-services/aai-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/aai/client/service/http/get/AaiHttpGetClientTest.java @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018-2019 NOKIA 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.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.get; + + +import org.junit.jupiter.api.BeforeEach; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.model.AaiModel; +import reactor.netty.http.client.HttpClient; + +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class AaiHttpGetClientTest { + + private static final String SUCCESS_RESPONSE = "{\"correlationId\":\"NOKnhfsadhff\"," + + "\"ipaddress-v4\":\"256.22.33.155\", " + + "\"ipaddress-v6\":\"200J:0db8:85a3:0000:0000:8a2e:0370:7334\"}"; + + private AaiHttpGetClient aaiReactiveHttpGetClient; + private HttpClient httpClient; + + private AaiClientConfiguration aaiConfigurationMock; + private AaiModel aaiModel; + private Map<String,String> aaiHeaders; + + + @BeforeEach + void setUp() { + setupHeaders(); + aaiModel = mock(AaiModel.class); + aaiConfigurationMock = mock(AaiClientConfiguration.class); + + when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); + when(aaiConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiConfigurationMock.aaiPort()).thenReturn(1234); + when(aaiConfigurationMock.aaiUserName()).thenReturn("PRH"); + when(aaiConfigurationMock.aaiUserPassword()).thenReturn("PRH"); + when(aaiConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); + when(aaiConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf"); + when(aaiConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders); + + when(aaiModel.getCorrelationId()).thenReturn("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/json"); + } + + private void mockHttpClientObject() { + //when(HttpClient.create().) + } + +}
\ No newline at end of file |