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