diff options
author | pwielebs <piotr.wielebski@nokia.com> | 2018-05-23 15:31:11 +0200 |
---|---|---|
committer | pwielebs <piotr.wielebski@nokia.com> | 2018-05-24 11:50:34 +0200 |
commit | 7ddaf390698fe5ae9143d91e7011059b3973f8ce (patch) | |
tree | 4d483f24c6b73ce2e0d0a8237410856f8e01edb6 /prh-aai-client/src/main | |
parent | b6bc8925a354825545b8527879e0f687b0dbab3a (diff) |
Refactor of prh-aai-client
Change-Id: Idbca6fe4c050c789f4479164846437039d3b549d
Issue-ID: DCAEGEN2-451
Signed-off-by: pwielebs <piotr.wielebski@nokia.com>
Diffstat (limited to 'prh-aai-client/src/main')
3 files changed, 15 insertions, 69 deletions
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java index aa4630b5..dfacc6b1 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java @@ -29,7 +29,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.prh.utils.HttpUtils; +import org.onap.dcaegen2.services.prh.model.utils.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java index bb4d145e..dce326ec 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java @@ -20,19 +20,14 @@ package org.onap.dcaegen2.services.prh.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.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.model.CommonFunctions; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.prh.utils.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +48,7 @@ public class AAIProducerClient implements AAIExtendedHttpClient { private final String aaiProtocol; private final Integer aaiHostPortNumber; private final String aaiPath; - private final Map<String, String> aaiHeaders; + private final Map<String,String> aaiHeaders; public AAIProducerClient(AAIClientConfiguration aaiClientConfiguration) { @@ -67,16 +62,16 @@ public class AAIProducerClient implements AAIExtendedHttpClient { @Override - public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws - URISyntaxException { - return createRequest(consumerDmaapModel).flatMap(x -> { + public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException { + return createRequest(consumerDmaapModel).flatMap(httpRequestBase -> { try { - return closeableHttpClient.execute(x, aaiResponseHandler()); + return closeableHttpClient.execute(httpRequestBase, CommonFunctions::handleResponse); } catch (IOException e) { logger.warn(EXCEPTION_MESSAGE, e); return Optional.empty(); } }); + } private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException { @@ -92,38 +87,20 @@ public class AAIProducerClient implements AAIExtendedHttpClient { .setPath(aaiPath + "/" + pnfName).build(); } - private ResponseHandler<Optional<Integer>> aaiResponseHandler() { - return (HttpResponse httpResponse) -> { - final Integer responseCode = httpResponse.getStatusLine().getStatusCode(); - logger.info("Status code of operation: {}", responseCode); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode)) { - logger.trace("HTTP response successful."); - return Optional.of(responseCode); - } else { - String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.warn("HTTP response not successful : {}", aaiResponse); - return Optional.of(responseCode); + Optional<HttpRequestBase> createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { + return Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel)).filter(x-> !x.isEmpty()).flatMap(myJson -> { + try { + return Optional.of(createHttpPatch(extendedURI, myJson)); + } catch (UnsupportedEncodingException e) { + logger.warn(EXCEPTION_MESSAGE, e); } - }; - } - - private Optional<HttpRequestBase> createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { - return Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel)).filter(x -> !x.isEmpty()) - .flatMap(myJson -> { - try { - return Optional.of(createHttpPatch(extendedURI, myJson)); - } catch (UnsupportedEncodingException e) { - logger.warn(EXCEPTION_MESSAGE, e); - } - return Optional.empty(); - }); + return Optional.empty(); + }); } private HttpPatch createHttpPatch(URI extendedURI, String jsonBody) throws UnsupportedEncodingException { HttpPatch httpPatch = new HttpPatch(extendedURI); - httpPatch.setEntity(new StringEntity(jsonBody)); + httpPatch.setEntity( new StringEntity(jsonBody)); aaiHeaders.forEach(httpPatch::addHeader); httpPatch.addHeader("Content-Type", "application/merge-patch+json"); return httpPatch; diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/utils/HttpUtils.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/utils/HttpUtils.java deleted file mode 100644 index 3d5c3abf..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/utils/HttpUtils.java +++ /dev/null @@ -1,31 +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.utils; - -import org.apache.http.HttpStatus; - -public final class HttpUtils implements HttpStatus { - - private HttpUtils() {} - - public static boolean isSuccessfulResponseCode(Integer statusCode) { - return statusCode >= 200 && statusCode < 300; - } -} |