diff options
author | liboNet <libo.zhu@intel.com> | 2018-09-11 11:07:04 +0800 |
---|---|---|
committer | liboNet <libo.zhu@intel.com> | 2018-09-13 12:37:03 +0800 |
commit | 571b40bd86ffeb24f707539f3fe535ff1b2e2ebc (patch) | |
tree | 6d1243fe9cdacde98bd41b9ad5007d3176088109 /plugins/reception-plugins/src/main/java | |
parent | 1b5763e20c91f40da4ad44b2d6bd09331705b27a (diff) |
Adding code for dynamically generate Polices
* Update sdc tosca version to 1.4.1
* Define the objects to represents the policy
used by OOF/Mutlicloud/AAI etc
* Integration into the distribution framework
to update PdpxPolicy and add new File
PolicyDecoderCsarPdpx to do decoder from CSAR
* Add two Test case including 2 VNFs, each VNF
maps to 1 policy, so to generate 2 polices
* use @SerializedName make the variable name
different with that in the out json field
Change-Id: Icc7175082063db8a245bbe576085cdf5a4b51adc
Issue-ID: POLICY-927
Signed-off-by: liboNet <libo.zhu@intel.com>
Diffstat (limited to 'plugins/reception-plugins/src/main/java')
9 files changed, 861 insertions, 2 deletions
diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Attribute.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Attribute.java new file mode 100644 index 00000000..c86bae9e --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Attribute.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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 com.google.gson.annotations.SerializedName; + +/** + * The attribute acts an abstraction belonging to or characteristic of an directive. + * + * @author Libo Zhu (libo.zhu@intel.com) + */ +class Attribute { + @SerializedName(value = "attribute_name") + private String attributeName; + @SerializedName(value = "attribute_value") + private String attributeValue; + + public void setAttributeName(String attributeName) { + this.attributeName = attributeName; + } + + public String getAttributeName() { + return attributeName; + } + + public void setAttributeValue(String attributeValue) { + this.attributeValue = attributeValue; + } + + public String getAttributeValue() { + return attributeValue; + } + +} + diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Content.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Content.java new file mode 100644 index 00000000..264ba8a6 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Content.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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 java.util.List; +import java.util.ArrayList; + +/** + * The content acts the high level abstraction which to be used by OOF to do Optimization. + * + * @author Libo Zhu (libo.zhu@intel.com) + */ +class Content { + private String resources; + private String identity; + private List<String> policyScope = new ArrayList<>(); + private String policyType = "Optimization"; + private List<FlavorFeature> flavorFeatures = new ArrayList<>(); + + public void setResources(String resources) { + this.resources = resources; + } + + public String getResources() { + return resources; + } + + public void setIdentity(String identity) { + this.identity = identity; + } + + public String getIdentity() { + return identity; + } + + public List<String> getPolicyScope() { + return policyScope; + } + + public void setPolicyType(String policyType) { + this.policyType = policyType; + } + + public String getPolicyType() { + return policyType; + } + + public List<FlavorFeature> getFlavorFeatures() { + return flavorFeatures; + } + +} + diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Directive.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Directive.java new file mode 100644 index 00000000..c04fc91d --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Directive.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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 java.util.List; +import java.util.ArrayList; + + +/** + * The attribute acts an abstraction to indicate OOF which supports two different Models + * (Heat and TOSCA), two areas are wrapped: in the VNFC level to indicate the flavor, + * in the hpa_feature level to contains specified information. + * + * @author Libo Zhu (libo.zhu@intel.com) + */ +class Directive{ + private String type; + private List<Attribute> attributes = new ArrayList<>(); + + public void setType(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public List<Attribute> getAttributes() { + return attributes; + } + +} + 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 new file mode 100644 index 00000000..3fba96d4 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java @@ -0,0 +1,263 @@ +/*- + * ============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 java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.HashMap; + +import java.io.FileWriter; +import java.io.Writer; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.annotations.SerializedName; + +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl; +import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; +import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; + +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.CapabilityAssignment; +import org.onap.sdc.toscaparser.api.CapabilityAssignments; +import org.onap.sdc.toscaparser.api.elements.Metadata; + +/** + * Extract concerned info from NodeTemplate, currently ONLY HPA Feature. + * + * @author Libo Zhu (libo.zhu@intel.com) + */ +public class ExtractFromNode { + + private static final Logger LOGGER = FlexLogger.getLogger(ExtractFromNode.class); + private static final String CONTENT_RESOURCES = "name"; + private static final String VDU_TYPE = "tosca.nodes.nfv.Vdu.Compute"; + private static final String VDU_CP_TYPE = "tosca.nodes.nfv.VduCp"; + private static final String VIRTUAL_MEM_SIZE_PATH = "virtual_memory#virtual_mem_size"; + 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 NUMBER_OF_PAGES_PATH = "virtual_memory#vdu_memory_requirements#numberOfPages"; + private static final String BASIC_CAPABILITIES = "BasicCapabilities"; + + + ISdcCsarHelper sdcCsarHelper; + final Gson gson = new GsonBuilder() + .serializeNulls() + .setPrettyPrinting() + .disableHtmlEscaping() + .create(); + + + public void setSdcCsarHelper(ISdcCsarHelper sdcCsarHelper) { + this.sdcCsarHelper = sdcCsarHelper; + } + + /* + * 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 PdpxPolicy extractInfo(NodeTemplate node) throws PolicyDecodingException { + PdpxPolicy pdpxPolicy = new PdpxPolicy(); + Content content = pdpxPolicy.getContent(); + + String outputFile = sdcCsarHelper.getNodeTemplateMetadata(node).getValue("name"); + outputFile += ".json"; + LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(node)); + LOGGER.debug("outputFile = " + outputFile); + + List<NodeTemplate> lnodeChild = sdcCsarHelper.getNodeTemplateChildren(node); + LOGGER.debug("the size of lnodeChild = " + lnodeChild.size()); + + //Store all the VDUs under one VNF + List<NodeTemplate> lnodeVdu = new ArrayList<>(); + //Store all the Cps under one VNF + List<NodeTemplate> lnodeVduCp = new ArrayList<>(); + for ( NodeTemplate nodeChild : lnodeChild) { + String type = sdcCsarHelper.getTypeOfNodeTemplate(nodeChild); + LOGGER.debug("the type of this nodeChild = " + type); + LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(nodeChild)); + if ( type.equalsIgnoreCase(VDU_TYPE)) { + lnodeVdu.add(nodeChild); + } else if ( type.equalsIgnoreCase(VDU_CP_TYPE)) { + lnodeVduCp.add(nodeChild); + } + } + + LOGGER.debug("the size of vdu is =" + lnodeVdu.size()); + LOGGER.debug("the size of cp is =" + lnodeVduCp.size()); + + extractInfoVdu(lnodeVdu, content); + extractInfoVduCp(lnodeVduCp, content); + + if (content.getFlavorFeatures().isEmpty() ){ + return null; + } + + try (Writer writer = new FileWriter(outputFile)) { + gson.toJson(pdpxPolicy, writer); + } catch (Exception e) { + LOGGER.error("can't write generated policies to file " , e); + throw new PolicyDecodingException ("Exception caught when writing generated policies to file ", e); + } + return pdpxPolicy; + } + + + /* + * 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, Content content) { + //each VDU <=> FlavorFeature + for ( NodeTemplate node : lnodeVdu) { + String id = sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, "name"); + FlavorFeature flavorFeature = new FlavorFeature(); + flavorFeature.setId(id); + Attribute flavorAttribute = new Attribute(); + flavorAttribute.setAttributeName("flavorName"); + flavorAttribute.setAttributeValue(""); + Directive flavorDirective = new Directive(); + flavorDirective.setType("flavor_directive"); + flavorDirective.getAttributes().add(flavorAttribute); + flavorFeature.getDirectives().add(flavorDirective); + + CapabilityAssignments capabilityAssignments = sdcCsarHelper.getCapabilitiesOf(node); + CapabilityAssignment capabilityAssignment = capabilityAssignments.getCapabilityByName("virtual_compute"); + if (capabilityAssignment != null) { + generateBasicCapability(capabilityAssignment, flavorFeature); + generateHugePages(capabilityAssignment, flavorFeature); + } + + content.getFlavorFeatures().add(flavorFeature); + } + } + + /* + * 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); + if (virtualMemSize != null) { + LOGGER.debug("the virtualMemSize = " + virtualMemSize); + HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("virtualMemSize", virtualMemSize); + FlavorProperty flavorProperty = new FlavorProperty(); + flavorProperty.setHpaFeature(BASIC_CAPABILITIES); + flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute); + flavorFeature.getFlavorProperties().add(flavorProperty); + } + + //the format is int like 2 + 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(); + flavorProperty.setHpaFeature(BASIC_CAPABILITIES); + if (cpuArchitecture != null) { + flavorProperty.setArchitecture(cpuArchitecture); + } + flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute); + flavorFeature.getFlavorProperties().add(flavorProperty); + } + } + + /* + * 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 + * @return the format used in underlayer component + */ + private HpaFeatureAttribute generateHpaFeatureAttribute(final String hpaAttributeKey, final String featureValue){ + + HpaFeatureAttribute hpaFeatureAttribute = new HpaFeatureAttribute(); + hpaFeatureAttribute.setHpaAttributeKey(hpaAttributeKey); + String tmp = featureValue.replace(" ",""); + String pattern = "(\\D*)(\\d+)(\\D*)"; + Pattern r = Pattern.compile(pattern); + Matcher m = r.matcher(tmp); + if (m.find()) { + LOGGER.debug("operator = " + m.group(1)); + LOGGER.debug("value = " + m.group(2)); + LOGGER.debug("unit = " + m.group(3)); + hpaFeatureAttribute.setOperator(m.group(1)); + hpaFeatureAttribute.setHpaAttributeValue(m.group(2)); + hpaFeatureAttribute.setUnit(m.group(3)); + } + return hpaFeatureAttribute; + } + + /* + * GenerateHugePages, 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 generateHugePages(final CapabilityAssignment capabilityAssignment, FlavorFeature flavorFeature){ + //the format is a map like: {"schema-version": "0", "schema-location": "", "platform-id": "generic", + // "mandatory": true, "configuration-value": "2 MB"} + String numberOfPages = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, + NUMBER_OF_PAGES_PATH); + if (numberOfPages != null) { + LOGGER.debug("the virtualMemSize = " + numberOfPages); + //TODO add HugePages support + } + } + + /* + * ExtractInfoVduCp, supposted hpa features, under the virtual_network_interface_requirements of + * tosca.nodes.nfv.VduCp. + * + * @param lnodeVduCp the list of VduCp node + * @param content to be change based on lnodeVduCp + */ + @SuppressWarnings("unchecked") + public void extractInfoVduCp(final List<NodeTemplate> lnodeVduCp, Content content) { + for ( NodeTemplate node : lnodeVduCp) { + //TODO to add VDU cp Hpa feature extract + } + } + +} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorFeature.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorFeature.java new file mode 100644 index 00000000..551fbdee --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorFeature.java @@ -0,0 +1,67 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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 java.util.List; +import java.util.ArrayList; + +/** + * The FlavorFeature includes all the specified flavor infos used in multicloud, it represents one VDU of TOSCA. + * + * @author Libo Zhu (libo.zhu@intel.com) + */ +class FlavorFeature { + private String id ; + private String type = "tosca.node.nfv.Vdu.Compute"; + private List<Directive> directives = new ArrayList<>(); + private List<FlavorProperty> flavorProperties = new ArrayList<>(); + + public void setId(String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public void setType(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public List<FlavorProperty> getFlavorProperties() { + return flavorProperties; + } + + public List<Directive> getDirectives() { + return directives; + } + + @Override + public String toString() { + return "{ id = " + id + ", type = " + type + ", \n" + "directivies:["+directives + ",\n" + + flavorProperties + "}"; + } +} + diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorProperty.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorProperty.java new file mode 100644 index 00000000..552164c5 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorProperty.java @@ -0,0 +1,91 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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 java.util.List; +import java.util.ArrayList; + +import com.google.gson.annotations.SerializedName; + +/** + * The FlavorProperty includes all the properties of Flavor. + * + * @author Libo Zhu (libo.zhu@intel.com) + */ +class FlavorProperty{ + @SerializedName(value = "hpa-feature") + private String hpaFeature; + private String mandatory = "true"; + private String architecture = "generic"; + @SerializedName(value = "hpa-version") + private String hpaVersion = "v1"; + private List<Directive> directives = new ArrayList<>(); + @SerializedName(value = "hpa-feature-attributes") + private List<HpaFeatureAttribute> hpaFeatureAttributes = new ArrayList<>(); + + public void setHpaFeature(String hpaFeature) { + this.hpaFeature = hpaFeature; + } + + public String getHpaFeature() { + return hpaFeature; + } + + public void setMandatory(String mandatory) { + this.mandatory = mandatory; + } + + public String getMandatory() { + return mandatory; + } + + public void setArchitecture(String architecture) { + this.architecture = architecture; + } + + public String getArchitecture() { + return architecture; + } + + public void setHpaVersion(String hpaVersion) { + this.hpaVersion = hpaVersion; + } + + public String getHpaVersion() { + return hpaVersion; + } + + public List<Directive> getDirectives() { + return directives; + } + + public List<HpaFeatureAttribute> getHpaFeatureAttributes() { + return hpaFeatureAttributes; + } + + @Override + public String toString() { + return "{ hpaFeature:" + hpaFeature + ", mandatory = " + mandatory + ",architecture:" + architecture + + ", hpaFeatureAttributes : [" + hpaFeatureAttributes + "]"; + } + +} + diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/HpaFeatureAttribute.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/HpaFeatureAttribute.java new file mode 100644 index 00000000..7574ee87 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/HpaFeatureAttribute.java @@ -0,0 +1,74 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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 com.google.gson.annotations.SerializedName; + +/** + * The HpaFeatureAttribute represents a way to provide match expression. + * + * @author Libo Zhu (libo.zhu@intel.com) + */ +class HpaFeatureAttribute{ + @SerializedName(value = "hpa-attribute-key") + private String hpaAttributeKey; + @SerializedName(value = "hap-attribute-value") + private String hpaAttributeValue; + private String operator; + private String unit; + + public HpaFeatureAttribute(){} + + public void setHpaAttributeKey(String hpaAttributeKey) { + this.hpaAttributeKey = hpaAttributeKey; + } + + public String getHpaAttributeKey() { + return hpaAttributeKey; + } + + public void setHpaAttributeValue(String hpaAttributeValue) { + this.hpaAttributeValue = hpaAttributeValue; + } + + public String getHpaAttributeValue() { + return hpaAttributeValue; + } + + public void setOperator(String operator) { + this.operator = operator; + } + + public String getOperator() { + return operator; + } + + public void setUnit(String unit) { + this.unit = unit; + } + + public String getUnit() { + return unit; + } +} + + + diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PdpxPolicy.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PdpxPolicy.java index 135d2564..179024a8 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PdpxPolicy.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PdpxPolicy.java @@ -28,14 +28,101 @@ import org.onap.policy.distribution.reception.decoding.PolicyDecoder; */ public class PdpxPolicy implements Policy { + private String service; + private String policyName; + private String description; + private String templateVersion; + private String version; + private String priority; + private String riskType; + private String riskLevel; + private String guard; + private Content content = new Content(); + @Override public String getPolicyName() { - return null; + return policyName; } @Override public String getPolicyType() { - return null; + return content.getPolicyType(); + } + + public void setService(String service) { + this.service = service; + } + + public String getService() { + return service; + } + + public void setPolicyName(String policyName) { + this.policyName = policyName; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getDescription() { + return description; } + public void setTemplateVersion(String templateVersion) { + this.templateVersion = templateVersion; + } + + public String getTemplateVersion() { + return templateVersion; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getPriority() { + return priority; + } + + public void setPriority(String priority) { + this.priority = priority; + } + + public String getGuard() { + return guard; + } + + public void setGuard(String guard) { + this.guard = guard; + } + + public String getRiskLevel() { + return riskLevel; + } + + public void setRiskLevel(String riskLevel) { + this.riskLevel = riskLevel; + } + + public String getRiskType() { + return riskType; + } + + public void setRiskType(String riskType) { + this.riskType = riskType; + } + + public Content getContent(){ + return content; + } + + public void setContent(Content content) { + this.content = content; + } + } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java new file mode 100644 index 00000000..0cd7bc16 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java @@ -0,0 +1,102 @@ +/*- + * ============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 java.io.File; +import java.util.ArrayList; +import java.util.List; + +import java.util.Collection; +import java.util.Collections; + + +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl; +import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; + +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.elements.Metadata; + +/** + * Decodes PDP-X policies from a TOSCA file. + */ +public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> { + + private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderCsarPdpx.class); + + @Override + public Collection<PdpxPolicy> decode(Csar csar) throws PolicyDecodingException { + // logic for generating the policies from the CSAR. + List<PdpxPolicy> lPdpxPolicy = new ArrayList<>(); + ISdcCsarHelper sdcCsarHelper = parseCsar(csar); + List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList(); + LOGGER.debug("the size of Vf = " + lnodeVf.size()); + ExtractFromNode extractFromNode = new ExtractFromNode(); + extractFromNode.setSdcCsarHelper(sdcCsarHelper); + for ( NodeTemplate node : lnodeVf) { + PdpxPolicy ret = extractFromNode.extractInfo(node); + if (ret != null) { + lPdpxPolicy.add(ret); + } + } + return lPdpxPolicy; + } + + @Override + public boolean canHandle(PolicyInput policyInput) { + return policyInput.getClass().isAssignableFrom(Csar.class); + } + + /** + * Parse the input Csar by SDC tosca tool. + * + * @param csar represents the service TOSCA Csar + * @return the object to represents the content of input csar + * @throws PolicyDecodingException if parse fails + */ + public ISdcCsarHelper parseCsar(Csar csar) throws PolicyDecodingException { + ISdcCsarHelper sdcCsarHelper ; + try { + + SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();//Autoclosable + + LOGGER.debug("tosca File Path = " + csar.getCsarPath()); + + File spoolFile = new File(csar.getCsarPath()); + + sdcCsarHelper = factory.getSdcCsarHelper(spoolFile.getAbsolutePath()); + + } catch (Exception e) { + LOGGER.error("Exception got in parseTosca",e); + throw new PolicyDecodingException ("Exception caught when passing the csar file to the parser ", e); + } + return sdcCsarHelper; + } + +} |