Log POST requests 78/88378/1
authorJim Hahn <jrh3@att.com>
Thu, 23 May 2019 15:29:51 +0000 (11:29 -0400)
committerJim Hahn <jrh3@att.com>
Thu, 23 May 2019 15:29:51 +0000 (11:29 -0400)
Add logging of POST requests received via the REST API.

Change-Id: I8c71df3a45b8a3369b48eee06165dd5034c115a6
Issue-ID: POLICY-1777
Signed-off-by: Jim Hahn <jrh3@att.com>
main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java
main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java

index 2431542..5132bc6 100644 (file)
@@ -54,7 +54,13 @@ import org.onap.policy.api.main.rest.provider.HealthCheckProvider;
 import org.onap.policy.api.main.rest.provider.PolicyProvider;\r
 import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;\r
 import org.onap.policy.api.main.rest.provider.StatisticsProvider;\r
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;\r
 import org.onap.policy.common.endpoints.report.HealthCheckReport;\r
+import org.onap.policy.common.endpoints.utils.NetLoggerUtil;\r
+import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;\r
+import org.onap.policy.common.utils.coder.Coder;\r
+import org.onap.policy.common.utils.coder.CoderException;\r
+import org.onap.policy.common.utils.coder.StandardCoder;\r
 import org.onap.policy.models.base.PfModelException;\r
 import org.onap.policy.models.base.PfModelRuntimeException;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
@@ -93,6 +99,8 @@ public class ApiRestController {
 \r
     private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestController.class);\r
 \r
+    private final Coder coder = new StandardCoder();\r
+\r
     /**\r
      * Retrieves the healthcheck status of the API component.\r
      *\r
@@ -478,6 +486,10 @@ public class ApiRestController {
             @ApiParam(value = "Entity body of policy type", required = true) ToscaServiceTemplate body,\r
             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
 \r
+        if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {\r
+            NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes", toJson(body));\r
+        }\r
+\r
         try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) {\r
             ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(body);\r
             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.POST);\r
@@ -948,6 +960,12 @@ public class ApiRestController {
             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,\r
             @ApiParam(value = "Entity body of policy", required = true) ToscaServiceTemplate body) {\r
 \r
+        if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {\r
+            NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST,\r
+                            "/policytypes/" + policyTypeId + "/versions/" + policyTypeVersion + "/policies",\r
+                            toJson(body));\r
+        }\r
+\r
         try (PolicyProvider policyProvider = new PolicyProvider()) {\r
             ToscaServiceTemplate serviceTemplate = policyProvider\r
                     .createPolicy(policyTypeId, policyTypeVersion, body);\r
@@ -1044,6 +1062,26 @@ public class ApiRestController {
         return rb.header("X-ONAP-RequestID", requestId);\r
     }\r
 \r
+    /**\r
+     * Converts an object to a JSON string.\r
+     *\r
+     * @param object object to convert\r
+     * @return a JSON string representing the object\r
+     */\r
+    private String toJson(Object object) {\r
+        if (object == null) {\r
+            return null;\r
+        }\r
+\r
+        try {\r
+            return coder.encode(object);\r
+\r
+        } catch (CoderException e) {\r
+            LOGGER.warn("cannot convert {} to JSON", object.getClass().getName(), e);\r
+            return null;\r
+        }\r
+    }\r
+\r
     private enum Target {\r
         POLICY, POLICY_TYPE, OTHER\r
     }\r
index cb99980..05bb92f 100644 (file)
@@ -45,6 +45,12 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
 import org.onap.policy.api.main.rest.provider.LegacyGuardPolicyProvider;
 import org.onap.policy.api.main.rest.provider.LegacyOperationalPolicyProvider;
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
+import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
@@ -66,6 +72,8 @@ public class LegacyApiRestController {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyApiRestController.class);
 
+    private final Coder coder = new StandardCoder();
+
     /**
      * Retrieves the latest version of a particular guard policy.
      *
@@ -236,6 +244,11 @@ public class LegacyApiRestController {
             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
             @ApiParam(value = "Entity body of policy", required = true) LegacyGuardPolicyInput body) {
 
+        if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
+            NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST,
+                            "/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies", toJson(body));
+        }
+
         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
             Map<String, LegacyGuardPolicyOutput> policy = guardPolicyProvider.createGuardPolicy(body);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
@@ -482,6 +495,11 @@ public class LegacyApiRestController {
             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
             @ApiParam(value = "Entity body of policy", required = true) LegacyOperationalPolicy body) {
 
+        if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
+            NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST,
+                            "/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies", toJson(body));
+        }
+
         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
             LegacyOperationalPolicy policy = operationalPolicyProvider.createOperationalPolicy(body);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
@@ -570,4 +588,24 @@ public class LegacyApiRestController {
         }
         return rb.header("X-ONAP-RequestID", requestId);
     }
+
+    /**
+     * Converts an object to a JSON string.
+     *
+     * @param object object to convert
+     * @return a JSON string representing the object
+     */
+    private String toJson(Object object) {
+        if (object == null) {
+            return null;
+        }
+
+        try {
+            return coder.encode(object);
+
+        } catch (CoderException e) {
+            LOGGER.warn("cannot convert {} to JSON", object.getClass().getName(), e);
+            return null;
+        }
+    }
 }
\ No newline at end of file