From 375001472fa8c286ac61557c63f2703da923b1d1 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Tue, 6 Aug 2019 19:36:13 +0000 Subject: introduce base adaptor introduce base adaptor for other adaptors to extend Issue-ID: CCSDK-1595 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I35d4499491cd87a7d651fa9ed654ad7c677ae2b8 --- base/http/features/ccsdk-base-http/pom.xml | 26 ++++ base/http/features/features-base-http/pom.xml | 29 +++++ base/http/features/pom.xml | 23 ++++ base/http/installer/pom.xml | 141 +++++++++++++++++++++ .../src/assembly/assemble_installer_zip.xml | 59 +++++++++ .../src/assembly/assemble_mvnrepo_zip.xml | 49 +++++++ .../src/main/resources/scripts/install-feature.sh | 39 ++++++ base/http/pom.xml | 22 ++++ base/http/provider/pom.xml | 36 ++++++ .../adaptors/base/http/AbstractHttpAdapter.java | 109 ++++++++++++++++ .../sli/adaptors/base/http/BasicAuthFilter.java | 24 ++++ .../base/http/AbstractHttpAdapterTest.java | 47 +++++++ .../adaptors/base/http/BasicAuthFilterTest.java | 20 +++ .../src/test/resources/testprops.properties | 1 + 14 files changed, 625 insertions(+) create mode 100644 base/http/features/ccsdk-base-http/pom.xml create mode 100644 base/http/features/features-base-http/pom.xml create mode 100644 base/http/features/pom.xml create mode 100644 base/http/installer/pom.xml create mode 100644 base/http/installer/src/assembly/assemble_installer_zip.xml create mode 100644 base/http/installer/src/assembly/assemble_mvnrepo_zip.xml create mode 100644 base/http/installer/src/main/resources/scripts/install-feature.sh create mode 100644 base/http/pom.xml create mode 100644 base/http/provider/pom.xml create mode 100644 base/http/provider/src/main/java/org/onap/ccsdk/sli/adaptors/base/http/AbstractHttpAdapter.java create mode 100644 base/http/provider/src/main/java/org/onap/ccsdk/sli/adaptors/base/http/BasicAuthFilter.java create mode 100644 base/http/provider/src/test/java/org/onap/ccsdk/sli/adaptors/base/http/AbstractHttpAdapterTest.java create mode 100644 base/http/provider/src/test/java/org/onap/ccsdk/sli/adaptors/base/http/BasicAuthFilterTest.java create mode 100644 base/http/provider/src/test/resources/testprops.properties (limited to 'base/http') diff --git a/base/http/features/ccsdk-base-http/pom.xml b/base/http/features/ccsdk-base-http/pom.xml new file mode 100644 index 00000000..0e18c737 --- /dev/null +++ b/base/http/features/ccsdk-base-http/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + single-feature-parent + 1.3.2 + + + + org.onap.ccsdk.sli.adaptors + ccsdk-base-http + 0.5.1-SNAPSHOT + feature + + ccsdk-sli-adaptors :: base-http :: ${project.artifactId} + + + + ${project.groupId} + base-http-provider + ${project.version} + + + diff --git a/base/http/features/features-base-http/pom.xml b/base/http/features/features-base-http/pom.xml new file mode 100644 index 00000000..84004858 --- /dev/null +++ b/base/http/features/features-base-http/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + feature-repo-parent + 1.3.2 + + + + org.onap.ccsdk.sli.adaptors + features-base-http + 0.5.1-SNAPSHOT + feature + + ccsdk-sli-adaptors :: base-http :: ${project.artifactId} + + + + ${project.groupId} + ccsdk-base-http + ${project.version} + xml + features + + + + diff --git a/base/http/features/pom.xml b/base/http/features/pom.xml new file mode 100644 index 00000000..a9abe519 --- /dev/null +++ b/base/http/features/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + odlparent-lite + 1.3.2 + + + + org.onap.ccsdk.sli.adaptors + base-http-features + 0.5.1-SNAPSHOT + pom + + ccsdk-sli-adaptors :: base-http :: ${project.artifactId} + + + ccsdk-base-http + features-base-http + + diff --git a/base/http/installer/pom.xml b/base/http/installer/pom.xml new file mode 100644 index 00000000..ac233cb4 --- /dev/null +++ b/base/http/installer/pom.xml @@ -0,0 +1,141 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + odlparent-lite + 1.3.2 + + + + org.onap.ccsdk.sli.adaptors + base-http-installer + 0.5.1-SNAPSHOT + pom + + ccsdk-sli-adaptors :: base-http :: ${project.artifactId} + + + ccsdk-base-http + ${application.name} + mvn:org.onap.ccsdk.sli.adaptors/${features.boot}/${project.version}/xml/features + false + + + + + + org.onap.ccsdk.sli.adaptors + ${application.name} + ${project.version} + xml + features + + + * + * + + + + + + org.onap.ccsdk.sli.adaptors + base-http-provider + ${project.version} + + + + + + + maven-assembly-plugin + 2.6 + + + maven-repo-zip + + single + + package + + true + stage/${application.name}-${project.version} + + src/assembly/assemble_mvnrepo_zip.xml + + true + + + + installer-zip + + single + + package + + true + ${application.name}-${project.version}-installer + + src/assembly/assemble_installer_zip.xml + + false + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + + copy-dependencies + + prepare-package + + false + ${project.build.directory}/assembly/system + false + true + true + true + false + false + ccsdk-base-http,base-http-provider + provided + + + + + + maven-resources-plugin + 2.6 + + + copy-version + + copy-resources + + validate + + ${basedir}/target/stage + + + src/main/resources/scripts + + install-feature.sh + + true + + + + + + + + + + + diff --git a/base/http/installer/src/assembly/assemble_installer_zip.xml b/base/http/installer/src/assembly/assemble_installer_zip.xml new file mode 100644 index 00000000..3bed4b5e --- /dev/null +++ b/base/http/installer/src/assembly/assemble_installer_zip.xml @@ -0,0 +1,59 @@ + + + + + + installer_zip + + zip + + + + false + + + + target/stage/ + ${application.name} + 755 + + *.sh + + + + target/stage/ + ${application.name} + 644 + + *.sh + + + + + + + diff --git a/base/http/installer/src/assembly/assemble_mvnrepo_zip.xml b/base/http/installer/src/assembly/assemble_mvnrepo_zip.xml new file mode 100644 index 00000000..35e79db1 --- /dev/null +++ b/base/http/installer/src/assembly/assemble_mvnrepo_zip.xml @@ -0,0 +1,49 @@ + + + + + + repo + + zip + + + + false + + + + target/assembly/ + . + + + + + + + + diff --git a/base/http/installer/src/main/resources/scripts/install-feature.sh b/base/http/installer/src/main/resources/scripts/install-feature.sh new file mode 100644 index 00000000..6e3d4169 --- /dev/null +++ b/base/http/installer/src/main/resources/scripts/install-feature.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +### +# ============LICENSE_START======================================================= +# openECOMP : SDN-C +# ================================================================================ +# Copyright (C) 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========================================================= +### + +ODL_HOME=${ODL_HOME:-/opt/opendaylight/current} +ODL_KARAF_CLIENT=${ODL_KARAF_CLIENT:-${ODL_HOME}/bin/client} +INSTALLERDIR=$(dirname $0) + +REPOZIP=${INSTALLERDIR}/${features.boot}-${project.version}.zip + +if [ -f ${REPOZIP} ] +then + unzip -nd ${ODL_HOME} ${REPOZIP} +else + echo "ERROR : repo zip ($REPOZIP) not found" + exit 1 +fi + +${ODL_KARAF_CLIENT} feature:repo-add ${features.repositories} +${ODL_KARAF_CLIENT} feature:install ${features.boot} diff --git a/base/http/pom.xml b/base/http/pom.xml new file mode 100644 index 00000000..368e3fb1 --- /dev/null +++ b/base/http/pom.xml @@ -0,0 +1,22 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + odlparent-lite + 1.3.2 + + + + org.onap.ccsdk.sli.adaptors.base + http + 0.5.1-SNAPSHOT + pom + + + provider + features + installer + + diff --git a/base/http/provider/pom.xml b/base/http/provider/pom.xml new file mode 100644 index 00000000..4cbe85c1 --- /dev/null +++ b/base/http/provider/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + binding-parent + 1.3.2 + + + + org.onap.ccsdk.sli.adaptors + base-http-provider + 0.5.1-SNAPSHOT + bundle + + + + javax.ws.rs + javax.ws.rs-api + provided + + + org.slf4j + slf4j-api + provided + + + org.glassfish.jersey.core + jersey-client + ${jersey.version} + test + + + diff --git a/base/http/provider/src/main/java/org/onap/ccsdk/sli/adaptors/base/http/AbstractHttpAdapter.java b/base/http/provider/src/main/java/org/onap/ccsdk/sli/adaptors/base/http/AbstractHttpAdapter.java new file mode 100644 index 00000000..fda4bafb --- /dev/null +++ b/base/http/provider/src/main/java/org/onap/ccsdk/sli/adaptors/base/http/AbstractHttpAdapter.java @@ -0,0 +1,109 @@ +package org.onap.ccsdk.sli.adaptors.base.http; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLSession; +import javax.ws.rs.client.ClientBuilder; +import javax.xml.bind.DatatypeConverter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class AbstractHttpAdapter { + protected static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR"; + private static final String SDNC_CONFIG_DIR_DEFAULT = "/opt/sdnc/data/properties"; + protected static final int DEFAULT_HTTP_CONNECT_TIMEOUT_MS = 60000; // 1 minute + protected static final int DEFAULT_HTTP_READ_TIMEOUT_MS = 1800000; // 30 minutes + protected ClientBuilder clientBuilder; + + private static final Logger logger = LoggerFactory.getLogger(AbstractHttpAdapter.class); + + public AbstractHttpAdapter() { + clientBuilder = ClientBuilder.newBuilder(); + setTimeouts(); + registerLoggingFilter(); + defaultHostNameVerifier(); + } + + private void defaultHostNameVerifier() { + clientBuilder.hostnameVerifier(new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + }); + } + + protected abstract void registerLoggingFilter(); + + private void setTimeouts() { + Integer httpReadTimeout = readOptionalInteger("HTTP_READ_TIMEOUT_MS", DEFAULT_HTTP_READ_TIMEOUT_MS); + Integer httpConnectTimeout = readOptionalInteger("HTTP_CONNECT_TIMEOUT_MS", DEFAULT_HTTP_CONNECT_TIMEOUT_MS); + + // restore once we migrate to once we migrate to javax.ws.rs-api 2.1 + // clientBuilder.connectTimeout(30, TimeUnit.SECONDS); + // clientBuilder.readTimeout(30, TimeUnit.SECONDS); + + // Setting jersey specific properties is ugly, such behavior should be removed + // once we migrate to javax.ws.rs-api 2.1 + clientBuilder.property("jersey.config.client.readTimeout", httpReadTimeout); + clientBuilder.property("jersey.config.client.connectTimeout", httpConnectTimeout); + } + + public Properties getProperties(String propertiesFileName) throws FileNotFoundException, IOException { + // Check System property, then environment variable then default if null + String propDir = System.getProperty(SDNC_CONFIG_DIR); + if (propDir == null || propDir.length() < 1) { + propDir = System.getenv(SDNC_CONFIG_DIR); + } + if (propDir == null || propDir.length() < 1) { + propDir = SDNC_CONFIG_DIR_DEFAULT; + } + Properties properties = new Properties(); + // forward slash is checked to support path src/test/resources on windows machine + if (!propDir.endsWith(File.separator) && !propDir.endsWith("/")) { + propDir = propDir + File.separator; + } + String path = propDir + propertiesFileName; + properties.load(new FileInputStream(path)); + logger.trace("Initialized properties from ({}) properties ({})", path, properties); + return properties; + } + + protected void addBasicAuthCredentials(String username, String password) { + String basicAuthValue = getBasicAuthValue(username,password); + clientBuilder.register(new BasicAuthFilter(basicAuthValue)); + } + + protected String getBasicAuthValue(String userName, String password) { + String token = userName + ":" + password; + try { + return "BASIC " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8")); + } catch (Exception e) { + logger.error("getBasicAuthValue threw an exception, credentials will be null", e); + } + return null; + } + + public ClientBuilder getClientBuilder() { + return clientBuilder; + } + + private Integer readOptionalInteger(String propertyName, Integer defaultValue) { + String stringValue = System.getProperty(propertyName); + if (stringValue != null && stringValue.length() > 0) { + try { + return Integer.valueOf(stringValue); + } catch (NumberFormatException e) { + logger.warn("property " + propertyName + " had the value " + stringValue + " that could not be converted to an Integer, default " + defaultValue + " will be used instead", e); + } + } + return defaultValue; + } + +} diff --git a/base/http/provider/src/main/java/org/onap/ccsdk/sli/adaptors/base/http/BasicAuthFilter.java b/base/http/provider/src/main/java/org/onap/ccsdk/sli/adaptors/base/http/BasicAuthFilter.java new file mode 100644 index 00000000..48d996ef --- /dev/null +++ b/base/http/provider/src/main/java/org/onap/ccsdk/sli/adaptors/base/http/BasicAuthFilter.java @@ -0,0 +1,24 @@ +package org.onap.ccsdk.sli.adaptors.base.http; + +import java.io.IOException; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.core.MultivaluedMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BasicAuthFilter implements ClientRequestFilter { + private final String basicAuthValue; + + + public BasicAuthFilter(String basicAuthValue) { + this.basicAuthValue = basicAuthValue; + } + + public void filter(ClientRequestContext requestContext) throws IOException { + MultivaluedMap headers = requestContext.getHeaders(); + headers.add("Authorization", basicAuthValue); + } +} diff --git a/base/http/provider/src/test/java/org/onap/ccsdk/sli/adaptors/base/http/AbstractHttpAdapterTest.java b/base/http/provider/src/test/java/org/onap/ccsdk/sli/adaptors/base/http/AbstractHttpAdapterTest.java new file mode 100644 index 00000000..ee2d223d --- /dev/null +++ b/base/http/provider/src/test/java/org/onap/ccsdk/sli/adaptors/base/http/AbstractHttpAdapterTest.java @@ -0,0 +1,47 @@ +package org.onap.ccsdk.sli.adaptors.base.http; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Properties; +import java.util.Set; + +import javax.ws.rs.client.Client; + +import org.junit.Test; + +public class AbstractHttpAdapterTest { + + public class NonLoggingHttpAdapter extends AbstractHttpAdapter { + @Override + protected void registerLoggingFilter() { + // nonlogging, do nothing + } + } + + @Test + public void checkTimeouts() throws Exception { + NonLoggingHttpAdapter adapter = new NonLoggingHttpAdapter(); + Client client = adapter.getClientBuilder().build(); + assertNotNull(client.getConfiguration().getProperty("jersey.config.client.readTimeout")); + assertNotNull(client.getConfiguration().getProperty("jersey.config.client.connectTimeout")); + } + + @Test + public void propertiesTest() throws Exception { + System.setProperty(AbstractHttpAdapter.SDNC_CONFIG_DIR, "src/test/resources/"); + NonLoggingHttpAdapter adapter = new NonLoggingHttpAdapter(); + Properties props = adapter.getProperties("testprops.properties"); + assertNotNull(props); + assertEquals("world", props.get("hello")); + } + + @Test + public void basicAuthFilter() throws Exception { + NonLoggingHttpAdapter adapter = new NonLoggingHttpAdapter(); + adapter.addBasicAuthCredentials("hello", "world"); + Set objs = adapter.getClientBuilder().getConfiguration().getInstances(); + assertEquals(BasicAuthFilter.class,objs.iterator().next().getClass()); + } + +} diff --git a/base/http/provider/src/test/java/org/onap/ccsdk/sli/adaptors/base/http/BasicAuthFilterTest.java b/base/http/provider/src/test/java/org/onap/ccsdk/sli/adaptors/base/http/BasicAuthFilterTest.java new file mode 100644 index 00000000..b9565ba6 --- /dev/null +++ b/base/http/provider/src/test/java/org/onap/ccsdk/sli/adaptors/base/http/BasicAuthFilterTest.java @@ -0,0 +1,20 @@ +package org.onap.ccsdk.sli.adaptors.base.http; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class BasicAuthFilterTest { + + @Test + public void notNullParameters() throws Exception { + BasicAuthFilter myFilter = new BasicAuthFilter("helloworld"); + assertNotNull(myFilter); + } + + @Test + public void nullParameters() throws Exception { + BasicAuthFilter myFilter = new BasicAuthFilter(null); + assertNotNull(myFilter); + } +} diff --git a/base/http/provider/src/test/resources/testprops.properties b/base/http/provider/src/test/resources/testprops.properties new file mode 100644 index 00000000..3f602680 --- /dev/null +++ b/base/http/provider/src/test/resources/testprops.properties @@ -0,0 +1 @@ +hello = world \ No newline at end of file -- cgit 1.2.3-korg