From 19bb7116409b4eaa546b48b7d7389e4de04e4f03 Mon Sep 17 00:00:00 2001 From: Joanna Jeremicz Date: Wed, 2 Oct 2019 07:15:16 +0200 Subject: Restructure aai-client and get rid of common-dependency module Issue-ID: DCAEGEN2-1792 Change-Id: Ifcc69a917b2aec02615df93d10979d6c38a25ff2 Signed-off-by: Joanna Jeremicz --- rest-services/http-client/pom.xml | 11 +- .../sdk/rest/services/adapters/http/URI.java | 164 +++++++++++++++++++++ .../http/logging/GlobalDiagnosticContext.java | 71 +++++++++ .../adapters/http/logging/MdcVariables.java | 65 ++++++++ .../http/logging/RequestDiagnosticContext.java | 108 ++++++++++++++ .../sdk/rest/services/adapters/http/URITest.java | 45 ++++++ .../adapters/http/logging/MdcVariablesTest.java | 36 +++++ 7 files changed, 494 insertions(+), 6 deletions(-) create mode 100644 rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URI.java create mode 100644 rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/GlobalDiagnosticContext.java create mode 100644 rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariables.java create mode 100644 rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/RequestDiagnosticContext.java create mode 100644 rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URITest.java create mode 100644 rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariablesTest.java (limited to 'rest-services/http-client') diff --git a/rest-services/http-client/pom.xml b/rest-services/http-client/pom.xml index dd5be34e..d3b8f124 100644 --- a/rest-services/http-client/pom.xml +++ b/rest-services/http-client/pom.xml @@ -28,7 +28,7 @@ org.onap.dcaegen2.services.sdk dcaegen2-services-sdk-rest-services - 1.3.1-SNAPSHOT + 1.3.2-SNAPSHOT org.onap.dcaegen2.services.sdk.rest.services @@ -44,11 +44,6 @@ ssl ${project.version} - - org.onap.dcaegen2.services.sdk.rest.services - common-dependency - ${project.version} - io.projectreactor.netty reactor-netty @@ -61,6 +56,10 @@ org.immutables value + + org.immutables + gson + org.jetbrains annotations diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URI.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URI.java new file mode 100644 index 00000000..6b74a82a --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URI.java @@ -0,0 +1,164 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2019 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.uri; + +public final class URI { + private String scheme; + private String host; + private int port; + private String path; + private String fragment; + private String authority; + private String userInfo; + private String query; + private String schemeSpecificPart; + private String string; + + private URI() { + } + + public static final class URIBuilder { + private String scheme; + private String host; + private int port; + private String path; + private String fragment; + private String authority; + private String userInfo; + private String query; + private String schemeSpecificPart; + + public URIBuilder scheme(String scheme) { + this.scheme = scheme; + return this; + } + + public URIBuilder host(String host) { + this.host = host; + return this; + } + + public URIBuilder port(int port) { + this.port = port; + return this; + } + + public URIBuilder path(String path) { + this.path = path; + return this; + } + + public URIBuilder fragment(String fragment) { + this.fragment = fragment; + return this; + } + + public URIBuilder authority(String authority) { + this.authority = authority; + return this; + } + + public URIBuilder userInfo(String userInfo) { + this.userInfo = userInfo; + return this; + } + + public URIBuilder query(String query) { + this.query = query; + return this; + } + + public URIBuilder schemeSpecificPart(String schemeSpecificPart) { + this.schemeSpecificPart = schemeSpecificPart; + return this; + } + + public URI build() { + URI uri = new URI(); + uri.scheme = this.scheme; + uri.host = this.host; + uri.port = this.port; + uri.path = this.path; + uri.fragment = this.fragment; + uri.authority = this.authority; + uri.userInfo = this.userInfo; + uri.query = this.query; + uri.schemeSpecificPart = this.schemeSpecificPart; + return uri; + } + } + + @Override + public String toString() { + defineString(); + return string; + } + + private void defineString() { + if (string != null) return; + + StringBuffer sb = new StringBuffer(); + if (scheme != null) { + sb.append(scheme); + sb.append(':'); + } + if (isOpaque()) { + sb.append(schemeSpecificPart); + } else { + if (host != null) { + sb.append("//"); + if (userInfo != null) { + sb.append(userInfo); + sb.append('@'); + } + boolean needBrackets = ((host.indexOf(':') >= 0) + && !host.startsWith("[") + && !host.endsWith("]")); + if (needBrackets) sb.append('['); + sb.append(host); + if (needBrackets) sb.append(']'); + if (port != -1) { + sb.append(':'); + sb.append(port); + } + } else if (authority != null) { + sb.append("//"); + sb.append(authority); + } + if (path != null) + sb.append(path); + if (query != null) { + sb.append('?'); + sb.append(query); + } + } + if (fragment != null) { + sb.append('#'); + sb.append(fragment); + } + string = sb.toString(); + } + + private boolean isOpaque() { + return path == null; + } +} \ No newline at end of file diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/GlobalDiagnosticContext.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/GlobalDiagnosticContext.java new file mode 100644 index 00000000..db4a0fd6 --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/GlobalDiagnosticContext.java @@ -0,0 +1,71 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. 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 io.vavr.collection.HashMap; +import io.vavr.collection.Map; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.UUID; +import org.immutables.value.Value; +import org.jetbrains.annotations.Nullable; +import org.slf4j.MDC; + +/** + * @author Piotr Jaszczyk + * @since 1.1.2 + */ +@Value.Immutable(singleton = true) +public interface GlobalDiagnosticContext { + + @Value.Default + default String instanceId() { + return UUID.randomUUID().toString(); + } + + @Value.Default + default String serverFqdn() { + try { + return InetAddress.getLocalHost().toString(); + } catch (UnknownHostException ex) { + return InetAddress.getLoopbackAddress().toString(); + } + } + + @Value.Default + default String serviceName() { + return System.getenv().getOrDefault("HOSTNAME", "unknown_service"); + } + + @Value.Derived + default Map asMap() { + return HashMap.of( + MdcVariables.INSTANCE_ID, instanceId(), + MdcVariables.SERVER_FQDN, serverFqdn(), + MdcVariables.SERVICE_NAME, serviceName()); + } + + static GlobalDiagnosticContext instance() { + return ImmutableGlobalDiagnosticContext.of(); + } +} + + diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariables.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariables.java new file mode 100644 index 00000000..652e3541 --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariables.java @@ -0,0 +1,65 @@ +/* + * ============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 { + + @Deprecated + public static final String X_ONAP_REQUEST_ID = "X-ONAP-RequestID"; + @Deprecated + public static final String X_INVOCATION_ID = "X-InvocationID"; + + public static final String INSTANCE_UUID = "InstanceUUID"; + public static final String RESPONSE_CODE = "ResponseCode"; + public static final String REQUEST_ID = "RequestID"; + public static final String CLIENT_NAME = "PartnerName"; + public static final String CLIENT_IP = "ClientIPAddress"; + public static final String INVOCATION_ID = "InvocationID"; + public static final String INVOCATION_TIMESTAMP = "InvokeTimestamp"; + public static final String STATUS_CODE = "StatusCode"; + public static final String INSTANCE_ID = "InstanceID"; + public static final String SERVER_FQDN = "ServerFQDN"; + public static final String SERVICE_NAME = "ServiceName"; + + private static final String HTTP_HEADER_PREFIX = "X-"; + + private MdcVariables() { + } + + public static String httpHeader(String mdcName) { + return HTTP_HEADER_PREFIX + mdcName; + } + + /** + * @deprecated use {@link RequestDiagnosticContext#withSlf4jMdc(Runnable)}. + * @param mdcContextMap + */ + @Deprecated + public static void setMdcContextMap(Map mdcContextMap) { + if (mdcContextMap != null) { + MDC.setContextMap(mdcContextMap); + } + } +} diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/RequestDiagnosticContext.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/RequestDiagnosticContext.java new file mode 100644 index 00000000..0c4a4b1f --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/RequestDiagnosticContext.java @@ -0,0 +1,108 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. 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 io.vavr.collection.HashMap; +import io.vavr.collection.Map; +import java.util.UUID; +import org.immutables.value.Value; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.MDC; + +/** + * @author Piotr Jaszczyk + * @since 1.1.2 + */ +@Value.Immutable +public interface RequestDiagnosticContext { + + UUID requestId(); + + @Nullable UUID invocationId(); + + @Value.Default + default GlobalDiagnosticContext global() { + return GlobalDiagnosticContext.instance(); + } + + @Value.Derived + default Map remoteCallHttpHeaders() { + java.util.Map result = new java.util.HashMap<>(); + + result.put(MdcVariables.httpHeader(MdcVariables.REQUEST_ID), requestId().toString()); + + if (invocationId() != null) { + result.put(MdcVariables.httpHeader(MdcVariables.INVOCATION_ID), invocationId().toString()); + } + + return HashMap.ofAll(result); + } + + @Value.Derived + default Map asMap() { + java.util.Map result = new java.util.HashMap<>(); + + if (requestId() != null) { + result.put(MdcVariables.REQUEST_ID, requestId().toString()); + } + + if (invocationId() != null) { + result.put(MdcVariables.INVOCATION_ID, invocationId().toString()); + } + + return global().asMap().merge(HashMap.ofAll(result)); + } + + default void withSlf4jMdc(Runnable runnable) { + withSlf4jMdc(true, runnable); + } + + default void withSlf4jMdc(boolean loglevelEnabled, Runnable runnable) { + if (loglevelEnabled) { + final java.util.Map ctxBefore = MDC.getCopyOfContextMap(); + try { + MDC.setContextMap(asMap().toJavaMap()); + runnable.run(); + } finally { + if (ctxBefore == null) { + MDC.clear(); + } else { + MDC.setContextMap(ctxBefore); + } + } + } + } + + default @NotNull RequestDiagnosticContext withNewInvocationId() { + return ImmutableRequestDiagnosticContext.copyOf(this) + .withInvocationId(UUID.randomUUID()); + } + + static ImmutableRequestDiagnosticContext create() { + return ImmutableRequestDiagnosticContext.builder() + .requestId(UUID.randomUUID()) + .invocationId(UUID.randomUUID()) + .build(); + } +} + + diff --git a/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URITest.java b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URITest.java new file mode 100644 index 00000000..b4a59638 --- /dev/null +++ b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URITest.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2019 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.uri; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class URITest { + + @Test + void buildProperUri() { + String expectedValue = "http://user@localhost:8080path?query#fragment"; + URI uri = new URI.URIBuilder().scheme("http") + .host("localhost") + .port(8080) + .path("path") + .fragment("fragment") + .authority("authority") + .userInfo("user") + .query("query") + .build(); + + assertEquals(expectedValue, uri.toString()); + } +} \ No newline at end of file diff --git a/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariablesTest.java b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariablesTest.java new file mode 100644 index 00000000..bb0cc6cf --- /dev/null +++ b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariablesTest.java @@ -0,0 +1,36 @@ +/* + * ============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 static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class MdcVariablesTest { + + @Test + void shouldReturnProperHttpHeader() { + String expectedValue = "X-header"; + String returnedValue = MdcVariables.httpHeader("header"); + + assertEquals(expectedValue, returnedValue); + } +} \ No newline at end of file -- cgit 1.2.3-korg