diff options
5 files changed, 240 insertions, 146 deletions
diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java index 7c839a28..d60aa113 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java @@ -20,15 +20,17 @@ package org.onap.policy.distribution.reception.decoding.pdpx; +import com.google.common.collect.ImmutableMap; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -38,7 +40,6 @@ import org.onap.sdc.toscaparser.api.CapabilityAssignment; import org.onap.sdc.toscaparser.api.CapabilityAssignments; import org.onap.sdc.toscaparser.api.NodeTemplate; import org.onap.sdc.toscaparser.api.RequirementAssignment; -import org.onap.sdc.toscaparser.api.elements.Metadata; /** * Extract concerned info from NodeTemplate, currently ONLY HPA Feature. @@ -47,6 +48,7 @@ import org.onap.sdc.toscaparser.api.elements.Metadata; */ public class ExtractFromNode { + private static final String CONFIGURATION_VALUE = "configuration-value"; private static final Logger LOGGER = FlexLogger.getLogger(ExtractFromNode.class); private static final String VDU_TYPE = "tosca.nodes.nfv.Vdu.Compute"; private static final String VDU_CP_TYPE = "tosca.nodes.nfv.VduCp"; @@ -54,18 +56,15 @@ public class ExtractFromNode { private static final String NUM_VIRTUAL_CPU_PATH = "virtual_cpu#num_virtual_cpu"; private static final String CPU_ARCHITECTURE_PATH = "virtual_cpu#cpu_architecture"; private static final String MEMORY_PAGE_SIZE_PATH = "virtual_memory#vdu_memory_requirements#memoryPageSize"; - private static final String NETWORK_INTERFACE_TYPE_PATH = - "virtual_network_interface_requirements#network_interface_requirements#interfaceType"; - private static final String NETWORK_PCI_PATH = - "virtual_network_interface_requirements#nic_io_requirements"; + private static final String NETWORK_INTERFACE_TYPE_PATH = + "virtual_network_interface_requirements#network_interface_requirements#interfaceType"; + private static final String NETWORK_PCI_PATH = "virtual_network_interface_requirements#nic_io_requirements"; private static final String BASIC_CAPABILITIES_HPA_FEATURE = "BasicCapabilities"; private static final String HUGE_PAGES_HPA_FEATURE = "hugePages"; - private static final Map<String, String> NETWORK_HPA_FEATURE_MAP = new HashMap() {{ - put("SR-IOV", "SriovNICNetwork"); - put("PCI-Passthrough", "pciePassthrough"); - }}; + private static final Map<String, String> NETWORK_HPA_FEATURE_MAP = + ImmutableMap.of("SR-IOV", "SriovNICNetwork", "PCI-Passthrough", "pciePassthrough"); - ISdcCsarHelper sdcCsarHelper; + private ISdcCsarHelper sdcCsarHelper; final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create(); public void setSdcCsarHelper(final ISdcCsarHelper sdcCsarHelper) { @@ -73,13 +72,13 @@ public class ExtractFromNode { } /** - * ExtractInfo from VNF , each VNF may includes more than one VDUs and CPs return new generated - * PdpxPolicy if it has got Hpa feature info or else return null. + * ExtractInfo from VNF , each VNF may includes more than one VDUs and CPs return new generated PdpxPolicy if it has + * got Hpa feature info or else return null. * * @param node the NodeTemplate - * + * * @return the extracted info from input node - * + * * @throws PolicyDecodingException if extract fails */ public Content extractInfo(final NodeTemplate node) throws PolicyDecodingException { @@ -95,7 +94,7 @@ public class ExtractFromNode { for (final NodeTemplate nodeChild : lnodeChild) { final String type = sdcCsarHelper.getTypeOfNodeTemplate(nodeChild); LOGGER.debug("the type of this nodeChild = " + type); - LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(nodeChild)); + LOGGER.debug("the meta data of this nodeChild = " + sdcCsarHelper.getNodeTemplateMetadata(nodeChild)); if (type.equalsIgnoreCase(VDU_TYPE)) { lnodeVdu.add(nodeChild); } else if (type.equalsIgnoreCase(VDU_CP_TYPE)) { @@ -111,17 +110,15 @@ public class ExtractFromNode { if (content.getFlavorFeatures().isEmpty()) { return null; } - return content; } /** - * ExtractInfofromVdu, supported hpa features, All under the capability of - * tosca.nodes.nfv.Vdu.Compute. + * ExtractInfofromVdu, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute. * * @param lnodeVdu the list of Vdu node - * + * * @param content to be change based on lnodeVdu */ public void extractInfoVdu(final List<NodeTemplate> lnodeVdu, final Content content) { @@ -142,42 +139,43 @@ public class ExtractFromNode { capabilityAssignments.getCapabilityByName("virtual_compute"); if (capabilityAssignment != null) { generateBasicCapability(capabilityAssignment, flavorFeature); - generateHugePages(capabilityAssignment,flavorFeature); + generateHugePages(capabilityAssignment, flavorFeature); } content.getFlavorFeatures().add(flavorFeature); } } /** - * GenerateBasicCapability, supported hpa features, All under the capability of - * tosca.nodes.nfv.Vdu.Compute. + * GenerateBasicCapability, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute. * * @param capabilityAssignment represents the capability of node - * + * * @param flavorFeature represents all the features of specified flavor */ - private void generateBasicCapability(final CapabilityAssignment capabilityAssignment, FlavorFeature flavorFeature){ - //the format is xxx MB/GB like 4096 MB - String virtualMemSize = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, - VIRTUAL_MEM_SIZE_PATH); + private void generateBasicCapability(final CapabilityAssignment capabilityAssignment, + final FlavorFeature flavorFeature) { + // the format is xxx MB/GB like 4096 MB + final String virtualMemSize = + sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, VIRTUAL_MEM_SIZE_PATH); if (virtualMemSize != null) { LOGGER.debug("the virtualMemSize = " + virtualMemSize); - HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("virtualMemSize", virtualMemSize); - FlavorProperty flavorProperty = new FlavorProperty(); + final HpaFeatureAttribute hpaFeatureAttribute = + generateHpaFeatureAttribute("virtualMemSize", virtualMemSize); + final FlavorProperty flavorProperty = new FlavorProperty(); flavorProperty.setHpaFeature(BASIC_CAPABILITIES_HPA_FEATURE); flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute); flavorFeature.getFlavorProperties().add(flavorProperty); } - - //the format is int like 2 - String numVirtualCpu = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, - NUM_VIRTUAL_CPU_PATH); + + // the format is int like 2 + final String numVirtualCpu = + sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, NUM_VIRTUAL_CPU_PATH); if (numVirtualCpu != null) { LOGGER.debug("the numVirtualCpu = " + numVirtualCpu); - HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("numVirtualCpu", numVirtualCpu); - String cpuArchitecture = sdcCsarHelper.getCapabilityPropertyLeafValue - (capabilityAssignment,CPU_ARCHITECTURE_PATH); - FlavorProperty flavorProperty = new FlavorProperty(); + final HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("numVirtualCpu", numVirtualCpu); + final String cpuArchitecture = + sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, CPU_ARCHITECTURE_PATH); + final FlavorProperty flavorProperty = new FlavorProperty(); flavorProperty.setHpaFeature(BASIC_CAPABILITIES_HPA_FEATURE); if (cpuArchitecture != null) { flavorProperty.setArchitecture(cpuArchitecture); @@ -188,17 +186,16 @@ public class ExtractFromNode { } /** - * GenerateHpaFeatureAttribute based on the value of featureValue. the format: - * "hpa-attribute-key": "pciVendorId", "hpa-attribute-value": "1234", "operator": "=", "unit": - * "xxx". + * GenerateHpaFeatureAttribute based on the value of featureValue. the format: "hpa-attribute-key": "pciVendorId", + * "hpa-attribute-value": "1234", "operator": "=", "unit": "xxx". * * @param hpaAttributeKey get from the high layer tosca DM - * + * * @param featureValue get from the high layer tosca DM - * + * */ private HpaFeatureAttribute generateHpaFeatureAttribute(final String hpaAttributeKey, final String featureValue) { - //based on input featureValue, return back a suitable hpaFeatureAttribute + // based on input featureValue, return back a suitable hpaFeatureAttribute final HpaFeatureAttribute hpaFeatureAttribute = new HpaFeatureAttribute(); hpaFeatureAttribute.setHpaAttributeKey(hpaAttributeKey); final String tmp = featureValue.replace(" ", ""); @@ -217,28 +214,27 @@ public class ExtractFromNode { } /** - * GenerateHugePages, supported hpa features, All under the capability of - * tosca.nodes.nfv.Vdu.Compute. The format is a map like: {"schema-version": "0", - * "schema-location": "", "platform-id": "generic", "mandatory": true, "configuration-value": "2 - * MB"} + * GenerateHugePages, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute. The format is + * a map like: {"schema-version": "0", "schema-location": "", "platform-id": "generic", "mandatory": true, + * "configuration-value": "2 MB"} * * @param capabilityAssignment represents the capability of node - * + * * @param flavorFeature represents all the features of specified flavor */ - private void generateHugePages(final CapabilityAssignment capabilityAssignment, FlavorFeature flavorFeature){ - String memoryPageSize = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, - MEMORY_PAGE_SIZE_PATH); + private void generateHugePages(final CapabilityAssignment capabilityAssignment, final FlavorFeature flavorFeature) { + final String memoryPageSize = + sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, MEMORY_PAGE_SIZE_PATH); LOGGER.debug("the memoryPageSize = " + memoryPageSize); if (memoryPageSize != null) { - Map<String, Object> retMap = gson.fromJson(memoryPageSize, - new TypeToken<HashMap<String, Object>>() {}.getType()); + final Map<String, Object> retMap = + gson.fromJson(memoryPageSize, new TypeToken<HashMap<String, Object>>() {}.getType()); LOGGER.debug("the retMap = " + retMap); - String memoryPageSizeValue = retMap.get("configuration-value").toString(); - String mandatory = retMap.get("mandatory").toString(); - HpaFeatureAttribute hpaFeatureAttribute = - generateHpaFeatureAttribute("memoryPageSize",memoryPageSizeValue); - FlavorProperty flavorProperty = new FlavorProperty(); + final String memoryPageSizeValue = retMap.get(CONFIGURATION_VALUE).toString(); + final String mandatory = retMap.get("mandatory").toString(); + final HpaFeatureAttribute hpaFeatureAttribute = + generateHpaFeatureAttribute("memoryPageSize", memoryPageSizeValue); + final FlavorProperty flavorProperty = new FlavorProperty(); flavorProperty.setHpaFeature(HUGE_PAGES_HPA_FEATURE); if (mandatory != null) { flavorProperty.setMandatory(mandatory); @@ -253,78 +249,82 @@ public class ExtractFromNode { * tosca.nodes.nfv.VduCp. * * @param lnodeVduCp the list of VduCp node - * + * * @param content to be change based on lnodeVduCp * @throws PolicyDecodingException if extract CP fails */ - public void extractInfoVduCp(final List<NodeTemplate> lnodeVduCp, Content content) throws PolicyDecodingException { + public void extractInfoVduCp(final List<NodeTemplate> lnodeVduCp, final Content content) + throws PolicyDecodingException { // each CP will binds to a VDU so need the vdu flavor map info. - Map<String, FlavorFeature> vduFlavorMap = new HashMap<>(); - for ( FlavorFeature flavorFeature: content.getFlavorFeatures()) { + final Map<String, FlavorFeature> vduFlavorMap = new HashMap<>(); + for (final FlavorFeature flavorFeature : content.getFlavorFeatures()) { LOGGER.debug("the id = " + flavorFeature.getId()); - vduFlavorMap.put(flavorFeature.getId(),flavorFeature); + vduFlavorMap.put(flavorFeature.getId(), flavorFeature); } - for ( NodeTemplate node : lnodeVduCp) { - String interfaceType = sdcCsarHelper.getNodeTemplatePropertyLeafValue(node,NETWORK_INTERFACE_TYPE_PATH); - LOGGER.debug("the interfaceType = " + interfaceType); + for (final NodeTemplate node : lnodeVduCp) { + final String interfaceType = + sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, NETWORK_INTERFACE_TYPE_PATH); + LOGGER.debug("the interfaceType = " + interfaceType); Map<String, Object> retMap = new HashMap<>(); if (interfaceType != null) { retMap = gson.fromJson(interfaceType, new TypeToken<HashMap<String, Object>>() {}.getType()); LOGGER.debug("the retMap = " + retMap); } - String networkHpaFeature; - if ( retMap.containsKey("configuration-value")) { - String interfaceTypeValue = retMap.get("configuration-value").toString(); + String networkHpaFeature; + if (retMap.containsKey(CONFIGURATION_VALUE)) { + final String interfaceTypeValue = retMap.get(CONFIGURATION_VALUE).toString(); LOGGER.debug(" the interfacetype value is =" + interfaceTypeValue); - if ( NETWORK_HPA_FEATURE_MAP.containsKey(interfaceTypeValue)) { + if (NETWORK_HPA_FEATURE_MAP.containsKey(interfaceTypeValue)) { networkHpaFeature = NETWORK_HPA_FEATURE_MAP.get(interfaceTypeValue); LOGGER.debug(" the networkHpaFeature is =" + networkHpaFeature); } else { LOGGER.debug(" unspported network interface "); return; } - }else{ + } else { LOGGER.debug(" no configuration-value defined in interfaceType"); return; } - - for (RequirementAssignment requriement: sdcCsarHelper.getRequirementsOf(node).getAll()) { - String nodeTemplateName = requriement.getNodeTemplateName().toLowerCase(); + + for (final RequirementAssignment requriement : sdcCsarHelper.getRequirementsOf(node).getAll()) { + final String nodeTemplateName = requriement.getNodeTemplateName().toLowerCase(); LOGGER.debug("getNodeTemplateName =" + nodeTemplateName); - if ( nodeTemplateName == null) { - continue; - } + if (nodeTemplateName == null) { + continue; + } if (!vduFlavorMap.containsKey(nodeTemplateName)) { throw new PolicyDecodingException("vdu Flavor Map should contains the key " + nodeTemplateName); } generateNetworkFeature(networkHpaFeature, node, vduFlavorMap.get(nodeTemplateName)); } } - } + } - /* - * GenerateNetworkFeature, all pci feature are grouped into FlavorFeature together. + /** + * GenerateNetworkFeature, all pci feature are grouped into FlavorFeature together. The format is a map like: + * {"schema-version": "0", "schema-location": "", "platform-id": "generic", "mandatory": true, + * "configuration-value": "2 MB"} * * @param networkHpaFeature represents the specified Hpa feature * @param node represents the CP Node * @param flavorFeature represents all the features of specified flavor */ - private void generateNetworkFeature(final String networkHpaFeature, final NodeTemplate node, FlavorFeature flavorFeature) { - //the format is a map like: {"schema-version": "0", "schema-location": "", "platform-id": "generic", - // "mandatory": true, "configuration-value": "2 MB"} - FlavorProperty flavorProperty = new FlavorProperty(); + private void generateNetworkFeature(final String networkHpaFeature, final NodeTemplate node, + final FlavorFeature flavorFeature) { + final FlavorProperty flavorProperty = new FlavorProperty(); flavorProperty.setHpaFeature(networkHpaFeature); - String[] pciKeys = { "pciVendorId", "pciDeviceId", "pciNumDevices", "physicalNetwork"}; - for (String pciKey: pciKeys) { + final String[] pciKeys = { "pciVendorId", "pciDeviceId", "pciNumDevices", "physicalNetwork" }; + for (final String pciKey : pciKeys) { LOGGER.debug("the pciKey = " + pciKey); - String pciKeyPath = NETWORK_PCI_PATH + "#" + pciKey; - String pciValue = sdcCsarHelper.getNodeTemplatePropertyLeafValue(node,pciKeyPath); + final String pciKeyPath = NETWORK_PCI_PATH + "#" + pciKey; + final String pciValue = sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, pciKeyPath); if (pciValue != null) { LOGGER.debug("the pciValue = " + pciValue); - Map<String, Object> retMap = gson.fromJson(pciValue, new TypeToken<HashMap<String, Object>>() {}.getType()); - String pciConfigValue = retMap.get("configuration-value").toString(); - HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute(pciKey,pciConfigValue); + final Map<String, Object> retMap = + gson.fromJson(pciValue, new TypeToken<HashMap<String, Object>>() {}.getType()); + final String pciConfigValue = retMap.get(CONFIGURATION_VALUE).toString(); + final HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute(pciKey, pciConfigValue); flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute); } } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxConfigurationParameterBuilder.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxConfigurationParameterBuilder.java index 199d582b..4c781030 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxConfigurationParameterBuilder.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxConfigurationParameterBuilder.java @@ -32,31 +32,55 @@ public class PolicyDecoderCsarPdpxConfigurationParameterBuilder { private String riskType; private String riskLevel; + public String getPolicyNamePrefix() { + return policyNamePrefix; + } + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setPolicyNamePrefix(final String policyNamePrefix) { this.policyNamePrefix = policyNamePrefix; return this; } + public String getOnapName() { + return onapName; + } + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setOnapName(final String onapName) { this.onapName = onapName; return this; } + public String getVersion() { + return version; + } + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setVersion(final String version) { this.version = version; return this; } + public String getPriority() { + return priority; + } + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setPriority(final String priority) { this.priority = priority; return this; } + public String getRiskType() { + return riskType; + } + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setRiskType(final String riskType) { this.riskType = riskType; return this; } + public String getRiskLevel() { + return riskLevel; + } + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setRiskLevel(final String riskLevel) { this.riskLevel = riskLevel; return this; @@ -65,31 +89,6 @@ public class PolicyDecoderCsarPdpxConfigurationParameterBuilder { public PolicyDecoderCsarPdpxConfigurationParameterGroup build() { return new PolicyDecoderCsarPdpxConfigurationParameterGroup(this); } - - public String getPolicyNamePrefix() { - return policyNamePrefix; - } - - public String getOnapName() { - return onapName; - } - - public String getVersion() { - return version; - } - - public String getPriority() { - return priority; - } - - public String getRiskType() { - return riskType; - } - - public String getRiskLevel() { - return riskLevel; - } - } 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 9516ad70..80c5172f 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 @@ -70,16 +70,14 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Poli @Override public Collection<PolicyAsString> decode(final Csar csar) throws PolicyDecodingException { final Collection<PolicyAsString> policyList = new ArrayList<>(); - - try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) { + ZipFile zipFile = null; + try { + zipFile = new ZipFile(csar.getCsarPath()); final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); if (entry.getName().contains(decoderParameters.getPolicyFileName())) { - final StringWriter writer = new StringWriter(); - IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8"); - final PolicyAsString poilcy = new PolicyAsString(decoderParameters.getPolicyFileName(), - decoderParameters.getPolicyType(), writer.toString()); + final PolicyAsString poilcy = createPolicy(zipFile, entry); policyList.add(poilcy); } } @@ -87,8 +85,31 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Poli final String message = "Failed decoding the policy"; LOGGER.error(message, exp); throw new PolicyDecodingException(message, exp); + } finally { + if (zipFile != null) { + try { + zipFile.close(); + } catch (final IOException exp) { + LOGGER.error("Failed closing the zipFile", exp); + } + } } - return policyList; } + + /** + * Creates the policy from given input. + * + * @param zipFile the csar file + * @param entry an entry in the csar file + * @return the created policy + * @throws IOException if policy creation fails + */ + private PolicyAsString createPolicy(final ZipFile zipFile, final ZipEntry entry) throws IOException { + final StringWriter writer = new StringWriter(); + IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8"); + final PolicyAsString poilcy = new PolicyAsString(decoderParameters.getPolicyFileName(), + decoderParameters.getPolicyType(), writer.toString()); + return poilcy; + } } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpx.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpx.java index 0585b23a..21593868 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpx.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpx.java @@ -25,6 +25,8 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.Collection; + +import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.parameters.ParameterGroup; @@ -46,23 +48,31 @@ public class TestPolicyDecoderCsarPdpx { */ @BeforeClass public static void setUp() { - ParameterGroup parameterGroup = new PolicyDecoderCsarPdpxConfigurationParameterBuilder().setOnapName("onapName") - .setPolicyNamePrefix("OOF").setPriority("5").setRiskLevel("2").setRiskType("Test").setVersion("1.0") - .build(); + final ParameterGroup parameterGroup = new PolicyDecoderCsarPdpxConfigurationParameterBuilder() + .setOnapName("onapName").setPolicyNamePrefix("OOF").setPriority("5").setRiskLevel("2") + .setRiskType("Test").setVersion("1.0").build(); parameterGroup.setName(CSAR_TO_OPTIMIZATION_POLICY_CONFIGURATION); ParameterService.register(parameterGroup); } + /** + * Tear down. + */ + @AfterClass + public static void tearDown() { + ParameterService.deregister(CSAR_TO_OPTIMIZATION_POLICY_CONFIGURATION); + } + @Test public void testHpaPolicy2Vnf() throws IOException, PolicyDecodingException { - Csar csar = new Csar("src/test/resources/service-TestNs8-csar.csar"); - PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx(); + final Csar csar = new Csar("src/test/resources/service-TestNs8-csar.csar"); + final PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx(); policyDecoderCsarPdpx.configure(CSAR_TO_OPTIMIZATION_POLICY_CONFIGURATION); - Collection<OptimizationPolicy> ret = policyDecoderCsarPdpx.decode(csar); + final Collection<OptimizationPolicy> ret = policyDecoderCsarPdpx.decode(csar); assertEquals(2, ret.size()); - OptimizationPolicy policy = (OptimizationPolicy) ret.toArray()[0]; + final OptimizationPolicy policy = (OptimizationPolicy) ret.toArray()[0]; assertEquals("onapName", policy.getOnapName()); assertTrue(policy.getPolicyName().startsWith("OOF.")); @@ -86,12 +96,12 @@ public class TestPolicyDecoderCsarPdpx { @Test public void testHpaPolicySriov() throws IOException, PolicyDecodingException { - Csar csar = new Csar("src/test/resources/hpaPolicySriov.csar"); - PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx(); + final Csar csar = new Csar("src/test/resources/hpaPolicySriov.csar"); + final PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx(); policyDecoderCsarPdpx.configure(CSAR_TO_OPTIMIZATION_POLICY_CONFIGURATION); - Collection<OptimizationPolicy> policies = policyDecoderCsarPdpx.decode(csar); - OptimizationPolicy policy = (OptimizationPolicy) policies.toArray()[0]; + final Collection<OptimizationPolicy> policies = policyDecoderCsarPdpx.decode(csar); + final OptimizationPolicy policy = (OptimizationPolicy) policies.toArray()[0]; assertEquals("onapName", policy.getOnapName()); assertTrue(policy.getPolicyName().startsWith("OOF.")); @@ -126,13 +136,13 @@ public class TestPolicyDecoderCsarPdpx { @Test public void testHpaPolicyPciePassthrough() throws IOException, PolicyDecodingException { - Csar csar = new Csar("src/test/resources/hpaPolicyPciePassthrough.csar"); - PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx(); + final Csar csar = new Csar("src/test/resources/hpaPolicyPciePassthrough.csar"); + final PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx(); policyDecoderCsarPdpx.configure(CSAR_TO_OPTIMIZATION_POLICY_CONFIGURATION); - Collection<OptimizationPolicy> policies = policyDecoderCsarPdpx.decode(csar); + final Collection<OptimizationPolicy> policies = policyDecoderCsarPdpx.decode(csar); assertEquals(2, policies.size()); - OptimizationPolicy policy = (OptimizationPolicy) policies.toArray()[0]; + final OptimizationPolicy policy = (OptimizationPolicy) policies.toArray()[0]; assertEquals("onapName", policy.getOnapName()); assertTrue(policy.getPolicyName().startsWith("OOF.")); @@ -169,13 +179,13 @@ public class TestPolicyDecoderCsarPdpx { @Test public void testHpaPolicyHugePage() throws IOException, PolicyDecodingException { - Csar csar = new Csar("src/test/resources/hpaPolicyHugePage.csar"); - PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx(); + final Csar csar = new Csar("src/test/resources/hpaPolicyHugePage.csar"); + final PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx(); policyDecoderCsarPdpx.configure(CSAR_TO_OPTIMIZATION_POLICY_CONFIGURATION); - Collection<OptimizationPolicy> policies = policyDecoderCsarPdpx.decode(csar); + final Collection<OptimizationPolicy> policies = policyDecoderCsarPdpx.decode(csar); assertEquals(2, policies.size()); - OptimizationPolicy policy = (OptimizationPolicy) policies.toArray()[0]; + final OptimizationPolicy policy = (OptimizationPolicy) policies.toArray()[0]; assertEquals("onapName", policy.getOnapName()); assertTrue(policy.getPolicyName().startsWith("OOF.")); diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpxConfigurationParameterGroup.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpxConfigurationParameterGroup.java new file mode 100644 index 00000000..bebb8459 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpxConfigurationParameterGroup.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.decoding.pdpx; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationStatus; + +/** + * Class to perform unit test of {@link PolicyDecoderCsarPdpxConfigurationParameterGroup}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class TestPolicyDecoderCsarPdpxConfigurationParameterGroup { + + private static final String OPTIMIZATION_POLICY_CONFIGURATION = "optimizationPolicyConfiguration"; + + @Test + public void testBuilderAndGetters() { + final PolicyDecoderCsarPdpxConfigurationParameterGroup parameterGroup = + new PolicyDecoderCsarPdpxConfigurationParameterBuilder().setOnapName("onapName") + .setPolicyNamePrefix("OOF").setPriority("5").setRiskLevel("2").setRiskType("Test") + .setVersion("1.0").build(); + parameterGroup.setName(OPTIMIZATION_POLICY_CONFIGURATION); + + assertEquals(OPTIMIZATION_POLICY_CONFIGURATION, parameterGroup.getName()); + assertEquals("onapName", parameterGroup.getOnapName()); + assertEquals("OOF", parameterGroup.getPolicyNamePrefix()); + assertEquals("5", parameterGroup.getPriority()); + assertEquals("2", parameterGroup.getRiskLevel()); + assertEquals("Test", parameterGroup.getRiskType()); + assertEquals("1.0", parameterGroup.getVersion()); + assertEquals(ValidationStatus.CLEAN, parameterGroup.validate().getStatus()); + } + + @Test + public void testInvalidParameters() { + final PolicyDecoderCsarPdpxConfigurationParameterGroup parameterGroup = + new PolicyDecoderCsarPdpxConfigurationParameterBuilder().setOnapName("").setPolicyNamePrefix("") + .setPriority("").setRiskLevel("").setRiskType("").setVersion("").build(); + parameterGroup.setName(OPTIMIZATION_POLICY_CONFIGURATION); + + assertEquals(ValidationStatus.INVALID, parameterGroup.validate().getStatus()); + } +} |