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 +- 10 files changed, 467 insertions(+), 369 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 (limited to 'prh-aai-client/src/main/java/org/onap') 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(); } -- cgit 1.2.3-korg