From 9003b3a60dff679d5266b2c1af497890a2cc0ef3 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 23 May 2019 11:29:51 -0400 Subject: Log POST requests Add logging of POST requests received via the REST API. Change-Id: I8c71df3a45b8a3369b48eee06165dd5034c115a6 Issue-ID: POLICY-1777 Signed-off-by: Jim Hahn --- .../policy/api/main/rest/ApiRestController.java | 38 ++++++++++++++++++++++ .../api/main/rest/LegacyApiRestController.java | 38 ++++++++++++++++++++++ 2 files changed, 76 insertions(+) (limited to 'main') diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java index 24315427..5132bc63 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java @@ -54,7 +54,13 @@ import org.onap.policy.api.main.rest.provider.HealthCheckProvider; import org.onap.policy.api.main.rest.provider.PolicyProvider; import org.onap.policy.api.main.rest.provider.PolicyTypeProvider; import org.onap.policy.api.main.rest.provider.StatisticsProvider; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.report.HealthCheckReport; +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.authorative.concepts.ToscaPolicy; @@ -93,6 +99,8 @@ public class ApiRestController { private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestController.class); + private final Coder coder = new StandardCoder(); + /** * Retrieves the healthcheck status of the API component. * @@ -478,6 +486,10 @@ public class ApiRestController { @ApiParam(value = "Entity body of policy type", required = true) ToscaServiceTemplate body, @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { + if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) { + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes", toJson(body)); + } + try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) { ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(body); updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.POST); @@ -948,6 +960,12 @@ public class ApiRestController { @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId, @ApiParam(value = "Entity body of policy", required = true) ToscaServiceTemplate body) { + if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) { + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, + "/policytypes/" + policyTypeId + "/versions/" + policyTypeVersion + "/policies", + toJson(body)); + } + try (PolicyProvider policyProvider = new PolicyProvider()) { ToscaServiceTemplate serviceTemplate = policyProvider .createPolicy(policyTypeId, policyTypeVersion, body); @@ -1044,6 +1062,26 @@ public class ApiRestController { 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; + } + } + private enum Target { POLICY, POLICY_TYPE, OTHER } diff --git a/main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java index cb999808..05bb92f8 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java @@ -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 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 -- cgit 1.2.3-korg