summaryrefslogtreecommitdiffstats
path: root/policy-utils/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'policy-utils/src/main/java')
-rw-r--r--policy-utils/src/main/java/org/onap/policy/drools/utils/NetworkUtil.java113
-rw-r--r--policy-utils/src/main/java/org/onap/policy/drools/utils/logging/MDCTransaction.java176
2 files changed, 88 insertions, 201 deletions
diff --git a/policy-utils/src/main/java/org/onap/policy/drools/utils/NetworkUtil.java b/policy-utils/src/main/java/org/onap/policy/drools/utils/NetworkUtil.java
deleted file mode 100644
index 229927e5..00000000
--- a/policy-utils/src/main/java/org/onap/policy/drools/utils/NetworkUtil.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP
- * ================================================================================
- * Copyright (C) 2017-2018 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.policy.drools.utils;
-
-import java.io.IOException;
-import java.net.ConnectException;
-import java.net.InetAddress;
-import java.net.Socket;
-
-import java.net.UnknownHostException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Network Utilities
- */
-public class NetworkUtil {
-
- public static final Logger logger = LoggerFactory.getLogger(NetworkUtil.class.getName());
-
- /**
- * IPv4 Wildcard IP address
- */
- public static final String IPv4_WILDCARD_ADDRESS = "0.0.0.0";
-
- private NetworkUtil() {
- // Empty constructor
- }
-
- /**
- * try to connect to $host:$port $retries times while we are getting connection failures.
- *
- * @param host host
- * @param port port
- * @param retries number of attempts
- * @return true is port is open, false otherwise
- * @throws InterruptedException if execution has been interrupted
- */
- public static boolean isTcpPortOpen(String host, int port, int retries, long interval)
- throws InterruptedException, IOException {
- int retry = 0;
- while (retry < retries) {
- try (Socket s = new Socket(host, port)) {
- logger.debug("{}:{} connected - retries={} interval={}", host, port, retries, interval);
- return true;
- } catch (final ConnectException e) {
- retry++;
- logger.trace("{}:{} connected - retries={} interval={}", host, port, retries, interval, e);
- Thread.sleep(interval);
- }
- }
-
- logger.warn("{}:{} closed = retries={} interval={}", host, port, retries, interval);
- return false;
- }
-
- /**
- * gets host name
- *
- * @return host name
- */
- public static String getHostname() {
-
- String hostname = System.getenv("HOSTNAME");
- if (hostname != null && !hostname.isEmpty()) {
- return hostname;
- }
-
- try {
- return InetAddress.getLocalHost().getHostName();
- } catch (UnknownHostException e) {
- logger.warn("cannot resolve local hostname", e);
- /* continue */
- }
-
- return "localhost";
- }
-
- /**
- * gets host's IP
- *
- * @return host IP
- */
- public static String getHostIp() {
-
- try {
- return InetAddress.getLocalHost().getHostAddress();
- } catch (UnknownHostException e) {
- logger.warn("cannot resolve local hostname", e);
- /* continue */
- }
-
- return "127.0.0.1";
- }
-}
diff --git a/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/MDCTransaction.java b/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/MDCTransaction.java
index 1a27dde6..39fbe8fb 100644
--- a/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/MDCTransaction.java
+++ b/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/MDCTransaction.java
@@ -24,7 +24,8 @@ import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.UUID;
-import org.onap.policy.drools.utils.NetworkUtil;
+
+import org.onap.policy.common.utils.network.NetworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
@@ -32,51 +33,28 @@ import org.slf4j.MDC;
/**
* MDC Transaction Utility Class.
*
- * There is an implicit 2-level tree of Transactions in ONAP: transactions
- * and subtransactions.
+ * There is an implicit 2-level tree of Transactions in ONAP: transactions and subtransactions.
*
- * 1. The top level transaction relates to the overarching transaction
- * id (ie. RequestId) and should be made available to subtransactions
- * for reuse in the ThreadLocal MDC structure.
+ * 1. The top level transaction relates to the overarching transaction id (ie. RequestId) and should
+ * be made available to subtransactions for reuse in the ThreadLocal MDC structure.
*
- * This is the data to be inherited and common to all subtransactions
- * (not a common case but could be modified by subtransactions):
+ * This is the data to be inherited and common to all subtransactions (not a common case but could
+ * be modified by subtransactions):
*
- * Request ID
- * Virtual Server Name
- * Partner Name
- * Server
- * Server IP Address
- * Server FQDN
+ * Request ID Virtual Server Name Partner Name Server Server IP Address Server FQDN
*
- * 2. The second level at the leaves is formed by subtransactions and the key
- * identifier is the invocation id.
+ * 2. The second level at the leaves is formed by subtransactions and the key identifier is the
+ * invocation id.
*
- * Begin Timestamp
- * End Timestamp
- * Elapsed Time
- * Service Instance ID
- * Service Name
- * Status Code
- * Response Code
- * Response Description
- * Instance UUID
- * Severity
- * Target Entity
- * Target Service Name
- * Server
- * Server IP Address
- * Server FQDN
- * Client IP Address
- * Process Key
- * Remote Host
- * Alert Severity
- * Target Virtual Entity
+ * Begin Timestamp End Timestamp Elapsed Time Service Instance ID Service Name Status Code Response
+ * Code Response Description Instance UUID Severity Target Entity Target Service Name Server Server
+ * IP Address Server FQDN Client IP Address Process Key Remote Host Alert Severity Target Virtual
+ * Entity
*
*
* The naming convention for the fields must match the naming given at
*
- * https://wiki.onap.org/pages/viewpage.action?pageId=20087036
+ * https://wiki.onap.org/pages/viewpage.action?pageId=20087036
*/
public interface MDCTransaction {
/*
@@ -165,8 +143,8 @@ public interface MDCTransaction {
String TARGET_SERVICE_NAME = "TargetServiceName";
/**
- * Server Subtransactions inherit this value. if (this.getSources().size() == 1)
- this.getSources().get(0).getTopic();
+ * Server Subtransactions inherit this value. if (this.getSources().size() == 1)
+ * this.getSources().get(0).getTopic();
*/
String SERVER = "Server";
@@ -246,16 +224,14 @@ public interface MDCTransaction {
MDCTransaction flush();
/**
- * convenience method to log a metric. Alternatively caller
- * could call flush() and the logging statement directly for
- * further granularity.
+ * convenience method to log a metric. Alternatively caller could call flush() and the logging
+ * statement directly for further granularity.
*/
MDCTransaction metric();
/**
- * convenience method to log a transaction record. Alternatively caller
- * could call flush() and the logging statement directly for
- * further granularity.
+ * convenience method to log a transaction record. Alternatively caller could call flush() and
+ * the logging statement directly for further granularity.
*/
MDCTransaction transaction();
@@ -348,6 +324,7 @@ public interface MDCTransaction {
* set virtual server
*/
MDCTransaction setVirtualServerName(String virtualServerName);
+
/**
* sets end time
*/
@@ -549,6 +526,7 @@ public interface MDCTransaction {
}
+
class MDCTransactionImpl implements MDCTransaction {
private final static Logger logger = LoggerFactory.getLogger(MDCTransactionImpl.class.getName());
@@ -602,8 +580,8 @@ class MDCTransactionImpl implements MDCTransaction {
/**
* MDC Transaction
*
- * @param requestId transaction id
- * @param partner transaction origin
+ * @param requestId transaction id
+ * @param partner transaction origin
*/
public MDCTransactionImpl(String requestId, String partner) {
MDC.clear();
@@ -680,24 +658,24 @@ class MDCTransactionImpl implements MDCTransaction {
*/
@Override
public MDCTransaction resetSubTransaction() {
- MDC.remove(INVOCATION_ID);
- MDC.remove(BEGIN_TIMESTAMP);
- MDC.remove(END_TIMESTAMP);
- MDC.remove(ELAPSED_TIME);
- MDC.remove(SERVICE_INSTANCE_ID);
- MDC.remove(STATUS_CODE);
- MDC.remove(RESPONSE_CODE);
- MDC.remove(RESPONSE_DESCRIPTION);
- MDC.remove(INSTANCE_UUID);
- MDC.remove(TARGET_ENTITY);
- MDC.remove(TARGET_SERVICE_NAME);
- MDC.remove(PROCESS_KEY);
- MDC.remove(CLIENT_IP_ADDRESS);
- MDC.remove(REMOTE_HOST);
- MDC.remove(ALERT_SEVERITY);
- MDC.remove(TARGET_VIRTUAL_ENTITY);
-
- return this;
+ MDC.remove(INVOCATION_ID);
+ MDC.remove(BEGIN_TIMESTAMP);
+ MDC.remove(END_TIMESTAMP);
+ MDC.remove(ELAPSED_TIME);
+ MDC.remove(SERVICE_INSTANCE_ID);
+ MDC.remove(STATUS_CODE);
+ MDC.remove(RESPONSE_CODE);
+ MDC.remove(RESPONSE_DESCRIPTION);
+ MDC.remove(INSTANCE_UUID);
+ MDC.remove(TARGET_ENTITY);
+ MDC.remove(TARGET_SERVICE_NAME);
+ MDC.remove(PROCESS_KEY);
+ MDC.remove(CLIENT_IP_ADDRESS);
+ MDC.remove(REMOTE_HOST);
+ MDC.remove(ALERT_SEVERITY);
+ MDC.remove(TARGET_VIRTUAL_ENTITY);
+
+ return this;
}
@Override
@@ -711,32 +689,41 @@ class MDCTransactionImpl implements MDCTransaction {
*/
@Override
public MDCTransaction flush() {
- if (this.requestId != null && !this.requestId.isEmpty())
+ if (this.requestId != null && !this.requestId.isEmpty()) {
MDC.put(REQUEST_ID, this.requestId);
+ }
- if (this.invocationId != null && !this.invocationId.isEmpty())
+ if (this.invocationId != null && !this.invocationId.isEmpty()) {
MDC.put(INVOCATION_ID, this.invocationId);
+ }
- if (this.partner != null)
+ if (this.partner != null) {
MDC.put(PARTNER_NAME, this.partner);
+ }
- if (this.virtualServerName != null)
+ if (this.virtualServerName != null) {
MDC.put(VIRTUAL_SERVER_NAME, this.virtualServerName);
+ }
- if (this.server != null)
+ if (this.server != null) {
MDC.put(SERVER, this.server);
+ }
- if (this.serverIpAddress != null)
+ if (this.serverIpAddress != null) {
MDC.put(SERVER_IP_ADDRESS, this.serverIpAddress);
+ }
- if (this.serverFqdn != null)
+ if (this.serverFqdn != null) {
MDC.put(SERVER_FQDN, this.serverFqdn);
+ }
- if (this.serviceName != null)
+ if (this.serviceName != null) {
MDC.put(SERVICE_NAME, this.serviceName);
+ }
- if (this.startTime != null)
+ if (this.startTime != null) {
MDC.put(BEGIN_TIMESTAMP, timestamp(this.startTime));
+ }
if (this.endTime != null) {
MDC.put(END_TIMESTAMP, timestamp(this.endTime));
@@ -749,49 +736,62 @@ class MDCTransactionImpl implements MDCTransaction {
MDC.put(ELAPSED_TIME, String.valueOf(this.elapsedTime));
} else {
if (endTime != null && startTime != null) {
- this.elapsedTime = Duration.between(startTime, endTime).toMillis();
+ this.elapsedTime = Duration.between(startTime, endTime).toMillis();
MDC.put(ELAPSED_TIME, String.valueOf(this.elapsedTime));
}
}
- if (this.serviceInstanceId != null)
+ if (this.serviceInstanceId != null) {
MDC.put(SERVICE_INSTANCE_ID, this.serviceInstanceId);
+ }
- if (this.instanceUUID != null)
+ if (this.instanceUUID != null) {
MDC.put(INSTANCE_UUID, this.instanceUUID);
+ }
- if (this.processKey != null)
+ if (this.processKey != null) {
MDC.put(PROCESS_KEY, this.processKey);
+ }
- if (this.statusCode != null)
+ if (this.statusCode != null) {
MDC.put(STATUS_CODE, this.statusCode);
+ }
- if (this.responseCode != null)
+ if (this.responseCode != null) {
MDC.put(RESPONSE_CODE, this.responseCode);
+ }
- if (this.responseDescription != null)
+ if (this.responseDescription != null) {
MDC.put(RESPONSE_DESCRIPTION, this.responseDescription);
+ }
- if (this.severity != null)
+ if (this.severity != null) {
MDC.put(SEVERITY, this.severity);
+ }
- if (this.alertSeverity != null)
+ if (this.alertSeverity != null) {
MDC.put(ALERT_SEVERITY, this.alertSeverity);
+ }
- if (this.targetEntity != null)
+ if (this.targetEntity != null) {
MDC.put(TARGET_ENTITY, this.targetEntity);
+ }
- if (this.targetServiceName != null)
+ if (this.targetServiceName != null) {
MDC.put(TARGET_SERVICE_NAME, this.targetServiceName);
+ }
- if (this.targetVirtualEntity != null)
+ if (this.targetVirtualEntity != null) {
MDC.put(TARGET_VIRTUAL_ENTITY, this.targetVirtualEntity);
+ }
- if (this.clientIpAddress != null)
+ if (this.clientIpAddress != null) {
MDC.put(CLIENT_IP_ADDRESS, this.clientIpAddress);
+ }
- if (this.remoteHost != null)
+ if (this.remoteHost != null) {
MDC.put(REMOTE_HOST, this.remoteHost);
+ }
return this;
}