diff options
Diffstat (limited to 'prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh')
6 files changed, 501 insertions, 0 deletions
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AAIClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AAIClientConfiguration.java new file mode 100644 index 00000000..d652ccb7 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AAIClientConfiguration.java @@ -0,0 +1,67 @@ +/*- + * ============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.config; + + +import java.io.Serializable; +import java.util.Map; + +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(); + + @Value.Parameter + public abstract String aaiBasePath(); + + @Value.Parameter + public abstract String aaiPnfPath(); + + @Value.Parameter + public abstract Map<String,String> aaiHeaders(); + +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClient.java new file mode 100644 index 00000000..09d7f6ae --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/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.prh.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/prh/service/AAIClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClientImpl.java new file mode 100644 index 00000000..5e938528 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClientImpl.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.prh.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.prh.config.AAIClientConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +public class AAIClientImpl implements AAIClient { + + private Logger logger = LoggerFactory.getLogger(AAIClientImpl.class); + + private AAIClientConfiguration aaiClientConfig; + + + public AAIClientImpl(AAIClientConfiguration aaiClientConfiguration) { + this.aaiClientConfig = aaiClientConfiguration; + } + + @Override + public CloseableHttpClient getAAIHttpClient() { + + final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + final boolean aaiIgnoreSSLCertificateErrors = aaiClientConfig.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 = aaiClientConfig.aaiUserName(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (aaiUserName != null) { + final String aaiHost = aaiClientConfig.aaiHost(); + final Integer aaiHostPortNumber = aaiClientConfig.aaiHostPortNumber(); + final String aaiUserPassword = aaiClientConfig.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/prh/service/AAIConsumerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java new file mode 100644 index 00000000..7836a75d --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java @@ -0,0 +1,127 @@ +/*- + * ============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.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.prh.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +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.Map; +import java.util.Objects; +import java.util.Optional; + +public class AAIConsumerClient { + + Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); + + private final CloseableHttpClient closeableHttpClient; + 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 aaiClientConfiguration) { + closeableHttpClient = new AAIClientImpl(aaiClientConfiguration).getAAIHttpClient(); + aaiHost = aaiClientConfiguration.aaiHost(); + aaiProtocol = aaiClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiClientConfiguration.aaiHostPortNumber(); + aaiPath = aaiClientConfiguration.aaiBasePath() + aaiClientConfiguration.aaiPnfPath(); + aaiHeaders = aaiClientConfiguration.aaiHeaders(); + } + + public Optional<String> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { + Optional<HttpRequestBase> request = createRequest(consumerDmaapModel); + try { + return closeableHttpClient.execute(request.get(), aaiResponseHandler()); + } catch (IOException e) { + logger.warn("Exception while executing http client: ", e); + throw new IOException(); + } + } + + private URI createAAIExtendedURI(String pnfName) { + + URI extendedURI = null; + + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(aaiPath + "/" + pnfName); + + try { + extendedURI = uriBuilder.build(); + logger.trace("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.warn("Exception while building extended URI: {}", e); + } + + return extendedURI; + } + + private ResponseHandler<Optional<String>> aaiResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.trace("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode) ) { + logger.trace("HTTP response successful."); + final String aaiResponse = EntityUtils.toString(responseEntity); + return Optional.of(aaiResponse); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.warn("HTTP response not successful : {}", aaiResponse); + return Optional.of(String.valueOf(responseCode)); + } + }; + } + + private HttpRequestBase createHttpRequest(URI extendedURI) { + return isExtendedURINotNull(extendedURI) ? new HttpGet(extendedURI) : null; + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) { + final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); + HttpRequestBase request = createHttpRequest(extendedURI); + 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/prh/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java new file mode 100644 index 00000000..cb884aed --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java @@ -0,0 +1,31 @@ +/*- + * ============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.service; + +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; + +import java.io.IOException; +import java.util.Optional; + + +@FunctionalInterface +public interface AAIExtendedHttpClient { + Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException; +} 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 new file mode 100644 index 00000000..0db9b114 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/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.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.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.Map; +import java.util.Objects; +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; + private final String aaiPath; + private final Map<String,String> aaiHeaders; + + + public AAIProducerClient(AAIClientConfiguration aaiClientConfiguration) { + closeableHttpClient = new AAIClientImpl(aaiClientConfiguration).getAAIHttpClient(); + aaiHost = aaiClientConfiguration.aaiHost(); + aaiProtocol = aaiClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiClientConfiguration.aaiHostPortNumber(); + aaiPath = aaiClientConfiguration.aaiBasePath() + aaiClientConfiguration.aaiPnfPath(); + aaiHeaders = aaiClientConfiguration.aaiHeaders(); + } + + + @Override + public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { + Optional<HttpRequestBase> request = createRequest(consumerDmaapModel); + try { + return closeableHttpClient.execute(request.get(), aaiResponseHandler()); + } catch (IOException e) { + logger.warn("Exception while executing http client: ", e); + throw new IOException(); + } + } + + private URI createAAIExtendedURI(final String pnfName) { + URI extendedURI = null; + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(aaiPath + "/" + pnfName); + try { + extendedURI = uriBuilder.build(); + logger.trace("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.warn("Exception while building extended URI: ", e); + } + return extendedURI; + } + + private ResponseHandler<Optional<Integer>> aaiResponseHandler() { + return (HttpResponse httpResponse) -> { + final Integer responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.trace("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); + } + }; + } + + 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; + } + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + + private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) { + return Optional.of(parseJson(jsonBody).get()); + } + + private HttpPatch createHttpPatch(URI extendedURI, Optional<String> jsonBody) { + HttpPatch httpPatch = new HttpPatch(extendedURI); + Optional<StringEntity> stringEntity = createStringEntity(jsonBody); + httpPatch.setEntity(stringEntity.get()); + return httpPatch; + } + + private Optional<StringEntity> parseJson(Optional<String> jsonBody) { + Optional<StringEntity> stringEntity = Optional.empty(); + try { + stringEntity = Optional.of(new StringEntity(jsonBody.get())); + } catch (UnsupportedEncodingException e) { + logger.warn("Exception while parsing JSON: ", e); + } + return stringEntity; + } + + 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); + } +} |