From b9f65858446ee35a652db53bbca20cc572857116 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Tue, 20 Nov 2018 16:46:32 +0100 Subject: AAI client & common module added * added module containing common code for other sdk modules * added reactive rest client for AAI data base with unit tests * improved poms * license text corrected accoriding to SDK Change-Id: I23ea5fbf0e45170b2a1d70ad300bd72121d16ec1 Issue-ID: DCAEGEN2-986 Signed-off-by: pwielebs --- rest-services/common-dependency/pom.xml | 54 ++++++++++ .../sdk/rest/services/model/CommonFunctions.java | 46 +++++++++ .../rest/services/model/ConsumerDmaapModel.java | 43 ++++++++ .../rest/services/model/logging/MdcVariables.java | 45 +++++++++ .../services/sdk/rest/services/ssl/SslFactory.java | 112 +++++++++++++++++++++ 5 files changed, 300 insertions(+) create mode 100644 rest-services/common-dependency/pom.xml create mode 100644 rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/CommonFunctions.java create mode 100644 rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/ConsumerDmaapModel.java create mode 100644 rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java create mode 100644 rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/ssl/SslFactory.java (limited to 'rest-services/common-dependency') diff --git a/rest-services/common-dependency/pom.xml b/rest-services/common-dependency/pom.xml new file mode 100644 index 00000000..7289514a --- /dev/null +++ b/rest-services/common-dependency/pom.xml @@ -0,0 +1,54 @@ + + + + 4.0.0 + + + org.onap.dcaegen2.services.sdk + dcaegen2-services-sdk-rest-services + 1.0.0-SNAPSHOT + ../pom.xml + + + org.onap.dcaegen2.services.sdk.rest.services + common-dependency + 1.0.0-SNAPSHOT + + dcaegen2-services-sdk-rest-services-common-dependency + Common functionality in the project + jar + + + + io.projectreactor.netty + reactor-netty + + + org.immutables + gson + + + org.immutables + value + + + ch.qos.logback + logback-classic + + + org.slf4j + jul-to-slf4j + + + org.slf4j + log4j-over-slf4j + + + org.springframework + spring-web + + + + \ No newline at end of file diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/CommonFunctions.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/CommonFunctions.java new file mode 100644 index 00000000..55b12e0a --- /dev/null +++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/CommonFunctions.java @@ -0,0 +1,46 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.model; + +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapterFactory; + +import java.util.ServiceLoader; + + +public class CommonFunctions { + + private CommonFunctions() { + } + + /** + * Method for serialization object by GSON. + * + * @param consumerDmaapModel - object which will be serialized + * @return string from serialization + */ + public static String createJsonBody(ConsumerDmaapModel consumerDmaapModel) { + GsonBuilder gsonBuilder = new GsonBuilder(); + ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory); + return gsonBuilder.create().toJson(ImmutableConsumerDmaapModel.builder().ipv4(consumerDmaapModel.getIpv4()) + .ipv6(consumerDmaapModel.getIpv6()).correlationId(consumerDmaapModel.getCorrelationId()).build()); + } +} \ No newline at end of file diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/ConsumerDmaapModel.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/ConsumerDmaapModel.java new file mode 100644 index 00000000..1bf9ff62 --- /dev/null +++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/ConsumerDmaapModel.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.model; + +import com.google.gson.annotations.SerializedName; +import org.immutables.gson.Gson; +import org.immutables.value.Value; + +/** + * @author Przemysław Wąsala on 5/8/18 + */ + +@Value.Immutable +@Gson.TypeAdapters(fieldNamingStrategy = true) +public interface ConsumerDmaapModel { + + @SerializedName(value = "correlationId", alternate = "correlationId") + String getCorrelationId(); + + @SerializedName(value = "ipaddress-v4-oam", alternate = "ipaddress-v4-oam") + String getIpv4(); + + @SerializedName(value = "ipaddress-v6-oam", alternate = "ipaddress-v6-oam") + String getIpv6(); +} diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java new file mode 100644 index 00000000..22090e9f --- /dev/null +++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.model.logging; + +import org.slf4j.MDC; + +import java.util.Map; + +public final class MdcVariables { + + public static final String X_ONAP_REQUEST_ID = "X-ONAP-RequestID"; + public static final String X_INVOCATION_ID = "X-InvocationID"; + public static final String REQUEST_ID = "RequestID"; + public static final String INVOCATION_ID = "InvocationID"; + public static final String INSTANCE_UUID = "InstanceUUID"; + public static final String RESPONSE_CODE = "ResponseCode"; + public static final String SERVICE_NAME = "ServiceName"; + + private MdcVariables() { + } + + public static void setMdcContextMap(Map mdcContextMap) { + if (mdcContextMap != null) { + MDC.setContextMap(mdcContextMap); + } + } +} diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/ssl/SslFactory.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/ssl/SslFactory.java new file mode 100644 index 00000000..cce811c5 --- /dev/null +++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/ssl/SslFactory.java @@ -0,0 +1,112 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.ssl; + +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLException; +import javax.net.ssl.TrustManagerFactory; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.security.KeyStore; + +public class SslFactory { + + private static final Logger LOGGER = LoggerFactory.getLogger(SslFactory.class); + + /** + * Function for creating secure ssl context. + * + * @param keyStorePath - path to file with keystore + * @param keyStorePasswordPath - path to file with keystore password + * @param trustStorePath - path to file with truststore + * @param trustStorePasswordPath - path to file with truststore password + * @return configured ssl context + */ + public SslContext createSecureContext(String keyStorePath, + String keyStorePasswordPath, + String trustStorePath, + String trustStorePasswordPath) throws SSLException { + LOGGER.info("Creating secure ssl context for: {} {}", keyStorePath, trustStorePath); + try { + return SslContextBuilder + .forClient() + .keyManager(keyManagerFactory(keyStorePath, loadPasswordFromFile(keyStorePasswordPath))) + .trustManager(trustManagerFactory(trustStorePath, loadPasswordFromFile(trustStorePasswordPath))) + .build(); + } catch (GeneralSecurityException | IOException ex) { + throw new SSLException(ex); + } + } + + /** + * Function for creating insecure ssl context. + * + * @return configured insecure ssl context + */ + public SslContext createInsecureContext() throws SSLException { + LOGGER.info("Creating insecure ssl context"); + return SslContextBuilder + .forClient() + .trustManager(InsecureTrustManagerFactory.INSTANCE) + .build(); + } + + private KeyManagerFactory keyManagerFactory(String path, String password) + throws GeneralSecurityException, IOException { + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + kmf.init(loadKeyStoreFromFile(path, password), + password.toCharArray()); + return kmf; + } + + private TrustManagerFactory trustManagerFactory(String path, String password) + throws GeneralSecurityException, IOException { + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(loadKeyStoreFromFile(path, password)); + return tmf; + } + + private KeyStore loadKeyStoreFromFile(String path, String keyStorePassword) + throws GeneralSecurityException, IOException { + KeyStore ks = KeyStore.getInstance("jks"); + ks.load(getResource(path), keyStorePassword.toCharArray()); + return ks; + } + + private InputStream getResource(String path) throws FileNotFoundException { + return new FileInputStream(path); + } + + private String loadPasswordFromFile(String path) throws IOException { + return new String(Files.readAllBytes(Paths.get(path))); + } +} -- cgit 1.2.3-korg