diff options
Diffstat (limited to 'plugins/reception-plugins/src/main/java/org/onap')
6 files changed, 361 insertions, 50 deletions
diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ConfigBody.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ConfigBody.java new file mode 100644 index 00000000..72d9bd2b --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ConfigBody.java @@ -0,0 +1,97 @@ +/*- + * ============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; + +public class ConfigBody { + + private String service; + private String description; + private String version; + private String priority; + private String riskType; + private String riskLevel; + private String guard; + private Content content; + + public String getService() { + return service; + } + + public void setService(String service) { + this.service = service; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + 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 getRiskType() { + return riskType; + } + + public void setRiskType(String riskType) { + this.riskType = riskType; + } + + public String getRiskLevel() { + return riskLevel; + } + + public void setRiskLevel(String riskLevel) { + this.riskLevel = riskLevel; + } + + public String getGuard() { + return guard; + } + + public void setGuard(String guard) { + this.guard = guard; + } + + 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/ExtractFromNode.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java index 1d0f621b..7c839a28 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,12 @@ package org.onap.policy.distribution.reception.decoding.pdpx; -import java.io.FileWriter; -import java.io.Writer; import java.util.ArrayList; 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; @@ -76,14 +73,16 @@ 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 PdpxPolicy extractInfo(final NodeTemplate node) throws PolicyDecodingException { + public Content extractInfo(final NodeTemplate node) throws PolicyDecodingException { LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(node)); final List<NodeTemplate> lnodeChild = sdcCsarHelper.getNodeTemplateChildren(node); @@ -106,31 +105,23 @@ public class ExtractFromNode { LOGGER.debug("the size of vdu is =" + lnodeVdu.size()); LOGGER.debug("the size of cp is =" + lnodeVduCp.size()); - final PdpxPolicy pdpxPolicy = new PdpxPolicy(); - final Content content = pdpxPolicy.getContent(); + final Content content = new Content(); extractInfoVdu(lnodeVdu, content); extractInfoVduCp(lnodeVduCp, content); if (content.getFlavorFeatures().isEmpty()) { return null; } - String outputFile = sdcCsarHelper.getNodeTemplateMetadata(node).getValue("name"); - outputFile += ".json"; - LOGGER.debug("outputFile = " + outputFile); - try (Writer writer = new FileWriter(outputFile)) { - gson.toJson(pdpxPolicy, writer); - } catch (final Exception exp) { - final String message = "Failed writing generated policies to file"; - LOGGER.error(message, exp); - throw new PolicyDecodingException(message, exp); - } - return pdpxPolicy; + + 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) { @@ -158,9 +149,11 @@ public class ExtractFromNode { } /** - * 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){ @@ -195,11 +188,14 @@ 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 @@ -221,11 +217,13 @@ 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){ @@ -255,6 +253,7 @@ 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 */ 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 index 03f0c041..030c470b 100644 --- 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 @@ -20,14 +20,17 @@ package org.onap.policy.distribution.reception.decoding.pdpx; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.List; - import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.model.OptimizationPolicy; import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; @@ -38,25 +41,40 @@ import org.onap.sdc.toscaparser.api.NodeTemplate; /** * Decodes PDP-X policies from a CSAR file. */ -public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> { +public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, OptimizationPolicy> { private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderCsarPdpx.class); + private final Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().create(); + private PolicyDecoderCsarPdpxConfigurationParameterGroup decoderParameters; @Override - public Collection<PdpxPolicy> decode(final Csar csar) throws PolicyDecodingException { - final List<PdpxPolicy> lPdpxPolicy = new ArrayList<>(); + public Collection<OptimizationPolicy> decode(final Csar csar) throws PolicyDecodingException { + final List<OptimizationPolicy> policys = new ArrayList<>(); final ISdcCsarHelper sdcCsarHelper = parseCsar(csar); final List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList(); LOGGER.debug("the size of Vf = " + lnodeVf.size()); final ExtractFromNode extractFromNode = new ExtractFromNode(); extractFromNode.setSdcCsarHelper(sdcCsarHelper); for (final NodeTemplate node : lnodeVf) { - final PdpxPolicy ret = extractFromNode.extractInfo(node); - if (ret != null) { - lPdpxPolicy.add(ret); + final Content content = extractFromNode.extractInfo(node); + if (content != null) { + final OptimizationPolicy policy = new OptimizationPolicy(); + policy.setOnapName(decoderParameters.getOnapName()); + policy.setPolicyName(decoderParameters.getPolicyNamePrefix() + "." + content.getIdentity()); + ConfigBody configBody = new ConfigBody(); + configBody.setService("hpaPolicy"); + configBody.setDescription("OOF Policy"); + configBody.setVersion(decoderParameters.getVersion()); + configBody.setPriority(decoderParameters.getPriority()); + configBody.setRiskLevel(decoderParameters.getRiskLevel()); + configBody.setRiskType(decoderParameters.getRiskType()); + configBody.setGuard("false"); + configBody.setContent(content); + policy.setConfigBody(gson.toJson(configBody)); + policys.add(policy); } } - return lPdpxPolicy; + return policys; } @Override @@ -88,6 +106,6 @@ public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> { @Override public void configure(final String parameterGroupName) { - throw new UnsupportedOperationException("The method is not supprted"); + decoderParameters = ParameterService.get(parameterGroupName); } } 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 new file mode 100644 index 00000000..199d582b --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxConfigurationParameterBuilder.java @@ -0,0 +1,95 @@ +/*- + * ============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; + +/** + * This class builds an instance of {@link PolicyDecodeCsarPdpxConfigurationParameterGroup} class. + */ +public class PolicyDecoderCsarPdpxConfigurationParameterBuilder { + + private String policyNamePrefix; + private String onapName; + private String version; + private String priority; + private String riskType; + private String riskLevel; + + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setPolicyNamePrefix(final String policyNamePrefix) { + this.policyNamePrefix = policyNamePrefix; + return this; + } + + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setOnapName(final String onapName) { + this.onapName = onapName; + return this; + } + + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setVersion(final String version) { + this.version = version; + return this; + } + + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setPriority(final String priority) { + this.priority = priority; + return this; + } + + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setRiskType(final String riskType) { + this.riskType = riskType; + return this; + } + + public PolicyDecoderCsarPdpxConfigurationParameterBuilder setRiskLevel(final String riskLevel) { + this.riskLevel = riskLevel; + return this; + } + + 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/pdpx/PolicyDecoderCsarPdpxConfigurationParameterGroup.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxConfigurationParameterGroup.java new file mode 100644 index 00000000..d785b827 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxConfigurationParameterGroup.java @@ -0,0 +1,112 @@ +/*- + * ============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 org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.distribution.reception.handling.sdc.SdcReceptionHandlerConfigurationParameterGroup; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; + +/** + * This class handles reading, parsing and validating of the paramaters for the + * {@link PolicyDecoderCsarPdpx} + */ +public class PolicyDecoderCsarPdpxConfigurationParameterGroup extends PolicyDecoderConfigurationParameterGroup { + + private String policyNamePrefix; + private String onapName; + private String version; + private String priority; + private String riskType; + private String riskLevel; + + /** + * The constructor for instantiating {@link SdcReceptionHandlerConfigurationParameterGroup} + * class. + * + * @param builder the SDC configuration builder + */ + public PolicyDecoderCsarPdpxConfigurationParameterGroup( + final PolicyDecoderCsarPdpxConfigurationParameterBuilder builder) { + policyNamePrefix = builder.getPolicyNamePrefix(); + onapName = builder.getOnapName(); + version = builder.getVersion(); + priority = builder.getPriority(); + riskType = builder.getRiskType(); + riskLevel = builder.getRiskLevel(); + } + + 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; + } + + /** + * {@inheritDoc} + */ + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + validateStringElement(validationResult, policyNamePrefix, "policyNamePrefix"); + validateStringElement(validationResult, onapName, "onapName"); + validateStringElement(validationResult, version, "version"); + validateStringElement(validationResult, priority, "priority"); + validateStringElement(validationResult, riskType, "riskType"); + validateStringElement(validationResult, riskLevel, "riskLevel"); + return validationResult; + } + + /** + * Validate the string element. + * + * @param validationResult the result object + * @param element the element to validate + * @param elementName the element name for error message + */ + private void validateStringElement(final GroupValidationResult validationResult, final String element, + final String elementName) { + if (!ParameterValidationUtils.validateStringParameter(element)) { + validationResult.setResult(elementName, ValidationStatus.INVALID, + elementName + " must be a non-blank string"); + } + } +} + diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterGroup.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterGroup.java index bbee652a..0140fb53 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterGroup.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterGroup.java @@ -21,16 +21,15 @@ package org.onap.policy.distribution.reception.handling.sdc; import java.util.List; -import java.util.UUID; - import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.common.utils.validation.ParameterValidationUtils; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup; /** - * This class handles reading, parsing and validating of the Policy SDC Service Distribution parameters from Json - * format, which strictly adheres to the interface:IConfiguration, defined by SDC SDK. + * This class handles reading, parsing and validating of the Policy SDC Service Distribution + * parameters from Json format, which strictly adheres to the interface:IConfiguration, defined by + * SDC SDK. */ public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHandlerConfigurationParameterGroup { @@ -51,7 +50,8 @@ public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHan private boolean isUseHttpsWithDmaap; /** - * The constructor for instantiating {@link SdcReceptionHandlerConfigurationParameterGroup} class. + * The constructor for instantiating {@link SdcReceptionHandlerConfigurationParameterGroup} + * class. * * @param builder the SDC configuration builder */ @@ -136,16 +136,6 @@ public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHan } /** - * Set the name of this group. - * - * @param name the name to set. - */ - @Override - public void setName(final String name) { - super.setName(name + "_" + UUID.randomUUID().toString()); - } - - /** * {@inheritDoc} */ @Override |