diff options
author | Rashmi Pujar <rashmi.pujar@bell.ca> | 2019-11-25 12:56:24 -0500 |
---|---|---|
committer | Rashmi Pujar <rashmi.pujar@bell.ca> | 2019-11-26 13:27:37 -0500 |
commit | 281f653d38161409b553ff01b30f80a6903827b5 (patch) | |
tree | 11b17c1add60dd00089f9e2cb5a74128967b310a | |
parent | 0ca7816c5f88288ed72f7b69f0813c8da3b63ebe (diff) |
CDS actor payload to be JSON object when applicable
When the policy-payload supplied by CLAMP is a valid JSON string
the CDS Blueprint payload must be a JSON object, else retain the
CDS Blueprint payload to be a string.
Issue-ID: POLICY-2253
Signed-off-by: Rashmi Pujar <rashmi.pujar@bell.ca>
Change-Id: I6380ab0424339603ecf52d298e4f6f0cd8df754b
3 files changed, 71 insertions, 12 deletions
diff --git a/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/constants/CdsActorConstants.java b/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/constants/CdsActorConstants.java index 8f929c83e..0dc5bfb34 100644 --- a/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/constants/CdsActorConstants.java +++ b/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/constants/CdsActorConstants.java @@ -32,6 +32,7 @@ public class CdsActorConstants { // CDS blueprint archive parameters public static final String KEY_CBA_NAME = "artifact_name"; public static final String KEY_CBA_VERSION = "artifact_version"; + public static final String KEY_POLICY_PAYLOAD_DATA = "data"; public static final String KEY_RESOLUTION_KEY = "resolution-key"; public static final String CDS_REQUEST_SUFFIX = "-request"; public static final String CDS_REQUEST_PROPERTIES_SUFFIX = "-properties"; diff --git a/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/request/CdsActionRequest.java b/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/request/CdsActionRequest.java index 4193db59f..e362fe874 100644 --- a/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/request/CdsActionRequest.java +++ b/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/request/CdsActionRequest.java @@ -57,14 +57,26 @@ public class CdsActionRequest implements Serializable { * @throws CoderException if error occurs when serializing to JSON string */ public String generateCdsPayload() throws CoderException { - // 1. Build the innermost object to include AAI properties and policy payload information - Map<String, String> cdsActionPropsMap = new LinkedHashMap<>(); - cdsActionPropsMap.putAll(aaiProperties); - cdsActionPropsMap.putAll(policyPayload); + // 1a. Build the innermost object to include AAI properties + Map<String, Object> cdsActionPropsMap = new LinkedHashMap<>(aaiProperties); if (additionalEventParams != null) { cdsActionPropsMap.putAll(additionalEventParams); } + // 1b. Build the innermost object to include policy payload. + // If the policy payload data is a valid JSON string convert to an object else retain as a string. + if (policyPayload != null && !policyPayload.isEmpty()) { + String payload = policyPayload.get(CdsActorConstants.KEY_POLICY_PAYLOAD_DATA); + try { + Object payloadObj = CODER.decode(payload, Object.class); + cdsActionPropsMap.put(CdsActorConstants.KEY_POLICY_PAYLOAD_DATA, payloadObj); + } catch (CoderException e) { + cdsActionPropsMap.put(CdsActorConstants.KEY_POLICY_PAYLOAD_DATA, payload); + } + } else { + cdsActionPropsMap.put(CdsActorConstants.KEY_POLICY_PAYLOAD_DATA, ""); + } + // 2. Build the enclosing CDS action request properties object to contain (1) and the resolution-key Map<String, Object> cdsActionRequestMap = new LinkedHashMap<>(); cdsActionRequestMap.put(CdsActorConstants.KEY_RESOLUTION_KEY, resolutionKey); diff --git a/models-interactions/model-actors/actor.cds/src/test/java/org/onap/policy/controlloop/actor/cds/request/CdsActionRequestTest.java b/models-interactions/model-actors/actor.cds/src/test/java/org/onap/policy/controlloop/actor/cds/request/CdsActionRequestTest.java index e34fa33ab..7266d0311 100644 --- a/models-interactions/model-actors/actor.cds/src/test/java/org/onap/policy/controlloop/actor/cds/request/CdsActionRequestTest.java +++ b/models-interactions/model-actors/actor.cds/src/test/java/org/onap/policy/controlloop/actor/cds/request/CdsActionRequestTest.java @@ -18,10 +18,13 @@ package org.onap.policy.controlloop.actor.cds.request; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableMap; +import java.util.HashMap; import java.util.Map; +import org.junit.Before; import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants; @@ -29,23 +32,65 @@ import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants; public class CdsActionRequestTest { private static final String TEST_ACTION_NAME = "vfw-modify-config"; + private CdsActionRequest req = new CdsActionRequest(); - @Test - public void testGenerateCdsPayload() throws CoderException { - // Setup the CdsActionRequest object - CdsActionRequest req = new CdsActionRequest(); + /** + * Setup the CdsActionRequest object. + */ + @Before + public void setUp() { req.setActionName(TEST_ACTION_NAME); req.setResolutionKey("1234567890"); - Map<String, String> payloadProps = ImmutableMap.of("data", "{\"active-streams\":\"5\"}"); - req.setPolicyPayload(payloadProps); Map<String, String> aaiParams = ImmutableMap.of("service-instance.service-instance-id", "1234", "generic-vnf.vnf-id", "5678"); req.setAaiProperties(aaiParams); - Map<String, String> eventParams = - ImmutableMap.of("event-param-1", "1234", "event-param-2", "5678"); + Map<String, String> eventParams = ImmutableMap.of("event-param-1", "1234", "event-param-2", "5678"); req.setAdditionalEventParams(eventParams); + } + + @Test + public void testGenerateCdsPayloadWhenPolicyPayloadIsNotValidJsonString() throws CoderException { + String payloadStr = "active-streams=5"; + Map<String, String> payloadProps = ImmutableMap.of(CdsActorConstants.KEY_POLICY_PAYLOAD_DATA, payloadStr); + req.setPolicyPayload(payloadProps); + + // Act + String result = req.generateCdsPayload(); + + // Assert + assertTrue(result.contains(TEST_ACTION_NAME + CdsActorConstants.CDS_REQUEST_PROPERTIES_SUFFIX)); + assertTrue(result.contains(TEST_ACTION_NAME + CdsActorConstants.CDS_REQUEST_SUFFIX)); + assertTrue(result.contains(CdsActorConstants.KEY_RESOLUTION_KEY)); + assertTrue(result.contains("\"" + CdsActorConstants.KEY_POLICY_PAYLOAD_DATA + "\":\"" + payloadStr + "\"")); + } + + @Test + public void testGenerateCdsPayloadWhenPolicyPayloadIsValidJsonString() throws CoderException { + String payloadStr = "{\"active-streams\":\"5\"}"; + Map<String, String> payloadProps = ImmutableMap.of(CdsActorConstants.KEY_POLICY_PAYLOAD_DATA, payloadStr); + req.setPolicyPayload(payloadProps); + + // Act + String result = req.generateCdsPayload(); + + // Assert + assertTrue(result.contains(TEST_ACTION_NAME + CdsActorConstants.CDS_REQUEST_PROPERTIES_SUFFIX)); + assertTrue(result.contains(TEST_ACTION_NAME + CdsActorConstants.CDS_REQUEST_SUFFIX)); + assertTrue(result.contains(CdsActorConstants.KEY_RESOLUTION_KEY)); + assertTrue(result.contains("\"" + CdsActorConstants.KEY_POLICY_PAYLOAD_DATA + "\":" + payloadStr)); + } + + @Test + public void testGenerateCdsPayloadWhenPolicyPayloadIsNull() throws CoderException { + Map<String, String> payloadProps = new HashMap<String, String>() { + private static final long serialVersionUID = 1L; + { + put(CdsActorConstants.KEY_POLICY_PAYLOAD_DATA, null); + } + }; + req.setPolicyPayload(payloadProps); // Act String result = req.generateCdsPayload(); @@ -54,5 +99,6 @@ public class CdsActionRequestTest { assertTrue(result.contains(TEST_ACTION_NAME + CdsActorConstants.CDS_REQUEST_PROPERTIES_SUFFIX)); assertTrue(result.contains(TEST_ACTION_NAME + CdsActorConstants.CDS_REQUEST_SUFFIX)); assertTrue(result.contains(CdsActorConstants.KEY_RESOLUTION_KEY)); + assertFalse(result.contains(CdsActorConstants.KEY_POLICY_PAYLOAD_DATA)); } } |