From e8a80102a45458b3f1d15e07dc0a63e1370c44a7 Mon Sep 17 00:00:00 2001 From: pkaras Date: Mon, 5 Nov 2018 16:04:54 +0100 Subject: DmaaP Publisher and consumer interfaces ssl setup Change-Id: I971d0fb222c6e8e15de4fc4a4d9eeb4ef3a99f44 Issue-ID: DCAEGEN2-944 Signed-off-by: piotr.karas --- .../prh/service/DMaaPReactiveWebClient.java | 72 ---------------------- .../ConsumerReactiveHttpClientFactory.java | 37 +++++++++++ .../consumer/DMaaPConsumerReactiveHttpClient.java | 9 +-- .../service/consumer/DMaaPReactiveWebClient.java | 72 ++++++++++++++++++++++ .../producer/DMaaPPublisherReactiveHttpClient.java | 12 ++-- .../PublisherReactiveHttpClientFactory.java | 32 ++++++++++ 6 files changed, 148 insertions(+), 86 deletions(-) delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactory.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClient.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactory.java (limited to 'prh-dmaap-client/src/main') diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java deleted file mode 100644 index 242a4251..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * 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.prh.service; - -import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.RESPONSE_CODE; -import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.SERVICE_NAME; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.web.reactive.function.client.ExchangeFilterFunction; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Mono; - -/** - * @author Przemysław Wąsala on 7/4/18 - */ -public class DMaaPReactiveWebClient { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - /** - * Construct Reactive WebClient with appropriate settings. - * - * @return WebClient - */ - public WebClient build() { - return WebClient.builder() - .filter(logRequest()) - .filter(logResponse()) - .build(); - } - - private ExchangeFilterFunction logResponse() { - return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { - MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode())); - logger.info("Response Status {}", clientResponse.statusCode()); - MDC.remove(RESPONSE_CODE); - return Mono.just(clientResponse); - }); - } - - private ExchangeFilterFunction logRequest() { - return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { - MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); - logger.info("Request: {} {}", clientRequest.method(), clientRequest.url()); - clientRequest.headers() - .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); - MDC.remove(SERVICE_NAME); - return Mono.just(clientRequest); - }); - } - -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactory.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactory.java new file mode 100644 index 00000000..951e0b0b --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactory.java @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * 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.prh.service.consumer; + +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; + +public class ConsumerReactiveHttpClientFactory { + + private final DMaaPReactiveWebClient reactiveWebClient; + + public ConsumerReactiveHttpClientFactory(DMaaPReactiveWebClient reactiveWebClient) { + this.reactiveWebClient = reactiveWebClient; + } + + public DMaaPConsumerReactiveHttpClient create(DmaapConsumerConfiguration consumerConfiguration) { + return new DMaaPConsumerReactiveHttpClient(consumerConfiguration, reactiveWebClient.build()); + } + +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java index 02c6ba96..bed3b0a8 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java @@ -47,14 +47,14 @@ public class DMaaPConsumerReactiveHttpClient { private final String consumerGroup; private final String consumerId; private final String contentType; - private WebClient webClient; + private final WebClient webClient; /** * Constructor of DMaaPConsumerReactiveHttpClient. * * @param consumerConfiguration - DMaaP consumer configuration object */ - public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration) { + DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration, WebClient webClient) { this.dmaapHostName = consumerConfiguration.dmaapHostName(); this.dmaapProtocol = consumerConfiguration.dmaapProtocol(); this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber(); @@ -62,6 +62,7 @@ public class DMaaPConsumerReactiveHttpClient { this.consumerGroup = consumerConfiguration.consumerGroup(); this.consumerId = consumerConfiguration.consumerId(); this.contentType = consumerConfiguration.dmaapContentType(); + this.webClient = webClient; } /** @@ -95,10 +96,6 @@ public class DMaaPConsumerReactiveHttpClient { return dmaapTopicName + "/" + consumerGroup + "/" + consumerId; } - public DMaaPConsumerReactiveHttpClient createDMaaPWebClient(WebClient webClient) { - this.webClient = webClient; - return this; - } URI getUri() { return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber) diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClient.java new file mode 100644 index 00000000..9eb6ee62 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClient.java @@ -0,0 +1,72 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * 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.prh.service.consumer; + +import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.RESPONSE_CODE; +import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.SERVICE_NAME; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.web.reactive.function.client.ExchangeFilterFunction; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +/** + * @author Przemysław Wąsala on 7/4/18 + */ +public class DMaaPReactiveWebClient { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** + * Construct Reactive WebClient with appropriate settings. + * + * @return WebClient + */ + public WebClient build() { + return WebClient.builder() + .filter(logRequest()) + .filter(logResponse()) + .build(); + } + + private ExchangeFilterFunction logResponse() { + return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { + MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode())); + logger.info("Response Status {}", clientResponse.statusCode()); + MDC.remove(RESPONSE_CODE); + return Mono.just(clientResponse); + }); + } + + private ExchangeFilterFunction logRequest() { + return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { + MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); + logger.info("Request: {} {}", clientRequest.method(), clientRequest.url()); + clientRequest.headers() + .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); + MDC.remove(SERVICE_NAME); + return Mono.just(clientRequest); + }); + } + +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClient.java index 6919487a..d4687c51 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClient.java @@ -50,20 +50,21 @@ public class DMaaPPublisherReactiveHttpClient { private final String dmaapProtocol; private final String dmaapTopicName; private final String dmaapContentType; - - private RestTemplate restTemplate; + private final RestTemplate restTemplate; /** * Constructor DMaaPPublisherReactiveHttpClient. * * @param dmaapPublisherConfiguration - DMaaP producer configuration object */ - public DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) { + DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration, + RestTemplate restTemplate) { this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName(); this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol(); this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber(); this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName(); this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType(); + this.restTemplate = restTemplate; } /** @@ -90,11 +91,6 @@ public class DMaaPPublisherReactiveHttpClient { } - public DMaaPPublisherReactiveHttpClient createDMaaPWebClient(RestTemplate restTemplate) { - this.restTemplate = restTemplate; - return this; - } - URI getUri() { return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber) .path(dmaapTopicName).build(); diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactory.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactory.java new file mode 100644 index 00000000..0fc8f16a --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactory.java @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * 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.prh.service.producer; + +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; +import org.springframework.web.client.RestTemplate; + +public class PublisherReactiveHttpClientFactory { + + public DMaaPPublisherReactiveHttpClient create(DmaapPublisherConfiguration publisherConfiguration) { + return new DMaaPPublisherReactiveHttpClient(publisherConfiguration, new RestTemplate()); + } + +} -- cgit 1.2.3-korg