summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src')
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java190
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/StatusCode.java27
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/AuditDataTest.java59
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java2
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/SpyAuditData.java63
5 files changed, 258 insertions, 83 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java
index 7292762474..fdef45a57f 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java
@@ -17,34 +17,186 @@
package org.openecomp.sdc.logging.api;
/**
- * @author KATYR
+ * Builder to populate audit data for logging according to
+ * <a href="https://wiki.onap.org/download/attachments/1015849/ONAP%20application%20logging%20guidelines.pdf?api=v2>
+ * ONAP application logging guidelines</a>. This includes only data known to an application, and not otherwise available
+ * to the logging framework.
+ *
+ * @author KATYR, evitaliy
* @since February 15, 2018
- * This interface defines part of the Audit log application is responsible to provide.
- * Fields list is according to ONAP application logging guidelines
- * (https://wiki.onap.org/download/attachments/1015849/ONAP%20application%20logging%20guidelines.pdf?api=v2)
- * StartTime -> BeginTimestamp (Date-time that processing for the activities begins)
- * EndTime-> EndTimestamp (Date-time that processing for the activities being logged ends)
- * StatusCode -> StatusCode (indicate high level success or failure of the operation activities that is invoked)
- * ResponseCode -> ResponseCode(application-specific response code returned by the operation activities)
- * ResponseDescription - > ResponseDescription (human readable description of the response code)
- * ClientIpAddress -> ClientIpAddress (Requesting remote client application’s IP address)
*/
+public class AuditData {
+
+ // A concrete implementation interface was chosen over an interface to enable to gracefully
+ // add new fields without affecting client code
+
+ private final long startTime;
+ private final long endTime;
+ private final StatusCode statusCode;
+ private final String responseCode;
+ private final String responseDescription;
+ private final String clientIpAddress;
-public interface AuditData {
+ AuditData(final AuditDataBuilder builder) {
+ this.startTime = builder.startTime;
+ this.endTime = builder.endTime;
+ this.statusCode = builder.statusCode;
+ this.responseCode = builder.responseCode;
+ this.responseDescription = builder.responseDescription;
+ this.clientIpAddress = builder.clientIpAddress;
+ }
+
+ /**
+ * Begin timestamp of an API invocation
+ *
+ * @return timestamp
+ */
+ public long getStartTime() {
+ return startTime;
+ }
- enum StatusCode {
- COMPLETE, ERROR
+ /**
+ * End timestamp of an API invocation
+ *
+ * @return timestamp
+ */
+ public long getEndTime() {
+ return endTime;
}
- long getStartTime();
+ /**
+ * Result status of an API invocation
+ *
+ * @return protocol and application agnostic status code
+ */
+ public StatusCode getStatusCode() {
+ return statusCode;
+ }
- long getEndTime();
+ /**
+ * Application/protocol specific response status of an API invocation
+ *
+ * @return response code
+ */
+ public String getResponseCode() {
+ return responseCode;
+ }
- StatusCode getStatusCode();
+ /**
+ * Application/protocol specific response in a human-friendly way
+ *
+ * @return human-friendly response description
+ */
+ public String getResponseDescription() {
+ return responseDescription;
+ }
- String getResponseCode();
+ /**
+ * IP address of the invoking client when available
+ *
+ * @return IP address
+ */
+ public String getClientIpAddress() {
+ return clientIpAddress;
+ }
- String getResponseDescription();
+ @Override
+ public String toString() {
+ return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode +
+ ", responseCode='" + responseCode + ", responseDescription=" + responseDescription + ", clientIpAddress="
+ + clientIpAddress + '}';
+ }
- String getClientIpAddress();
+ public static AuditDataBuilder builder() {
+ return new AuditDataBuilder();
+ }
+
+ public static class AuditDataBuilder {
+
+ private long startTime;
+ private long endTime;
+ private StatusCode statusCode;
+ private String responseCode;
+ private String responseDescription;
+ private String clientIpAddress;
+
+ AuditDataBuilder() { /* package-private default constructor to hide the public one */ }
+
+ /**
+ * Begin timestamp of an activity being audited
+ *
+ * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
+ * @return this builder for fluent API
+ */
+ public AuditDataBuilder startTime(final long startTime) {
+ this.startTime = startTime;
+ return this;
+ }
+
+ /**
+ * End timestamp of an activity being audited
+ *
+ * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
+ * @return this builder for fluent API
+ */
+ public AuditDataBuilder endTime(final long endTime) {
+ this.endTime = endTime;
+ return this;
+ }
+
+ /**
+ * Indicate whether an invocation was successful. It is up the the application to decide if a particular result
+ * must be treated as a success or a failure.
+ *
+ * @param statusCode invocation status success/failure
+ * @return this builder for fluent API
+ */
+ public AuditDataBuilder statusCode(final StatusCode statusCode) {
+ this.statusCode = statusCode;
+ return this;
+ }
+
+ /**
+ * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
+ *
+ * @param responseCode response code that depends on application and invocation protocol
+ * @return this builder for fluent API
+ */
+ public AuditDataBuilder responseCode(final String responseCode) {
+ this.responseCode = responseCode;
+ return this;
+ }
+
+ /**
+ * Response description that explains {@link #responseCode(String)} in a human-friendly way. For a Web API, it
+ * is likely to be a standard HTTP response phrase.
+ *
+ * @param responseDescription human-friendly response description
+ * @return this builder for fluent API
+ */
+ public AuditDataBuilder responseDescription(final String responseDescription) {
+ this.responseDescription = responseDescription;
+ return this;
+ }
+
+ /**
+ * IP address of an invoking client.
+ *
+ * @param clientIpAddress IP address
+ * @return this builder for fluent API
+ */
+ public AuditDataBuilder clientIpAddress(final String clientIpAddress) {
+ this.clientIpAddress = clientIpAddress;
+ return this;
+ }
+
+ /**
+ * Create an instance of {@link AuditData}
+ *
+ * @return a populated instance of audit data
+ */
+ public AuditData build() {
+ return new AuditData(this);
+ }
+ }
}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/StatusCode.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/StatusCode.java
new file mode 100644
index 0000000000..dd8bd57dcc
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/StatusCode.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright © 2016-2018 European Support Limited
+ *
+ * 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.
+ */
+
+package org.openecomp.sdc.logging.api;
+
+/**
+ * Protocol-agnostic status codes to indicate the result status (success, failure) of an API invocation.
+ *
+ * @author EVITALIY
+ * @since 04 Mar 18
+ */
+public enum StatusCode {
+ COMPLETE, ERROR
+}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/AuditDataTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/AuditDataTest.java
new file mode 100644
index 0000000000..08ce5089b7
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/AuditDataTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2016-2018 European Support Limited
+ *
+ * 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.
+ */
+
+package org.openecomp.sdc.logging.api;
+
+import static org.testng.Assert.*;
+
+import org.testng.annotations.Test;
+
+/**
+ * @author EVITALIY
+ * @since 04 Mar 18
+ */
+public class AuditDataTest {
+
+ @Test
+ public void allPropertiesReadWhenPopulated() {
+
+ final long start = System.currentTimeMillis();
+ final long end = start + 100;
+ final String responseCode = "Response-Code";
+ final String responseDescription = "Response-Description";
+ final String ipAddress = "10.56.20.70";
+
+ AuditData data = AuditData.builder().startTime(start).endTime(end).statusCode(StatusCode.COMPLETE)
+ .responseCode(responseCode).responseDescription(responseDescription).clientIpAddress(ipAddress).build();
+
+ assertEquals(data.getClientIpAddress(), ipAddress);
+ assertEquals(data.getEndTime(), end);
+ assertEquals(data.getStartTime(), start);
+ assertEquals(data.getResponseCode(), responseCode);
+ assertEquals(data.getResponseDescription(), responseDescription);
+ assertEquals(data.getStatusCode(), StatusCode.COMPLETE);
+ }
+
+ @Test
+ public void allPropertiesEmptyWhenUnpopulated() {
+ AuditData data = AuditData.builder().build();
+ assertEquals(data.getStartTime(), 0);
+ assertEquals(data.getEndTime(), 0);
+ assertNull(data.getClientIpAddress());
+ assertNull(data.getResponseCode());
+ assertNull(data.getResponseDescription());
+ assertNull(data.getStatusCode());
+ }
+} \ No newline at end of file
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java
index e174a32271..1889f3e172 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java
@@ -70,7 +70,7 @@ public class LoggerFactoryTest {
logger.warn("");
logger.info("");
logger.debug("");
- logger.audit(new SpyAuditData());
+ logger.audit(null);
logger.metrics("");
}
}
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/SpyAuditData.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/SpyAuditData.java
deleted file mode 100644
index 8766f2598b..0000000000
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/SpyAuditData.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright © 2016-2018 European Support Limited
- *
- * 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.
- */
-package org.openecomp.sdc.logging.api;
-
-import java.util.HashSet;
-import java.util.Set;
-
-public class SpyAuditData implements AuditData{
- private final Set<String> calledMethods = new HashSet<>();
-
- @Override
- public long getStartTime() {
- calledMethods.add("getStartTime");
- return 0;
- }
-
- @Override
- public long getEndTime() {
- calledMethods.add("getEndTime");
- return 0;
- }
-
- @Override
- public AuditData.StatusCode getStatusCode() {
- calledMethods.add("getEndTime");
- return null;
- }
-
- @Override
- public String getResponseCode() {
- calledMethods.add("getResponseCode");
- return null;
- }
-
- @Override
- public String getResponseDescription() {
- calledMethods.add("getResponseDescription");
- return null;
- }
-
- @Override
- public String getClientIpAddress() {
- calledMethods.add("getClientIpAddress");
- return null;
- }
-
- public boolean wasCalled(String method) {
- return calledMethods.contains(method);
- }
-}