diff options
Diffstat (limited to 'prh-aai-client/src/main/java/org/onap/dcaegen2/services/service')
4 files changed, 69 insertions, 75 deletions
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 index c2b40b9a..f58919e5 100644 --- 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 @@ -34,7 +34,6 @@ 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; @@ -46,7 +45,7 @@ public class AAIClientImpl implements AAIClient { private AAIClientConfiguration aaiHttpClientConfig; - @Autowired + 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 index 24790be2..ffaf1aff 100644 --- 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 @@ -28,7 +28,7 @@ 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.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.utils.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,9 +36,11 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.util.Map; +import java.util.Objects; import java.util.Optional; -public class AAIConsumerClient implements AAIExtendedHttpClient { +public class AAIConsumerClient { Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); @@ -46,33 +48,30 @@ public class AAIConsumerClient implements AAIExtendedHttpClient { private final String aaiHost; private final String aaiProtocol; private final Integer aaiHostPortNumber; + private final String aaiPath; + private final Map<String,String> aaiHeaders; public AAIConsumerClient(AAIClientConfiguration aaiHttpClientConfiguration) { - final AAIClient aaiClient = new AAIClientImpl(aaiHttpClientConfiguration); - closeableHttpClient = aaiClient.getAAIHttpClient(); + closeableHttpClient = new AAIClientImpl(aaiHttpClientConfiguration).getAAIHttpClient(); aaiHost = aaiHttpClientConfiguration.aaiHost(); aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); + aaiPath = aaiHttpClientConfiguration.aaiBasePath() + aaiHttpClientConfiguration.aaiPnfPath(); + aaiHeaders = aaiHttpClientConfiguration.aaiHeaders(); } - @Override - public Optional<String> getHttpResponse(HttpRequestDetails requestDetails) { - - Optional<String> extendedDetails = Optional.empty(); - Optional<HttpRequestBase> request = createRequest(requestDetails); - + public Optional<String> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { + Optional<HttpRequestBase> request = createRequest(consumerDmaapModel); try { - extendedDetails = closeableHttpClient.execute(request.get(), aaiResponseHandler()); + return closeableHttpClient.execute(request.get(), aaiResponseHandler()); } catch (IOException e) { - logger.error("Exception while executing HTTP request: {}", e); + logger.warn("Exception while executing http client: ", e); + throw new IOException(); } - - return extendedDetails; } - - private URI createAAIExtendedURI(final String path, String pnfName) { + private URI createAAIExtendedURI(String pnfName) { URI extendedURI = null; @@ -80,13 +79,13 @@ public class AAIConsumerClient implements AAIExtendedHttpClient { .setScheme(aaiProtocol) .setHost(aaiHost) .setPort(aaiHostPortNumber) - .setPath(path + "/" + pnfName); + .setPath(aaiPath + "/" + pnfName); try { extendedURI = uriBuilder.build(); - logger.info("Building extended URI: {}", extendedURI); + logger.trace("Building extended URI: {}", extendedURI); } catch (URISyntaxException e) { - logger.error("Exception while building extended URI: {}", e); + logger.warn("Exception while building extended URI: {}", e); } return extendedURI; @@ -95,39 +94,34 @@ public class AAIConsumerClient implements AAIExtendedHttpClient { private ResponseHandler<Optional<String>> aaiResponseHandler() { return httpResponse -> { final int responseCode = httpResponse.getStatusLine().getStatusCode(); - logger.info("Status code of operation: {}", responseCode); + logger.trace("Status code of operation: {}", responseCode); final HttpEntity responseEntity = httpResponse.getEntity(); if (HttpUtils.isSuccessfulResponseCode(responseCode) ) { - logger.info("HTTP response successful."); + logger.trace("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); + logger.warn("HTTP response not successful : {}", aaiResponse); + return Optional.of(String.valueOf(responseCode)); } }; } private HttpRequestBase createHttpRequest(URI extendedURI) { - - if (isExtendedURINotNull(extendedURI)) { - return new HttpGet(extendedURI); - } else { - return null; - } + return isExtendedURINotNull(extendedURI) ? new HttpGet(extendedURI) : null; } private Boolean isExtendedURINotNull(URI extendedURI) { return extendedURI != null; } - private Optional<HttpRequestBase> createRequest(HttpRequestDetails requestDetails) { - - final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.pnfName()); + private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) { + final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); HttpRequestBase request = createHttpRequest(extendedURI); - requestDetails.headers().forEach(request::addHeader); + aaiHeaders.forEach(Objects.requireNonNull(request)::addHeader); + Objects.requireNonNull(request).addHeader("Content-Type", "application/json"); return Optional.of(request); } } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java index 24149e6d..71c94b68 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java @@ -19,12 +19,13 @@ */ package org.onap.dcaegen2.services.service; -import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.model.ConsumerDmaapModel; +import java.io.IOException; import java.util.Optional; @FunctionalInterface public interface AAIExtendedHttpClient { - Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails); + Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException; } 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 index 0b82578a..788d997e 100644 --- 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 @@ -30,7 +30,8 @@ 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.model.CommonFunctions; +import org.onap.dcaegen2.services.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.utils.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +40,8 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.util.Map; +import java.util.Objects; import java.util.Optional; public class AAIProducerClient implements AAIExtendedHttpClient { @@ -48,71 +51,69 @@ public class AAIProducerClient implements AAIExtendedHttpClient { private final String aaiHost; private final String aaiProtocol; private final Integer aaiHostPortNumber; + private final String aaiPath; + private final Map<String,String> aaiHeaders; public AAIProducerClient(AAIClientConfiguration aaiHttpClientConfiguration) { - final AAIClient aaiHttpClient = new AAIClientImpl(aaiHttpClientConfiguration); - closeableHttpClient = aaiHttpClient.getAAIHttpClient(); + closeableHttpClient = new AAIClientImpl(aaiHttpClientConfiguration).getAAIHttpClient(); aaiHost = aaiHttpClientConfiguration.aaiHost(); aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); + aaiPath = aaiHttpClientConfiguration.aaiBasePath() + aaiHttpClientConfiguration.aaiPnfPath(); + aaiHeaders = aaiHttpClientConfiguration.aaiHeaders(); } - @Override - public Optional<String> getHttpResponse(HttpRequestDetails requestDetails) { - - Optional<String> extendedDetails = Optional.empty(); - Optional<HttpRequestBase> request = createRequest(requestDetails); + @Override + public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { + Optional<HttpRequestBase> request = createRequest(consumerDmaapModel); try { - extendedDetails = closeableHttpClient.execute(request.get(), aaiResponseHandler()); + return closeableHttpClient.execute(request.get(), aaiResponseHandler()); } catch (IOException e) { - logger.error("Exception while executing HTTP request: {}", e); + logger.warn("Exception while executing http client: ", e); + throw new IOException(); } - - return extendedDetails; } - private URI createAAIExtendedURI(final String path, final String pnfName) { - + private URI createAAIExtendedURI(final String pnfName) { URI extendedURI = null; - final URIBuilder uriBuilder = new URIBuilder() .setScheme(aaiProtocol) .setHost(aaiHost) .setPort(aaiHostPortNumber) - .setPath(path + "/" + pnfName); - + .setPath(aaiPath + "/" + pnfName); try { extendedURI = uriBuilder.build(); - logger.info("Building extended URI: {}", extendedURI); + logger.trace("Building extended URI: {}", extendedURI); } catch (URISyntaxException e) { - logger.error("Exception while building extended URI: {}", e); + logger.warn("Exception while building extended URI: ", e); } - return extendedURI; } - private ResponseHandler<Optional<String>> aaiResponseHandler() { + private ResponseHandler<Optional<Integer>> aaiResponseHandler() { return (HttpResponse httpResponse) -> { - final int responseCode = httpResponse.getStatusLine().getStatusCode(); - logger.info("Status code of operation: {}", responseCode); + final Integer responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.trace("Status code of operation: {}", responseCode); final HttpEntity responseEntity = httpResponse.getEntity(); if (HttpUtils.isSuccessfulResponseCode(responseCode)) { - logger.info("HTTP response successful."); - return Optional.of("" + responseCode); + logger.trace("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); + logger.warn("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()); + private HttpRequestBase createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { + String jsonBody = CommonFunctions.createJsonBody(consumerDmaapModel); + + if (isExtendedURINotNull(extendedURI) && jsonBody != null && !"".equals(jsonBody)) { + return createHttpPatch(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); } else { return null; } @@ -122,6 +123,7 @@ public class AAIProducerClient implements AAIExtendedHttpClient { return extendedURI != null; } + private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) { return Optional.of(parseJson(jsonBody).get()); } @@ -135,21 +137,19 @@ public class AAIProducerClient implements AAIExtendedHttpClient { private Optional<StringEntity> parseJson(Optional<String> jsonBody) { Optional<StringEntity> stringEntity = Optional.empty(); - try { stringEntity = Optional.of(new StringEntity(jsonBody.get())); } catch (UnsupportedEncodingException e) { - logger.error("Exception while parsing JSON: {}", e); + logger.warn("Exception while parsing JSON: ", e); } - return stringEntity; } - private Optional<HttpRequestBase> createRequest(HttpRequestDetails requestDetails) { - - final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.pnfName()); - HttpRequestBase request = createHttpRequest(extendedURI, requestDetails); - requestDetails.headers().forEach(request::addHeader); + private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) { + final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); + HttpRequestBase request = createHttpRequest(extendedURI, consumerDmaapModel); + aaiHeaders.forEach(Objects.requireNonNull(request)::addHeader); + Objects.requireNonNull(request).addHeader("Content-Type", "application/merge-patch+json"); return Optional.of(request); } } |