2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.decoding.pdpx;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.LinkedHashMap;
27 import java.util.List;
30 import org.onap.policy.common.parameters.ParameterService;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.distribution.model.Csar;
33 import org.onap.policy.distribution.model.PolicyInput;
34 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
35 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
42 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
43 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
44 import org.onap.sdc.toscaparser.api.NodeTemplate;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Decodes PDP-X policies from a CSAR file and creates ToscaServiceTemplate for Policy Lifecycle API's.
51 public class PolicyDecoderCsarPdpxLifecycleApi implements PolicyDecoder<Csar, ToscaServiceTemplate> {
53 private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderCsarPdpxLifecycleApi.class);
54 private final StandardCoder coder = new StandardCoder();
55 private PolicyDecoderCsarPdpxLifecycleApiParameters decoderParameters;
57 public static final String TOSCA_POLICY_SCOPE = "scope";
58 public static final String TOSCA_POLICY_SERVICES = "services";
59 public static final String TOSCA_POLICY_RESOURCES = "resources";
60 public static final String TOSCA_POLICY_IDENTITY = "identity";
61 public static final String TOSCA_POLICY_FLAVORFEATURES = "flavorfeatures";
63 public static final String TOSCA_POLICY_HPA_OOF = "Optimization";
66 public Collection<ToscaServiceTemplate> decode(final Csar csar) throws PolicyDecodingException {
67 final List<ToscaServiceTemplate> entities = new ArrayList<>();
68 final ISdcCsarHelper sdcCsarHelper = parseCsar(csar);
69 final List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList();
70 LOGGER.debug("the size of Vf = {}", lnodeVf.size());
71 final ExtractFromNode extractFromNode = new ExtractFromNode();
72 extractFromNode.setSdcCsarHelper(sdcCsarHelper);
73 final String serviceName = sdcCsarHelper.getServiceMetadata().getValue("name");
74 LOGGER.debug("the name of the service = {}", serviceName);
75 for (final NodeTemplate node : lnodeVf) {
76 final Content content = extractFromNode.extractInfo(node);
77 if (content != null) {
78 final ToscaServiceTemplate entity = new ToscaServiceTemplate();
79 Map<String, Object> props = new LinkedHashMap<>();
80 props.put(TOSCA_POLICY_SCOPE, content.getScope());
81 props.put(TOSCA_POLICY_SERVICES, content.getServices());
82 props.put(TOSCA_POLICY_RESOURCES, content.getResources());
83 props.put(TOSCA_POLICY_IDENTITY, content.getIdentity());
84 props.put(TOSCA_POLICY_FLAVORFEATURES, content.getFlavorFeatures());
85 ToscaPolicy policy = new ToscaPolicy();
86 policy.setProperties(props);
87 Map<String, ToscaPolicy> map = new LinkedHashMap<>();
88 String type = content.getPolicyType();
89 map.put(type, policy);
90 List<Map<String, ToscaPolicy>> policies = new ArrayList<>();
92 ToscaTopologyTemplate topologyTemplate = new ToscaTopologyTemplate();
93 topologyTemplate.setPolicies(policies);
94 entity.setToscaTopologyTemplate(topologyTemplate);
102 public boolean canHandle(final PolicyInput policyInput) {
103 return Csar.class.isAssignableFrom(policyInput.getClass());
107 * Parse the input Csar using SDC TOSCA parser.
109 * @param csar represents the service TOSCA Csar
110 * @return the object to represents the content of input csar
111 * @throws PolicyDecodingException if parse fails
113 public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException {
114 ISdcCsarHelper sdcCsarHelper;
116 final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
117 LOGGER.debug("Csar File Path = {}", csar.getCsarPath());
118 final File csarFile = new File(csar.getCsarPath());
119 sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
120 } catch (final Exception exp) {
121 final String message = "Failed passing the csar file";
122 LOGGER.error(message, exp);
123 throw new PolicyDecodingException(message, exp);
125 return sdcCsarHelper;
129 public void configure(final String parameterGroupName) {
130 decoderParameters = ParameterService.get(parameterGroupName);