From c489a2eb22484e798c39a978bc8b61821b92322f Mon Sep 17 00:00:00 2001 From: an4828 Date: Mon, 22 Jan 2018 17:17:34 -0500 Subject: TCA: Replace any openecomp reference by onap Change-Id: I7c6d812ab5c1d7b30c63653d1974b0b1abc099be Signed-off-by: an4828 Issue-ID: DCAEGEN2-224 Signed-off-by: an4828 --- .../dcae/apod/analytics/aai/AAIClientFactory.java | 74 ++++++++ .../aai/domain/config/AAIEnrichmentConfig.java | 33 ++++ .../aai/domain/config/AAIHttpClientConfig.java | 131 +++++++++++++ .../domain/config/AAIHttpClientConfigBuilder.java | 89 +++++++++ .../analytics/aai/module/AnalyticsAAIModule.java | 54 ++++++ .../analytics/aai/service/AAIEnrichmentClient.java | 45 +++++ .../aai/service/AAIEnrichmentClientFactory.java | 40 ++++ .../aai/service/AAIEnrichmentClientImpl.java | 202 +++++++++++++++++++++ .../apod/analytics/aai/service/AAIHttpClient.java | 39 ++++ .../aai/service/AAIHttpClientFactory.java | 41 +++++ .../analytics/aai/service/AAIHttpClientImpl.java | 159 ++++++++++++++++ .../aai/utils/ssl/AlwaysTrustingTrustStrategy.java | 55 ++++++ 12 files changed, 962 insertions(+) create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/AAIClientFactory.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIEnrichmentConfig.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIHttpClientConfig.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIHttpClientConfigBuilder.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/module/AnalyticsAAIModule.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClient.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClientFactory.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClientImpl.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClient.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClientFactory.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClientImpl.java create mode 100644 dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/utils/ssl/AlwaysTrustingTrustStrategy.java (limited to 'dcae-analytics-aai/src/main/java/org/onap/dcae') diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/AAIClientFactory.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/AAIClientFactory.java new file mode 100644 index 0000000..79322ef --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/AAIClientFactory.java @@ -0,0 +1,74 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import org.onap.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; +import org.onap.dcae.apod.analytics.aai.module.AnalyticsAAIModule; +import org.onap.dcae.apod.analytics.aai.service.AAIEnrichmentClient; +import org.onap.dcae.apod.analytics.aai.service.AAIEnrichmentClientFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Factory to create A&AI API Client. + * + * @author Rajiv Singla . Creation Date: 9/18/2017. + */ +public class AAIClientFactory { + + private static final Logger LOG = LoggerFactory.getLogger(AAIClientFactory.class); + + private final Injector injector; + + public AAIClientFactory(final AbstractModule guiceModule) { + LOG.info("Creating instance of AAI Client Factory with Module: {}", guiceModule.getClass().getSimpleName()); + this.injector = Guice.createInjector(guiceModule); + } + + /** + * Creates an instance of {@link AAIEnrichmentClient}. + * + * @param aaiHttpClientConfig A&AI Http Client Config + * + * @return An instance of A&AI Enrichment Client to fetch enrichment details from A&AI API. + */ + public AAIEnrichmentClient getEnrichmentClient(final AAIHttpClientConfig aaiHttpClientConfig) { + LOG.info("Creating instance of A&AI Enrichment Client with A&AI HttpClientConfig: {}", aaiHttpClientConfig); + final AAIEnrichmentClientFactory aaiEnrichmentClientFactory = + injector.getInstance(AAIEnrichmentClientFactory.class); + return aaiEnrichmentClientFactory.create(aaiHttpClientConfig); + } + + + /** + * Static method used to create an instance of {@link AAIClientFactory} itself using default + * guice {@link AnalyticsAAIModule} + * + * @return An instance of AAI Client Factory with {@link AnalyticsAAIModule} guice module configuration + */ + public static AAIClientFactory create() { + return new AAIClientFactory(new AnalyticsAAIModule()); + } + +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIEnrichmentConfig.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIEnrichmentConfig.java new file mode 100644 index 0000000..4fba3dd --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIEnrichmentConfig.java @@ -0,0 +1,33 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.domain.config; + +import java.io.Serializable; + +/** + *

+ * Marker interface for all A&AI API Enrichment Configs + *

+ * + * @author Rajiv Singla . Creation Date: 9/14/2017. + */ +public interface AAIEnrichmentConfig extends Serializable { +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIHttpClientConfig.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIHttpClientConfig.java new file mode 100644 index 0000000..3e64364 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIHttpClientConfig.java @@ -0,0 +1,131 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.domain.config; + +import com.google.common.base.Objects; + +import java.net.URL; + +/** + * Contains parameters required to create an instance of A&AI Http Client + * + * @author Rajiv Singla . Creation Date: 9/21/2017. + */ +public class AAIHttpClientConfig implements AAIEnrichmentConfig { + + private static final long serialVersionUID = 1L; + + private final String aaiHost; + private final Integer aaiHostPortNumber; + private final String aaiProtocol; + private final String aaiUserName; + private final String aaiUserPassword; + private final URL aaiProxyURL; + private final boolean aaiIgnoreSSLCertificateErrors; + + AAIHttpClientConfig(final String aaiHost, final Integer aaiHostPortNumber, final String aaiProtocol, + final String aaiUserName, final String aaiUserPassword, final URL aaiProxyURL, + final boolean aaiIgnoreSSLCertificateErrors) { + this.aaiHost = aaiHost; + this.aaiHostPortNumber = aaiHostPortNumber; + this.aaiProtocol = aaiProtocol; + this.aaiUserName = aaiUserName; + this.aaiUserPassword = aaiUserPassword; + this.aaiProxyURL = aaiProxyURL; + this.aaiIgnoreSSLCertificateErrors = aaiIgnoreSSLCertificateErrors; + } + + /** + * Provides A&AI Http Client Host + * + * @return A&AI Http Client Host + */ + public String getAaiHost() { + return aaiHost; + } + + /** + * Provides A&AI Http Client Host Port Number + * + * @return A&AI Http Client Host Port Number + */ + public Integer getAaiHostPortNumber() { + return aaiHostPortNumber; + } + + /** + * Provides A&AI Http Client Protocol + * + * @return A&AI Http Client Protocol + */ + public String getAaiProtocol() { + return aaiProtocol; + } + + /** + * Provides A&AI Http Client UserName + * + * @return A&AI Http Client UserName + */ + public String getAaiUserName() { + return aaiUserName; + } + + /** + * Provides A&AI Http Client UserPassword + * + * @return A&AI Http Client UserPassword + */ + public String getAaiUserPassword() { + return aaiUserPassword; + } + + /** + * Returns A&AI Proxy url + * + * @return A&AI Proxy url + */ + public URL getAaiProxyURL() { + return aaiProxyURL; + } + + /** + * Returns true if SSL Certificate errors can be ignored for A&AI Http client + * + * @return true if SSL Certificate errors can be ignored for A&AI Http client + */ + public boolean isAaiIgnoreSSLCertificateErrors() { + return aaiIgnoreSSLCertificateErrors; + } + + + @Override + public String toString() { + return Objects.toStringHelper(this) + .add("aaiHost", aaiHost) + .add("aaiHostPortNumber", aaiHostPortNumber) + .add("aaiProtocol", aaiProtocol) + .add("aaiUserName", aaiUserName) + .add("aaiProxyHost", aaiProxyURL == null ? null : aaiProxyURL.getHost()) + .add("aaiIgnoreSSLCertificateErrors", aaiIgnoreSSLCertificateErrors) + .toString(); + } +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIHttpClientConfigBuilder.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIHttpClientConfigBuilder.java new file mode 100644 index 0000000..ea5d591 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/domain/config/AAIHttpClientConfigBuilder.java @@ -0,0 +1,89 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.domain.config; + +import org.onap.dcae.apod.analytics.common.AnalyticsConstants; + +import java.io.Serializable; +import java.net.URL; + +/** + * A&AI Http Client Config Builder used to create immutable instance of {@link AAIHttpClientConfig} + * + * @author Rajiv Singla . Creation Date: 9/21/2017. + */ +public class AAIHttpClientConfigBuilder implements Serializable { + + private static final long serialVersionUID = 1L; + + private String aaiHost; + private Integer aaiHostPortNumber; + private String aaiProtocol; + private String aaiUserName; + private String aaiUserPassword; + private URL aaiProxyURL; + private boolean aaiIgnoreSSLCertificateErrors; + + public AAIHttpClientConfigBuilder(final String aaiHost) { + this.aaiHost = aaiHost; + this.aaiHostPortNumber = AnalyticsConstants.DEFAULT_PORT_NUMBER; + this.aaiProtocol = AnalyticsConstants.DEFAULT_PROTOCOL; + this.aaiUserName = AnalyticsConstants.DEFAULT_USER_NAME; + this.aaiUserPassword = AnalyticsConstants.DEFAULT_USER_PASSWORD; + this.aaiIgnoreSSLCertificateErrors = AnalyticsConstants + .TCA_DEFAULT_AAI_ENRICHMENT_IGNORE_SSL_CERTIFICATE_ERRORS; + } + + public AAIHttpClientConfigBuilder setAaiHostPortNumber(final Integer aaiHostPortNumber) { + this.aaiHostPortNumber = aaiHostPortNumber; + return this; + } + + public AAIHttpClientConfigBuilder setAaiProtocol(final String aaiProtocol) { + this.aaiProtocol = aaiProtocol; + return this; + } + + public AAIHttpClientConfigBuilder setAaiUserName(final String aaiUserName) { + this.aaiUserName = aaiUserName; + return this; + } + + public AAIHttpClientConfigBuilder setAaiUserPassword(final String aaiUserPassword) { + this.aaiUserPassword = aaiUserPassword; + return this; + } + + public AAIHttpClientConfigBuilder setAaiProxyURL(final URL aaiProxyURL) { + this.aaiProxyURL = aaiProxyURL; + return this; + } + + public AAIHttpClientConfigBuilder setAaiIgnoreSSLCertificateErrors(final boolean aaiIgnoreSSLCertificateErrors) { + this.aaiIgnoreSSLCertificateErrors = aaiIgnoreSSLCertificateErrors; + return this; + } + + public AAIHttpClientConfig build() { + return new AAIHttpClientConfig(aaiHost, aaiHostPortNumber, aaiProtocol, aaiUserName, aaiUserPassword, + aaiProxyURL, aaiIgnoreSSLCertificateErrors); + } +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/module/AnalyticsAAIModule.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/module/AnalyticsAAIModule.java new file mode 100644 index 0000000..dfaf3f3 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/module/AnalyticsAAIModule.java @@ -0,0 +1,54 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.module; + +import com.google.inject.AbstractModule; +import com.google.inject.assistedinject.FactoryModuleBuilder; +import org.onap.dcae.apod.analytics.aai.service.AAIEnrichmentClient; +import org.onap.dcae.apod.analytics.aai.service.AAIEnrichmentClientFactory; +import org.onap.dcae.apod.analytics.aai.service.AAIEnrichmentClientImpl; +import org.onap.dcae.apod.analytics.aai.service.AAIHttpClient; +import org.onap.dcae.apod.analytics.aai.service.AAIHttpClientFactory; +import org.onap.dcae.apod.analytics.aai.service.AAIHttpClientImpl; + +/** + *

+ * Guice Module to bind concrete implementation of interfaces used in Analytics A&AI API + *

+ * + * @author Rajiv Singla . Creation Date: 9/18/2017. + */ +public class AnalyticsAAIModule extends AbstractModule { + + /** + * Configures A&AI API guice modules + */ + @Override + protected void configure() { + + install(new FactoryModuleBuilder().implement(AAIHttpClient.class, AAIHttpClientImpl.class) + .build(AAIHttpClientFactory.class)); + + install(new FactoryModuleBuilder().implement(AAIEnrichmentClient.class, AAIEnrichmentClientImpl.class) + .build(AAIEnrichmentClientFactory.class)); + + } +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClient.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClient.java new file mode 100644 index 0000000..dc2d877 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClient.java @@ -0,0 +1,45 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.service; + +import java.util.Map; + +/** + *

+ * A client used to get enrichment details from A&AI + *

+ * + * @author Rajiv Singla . Creation Date: 9/15/2017. + */ +public interface AAIEnrichmentClient { + + /** + * Provides enrichment details from A&AI API and returns them as string. If no enrichment lookup fails returns null + * + * @param aaiAPIPath A&AI API Path + * @param queryParams A&AI Query Params map + * @param headers A&AI HTTP Headers + * + * @return Enrichment details from A&AI API and returns them as string. If enrichment lookup fails returns null + */ + String getEnrichmentDetails(String aaiAPIPath, Map queryParams, Map headers); + +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClientFactory.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClientFactory.java new file mode 100644 index 0000000..b0fe5b3 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClientFactory.java @@ -0,0 +1,40 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.service; + +import org.onap.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; + +/** + * Factory to initialize instance of {@link AAIEnrichmentClient} for Guice DI injection purposes. + * + * @author Rajiv Singla . Creation Date: 9/19/2017. + */ +public interface AAIEnrichmentClientFactory { + + /** + * Provides an instance of A&AI Enrichment Client used to get details from A&AI API + * + * @param aaiHttpClientConfig A&AI Http Client config used to create A&AI Enrichment client + * + * @return an instance of A&AI Enrichment Client used to get details from A&AI API + */ + AAIEnrichmentClient create(AAIHttpClientConfig aaiHttpClientConfig); +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClientImpl.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClientImpl.java new file mode 100644 index 0000000..b154005 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIEnrichmentClientImpl.java @@ -0,0 +1,202 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.service; + +import com.google.common.base.Optional; +import com.google.inject.Inject; +import com.google.inject.assistedinject.Assisted; +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.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +import org.onap.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; +import org.onap.dcae.apod.analytics.common.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.Iterator; +import java.util.Map; + +import javax.annotation.Nonnull; + + +/** + * A concrete implementation for {@link AAIEnrichmentClient} which uses A&AI REST API to get A&AI Enrichment details + * + * @author Rajiv Singla . Creation Date: 9/18/2017. + */ +public class AAIEnrichmentClientImpl implements AAIEnrichmentClient { + + private static final Logger LOG = LoggerFactory.getLogger(AAIEnrichmentClientImpl.class); + + private final CloseableHttpClient closeableHttpClient; + private final String aaiProtocol; + private final String aaiHost; + private final Integer aaiHostPortNumber; + + @Inject + public AAIEnrichmentClientImpl(@Assisted final AAIHttpClientConfig aaiHttpClientConfig, + final AAIHttpClientFactory aaiHttpClientFactory) { + final AAIHttpClient aaiHttpClient = aaiHttpClientFactory.create(aaiHttpClientConfig); + closeableHttpClient = aaiHttpClient.getAAIHttpClient(); + aaiProtocol = aaiHttpClientConfig.getAaiProtocol(); + aaiHost = aaiHttpClientConfig.getAaiHost(); + aaiHostPortNumber = aaiHttpClientConfig.getAaiHostPortNumber(); + } + + + /** + * Provides enrichment details from A&AI API and returns them as string. If no enrichment lookup fails returns null + * + * @param aaiAPIPath A&AI API Path + * @param queryParams A&AI Query Params map + * @param headers A&AI HTTP Headers + * + * @return Enrichment details from A&AI API and returns them as string. If enrichment lookup fails returns null + */ + @Override + public String getEnrichmentDetails(final String aaiAPIPath, final Map queryParams, + final Map headers) { + + final URI enrichmentURI = + createAAIEnrichmentURI(aaiProtocol, aaiHost, aaiHostPortNumber, aaiAPIPath, queryParams); + + if (enrichmentURI == null) { + return null; + } + + // create new get request + final HttpGet getRequest = new HttpGet(enrichmentURI); + // add http headers + for (Map.Entry headersEntry : headers.entrySet()) { + getRequest.addHeader(headersEntry.getKey(), headersEntry.getValue()); + } + + Optional enrichmentDetails = Optional.absent(); + // execute http get request + try { + enrichmentDetails = closeableHttpClient.execute(getRequest, aaiResponseHandler()); + } catch (IOException ex) { + LOG.error("Failed to get A&AI Enrichment Details for A&AI Enrichment URI: {} A&AI Error: {}", + enrichmentURI, ex); + } + + // return response + if (enrichmentDetails.isPresent()) { + return enrichmentDetails.get(); + } else { + return null; + } + } + + /** + * Create A&AI API Enrichment URI. If invalid URI - null will be returned + * + * @param protocol A&AI API protocol + * @param hostName A&AI API hostname + * @param portNumber A&AI API port number + * @param path A&AI API path + * @param queryParams A&AI API query parameters + * + * @return A&AI API Enrichment URI + */ + private URI createAAIEnrichmentURI(final String protocol, final String hostName, + final Integer portNumber, final String path, + Map queryParams) { + + final URIBuilder uriBuilder = new URIBuilder().setScheme(protocol).setHost(hostName).setPort(portNumber) + .setPath(path); + + // creates custom query string which is not encoded + final String customQuery = createCustomQuery(queryParams); + if (StringUtils.isNoneBlank(customQuery)) { + uriBuilder.setCustomQuery(customQuery); + } + + URI enrichmentURI = null; + try { + enrichmentURI = uriBuilder.build(); + } catch (URISyntaxException e) { + LOG.error("URI Syntax Exception when creating A&AI Enrichment URI. " + + "Protocol: {}, HostName: {}, Port: {}, Path: {}, Custom Query String: {}, Exception: {}", + protocol, hostName, portNumber, path, customQuery, e); + } + + LOG.trace("Created A&AI Enrichment URI: {}", enrichmentURI); + return enrichmentURI; + } + + /** + * Creates Custom Query string to be used for A&AI API URI as A&AI currently does not expect encoded + * query params. + * + * @param queryParams query param map + * + * @return custom query string which does not encode query param values + */ + private static 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()); + queryStringBuilder.append("="); + queryStringBuilder.append(queryParamsEntry.getValue()); + if (queryParamIterator.hasNext()) { + queryStringBuilder.append("&"); + } + } + return queryStringBuilder.toString(); + } + + /** + * Response Handler for A&AI Enrichment API + * + * @return Response Handler that + */ + static ResponseHandler> aaiResponseHandler() { + return new ResponseHandler>() { + @Override + public Optional handleResponse(final HttpResponse response) throws IOException { + final int responseCode = response.getStatusLine().getStatusCode(); + final HttpEntity responseEntity = response.getEntity(); + if (HTTPUtils.isSuccessfulResponseCode(responseCode) && null != responseEntity) { + final String aaiResponse = EntityUtils.toString(responseEntity); + return Optional.of(aaiResponse); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + LOG.error("Unable to fetch response from A&AI API. A&AI Response Code: {}, " + + "A&AI Response Message: {}", responseCode, aaiResponse); + return Optional.absent(); + } + } + }; + } + +} + diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClient.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClient.java new file mode 100644 index 0000000..3ceff5f --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClient.java @@ -0,0 +1,39 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.service; + +import org.apache.http.impl.client.CloseableHttpClient; + +/** + * An HTTP Client used to make REST calls to A&AI Enrichment API + * + * @author Rajiv Singla . Creation Date: 9/19/2017. + */ +public interface AAIHttpClient { + + /** + * Provides an instance of {@link CloseableHttpClient} used to make REST calls to A&AI Enrichment API + * + * @return An instance of Closeable HTTP Client used to make A&AI API Rest calls + */ + CloseableHttpClient getAAIHttpClient(); + +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClientFactory.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClientFactory.java new file mode 100644 index 0000000..ba04734 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClientFactory.java @@ -0,0 +1,41 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.service; + +import org.onap.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; + +/** + * Factory to initialize instance of {@link AAIHttpClient} for Guice DI injection purposes. + * + * @author Rajiv Singla . Creation Date: 9/22/2017. + */ +public interface AAIHttpClientFactory { + + /** + * Provides an instance of A&AI HTTP Client + * + * @param aaiHttpClientConfig A&AI HTTP Client Config + * + * @return An instance of A&AI HTTP Client + */ + AAIHttpClient create(AAIHttpClientConfig aaiHttpClientConfig); + +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClientImpl.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClientImpl.java new file mode 100644 index 0000000..03526ac --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/service/AAIHttpClientImpl.java @@ -0,0 +1,159 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.service; + +import com.google.inject.Inject; +import com.google.inject.assistedinject.Assisted; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHost; +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.impl.conn.DefaultProxyRoutePlanner; +import org.apache.http.ssl.SSLContextBuilder; +import org.onap.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; +import org.onap.dcae.apod.analytics.aai.utils.ssl.AlwaysTrustingTrustStrategy; +import org.onap.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URL; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +/** + *

+ * A concrete implementation of {@link AAIHttpClient} which provides Apache {@link CloseableHttpClient} for + * making rest calls to A&AI Enrichment API. + *

+ * + * @author Rajiv Singla . Creation Date: 9/19/2017. + */ +public class AAIHttpClientImpl implements AAIHttpClient { + + private static final Logger LOG = LoggerFactory.getLogger(AAIHttpClientImpl.class); + + private final AAIHttpClientConfig aaiHttpClientConfig; + + @Inject + public AAIHttpClientImpl(@Assisted final AAIHttpClientConfig aaiHttpClientConfig) { + this.aaiHttpClientConfig = aaiHttpClientConfig; + } + + /** + * Provides an instance of {@link CloseableHttpClient} used to make REST calls to A&AI Enrichment API + * + * @return An instance of Closeable HTTP Client used to make A&AI API Rest calls + */ + @Override + public CloseableHttpClient getAAIHttpClient() { + + final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + final boolean aaiIgnoreSSLCertificateErrors = aaiHttpClientConfig.isAaiIgnoreSSLCertificateErrors(); + + // Setup SSL Context to ignore SSL certificate issues if ignoreSSLCertificateErrors is true + LOG.info("SSL Certificate Errors attributed is set to : {}", aaiIgnoreSSLCertificateErrors); + + if (aaiIgnoreSSLCertificateErrors) { + LOG.warn("SSL Certificate Errors will be ignored for this A&AI Http Client Instance"); + try { + SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); + sslContextBuilder.loadTrustMaterial(null, new AlwaysTrustingTrustStrategy()); + httpClientBuilder.setSSLContext(sslContextBuilder.build()); + } catch (NoSuchAlgorithmException e) { + final String errorMessage = "NoSuchAlgorithmException while setting SSL Context for AAI HTTP Client."; + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } catch (KeyStoreException e) { + final String errorMessage = "KeyStoreException while setting SSL Context for AAI HTTP Client."; + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } catch (KeyManagementException e) { + final String errorMessage = "KeyManagementException while setting SSL Context for AAI HTTP Client."; + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + + httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); + + } else { + LOG.info("SSL Certification Errors will be enforced for A&AI Http Client instance"); + } + + // Setup credentials and proxy + final String aaiUserName = aaiHttpClientConfig.getAaiUserName(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (aaiUserName != null) { + final String aaiHost = aaiHttpClientConfig.getAaiHost(); + final Integer aaiHostPortNumber = aaiHttpClientConfig.getAaiHostPortNumber(); + final String aaiUserPassword = aaiHttpClientConfig.getAaiUserPassword(); + LOG.info("Setting A&AI host credentials for AAI Host: {}", aaiHost); + final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); + final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); + credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); + } else { + LOG.warn("A&AI userName not present. No credentials set for A&AI authentication"); + } + + final URL aaiProxyURL = aaiHttpClientConfig.getAaiProxyURL(); + + if (aaiProxyURL != null) { + final String aaiProxyHost = aaiProxyURL.getHost(); + final Integer aaiProxyPortNumber = aaiProxyURL.getPort(); + final String aaiProxyProtocol = aaiProxyURL.getProtocol(); + final HttpHost proxy = new HttpHost(aaiProxyHost, aaiProxyPortNumber, aaiProxyProtocol); + LOG.info("Setting A&AI Http Client default proxy as: {}", proxy); + final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); + httpClientBuilder.setRoutePlanner(routePlanner); + + final String userInfo = aaiProxyURL.getUserInfo(); + if (StringUtils.isNotBlank(userInfo)) { + final String[] userInfoArray = userInfo.split(":"); + final String aaiProxyUsername = userInfoArray[0]; + String aaiProxyPassword = null; + if (userInfoArray.length > 1) { + aaiProxyPassword = userInfoArray[1]; + } + LOG.info("Setting A&AI Http Client proxy credentials with username: {}", aaiProxyUsername); + final AuthScope aaiProxyAuthScope = new AuthScope(aaiProxyHost, aaiProxyPortNumber); + final Credentials aaiProxyCredentials = new UsernamePasswordCredentials(aaiProxyUsername, + aaiProxyPassword); + credentialsProvider.setCredentials(aaiProxyAuthScope, aaiProxyCredentials); + } else { + LOG.debug("NO A&AI Proxy Username present.Bypassing setting up A&AI Proxy authentication credentials"); + } + } else { + LOG.debug("A&AI proxy not Enabled - bypassing setting A&AI Proxy settings"); + } + + // setup credentials provider + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + + return httpClientBuilder.build(); + } + +} diff --git a/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/utils/ssl/AlwaysTrustingTrustStrategy.java b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/utils/ssl/AlwaysTrustingTrustStrategy.java new file mode 100644 index 0000000..1898412 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/onap/dcae/apod/analytics/aai/utils/ssl/AlwaysTrustingTrustStrategy.java @@ -0,0 +1,55 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T 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.dcae.apod.analytics.aai.utils.ssl; + +import org.apache.http.ssl.TrustStrategy; + +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +/** + * An implementation of SSL Trust Strategy which does no SSL certificate validation effectively + * bypassing any SSL certificate related issues + * + * @author Rajiv Singla . Creation Date: 9/19/2017. + */ +public class AlwaysTrustingTrustStrategy implements TrustStrategy { + /** + * Determines whether the certificate chain can be trusted without consulting the trust manager + * configured in the actual SSL context. This method can be used to override the standard JSSE + * certificate verification process. + *

+ * Please note that, if this method returns {@code false}, the trust manager configured + * in the actual SSL context can still clear the certificate as trusted. + * + * @param chain the peer certificate chain + * @param authType the authentication type based on the client certificate + * + * @return {@code true} if the certificate can be trusted without verification by + * the trust manager, {@code false} otherwise. + * + * @throws CertificateException thrown if the certificate is not trusted or invalid. + */ + @Override + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return true; + } +} -- cgit 1.2.3-korg