diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2021-08-18 17:17:23 -0400 |
---|---|---|
committer | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2021-08-19 19:23:03 +0000 |
commit | 9fca60734e14dadb6f89e327c6f7b65ad2223e45 (patch) | |
tree | 837cdd69fdfcdd9ec70467f064c7c95c34369b24 /plugins/reception-plugins | |
parent | e871bb8a1ed53cf7196d7c0547d94c099e780ab4 (diff) |
Add yaml support for policy & policy types
Adding support for yaml format of policy & policy types in distribution.
So that users can either define them in json or yaml format.
And then distribute to policy framework.
Issue-ID: POLICY-3509
Change-Id: Ifba7486bbc1e6876f0e3e39b0ae5ebe043567029
Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
(cherry picked from commit 801ab43d13537d19710e8f948d4614f158daafc7)
Diffstat (limited to 'plugins/reception-plugins')
3 files changed, 36 insertions, 1 deletions
diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java index 4703725d..e6bca426 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java @@ -32,6 +32,7 @@ import java.util.zip.ZipFile; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardYamlCoder; import org.onap.policy.distribution.model.Csar; import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; @@ -48,6 +49,7 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Tosc private PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters; private StandardCoder coder; + private StandardYamlCoder yamlCoder; private static final long MAX_FILE_SIZE = 512L * 1024; /** @@ -57,6 +59,7 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Tosc public void configure(final String parameterGroupName) { decoderParameters = ParameterService.get(parameterGroupName); coder = new StandardCoder(); + yamlCoder = new StandardYamlCoder(); } /** @@ -85,7 +88,7 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Tosc final ZipEntry entry = entries.nextElement(); // NOSONAR if (isZipEntryValid(entry.getName(), csar.getCsarPath(), entry.getSize())) { final ToscaServiceTemplate policy = - coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class); + decodeFile(zipFile, entry); policyList.add(policy); } } @@ -133,4 +136,22 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Tosc return false; } + + /** + * Method to decode either a json or yaml file into an object. + * + * @param zipFile the zip file + * @param entry the entry to read in the zip file. + * @return the decoded ToscaServiceTemplate object. + * @throws CoderException IOException if the file decoding fails. + */ + private ToscaServiceTemplate decodeFile(ZipFile zipFile, final ZipEntry entry) throws IOException, CoderException { + ToscaServiceTemplate policy = null; + if (entry.getName().endsWith(".json")) { + policy = coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class); + } else if (entry.getName().endsWith(".yaml")) { + policy = yamlCoder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class); + } + return policy; + } } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java index 812ef6d6..e76e9f38 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java @@ -81,6 +81,20 @@ public class PolicyDecoderFileInCsarToPolicyTest { } @Test + public void testDecodeYamlPolicy() throws PolicyDecodingException { + + final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); + decoder.configure(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName()); + + final File file = new File("src/test/resources/service-Sampleservice-yaml.csar"); + final Csar csar = new Csar(file.getAbsolutePath()); + + assertTrue(decoder.canHandle(csar)); + final Collection<ToscaEntity> policyHolders = decoder.decode(csar); + assertEquals(2, policyHolders.size()); + } + + @Test public void testDecodePolicyZipError() { final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); diff --git a/plugins/reception-plugins/src/test/resources/service-Sampleservice-yaml.csar b/plugins/reception-plugins/src/test/resources/service-Sampleservice-yaml.csar Binary files differnew file mode 100644 index 00000000..386dbeeb --- /dev/null +++ b/plugins/reception-plugins/src/test/resources/service-Sampleservice-yaml.csar |