diff options
Diffstat (limited to 'prh-aai-client/src/main/java')
6 files changed, 162 insertions, 64 deletions
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/AAIClientConfiguration.java index f9cbeb19..60a022b4 100644 --- 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/AAIClientConfiguration.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.dcaegen2.services.config; @@ -30,7 +31,7 @@ import org.springframework.stereotype.Component; @Value.Immutable(prehash = true) @Value.Style(builder = "new") @Gson.TypeAdapters -public abstract class AAIHttpClientConfiguration implements Serializable { +public abstract class AAIClientConfiguration implements Serializable { private static final long serialVersionUID = 1L; 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/AAIClient.java index c60027c2..33a8d644 100644 --- 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/AAIClient.java @@ -23,7 +23,7 @@ package org.onap.dcaegen2.services.service; import org.apache.http.impl.client.CloseableHttpClient; @FunctionalInterface -public interface AAIHttpClient { +public interface AAIClient { 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/AAIClientImpl.java index 90b551db..c2b40b9a 100644 --- 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/AAIClientImpl.java @@ -31,7 +31,7 @@ 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.onap.dcaegen2.services.config.AAIClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -40,14 +40,14 @@ import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; -public class AAIHttpClientImpl implements AAIHttpClient { +public class AAIClientImpl implements AAIClient { - Logger logger = LoggerFactory.getLogger(AAIHttpClientImpl.class); + Logger logger = LoggerFactory.getLogger(AAIClientImpl.class); - private AAIHttpClientConfiguration aaiHttpClientConfig; + private AAIClientConfiguration aaiHttpClientConfig; @Autowired - public AAIHttpClientImpl(AAIHttpClientConfiguration aaiHttpClientConfiguration) { + public AAIClientImpl(AAIClientConfiguration aaiHttpClientConfiguration) { this.aaiHttpClientConfig = aaiHttpClientConfiguration; } 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<String> getHttpResponse(HttpRequestDetails requestDetails) { + + Optional<String> extendedDetails = Optional.empty(); + Optional<HttpRequestBase> 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<Optional<String>> 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<HttpRequestBase> 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/AAIProducerClient.java index 20ae8d0b..0b82578a 100644 --- 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/AAIProducerClient.java @@ -17,38 +17,32 @@ * 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.HttpResponse; 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.config.AAIClientConfiguration; 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); +public class AAIProducerClient implements AAIExtendedHttpClient { + Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); private final CloseableHttpClient closeableHttpClient; private final String aaiHost; @@ -56,8 +50,8 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { private final Integer aaiHostPortNumber; - public AAIExtendedHttpClientImpl (AAIHttpClientConfiguration aaiHttpClientConfiguration) { - final AAIHttpClient aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientConfiguration); + public AAIProducerClient(AAIClientConfiguration aaiHttpClientConfiguration) { + final AAIClient aaiHttpClient = new AAIClientImpl(aaiHttpClientConfiguration); closeableHttpClient = aaiHttpClient.getAAIHttpClient(); aaiHost = aaiHttpClientConfiguration.aaiHost(); aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); @@ -79,18 +73,15 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { return extendedDetails; } - private URI createAAIExtendedURI(final String path, Map<String, String> queryParams) { + private URI createAAIExtendedURI(final String path, final String pnfName) { 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); - } + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(path + "/" + pnfName); try { extendedURI = uriBuilder.build(); @@ -102,45 +93,25 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { return extendedURI; } - private String createCustomQuery(@NonNull final Map<String, String> queryParams) { - final StringBuilder queryStringBuilder = new StringBuilder(""); - final Iterator<Map.Entry<String, String>> queryParamIterator = queryParams.entrySet().iterator(); - - while (queryParamIterator.hasNext()) { - final Map.Entry<String, String> queryParamsEntry = queryParamIterator.next(); - queryStringBuilder.append(queryParamsEntry.getKey()).append("=").append(queryParamsEntry.getValue()); - if (queryParamIterator.hasNext()) { - queryStringBuilder.append("&"); - } - } - - return queryStringBuilder.toString(); - } - private ResponseHandler<Optional<String>> aaiResponseHandler() { - return httpResponse -> { + 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) && responseEntity != null) { + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { logger.info("HTTP response successful."); - final String aaiResponse = EntityUtils.toString(responseEntity); - return Optional.of(aaiResponse); + return Optional.of("" + responseCode); } else { String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; logger.error("HTTP response not successful : {}", aaiResponse); - return Optional.empty(); + return Optional.of("" + responseCode); } }; } 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())) { + if (isExtendedURINotNull(extendedURI) && httpRequestDetails.jsonBody().isPresent()) { return createHttpPatch(extendedURI, httpRequestDetails.jsonBody()); } else { return null; @@ -174,13 +145,9 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { return stringEntity; } - private Boolean isPatchRequestValid(RequestVerbs requestVerb, Optional<String> jsonBody) { - return requestVerb == RequestVerbs.PATCH && jsonBody.isPresent(); - } - private Optional<HttpRequestBase> createRequest(HttpRequestDetails requestDetails) { - final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.queryParameters()); + 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<String> jsonBody(); @Value.Parameter - public abstract Map<String,String> queryParameters(); + public abstract String pnfName(); @Value.Parameter public abstract Map<String,String> headers(); - - @Value.Parameter - public abstract RequestVerbs requestVerb(); } |