From bf6fa5deff9190c14bba2b15271e44f1e3c21ee7 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Thu, 26 Apr 2018 17:04:09 +0200 Subject: AAI tasks added - aaiClients spllitted for consuming and producing - http removed from names of all clients and interfaces - HttpRequestDetails added as a second parameter for tasks - parameters required from dmaap consumer - pnf-name for URI (get request -> aaiConsumer) - pnf-name and ipv4-oam (and or ipv6-oam) (patch request -> aaiProducer) Change-Id: I87a1b46ab419cd00e9573e08c4d89cb384dd75b5 Issue-ID: DCAEGEN2-451 Signed-off-by: pwielebs --- .../services/config/AAIClientConfiguration.java | 56 ++++++ .../config/AAIHttpClientConfiguration.java | 55 ------ .../onap/dcaegen2/services/service/AAIClient.java | 29 ++++ .../dcaegen2/services/service/AAIClientImpl.java | 93 ++++++++++ .../services/service/AAIConsumerClient.java | 133 +++++++++++++++ .../service/AAIExtendedHttpClientImpl.java | 188 --------------------- .../dcaegen2/services/service/AAIHttpClient.java | 29 ---- .../services/service/AAIHttpClientImpl.java | 93 ---------- .../services/service/AAIProducerClient.java | 155 +++++++++++++++++ .../services/utils/HttpRequestDetails.java | 5 +- .../services/service/AAIConsumerClientTest.java | 92 ++++++++++ .../service/AAIExtendedHttpClientImplTest.java | 137 --------------- .../services/service/AAIHttpClientImplTest.java | 10 +- .../services/service/AAIProducerClientTest.java | 93 ++++++++++ .../config/AAIHttpClientConfigurationTest.java | 8 +- .../service/utils/HttpRequestDetailsTest.java | 10 +- prh-app-server/pom.xml | 5 + .../services/prh/configuration/AppConfig.java | 164 +++++++++--------- .../services/prh/configuration/Config.java | 4 +- .../services/prh/configuration/PrhAppConfig.java | 73 ++++---- .../prh/controllers/HeartbeatController.java | 1 + .../prh/controllers/ScheduleController.java | 1 + .../services/prh/tasks/AAIConsumerTask.java | 30 ++++ .../services/prh/tasks/AAIConsumerTaskImpl.java | 89 ++++++++++ .../services/prh/tasks/AAIProducerTask.java | 32 ++++ .../services/prh/tasks/AAIProducerTaskImpl.java | 93 ++++++++++ .../services/prh/tasks/AAIPublisherTask.java | 32 ---- .../services/prh/tasks/AAIPublisherTaskImpl.java | 80 --------- .../services/prh/tasks/DmaapConsumerTaskImpl.java | 5 +- .../services/prh/tasks/DmaapPublisherTaskImpl.java | 5 +- .../services/prh/tasks/ScheduledTasks.java | 14 +- .../services/prh/IT/ScheduledXmlContextITest.java | 11 +- .../prh/configuration/PrhAppConfigTest.java | 10 +- .../services/prh/tasks/AAIConsumerTaskSpy.java | 44 +++++ .../services/prh/tasks/AAIPublisherTaskSpy.java | 16 +- .../services/prh/tasks/ScheduleControllerSpy.java | 7 +- 36 files changed, 1112 insertions(+), 790 deletions(-) create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIConsumerClientTest.java delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIProducerClientTest.java create mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTask.java create mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java create mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTask.java create mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java delete mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTask.java delete mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskImpl.java create mode 100644 prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskSpy.java diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java new file mode 100644 index 00000000..60a022b4 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java @@ -0,0 +1,56 @@ +/*- + * ============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.config; + + +import java.io.Serializable; +import org.immutables.gson.Gson; +import org.immutables.value.Value; +import org.springframework.stereotype.Component; + + +@Component +@Value.Immutable(prehash = true) +@Value.Style(builder = "new") +@Gson.TypeAdapters +public abstract class AAIClientConfiguration implements Serializable { + + private static final long serialVersionUID = 1L; + + @Value.Parameter + public abstract String aaiHost(); + + @Value.Parameter + public abstract Integer aaiHostPortNumber(); + + @Value.Parameter + public abstract String aaiProtocol(); + + @Value.Parameter + public abstract String aaiUserName(); + + @Value.Parameter + public abstract String aaiUserPassword(); + + @Value.Parameter + public abstract Boolean aaiIgnoreSSLCertificateErrors(); + +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java deleted file mode 100644 index f9cbeb19..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java +++ /dev/null @@ -1,55 +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.config; - - -import java.io.Serializable; -import org.immutables.gson.Gson; -import org.immutables.value.Value; -import org.springframework.stereotype.Component; - - -@Component -@Value.Immutable(prehash = true) -@Value.Style(builder = "new") -@Gson.TypeAdapters -public abstract class AAIHttpClientConfiguration implements Serializable { - - private static final long serialVersionUID = 1L; - - @Value.Parameter - public abstract String aaiHost(); - - @Value.Parameter - public abstract Integer aaiHostPortNumber(); - - @Value.Parameter - public abstract String aaiProtocol(); - - @Value.Parameter - public abstract String aaiUserName(); - - @Value.Parameter - public abstract String aaiUserPassword(); - - @Value.Parameter - public abstract Boolean aaiIgnoreSSLCertificateErrors(); - -} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java new file mode 100644 index 00000000..33a8d644 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java @@ -0,0 +1,29 @@ +/*- + * ============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.service; + +import org.apache.http.impl.client.CloseableHttpClient; + +@FunctionalInterface +public interface AAIClient { + CloseableHttpClient getAAIHttpClient(); +} + diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java new file mode 100644 index 00000000..c2b40b9a --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java @@ -0,0 +1,93 @@ +/*- + * ============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.service; + +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContextBuilder; +import org.apache.http.ssl.TrustStrategy; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +public class AAIClientImpl implements AAIClient { + + Logger logger = LoggerFactory.getLogger(AAIClientImpl.class); + + private AAIClientConfiguration aaiHttpClientConfig; + + @Autowired + public AAIClientImpl(AAIClientConfiguration aaiHttpClientConfiguration) { + this.aaiHttpClientConfig = aaiHttpClientConfiguration; + } + + @Override + public CloseableHttpClient getAAIHttpClient() { + + final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + final boolean aaiIgnoreSSLCertificateErrors = aaiHttpClientConfig.aaiIgnoreSSLCertificateErrors(); + + TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; + + if (aaiIgnoreSSLCertificateErrors) { + try { + logger.info("Setting SSL Context for AAI HTTP Client"); + httpClientBuilder.setSSLContext(new SSLContextBuilder() + .loadTrustMaterial(null, acceptingTrustStrategy) + .build()); + + } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) { + logger.error("Exception while setting SSL Context for AAI HTTP Client: {}", e); + } + + httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); + } + + final String aaiUserName = aaiHttpClientConfig.aaiUserName(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (aaiUserName != null) { + final String aaiHost = aaiHttpClientConfig.aaiHost(); + final Integer aaiHostPortNumber = aaiHttpClientConfig.aaiHostPortNumber(); + final String aaiUserPassword = aaiHttpClientConfig.aaiUserPassword(); + final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); + final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); + credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); + } + + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + + return httpClientBuilder.build(); + } +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java new file mode 100644 index 00000000..24790be2 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java @@ -0,0 +1,133 @@ +/*- + * ============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.service; + +import org.apache.http.HttpEntity; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.HttpUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; + +public class AAIConsumerClient implements AAIExtendedHttpClient { + + Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); + + private final CloseableHttpClient closeableHttpClient; + private final String aaiHost; + private final String aaiProtocol; + private final Integer aaiHostPortNumber; + + + public AAIConsumerClient(AAIClientConfiguration aaiHttpClientConfiguration) { + final AAIClient aaiClient = new AAIClientImpl(aaiHttpClientConfiguration); + closeableHttpClient = aaiClient.getAAIHttpClient(); + aaiHost = aaiHttpClientConfiguration.aaiHost(); + aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); + } + + @Override + public Optional getHttpResponse(HttpRequestDetails requestDetails) { + + Optional extendedDetails = Optional.empty(); + Optional request = createRequest(requestDetails); + + try { + extendedDetails = closeableHttpClient.execute(request.get(), aaiResponseHandler()); + } catch (IOException e) { + logger.error("Exception while executing HTTP request: {}", e); + } + + return extendedDetails; + } + + + private URI createAAIExtendedURI(final String path, String pnfName) { + + URI extendedURI = null; + + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(path + "/" + pnfName); + + try { + extendedURI = uriBuilder.build(); + logger.info("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.error("Exception while building extended URI: {}", e); + } + + return extendedURI; + } + + private ResponseHandler> aaiResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.info("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode) ) { + logger.info("HTTP response successful."); + final String aaiResponse = EntityUtils.toString(responseEntity); + return Optional.of(aaiResponse); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.error("HTTP response not successful : {}", aaiResponse); + return Optional.of("" + responseCode); + } + }; + } + + private HttpRequestBase createHttpRequest(URI extendedURI) { + + if (isExtendedURINotNull(extendedURI)) { + return new HttpGet(extendedURI); + } else { + return null; + } + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + private Optional createRequest(HttpRequestDetails requestDetails) { + + final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.pnfName()); + HttpRequestBase request = createHttpRequest(extendedURI); + requestDetails.headers().forEach(request::addHeader); + return Optional.of(request); + } +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java deleted file mode 100644 index 20ae8d0b..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java +++ /dev/null @@ -1,188 +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.service; - -import org.apache.commons.lang3.StringUtils; -import org.apache.http.HttpEntity; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPatch; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.client.utils.URIBuilder; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.util.EntityUtils; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; -import org.onap.dcaegen2.services.utils.HttpRequestDetails; -import org.onap.dcaegen2.services.utils.HttpUtils; -import org.onap.dcaegen2.services.utils.RequestVerbs; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.lang.NonNull; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Iterator; -import java.util.Map; -import java.util.Optional; - -public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { - - Logger logger = LoggerFactory.getLogger(AAIExtendedHttpClientImpl.class); - - private final CloseableHttpClient closeableHttpClient; - private final String aaiHost; - private final String aaiProtocol; - private final Integer aaiHostPortNumber; - - - public AAIExtendedHttpClientImpl (AAIHttpClientConfiguration aaiHttpClientConfiguration) { - final AAIHttpClient aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientConfiguration); - closeableHttpClient = aaiHttpClient.getAAIHttpClient(); - aaiHost = aaiHttpClientConfiguration.aaiHost(); - aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); - aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); - } - - @Override - public Optional getHttpResponse(HttpRequestDetails requestDetails) { - - Optional extendedDetails = Optional.empty(); - Optional request = createRequest(requestDetails); - - try { - extendedDetails = closeableHttpClient.execute(request.get(), aaiResponseHandler()); - } catch (IOException e) { - logger.error("Exception while executing HTTP request: {}", e); - } - - return extendedDetails; - } - - private URI createAAIExtendedURI(final String path, Map queryParams) { - - URI extendedURI = null; - - final URIBuilder uriBuilder = new URIBuilder().setScheme(this.aaiProtocol).setHost(this.aaiHost) - .setPort(this.aaiHostPortNumber) - .setPath(path); - final String customQuery = createCustomQuery(queryParams); - - if (StringUtils.isNoneBlank(customQuery)) { - uriBuilder.setCustomQuery(customQuery); - } - - try { - extendedURI = uriBuilder.build(); - logger.info("Building extended URI: {}", extendedURI); - } catch (URISyntaxException e) { - logger.error("Exception while building extended URI: {}", e); - } - - return extendedURI; - } - - private String createCustomQuery(@NonNull final Map queryParams) { - final StringBuilder queryStringBuilder = new StringBuilder(""); - final Iterator> queryParamIterator = queryParams.entrySet().iterator(); - - while (queryParamIterator.hasNext()) { - final Map.Entry queryParamsEntry = queryParamIterator.next(); - queryStringBuilder.append(queryParamsEntry.getKey()).append("=").append(queryParamsEntry.getValue()); - if (queryParamIterator.hasNext()) { - queryStringBuilder.append("&"); - } - } - - return queryStringBuilder.toString(); - } - - private ResponseHandler> aaiResponseHandler() { - return httpResponse -> { - final int responseCode = httpResponse.getStatusLine().getStatusCode(); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode) && responseEntity != null) { - logger.info("HTTP response successful."); - final String aaiResponse = EntityUtils.toString(responseEntity); - return Optional.of(aaiResponse); - } else { - String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.error("HTTP response not successful : {}", aaiResponse); - return Optional.empty(); - } - }; - } - - private HttpRequestBase createHttpRequest(URI extendedURI, HttpRequestDetails httpRequestDetails) { - if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.GET))) { - return new HttpGet(extendedURI); - } else if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.PUT))) { - return new HttpPut(extendedURI); - } else if (isExtendedURINotNull(extendedURI) && - isPatchRequestValid(httpRequestDetails.requestVerb(),httpRequestDetails.jsonBody())) { - return createHttpPatch(extendedURI, httpRequestDetails.jsonBody()); - } else { - return null; - } - } - - private Boolean isExtendedURINotNull(URI extendedURI) { - return extendedURI != null; - } - - private Optional createStringEntity(Optional jsonBody) { - return Optional.of(parseJson(jsonBody).get()); - } - - private HttpPatch createHttpPatch(URI extendedURI, Optional jsonBody) { - HttpPatch httpPatch = new HttpPatch(extendedURI); - Optional stringEntity = createStringEntity(jsonBody); - httpPatch.setEntity(stringEntity.get()); - return httpPatch; - } - - private Optional parseJson(Optional jsonBody) { - Optional stringEntity = Optional.empty(); - - try { - stringEntity = Optional.of(new StringEntity(jsonBody.get())); - } catch (UnsupportedEncodingException e) { - logger.error("Exception while parsing JSON: {}", e); - } - - return stringEntity; - } - - private Boolean isPatchRequestValid(RequestVerbs requestVerb, Optional jsonBody) { - return requestVerb == RequestVerbs.PATCH && jsonBody.isPresent(); - } - - private Optional createRequest(HttpRequestDetails requestDetails) { - - final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.queryParameters()); - HttpRequestBase request = createHttpRequest(extendedURI, requestDetails); - requestDetails.headers().forEach(request::addHeader); - return Optional.of(request); - } -} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java deleted file mode 100644 index c60027c2..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java +++ /dev/null @@ -1,29 +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.service; - -import org.apache.http.impl.client.CloseableHttpClient; - -@FunctionalInterface -public interface AAIHttpClient { - CloseableHttpClient getAAIHttpClient(); -} - diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java deleted file mode 100644 index 90b551db..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java +++ /dev/null @@ -1,93 +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.service; - -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.Credentials; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.ssl.SSLContextBuilder; -import org.apache.http.ssl.TrustStrategy; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; - -public class AAIHttpClientImpl implements AAIHttpClient { - - Logger logger = LoggerFactory.getLogger(AAIHttpClientImpl.class); - - private AAIHttpClientConfiguration aaiHttpClientConfig; - - @Autowired - public AAIHttpClientImpl(AAIHttpClientConfiguration aaiHttpClientConfiguration) { - this.aaiHttpClientConfig = aaiHttpClientConfiguration; - } - - @Override - public CloseableHttpClient getAAIHttpClient() { - - final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); - final boolean aaiIgnoreSSLCertificateErrors = aaiHttpClientConfig.aaiIgnoreSSLCertificateErrors(); - - TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; - - if (aaiIgnoreSSLCertificateErrors) { - try { - logger.info("Setting SSL Context for AAI HTTP Client"); - httpClientBuilder.setSSLContext(new SSLContextBuilder() - .loadTrustMaterial(null, acceptingTrustStrategy) - .build()); - - } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) { - logger.error("Exception while setting SSL Context for AAI HTTP Client: {}", e); - } - - httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); - } - - final String aaiUserName = aaiHttpClientConfig.aaiUserName(); - - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - - if (aaiUserName != null) { - final String aaiHost = aaiHttpClientConfig.aaiHost(); - final Integer aaiHostPortNumber = aaiHttpClientConfig.aaiHostPortNumber(); - final String aaiUserPassword = aaiHttpClientConfig.aaiUserPassword(); - final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); - final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); - credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); - } - - httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - - return httpClientBuilder.build(); - } -} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java new file mode 100644 index 00000000..0b82578a --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java @@ -0,0 +1,155 @@ +/*- + * ============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.service; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.HttpUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; + +public class AAIProducerClient implements AAIExtendedHttpClient { + Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); + + private final CloseableHttpClient closeableHttpClient; + private final String aaiHost; + private final String aaiProtocol; + private final Integer aaiHostPortNumber; + + + public AAIProducerClient(AAIClientConfiguration aaiHttpClientConfiguration) { + final AAIClient aaiHttpClient = new AAIClientImpl(aaiHttpClientConfiguration); + closeableHttpClient = aaiHttpClient.getAAIHttpClient(); + aaiHost = aaiHttpClientConfiguration.aaiHost(); + aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); + } + + @Override + public Optional getHttpResponse(HttpRequestDetails requestDetails) { + + Optional extendedDetails = Optional.empty(); + Optional request = createRequest(requestDetails); + + try { + extendedDetails = closeableHttpClient.execute(request.get(), aaiResponseHandler()); + } catch (IOException e) { + logger.error("Exception while executing HTTP request: {}", e); + } + + return extendedDetails; + } + + private URI createAAIExtendedURI(final String path, final String pnfName) { + + URI extendedURI = null; + + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(path + "/" + pnfName); + + try { + extendedURI = uriBuilder.build(); + logger.info("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.error("Exception while building extended URI: {}", e); + } + + return extendedURI; + } + + private ResponseHandler> aaiResponseHandler() { + return (HttpResponse httpResponse) -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.info("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { + logger.info("HTTP response successful."); + return Optional.of("" + responseCode); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.error("HTTP response not successful : {}", aaiResponse); + return Optional.of("" + responseCode); + } + }; + } + + private HttpRequestBase createHttpRequest(URI extendedURI, HttpRequestDetails httpRequestDetails) { + if (isExtendedURINotNull(extendedURI) && httpRequestDetails.jsonBody().isPresent()) { + return createHttpPatch(extendedURI, httpRequestDetails.jsonBody()); + } else { + return null; + } + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + private Optional createStringEntity(Optional jsonBody) { + return Optional.of(parseJson(jsonBody).get()); + } + + private HttpPatch createHttpPatch(URI extendedURI, Optional jsonBody) { + HttpPatch httpPatch = new HttpPatch(extendedURI); + Optional stringEntity = createStringEntity(jsonBody); + httpPatch.setEntity(stringEntity.get()); + return httpPatch; + } + + private Optional parseJson(Optional jsonBody) { + Optional stringEntity = Optional.empty(); + + try { + stringEntity = Optional.of(new StringEntity(jsonBody.get())); + } catch (UnsupportedEncodingException e) { + logger.error("Exception while parsing JSON: {}", e); + } + + return stringEntity; + } + + private Optional createRequest(HttpRequestDetails requestDetails) { + + final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.pnfName()); + HttpRequestBase request = createHttpRequest(extendedURI, requestDetails); + requestDetails.headers().forEach(request::addHeader); + return Optional.of(request); + } +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java index 574cb22f..37e6b860 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java @@ -39,11 +39,8 @@ public abstract class HttpRequestDetails implements Serializable { public abstract Optional jsonBody(); @Value.Parameter - public abstract Map queryParameters(); + public abstract String pnfName(); @Value.Parameter public abstract Map headers(); - - @Value.Parameter - public abstract RequestVerbs requestVerb(); } diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIConsumerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIConsumerClientTest.java new file mode 100644 index 00000000..b95cc8d6 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIConsumerClientTest.java @@ -0,0 +1,92 @@ +/*- + * ============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.service; + +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AAIConsumerClientTest { + + private static AAIConsumerClient testedObject; + private static AAIClientConfiguration aaiHttpClientConfigurationMock = mock(AAIClientConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + private static HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.class); + private static final String JSON_MESSAGE = "{ \"pnf-id\": \"example-pnf-id-val-22343\", \"regional-resource-zone\":null, \"ipaddress-v4-oam\": \"11.22.33.44\" }"; + + @BeforeAll + public static void setup() throws NoSuchFieldException, IllegalAccessException { + + Map aaiHeaders = new HashMap<>(); + aaiHeaders.put("X-FromAppId", "prh"); + aaiHeaders.put("X-TransactionId", "9999"); + aaiHeaders.put("Accept", "application/json"); + aaiHeaders.put("authentication", "Basic QUFJOkFBSQ=="); + aaiHeaders.put("Real-Time", "true"); + aaiHeaders.put("Content-Type", "application/json"); + + when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); + when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); + when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); + + when(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf"); + when(httpRequestDetailsMock.headers()).thenReturn(aaiHeaders); + when(httpRequestDetailsMock.pnfName()).thenReturn("pnf-nokia-45fsfcx"); + when(httpRequestDetailsMock.jsonBody()).thenReturn(Optional.of(JSON_MESSAGE)); + + testedObject = new AAIConsumerClient(aaiHttpClientConfigurationMock); + setField(); + } + + + @Test + public void getExtendedDetails_returnsSuccess() throws IOException { + + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). + thenReturn(Optional.of(JSON_MESSAGE)); + Optional actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + Assertions.assertEquals(Optional.of(JSON_MESSAGE),actualResult); + } + + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(testedObject, closeableHttpClientMock); + } +} diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java deleted file mode 100644 index 9ff34b8e..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java +++ /dev/null @@ -1,137 +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.service; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPatch; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.impl.client.CloseableHttpClient; -import org.immutables.value.internal.$processor$.meta.$GsonMirrors; -import org.junit.Ignore; -import org.junit.jupiter.api.*; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; -import org.onap.dcaegen2.services.utils.HttpRequestDetails; -import org.onap.dcaegen2.services.utils.RequestVerbs; - -public class AAIExtendedHttpClientImplTest { - - private static AAIExtendedHttpClientImpl testedObject; - private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class); - private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); - private static HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.class); - private static Optional expectedResult = Optional.empty(); - private static final String JSON_MESSAGE = "{ \"ipaddress-v4-oam\": \"11.22.33.44\" }"; - private static final String PNF_ID = "NOKQTFCOC540002E"; - - @BeforeAll - public static void init() throws NoSuchFieldException, IllegalAccessException { - - Map queryParams = new HashMap<>(); - queryParams.put("pnf-id", PNF_ID); - - Map 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"); - - when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); - when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); - when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); - when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); - when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); - - when(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf"); - when(httpRequestDetailsMock.headers()).thenReturn(aaiHeaders); - when(httpRequestDetailsMock.queryParameters()).thenReturn(queryParams); - when(httpRequestDetailsMock.jsonBody()).thenReturn(Optional.of(JSON_MESSAGE)); - - testedObject = new AAIExtendedHttpClientImpl(aaiHttpClientConfigurationMock); - setField(); - } - - @AfterAll - public static void teardown() { - testedObject = null; - aaiHttpClientConfigurationMock = null; - closeableHttpClientMock = null; - httpRequestDetailsMock = null; - expectedResult = null; - } - - @Test - public void getHttpResponsePatch_success() throws IOException { - when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PATCH); - - expectedResult = Optional.of(JSON_MESSAGE); - - when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) - .thenReturn(expectedResult); - Optional actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); - - Assertions.assertEquals(expectedResult.get(), actualResult.get()); - } - - @Test - public void getHttpResponsePut_success() throws IOException { - when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT); - - expectedResult = Optional.of("getExtendedDetailsOK"); - - when(closeableHttpClientMock.execute(any(HttpPut.class), any(ResponseHandler.class))). - thenReturn(expectedResult); - Optional actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); - - Assertions.assertEquals(expectedResult.get(), actualResult.get()); - } - - @Test - public void getExtendedDetails_returnsNull() throws IOException { - when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.GET); - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). - thenReturn(Optional.empty()); - Optional actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); - Assertions.assertEquals(Optional.empty(),actualResult); - } - - @Test - public void getHttpResponsePut_failure() { - when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT); - - } - - private static void setField() throws NoSuchFieldException, IllegalAccessException { - Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); - field.setAccessible(true); - field.set(testedObject, closeableHttpClientMock); - } -} diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java index fd13f1e7..cfe1a7f6 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java @@ -21,7 +21,7 @@ package org.onap.dcaegen2.services.service; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; @@ -30,13 +30,13 @@ import static org.mockito.Mockito.when; public class AAIHttpClientImplTest { - private static AAIHttpClientImpl testedObject; - private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock; + private static AAIClientImpl testedObject; + private static AAIClientConfiguration aaiHttpClientConfigurationMock; @BeforeAll public static void setup() { - aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class); + aaiHttpClientConfigurationMock = mock(AAIClientConfiguration.class); when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); @@ -44,7 +44,7 @@ public class AAIHttpClientImplTest { when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PNF"); when(aaiHttpClientConfigurationMock.aaiIgnoreSSLCertificateErrors()).thenReturn(true); - testedObject = new AAIHttpClientImpl(aaiHttpClientConfigurationMock); + testedObject = new AAIClientImpl(aaiHttpClientConfigurationMock); } @Test diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIProducerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIProducerClientTest.java new file mode 100644 index 00000000..7f3978bd --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIProducerClientTest.java @@ -0,0 +1,93 @@ +/* + * ============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.service; + +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AAIProducerClientTest { + + private static AAIProducerClient testedObject; + private static AAIClientConfiguration aaiHttpClientConfigurationMock = mock(AAIClientConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + private static HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.class); + private static final String JsonBody = "{\"ipaddress-v4-oam\":\"11.22.33.155\"}"; + private static final String SUCCESS = "200"; + private static final String PNF_NAME = "nokia-pnf-nhfsadhff"; + + @BeforeAll + public static void init() throws NoSuchFieldException, IllegalAccessException { + + Map 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"); + + when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("eucalyptus.es-si-eu-dhn-20.eecloud.nsn-net.net"); + when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); + when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); + + when(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf"); + + when(httpRequestDetailsMock.headers()).thenReturn(aaiHeaders); + when(httpRequestDetailsMock.pnfName()).thenReturn(PNF_NAME); + when(httpRequestDetailsMock.jsonBody()).thenReturn(Optional.of(JsonBody)); + + testedObject = new AAIProducerClient(aaiHttpClientConfigurationMock); + setField(); + } + + @Test + public void getHttpResponsePatch_success() throws IOException { + + when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) + .thenReturn(Optional.of(SUCCESS)); + Optional actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + + Assertions.assertEquals(SUCCESS, actualResult.get()); + } + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(testedObject, closeableHttpClientMock); + } +} diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java index 76c2969a..29a00927 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java @@ -23,12 +23,12 @@ package org.onap.dcaegen2.services.service.config; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; -import org.onap.dcaegen2.services.config.ImmutableAAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.config.ImmutableAAIClientConfiguration; public class AAIHttpClientConfigurationTest { - private static AAIHttpClientConfiguration client; + private static AAIClientConfiguration client; private static final String AAI_HOST = "/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E"; private static final Integer PORT = 1234; private static final String PROTOCOL = "https"; @@ -36,7 +36,7 @@ public class AAIHttpClientConfigurationTest { @BeforeAll public static void init() { - client = new ImmutableAAIHttpClientConfiguration.Builder() + client = new ImmutableAAIClientConfiguration.Builder() .aaiHost(AAI_HOST) .aaiHostPortNumber(PORT) .aaiProtocol(PROTOCOL) diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java index 563e6921..0deb8adb 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java @@ -36,19 +36,16 @@ public class HttpRequestDetailsTest { private static HttpRequestDetails testObject; private static final String AAI_PATH = "aaiPathTest"; - private static final RequestVerbs HTTP_VERB = RequestVerbs.PATCH; - private static final String QUERY_KEY1 = "queryKey1"; - private static final String QUERY_VALUE1 = "queryValue1"; private static final String HEADERS_KEY1 = "headersKey1"; private static final String HEADERS_VALUE1 = "headersValue1"; private static final String JSON_MESSAGE = "{\"dare_to\": \"dream_big\"}"; + private static final String PNF_NAME = "pnf-nokia-5454885485"; @BeforeAll public static void init() { testObject = ImmutableHttpRequestDetails.builder() .aaiAPIPath(AAI_PATH) - .requestVerb(HTTP_VERB) - .putQueryParameters(QUERY_KEY1,QUERY_VALUE1) + .pnfName(PNF_NAME) .putHeaders(HEADERS_KEY1,HEADERS_VALUE1) .jsonBody(JSON_MESSAGE) .build(); @@ -58,8 +55,7 @@ public class HttpRequestDetailsTest { public void testGetters_success() { Assertions.assertEquals(AAI_PATH, testObject.aaiAPIPath()); Assertions.assertEquals(HEADERS_VALUE1, testObject.headers().get(HEADERS_KEY1)); - Assertions.assertEquals(QUERY_VALUE1, testObject.queryParameters().get(QUERY_KEY1)); - Assertions.assertEquals(RequestVerbs.PATCH, testObject.requestVerb()); + Assertions.assertEquals(PNF_NAME, testObject.pnfName()); Assertions.assertEquals(Optional.of(JSON_MESSAGE), testObject.jsonBody()); } } diff --git a/prh-app-server/pom.xml b/prh-app-server/pom.xml index e804c3e6..cb85db11 100644 --- a/prh-app-server/pom.xml +++ b/prh-app-server/pom.xml @@ -232,6 +232,11 @@ prh-dmaap-client ${project.parent.version} + + org.onap.dcaegen2.services.prh + prh-aai-client + 1.0.0-SNAPSHOT + diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java index 76633d26..15db50e1 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java @@ -19,17 +19,13 @@ */ package org.onap.dcaegen2.services.prh.configuration; -import java.util.Optional; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; -import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; -import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; -import org.onap.dcaegen2.services.config.ImmutableAAIHttpClientConfiguration; -import org.onap.dcaegen2.services.config.ImmutableDmaapConsumerConfiguration; -import org.onap.dcaegen2.services.config.ImmutableDmaapPublisherConfiguration; +import org.onap.dcaegen2.services.config.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; +import java.util.Optional; + /** * @author Przemysław Wąsala on 4/9/18 */ @@ -38,26 +34,26 @@ import org.springframework.stereotype.Component; @Configuration public class AppConfig extends PrhAppConfig { - @Value("${dmaap.dmaapConsumerConfiguration.dmmapHostName:}") - public String consumerDmmapHostName; + @Value("${dmaap.dmaapConsumerConfiguration.dmaapHostName:}") + public String consumerDmaapHostName; - @Value("${dmaap.dmaapConsumerConfiguration.dmmapPortNumber:}") - public Integer consumerDmmapPortNumber; + @Value("${dmaap.dmaapConsumerConfiguration.dmaapPortNumber:}") + public Integer consumerDmaapPortNumber; - @Value("${dmaap.dmaapConsumerConfiguration.dmmapTopicName:}") - public String consumerDmmapTopicName; + @Value("${dmaap.dmaapConsumerConfiguration.dmaapTopicName:}") + public String consumerDmaapTopicName; - @Value("${dmaap.dmaapConsumerConfiguration.dmmapProtocol:}") - public String consumerDmmapProtocol; + @Value("${dmaap.dmaapConsumerConfiguration.dmaapProtocol:}") + public String consumerDmaapProtocol; - @Value("${dmaap.dmaapConsumerConfiguration.dmmapUserName:}") - public String consumerDmmapUserName; + @Value("${dmaap.dmaapConsumerConfiguration.dmaapUserName:}") + public String consumerDmaapUserName; - @Value("${dmaap.dmaapConsumerConfiguration.dmmapUserPassword:}") - public String consumerDmmapUserPassword; + @Value("${dmaap.dmaapConsumerConfiguration.dmaapUserPassword:}") + public String consumerDmaapUserPassword; - @Value("${dmaap.dmaapConsumerConfiguration.dmmapContentType:}") - public String consumerDmmapContentType; + @Value("${dmaap.dmaapConsumerConfiguration.dmaapContentType:}") + public String consumerDmaapContentType; @Value("${dmaap.dmaapConsumerConfiguration.consumerId:}") public String consumerId; @@ -71,26 +67,26 @@ public class AppConfig extends PrhAppConfig { @Value("${dmaap.dmaapConsumerConfiguration.message-limit:}") public Integer consumerMessageLimit; - @Value("${dmaap.dmaapProducerConfiguration.dmmapHostName:}") - public String producerDmmapHostName; + @Value("${dmaap.dmaapProducerConfiguration.dmaapHostName:}") + public String producerDmaapHostName; - @Value("${dmaap.dmaapProducerConfiguration.dmmapPortNumber:}") - public Integer producerDmmapPortNumber; + @Value("${dmaap.dmaapProducerConfiguration.dmaapPortNumber:}") + public Integer producerDmaapPortNumber; - @Value("${dmaap.dmaapProducerConfiguration.dmmapTopicName:}") - public String producerDmmapTopicName; + @Value("${dmaap.dmaapProducerConfiguration.dmaapTopicName:}") + public String producerDmaapTopicName; - @Value("${dmaap.dmaapProducerConfiguration.dmmapProtocol:}") - public String producerDmmapProtocol; + @Value("${dmaap.dmaapProducerConfiguration.dmaapProtocol:}") + public String producerDmaapProtocol; - @Value("${dmaap.dmaapProducerConfiguration.dmmapUserName:}") - public String producerDmmapUserName; + @Value("${dmaap.dmaapProducerConfiguration.dmaapUserName:}") + public String producerDmaapUserName; - @Value("${dmaap.dmaapProducerConfiguration.dmmapUserPassword:}") - public String producerDmmapUserPassword; + @Value("${dmaap.dmaapProducerConfiguration.dmaapUserPassword:}") + public String producerDmaapUserPassword; - @Value("${dmaap.dmaapProducerConfiguration.dmmapContentType:}") - public String producerDmmapContentType; + @Value("${dmaap.dmaapProducerConfiguration.dmaapContentType:}") + public String producerDmaapContentType; @Value("${aai.aaiHttpClientConfiguration.aaiHost:}") public String aaiHost; @@ -113,63 +109,63 @@ public class AppConfig extends PrhAppConfig { @Override public DmaapConsumerConfiguration getDmaapConsumerConfiguration() { return new ImmutableDmaapConsumerConfiguration.Builder() - .dmaapUserPassword( - Optional.ofNullable(consumerDmmapUserPassword).orElse(dmaapConsumerConfiguration.dmaapUserPassword())) - .dmaapUserName( - Optional.ofNullable(consumerDmmapUserName).orElse(dmaapConsumerConfiguration.dmaapUserName())) - .dmaapHostName( - Optional.ofNullable(consumerDmmapHostName).orElse(dmaapConsumerConfiguration.dmaapHostName())) - .dmaapPortNumber( - Optional.ofNullable(consumerDmmapPortNumber).orElse(dmaapConsumerConfiguration.dmaapPortNumber())) - .dmaapProtocol( - Optional.ofNullable(consumerDmmapProtocol).orElse(dmaapConsumerConfiguration.dmaapProtocol())) - .dmaapContentType( - Optional.ofNullable(consumerDmmapContentType).orElse(dmaapConsumerConfiguration.dmaapContentType())) - .dmaapTopicName( - Optional.ofNullable(consumerDmmapTopicName).orElse(dmaapConsumerConfiguration.dmaapTopicName())) - .messageLimit( - Optional.ofNullable(consumerMessageLimit).orElse(dmaapConsumerConfiguration.messageLimit())) - .timeoutMS(Optional.ofNullable(consumerTimeoutMS).orElse(dmaapConsumerConfiguration.timeoutMS())) - .consumerGroup(Optional.ofNullable(consumerGroup).orElse(dmaapConsumerConfiguration.consumerGroup())) - .consumerId(Optional.ofNullable(consumerId).orElse(dmaapConsumerConfiguration.consumerId())) - .build(); + .dmaapUserPassword( + Optional.ofNullable(consumerDmaapUserPassword).orElse(dmaapConsumerConfiguration.dmaapUserPassword())) + .dmaapUserName( + Optional.ofNullable(consumerDmaapUserName).orElse(dmaapConsumerConfiguration.dmaapUserName())) + .dmaapHostName( + Optional.ofNullable(consumerDmaapHostName).orElse(dmaapConsumerConfiguration.dmaapHostName())) + .dmaapPortNumber( + Optional.ofNullable(consumerDmaapPortNumber).orElse(dmaapConsumerConfiguration.dmaapPortNumber())) + .dmaapProtocol( + Optional.ofNullable(consumerDmaapProtocol).orElse(dmaapConsumerConfiguration.dmaapProtocol())) + .dmaapContentType( + Optional.ofNullable(consumerDmaapContentType).orElse(dmaapConsumerConfiguration.dmaapContentType())) + .dmaapTopicName( + Optional.ofNullable(consumerDmaapTopicName).orElse(dmaapConsumerConfiguration.dmaapTopicName())) + .messageLimit( + Optional.ofNullable(consumerMessageLimit).orElse(dmaapConsumerConfiguration.messageLimit())) + .timeoutMS(Optional.ofNullable(consumerTimeoutMS).orElse(dmaapConsumerConfiguration.timeoutMS())) + .consumerGroup(Optional.ofNullable(consumerGroup).orElse(dmaapConsumerConfiguration.consumerGroup())) + .consumerId(Optional.ofNullable(consumerId).orElse(dmaapConsumerConfiguration.consumerId())) + .build(); } @Override - public AAIHttpClientConfiguration getAAIHttpClientConfiguration() { - return new ImmutableAAIHttpClientConfiguration.Builder() - .aaiHost(Optional.ofNullable(aaiHost).orElse(aaiHttpClientConfiguration.aaiHost())) - .aaiHostPortNumber( - Optional.ofNullable(aaiHostPortNumber).orElse(aaiHttpClientConfiguration.aaiHostPortNumber())) - .aaiIgnoreSSLCertificateErrors( - Optional.ofNullable(aaiIgnoreSSLCertificateErrors) - .orElse(aaiHttpClientConfiguration.aaiIgnoreSSLCertificateErrors())) - .aaiProtocol(Optional.ofNullable(aaiProtocol).orElse(aaiHttpClientConfiguration.aaiProtocol())) - .aaiUserName( - Optional.ofNullable(aaiUserName).orElse(aaiHttpClientConfiguration.aaiUserName())) - .aaiUserPassword(Optional.ofNullable( - aaiUserPassword).orElse(aaiHttpClientConfiguration.aaiUserPassword())) - .build(); + public AAIClientConfiguration getAAIClientConfiguration() { + return new ImmutableAAIClientConfiguration.Builder() + .aaiHost(Optional.ofNullable(aaiHost).orElse(aaiClientConfiguration.aaiHost())) + .aaiHostPortNumber( + Optional.ofNullable(aaiHostPortNumber).orElse(aaiClientConfiguration.aaiHostPortNumber())) + .aaiIgnoreSSLCertificateErrors( + Optional.ofNullable(aaiIgnoreSSLCertificateErrors) + .orElse(aaiClientConfiguration.aaiIgnoreSSLCertificateErrors())) + .aaiProtocol(Optional.ofNullable(aaiProtocol).orElse(aaiClientConfiguration.aaiProtocol())) + .aaiUserName( + Optional.ofNullable(aaiUserName).orElse(aaiClientConfiguration.aaiUserName())) + .aaiUserPassword(Optional.ofNullable( + aaiUserPassword).orElse(aaiClientConfiguration.aaiUserPassword())) + .build(); } @Override public DmaapPublisherConfiguration getDmaapPublisherConfiguration() { return new ImmutableDmaapPublisherConfiguration.Builder() - .dmaapContentType( - Optional.ofNullable(producerDmmapContentType).orElse(dmaapPublisherConfiguration.dmaapContentType())) - .dmaapHostName( - Optional.ofNullable(producerDmmapHostName).orElse(dmaapPublisherConfiguration.dmaapHostName())) - .dmaapPortNumber( - Optional.ofNullable(producerDmmapPortNumber).orElse(dmaapPublisherConfiguration.dmaapPortNumber())) - .dmaapProtocol( - Optional.ofNullable(producerDmmapProtocol).orElse(dmaapPublisherConfiguration.dmaapProtocol())) - .dmaapTopicName( - Optional.ofNullable(producerDmmapTopicName).orElse(dmaapPublisherConfiguration.dmaapTopicName())) - .dmaapUserName( - Optional.ofNullable(producerDmmapUserName).orElse(dmaapPublisherConfiguration.dmaapUserName())) - .dmaapUserPassword( - Optional.ofNullable(producerDmmapUserPassword).orElse(dmaapPublisherConfiguration.dmaapUserPassword())) - .build(); + .dmaapContentType( + Optional.ofNullable(producerDmaapContentType).orElse(dmaapPublisherConfiguration.dmaapContentType())) + .dmaapHostName( + Optional.ofNullable(producerDmaapHostName).orElse(dmaapPublisherConfiguration.dmaapHostName())) + .dmaapPortNumber( + Optional.ofNullable(producerDmaapPortNumber).orElse(dmaapPublisherConfiguration.dmaapPortNumber())) + .dmaapProtocol( + Optional.ofNullable(producerDmaapProtocol).orElse(dmaapPublisherConfiguration.dmaapProtocol())) + .dmaapTopicName( + Optional.ofNullable(producerDmaapTopicName).orElse(dmaapPublisherConfiguration.dmaapTopicName())) + .dmaapUserName( + Optional.ofNullable(producerDmaapUserName).orElse(dmaapPublisherConfiguration.dmaapUserName())) + .dmaapUserPassword( + Optional.ofNullable(producerDmaapUserPassword).orElse(dmaapPublisherConfiguration.dmaapUserPassword())) + .build(); } } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java index 8dd2bf3b..605a1bd7 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java @@ -19,7 +19,7 @@ */ package org.onap.dcaegen2.services.prh.configuration; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; @@ -30,7 +30,7 @@ public interface Config { DmaapConsumerConfiguration getDmaapConsumerConfiguration(); - AAIHttpClientConfiguration getAAIHttpClientConfiguration(); + AAIClientConfiguration getAAIClientConfiguration(); DmaapPublisherConfiguration getDmaapPublisherConfiguration(); diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java index 8720cf47..0473c1aa 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java @@ -19,24 +19,8 @@ */ package org.onap.dcaegen2.services.prh.configuration; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; -import com.google.gson.TypeAdapterFactory; -import java.io.BufferedInputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.ServiceLoader; -import javax.validation.constraints.NotEmpty; -import javax.validation.constraints.NotNull; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import com.google.gson.*; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; import org.slf4j.Logger; @@ -45,6 +29,13 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.io.*; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ServiceLoader; + /** * @author Przemysław Wąsala on 4/9/18 */ @@ -63,7 +54,7 @@ public abstract class PrhAppConfig implements Config { private static final Logger logger = LoggerFactory.getLogger(PrhAppConfig.class); private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); - AAIHttpClientConfiguration aaiHttpClientConfiguration; + AAIClientConfiguration aaiClientConfiguration; DmaapConsumerConfiguration dmaapConsumerConfiguration; @@ -79,8 +70,8 @@ public abstract class PrhAppConfig implements Config { } @Override - public AAIHttpClientConfiguration getAAIHttpClientConfiguration() { - return aaiHttpClientConfiguration; + public AAIClientConfiguration getAAIClientConfiguration() { + return aaiClientConfiguration; } @Override @@ -99,41 +90,41 @@ public abstract class PrhAppConfig implements Config { JsonElement rootElement = parser.parse(new InputStreamReader(inputStream)); if (rootElement.isJsonObject()) { jsonObject = rootElement.getAsJsonObject(); - aaiHttpClientConfiguration = deserializeType(gsonBuilder, - jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(AAI).getAsJsonObject(AAI_CONFIG), - AAIHttpClientConfiguration.class); + aaiClientConfiguration = deserializeType(gsonBuilder, + jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(AAI).getAsJsonObject(AAI_CONFIG), + AAIClientConfiguration.class); dmaapConsumerConfiguration = deserializeType(gsonBuilder, - jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER), - DmaapConsumerConfiguration.class); + jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER), + DmaapConsumerConfiguration.class); dmaapPublisherConfiguration = deserializeType(gsonBuilder, - jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER), - DmaapPublisherConfiguration.class); + jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER), + DmaapPublisherConfiguration.class); } } catch (FileNotFoundException e) { logger - .error( - "Configuration PrhAppConfig initFileStreamReader()::FileNotFoundException :: Execution Time - {}:{}", - dateTimeFormatter.format( - LocalDateTime.now()), e); + .error( + "Configuration PrhAppConfig initFileStreamReader()::FileNotFoundException :: Execution Time - {}:{}", + dateTimeFormatter.format( + LocalDateTime.now()), e); } catch (IOException e) { logger - .error( - "Configuration PrhAppConfig initFileStreamReader()::IOException :: Execution Time - {}:{}", - dateTimeFormatter.format( - LocalDateTime.now()), e); + .error( + "Configuration PrhAppConfig initFileStreamReader()::IOException :: Execution Time - {}:{}", + dateTimeFormatter.format( + LocalDateTime.now()), e); } catch (JsonSyntaxException e) { logger - .error( - "Configuration PrhAppConfig initFileStreamReader()::JsonSyntaxException :: Execution Time - {}:{}", - dateTimeFormatter.format( - LocalDateTime.now()), e); + .error( + "Configuration PrhAppConfig initFileStreamReader()::JsonSyntaxException :: Execution Time - {}:{}", + dateTimeFormatter.format( + LocalDateTime.now()), e); } } private T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject, - @NotNull Class type) { + @NotNull Class type) { return gsonBuilder.create().fromJson(jsonObject, type); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java index 95b360e1..b91b56c5 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/HeartbeatController.java @@ -21,6 +21,7 @@ package org.onap.dcaegen2.services.prh.controllers; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; + import org.onap.dcaegen2.services.prh.configuration.PrhAppConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/ScheduleController.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/ScheduleController.java index 6f08dd8a..733b7dfc 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/ScheduleController.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/ScheduleController.java @@ -24,6 +24,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ScheduledFuture; + import org.onap.dcaegen2.services.prh.configuration.PrhAppConfig; import org.onap.dcaegen2.services.prh.tasks.ScheduledTasks; import org.slf4j.Logger; diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTask.java new file mode 100644 index 00000000..5a3afd86 --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTask.java @@ -0,0 +1,30 @@ +/*- + * ============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.tasks; + +import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; + +public abstract class AAIConsumerTask extends Task { + + protected abstract void consume() throws AAINotFoundException; + + protected abstract T resolveConfiguration(); +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java new file mode 100644 index 00000000..64ccad5c --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java @@ -0,0 +1,89 @@ +/*- + * ============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.tasks; + +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.configuration.AppConfig; +import org.onap.dcaegen2.services.prh.configuration.Config; +import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; +import org.onap.dcaegen2.services.service.AAIProducerClient; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Optional; + +@Component +public class AAIConsumerTaskImpl extends AAIConsumerTask { + + private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); + private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + + private final Config prhAppConfig; + private final HttpRequestDetails requestDetails; + private AAIProducerClient producerClient; + public Optional response; + + @Autowired + public AAIConsumerTaskImpl(AppConfig prhAppConfig, HttpRequestDetails requestDetails) { + this.prhAppConfig = prhAppConfig; + this.requestDetails = requestDetails; + } + + @Override + protected void consume() throws AAINotFoundException { + logger.debug("Start task AAIConsumerTask::publish() :: Execution Time - {}", dateTimeFormatter.format( + LocalDateTime.now())); + + + producerClient = new AAIProducerClient(prhAppConfig.getAAIClientConfiguration()); + + response = producerClient.getHttpResponse(requestDetails); + + logger.debug("End task AAIConsumerTask::publish() :: Execution Time - {}", dateTimeFormatter.format( + LocalDateTime.now())); + + } + + @Override + public ResponseEntity execute(Object object) throws AAINotFoundException { + logger.debug("Start task AAIProducerTaskImpl::execute() :: Execution Time - {}", dateTimeFormatter.format( + LocalDateTime.now())); + consume(); + logger.debug("End task AAIPublisherTaskImpl::execute() :: Execution Time - {}", dateTimeFormatter.format( + LocalDateTime.now())); + return null; + } + + @Override + void initConfigs() { + } + + @Override + protected AAIClientConfiguration resolveConfiguration() { + return prhAppConfig.getAAIClientConfiguration(); + } +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTask.java new file mode 100644 index 00000000..787e614d --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTask.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.tasks; + +import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; + +/** + * @author Przemysław Wąsala on 4/13/18 + */ +public abstract class AAIProducerTask extends Task { + + protected abstract void publish() throws AAINotFoundException; + + protected abstract T resolveConfiguration(); +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java new file mode 100644 index 00000000..a5d173ba --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java @@ -0,0 +1,93 @@ +/* + * ============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.tasks; + +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.configuration.AppConfig; +import org.onap.dcaegen2.services.prh.configuration.Config; +import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; +import org.onap.dcaegen2.services.service.AAIProducerClient; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Optional; + +/** + * @author Przemysław Wąsala on 4/13/18 + */ +@Component +public class AAIProducerTaskImpl extends AAIProducerTask { + + private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); + private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + + private final Config prhAppConfig; + private AAIProducerClient producerClient; + private HttpRequestDetails requestDetails; + private String jsonBody = "{\"ipaddress-v4-oam\":\"11.22.33.155\"}"; + private String pnfName = "example-pnf-name-val-40510"; // pnf name received from dmaap required for URI + public Optional response; + + @Autowired + public AAIProducerTaskImpl(AppConfig prhAppConfig, HttpRequestDetails requestDetails) { + this.prhAppConfig = prhAppConfig; + this.requestDetails = requestDetails; + } + + @Override + protected void publish() throws AAINotFoundException { + logger.debug("Start task AAIConsumerTask::publish() :: Execution Time - {}", dateTimeFormatter.format( + LocalDateTime.now())); + + producerClient = new AAIProducerClient(prhAppConfig.getAAIClientConfiguration()); + + response = producerClient.getHttpResponse(requestDetails); + + logger.debug("End task AAIConsumerTask::publish() :: Execution Time - {}", dateTimeFormatter.format( + LocalDateTime.now())); + + } + + @Override + public ResponseEntity execute(Object object) throws AAINotFoundException { + logger.debug("Start task AAIProducerTaskImpl::execute() :: Execution Time - {}", dateTimeFormatter.format( + LocalDateTime.now())); + publish(); + logger.debug("End task AAIPublisherTaskImpl::execute() :: Execution Time - {}", dateTimeFormatter.format( + LocalDateTime.now())); + return null; + } + + @Override + void initConfigs() { + } + + @Override + protected AAIClientConfiguration resolveConfiguration() { + return prhAppConfig.getAAIClientConfiguration(); + } + +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTask.java deleted file mode 100644 index 8ea4ac8f..00000000 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTask.java +++ /dev/null @@ -1,32 +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.tasks; - -import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; - -/** - * @author Przemysław Wąsala on 4/13/18 - */ -public abstract class AAIPublisherTask extends Task { - - protected abstract void publish() throws AAINotFoundException; - - protected abstract T resolveConfiguration(); -} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskImpl.java deleted file mode 100644 index aa452047..00000000 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskImpl.java +++ /dev/null @@ -1,80 +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.tasks; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; -import org.onap.dcaegen2.services.prh.configuration.AppConfig; -import org.onap.dcaegen2.services.prh.configuration.Config; -import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; - -/** - * @author Przemysław Wąsala on 4/13/18 - */ -@Component -public class AAIPublisherTaskImpl extends AAIPublisherTask { - - private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); - private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); - - private final Config prhAppConfig; - - @Autowired - public AAIPublisherTaskImpl(AppConfig prhAppConfig) { - this.prhAppConfig = prhAppConfig; - - } - - @Override - protected void publish() throws AAINotFoundException { - logger.debug("Start task DmaapConsumerTask::publish() :: Execution Time - {}", dateTimeFormatter.format( - LocalDateTime.now())); - prhAppConfig.getAAIHttpClientConfiguration(); - logger.debug("End task DmaapConsumerTask::publish() :: Execution Time - {}", dateTimeFormatter.format( - LocalDateTime.now())); - - } - - @Override - public ResponseEntity execute(Object object) throws AAINotFoundException { - logger.debug("Start task AAIPublisherTaskImpl::execute() :: Execution Time - {}", dateTimeFormatter.format( - LocalDateTime.now())); - publish(); - logger.debug("End task AAIPublisherTaskImpl::execute() :: Execution Time - {}", dateTimeFormatter.format( - LocalDateTime.now())); - return null; - } - - @Override - void initConfigs() { - } - - @Override - protected AAIHttpClientConfiguration resolveConfiguration() { - return prhAppConfig.getAAIHttpClientConfiguration(); - } - -} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java index 0db49f7c..eb66361d 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java @@ -19,8 +19,6 @@ */ package org.onap.dcaegen2.services.prh.tasks; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; @@ -30,6 +28,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + /** * @author Przemysław Wąsala on 3/23/18 */ diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java index 072f9c7d..ff5f5893 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java @@ -19,8 +19,6 @@ */ package org.onap.dcaegen2.services.prh.tasks; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; @@ -30,6 +28,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + /** * @author Przemysław Wąsala on 4/13/18 */ diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java index c051afbc..2922da1a 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java @@ -39,14 +39,16 @@ public class ScheduledTasks { private final Task dmaapConsumerTask; private final Task dmaapProducerTask; - private final Task aaiPublisherTask; + private final Task aaiProducerTask; + private final Task aaiConsumerTask; @Autowired public ScheduledTasks(DmaapConsumerTask dmaapConsumerTask, DmaapPublisherTask dmaapPublisherTask, - AAIPublisherTask aaiPublisherTask) { + AAIProducerTask aaiPublisherTask, AAIConsumerTask aaiConsumerTask) { this.dmaapConsumerTask = dmaapConsumerTask; this.dmaapProducerTask = dmaapPublisherTask; - this.aaiPublisherTask = aaiPublisherTask; + this.aaiProducerTask = aaiPublisherTask; + this.aaiConsumerTask = aaiConsumerTask; } public void scheduleMainPrhEventTask() { @@ -65,8 +67,10 @@ public class ScheduledTasks { } private void setTaskExecutionFlow() { - dmaapConsumerTask.setNext(aaiPublisherTask); - aaiPublisherTask.setNext(dmaapProducerTask); + dmaapConsumerTask.setNext(aaiProducerTask); + aaiProducerTask.setNext(dmaapProducerTask); + aaiConsumerTask.setNext(aaiConsumerTask); + dmaapProducerTask.setNext(dmaapConsumerTask); } } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java index faecec4d..8094c993 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java @@ -23,19 +23,15 @@ import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.FutureTask; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.dcaegen2.services.prh.IT.junit5.mockito.MockitoExtension; import org.onap.dcaegen2.services.prh.configuration.PrhAppConfig; -import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.tasks.ScheduledTasks; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -77,6 +73,11 @@ class ServiceMockProvider { public PrhAppConfig getPrhAppConfig() { return mock(PrhAppConfig.class); } + + @Bean + public HttpRequestDetails getRequestDetails() { + return mock(HttpRequestDetails.class); + } } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java index 764bafec..e7d55419 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java @@ -88,13 +88,13 @@ class PrhAppConfigTest { prhAppConfig.initFileStreamReader(); appConfig.dmaapConsumerConfiguration = prhAppConfig.getDmaapConsumerConfiguration(); appConfig.dmaapPublisherConfiguration = prhAppConfig.getDmaapPublisherConfiguration(); - appConfig.aaiHttpClientConfiguration = prhAppConfig.getAAIHttpClientConfiguration(); + appConfig.aaiClientConfiguration = prhAppConfig.getAAIClientConfiguration(); // // Then // verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); - Assertions.assertNotNull(prhAppConfig.getAAIHttpClientConfiguration()); + Assertions.assertNotNull(prhAppConfig.getAAIClientConfiguration()); Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); Assertions.assertNotNull(prhAppConfig.getDmaapPublisherConfiguration()); Assertions @@ -102,7 +102,7 @@ class PrhAppConfigTest { Assertions .assertEquals(appConfig.getDmaapConsumerConfiguration(), prhAppConfig.getDmaapConsumerConfiguration()); Assertions - .assertEquals(appConfig.getAAIHttpClientConfiguration(), prhAppConfig.getAAIHttpClientConfiguration()); + .assertEquals(appConfig.getAAIClientConfiguration(), prhAppConfig.getAAIClientConfiguration()); } @@ -122,7 +122,7 @@ class PrhAppConfigTest { // verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); - Assertions.assertNull(prhAppConfig.getAAIHttpClientConfiguration()); + Assertions.assertNull(prhAppConfig.getAAIClientConfiguration()); Assertions.assertNull(prhAppConfig.getDmaapConsumerConfiguration()); Assertions.assertNull(prhAppConfig.getDmaapPublisherConfiguration()); @@ -147,7 +147,7 @@ class PrhAppConfigTest { // verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); - Assertions.assertNotNull(prhAppConfig.getAAIHttpClientConfiguration()); + Assertions.assertNotNull(prhAppConfig.getAAIClientConfiguration()); Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); Assertions.assertNull(prhAppConfig.getDmaapPublisherConfiguration()); diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskSpy.java new file mode 100644 index 00000000..57c3ce21 --- /dev/null +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskSpy.java @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * 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.tasks; + +import org.onap.dcaegen2.services.config.AAIClientConfiguration; + +import org.onap.dcaegen2.services.prh.configuration.AppConfig; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +import static org.mockito.Mockito.*; + + +@Configuration +public class AAIConsumerTaskSpy { + + @Bean + @Primary + public AAIConsumerTask registerSimpleAAIPublisherTask() { + AppConfig appConfig = mock(AppConfig.class); + HttpRequestDetails requestDetails = mock(HttpRequestDetails.class); + when(appConfig.getAAIClientConfiguration()).thenReturn(mock(AAIClientConfiguration.class)); + return spy(new AAIConsumerTaskImpl(appConfig, requestDetails)); + } +} \ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskSpy.java index c27c8813..8da7f85d 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskSpy.java @@ -19,16 +19,15 @@ */ package org.onap.dcaegen2.services.prh.tasks; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import static org.mockito.Mockito.*; + /** * @author Przemysław Wąsala on 4/13/18 */ @@ -37,9 +36,10 @@ public class AAIPublisherTaskSpy { @Bean @Primary - public AAIPublisherTask registerSimpleAAIPublisherTask() { + public AAIProducerTask registerSimpleAAIPublisherTask() { AppConfig appConfig = mock(AppConfig.class); - when(appConfig.getAAIHttpClientConfiguration()).thenReturn(mock(AAIHttpClientConfiguration.class)); - return spy(new AAIPublisherTaskImpl(appConfig)); + HttpRequestDetails requestDetails = mock(HttpRequestDetails.class); + when(appConfig.getAAIClientConfiguration()).thenReturn(mock(AAIClientConfiguration.class)); + return spy(new AAIProducerTaskImpl(appConfig, requestDetails)); } } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java index 2d599b23..606b0cb8 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java @@ -40,11 +40,14 @@ public class ScheduleControllerSpy { private DmaapPublisherTask dmaapPublisherTaskImplSpy; @Autowired - private AAIPublisherTask aaiPublisherTaskImplSpy; + private AAIProducerTask aaiPublisherTaskImplSpy; + + @Autowired + private AAIConsumerTask aaiConsumerTaskImplSpy; @Bean @Primary public ScheduledTasks registerSimpleScheduledTask() { - return spy(new ScheduledTasks(dmaapConsumerTaskImplSpy, dmaapPublisherTaskImplSpy, aaiPublisherTaskImplSpy)); + return spy(new ScheduledTasks(dmaapConsumerTaskImplSpy, dmaapPublisherTaskImplSpy, aaiPublisherTaskImplSpy, aaiConsumerTaskImplSpy)); } } -- cgit 1.2.3-korg