From a36022cc750ef71c35ac85d3864d907f81404ca6 Mon Sep 17 00:00:00 2001 From: ramverma Date: Tue, 1 Oct 2019 09:51:25 +0100 Subject: Create decoder/parameters infra for HPA policy Creating a new policy decoder & its related parameter classes for decoding the HPA policy as ToscaServiceTemplate which is accepted by the new Lifecycle API of Policy Framework. Added couple of changes per review comments. Issue-ID: POLICY-2122 Change-Id: I2d19b04cd24dd9c5b9fbf959757272077e6413d4 Signed-off-by: ramverma Signed-off-by: Jim Hahn --- .../pdpx/PolicyDecoderCsarPdpxLifecycleApi.java | 102 +++++++++++++++++++++ ...olicyDecoderCsarPdpxLifecycleApiParameters.java | 49 ++++++++++ .../decoding/pdpx/PolicyDecoderToscaPdpx.java | 52 ----------- .../handling/sdc/SdcReceptionHandler.java | 4 +- 4 files changed, 153 insertions(+), 54 deletions(-) create mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxLifecycleApi.java create mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxLifecycleApiParameters.java delete mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java (limited to 'plugins/reception-plugins/src/main/java/org') diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxLifecycleApi.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxLifecycleApi.java new file mode 100644 index 00000000..eadca94d --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxLifecycleApi.java @@ -0,0 +1,102 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.Collection; +import java.util.List; + +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Decodes PDP-X policies from a CSAR file and creates ToscaServiceTemplate for Policy Lifecycle API's. + */ +public class PolicyDecoderCsarPdpxLifecycleApi implements PolicyDecoder { + + private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderCsarPdpxLifecycleApi.class); + private final StandardCoder coder = new StandardCoder(); + private PolicyDecoderCsarPdpxLifecycleApiParameters decoderParameters; + + @Override + public Collection decode(final Csar csar) throws PolicyDecodingException { + final List entities = new ArrayList<>(); + final ISdcCsarHelper sdcCsarHelper = parseCsar(csar); + final List lnodeVf = sdcCsarHelper.getServiceVfList(); + LOGGER.debug("the size of Vf = {}", lnodeVf.size()); + final ExtractFromNode extractFromNode = new ExtractFromNode(); + extractFromNode.setSdcCsarHelper(sdcCsarHelper); + final String serviceName = sdcCsarHelper.getServiceMetadata().getValue("name"); + LOGGER.debug("the name of the service = {}", serviceName); + for (final NodeTemplate node : lnodeVf) { + final Content content = extractFromNode.extractInfo(node); + if (content != null) { + final ToscaServiceTemplate entity = new ToscaServiceTemplate(); + // TODO: add the logic for creating ToscaServiceTemplate for HPA policy + entities.add(entity); + } + } + return entities; + } + + @Override + public boolean canHandle(final PolicyInput policyInput) { + return Csar.class.isAssignableFrom(policyInput.getClass()); + } + + /** + * Parse the input Csar using SDC TOSCA parser. + * + * @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(final Csar csar) throws PolicyDecodingException { + ISdcCsarHelper sdcCsarHelper; + try { + final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); + LOGGER.debug("Csar File Path = {}", csar.getCsarPath()); + final File csarFile = new File(csar.getCsarPath()); + sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath()); + } catch (final Exception exp) { + final String message = "Failed passing the csar file"; + LOGGER.error(message, exp); + throw new PolicyDecodingException(message, exp); + } + return sdcCsarHelper; + } + + @Override + public void configure(final String parameterGroupName) { + decoderParameters = ParameterService.get(parameterGroupName); + } +} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxLifecycleApiParameters.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxLifecycleApiParameters.java new file mode 100644 index 00000000..a66a53a8 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpxLifecycleApiParameters.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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 lombok.Getter; + +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; + +/** + * This class handles the parameters needed for {@link PolicyDecoderCsarPdpxLifecycleApi}. + */ +@Getter +@NotNull +@NotBlank +public class PolicyDecoderCsarPdpxLifecycleApiParameters extends PolicyDecoderConfigurationParameterGroup { + + private String policyNamePrefix; + private String onapName; + private String version; + private String priority; + private String riskType; + private String riskLevel; + + public PolicyDecoderCsarPdpxLifecycleApiParameters() { + super(PolicyDecoderCsarPdpxLifecycleApiParameters.class.getSimpleName()); + } +} + diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java deleted file mode 100644 index 8cd6ecc1..00000000 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============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.Collection; -import java.util.Collections; - -import org.onap.policy.distribution.model.PolicyInput; -import org.onap.policy.distribution.model.Tosca; -import org.onap.policy.distribution.reception.decoding.PolicyDecoder; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; - -/** - * Decodes PDP-X policies from a TOSCA file. - */ -public class PolicyDecoderToscaPdpx implements PolicyDecoder { - - @Override - public Collection decode(final Tosca tosca) { - // Add logic for generating the policies from the TOSCA - return Collections.emptySet(); - } - - @Override - public boolean canHandle(final PolicyInput policyInput) { - return policyInput.getClass().isAssignableFrom(Tosca.class); - } - - @Override - public void configure(final String parameterGroupName) { - throw new UnsupportedOperationException("The method is not supprted"); - } - -} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java index c3d7e43b..9c336327 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java @@ -42,7 +42,7 @@ import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.INotificationData; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.api.results.IDistributionClientResult; -import org.onap.sdc.impl.DistributionClientFactory; +import org.onap.sdc.impl.DistributionClientImpl; import org.onap.sdc.utils.DistributionActionResultEnum; import org.onap.sdc.utils.DistributionStatusEnum; import org.slf4j.Logger; @@ -122,7 +122,7 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo * @return the {@link IDistributionClient} instance */ protected IDistributionClient createSdcDistributionClient() { - return DistributionClientFactory.createDistributionClient(); + return new DistributionClientImpl(); } /** -- cgit 1.2.3-korg