e1e1a9a911359140b9e81fdec1eecd74ce4037d3
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.decoding.hpa;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import org.onap.policy.common.parameters.ParameterService;
30 import org.onap.policy.distribution.model.Csar;
31 import org.onap.policy.distribution.model.PolicyInput;
32 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
33 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
37 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
38 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
39 import org.onap.sdc.toscaparser.api.NodeTemplate;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Decodes PDP-X policies from a CSAR file and creates ToscaServiceTemplate for Policy Lifecycle API's.
45  */
46 public class PolicyDecoderCsarHpa implements PolicyDecoder<Csar, ToscaServiceTemplate> {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderCsarHpa.class);
49     private PolicyDecoderCsarHpaParameters decoderParameters;
50
51     public static final String TOSCA_POLICY_SCOPE = "scope";
52     public static final String TOSCA_POLICY_SERVICES = "services";
53     public static final String TOSCA_POLICY_RESOURCES = "resources";
54     public static final String TOSCA_POLICY_IDENTITY = "identity";
55     public static final String TOSCA_POLICY_FLAVORFEATURES = "flavorfeatures";
56
57     public static final String TOSCA_POLICY_HPA_OOF = "Optimization";
58
59     @Override
60     public Collection<ToscaServiceTemplate> decode(final Csar csar) throws PolicyDecodingException {
61         final List<ToscaServiceTemplate> entities = new ArrayList<>();
62         final ISdcCsarHelper sdcCsarHelper = parseCsar(csar);
63         final List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList();
64         LOGGER.debug("the size of Vf = {}", lnodeVf.size());
65         final ExtractFromNode extractFromNode = new ExtractFromNode();
66         extractFromNode.setSdcCsarHelper(sdcCsarHelper);
67         final String serviceName = sdcCsarHelper.getServiceMetadata().getValue("name");
68         LOGGER.debug("the name of the service = {}", serviceName);
69         for (final NodeTemplate node : lnodeVf) {
70             final Content content = extractFromNode.extractInfo(node);
71             if (content != null) {
72                 final ToscaServiceTemplate entity = new ToscaServiceTemplate();
73                 Map<String, Object> props = new LinkedHashMap<>();
74                 props.put(TOSCA_POLICY_SCOPE, content.getScope());
75                 props.put(TOSCA_POLICY_SERVICES, content.getServices());
76                 props.put(TOSCA_POLICY_RESOURCES, content.getResources());
77                 props.put(TOSCA_POLICY_IDENTITY, content.getIdentity());
78                 props.put(TOSCA_POLICY_FLAVORFEATURES, content.getFlavorFeatures());
79                 ToscaPolicy policy = new ToscaPolicy();
80                 policy.setProperties(props);
81                 Map<String, ToscaPolicy> map = new LinkedHashMap<>();
82                 String type = content.getPolicyType();
83                 map.put(type, policy);
84                 List<Map<String, ToscaPolicy>> policies = new ArrayList<>();
85                 policies.add(map);
86                 ToscaTopologyTemplate topologyTemplate = new ToscaTopologyTemplate();
87                 topologyTemplate.setPolicies(policies);
88                 entity.setToscaTopologyTemplate(topologyTemplate);
89                 entities.add(entity);
90             }
91         }
92         return entities;
93     }
94
95     @Override
96     public boolean canHandle(final PolicyInput policyInput) {
97         return Csar.class.isAssignableFrom(policyInput.getClass());
98     }
99
100     /**
101      * Parse the input Csar using SDC TOSCA parser.
102      *
103      * @param csar represents the service TOSCA Csar
104      * @return the object to represents the content of input csar
105      * @throws PolicyDecodingException if parse fails
106      */
107     public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException {
108         ISdcCsarHelper sdcCsarHelper;
109         try {
110             final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
111             LOGGER.debug("Csar File Path = {}", csar.getCsarPath());
112             final File csarFile = new File(csar.getCsarPath());
113             sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
114         } catch (final Exception exp) {
115             throw new PolicyDecodingException( "Failed passing the csar file", exp);
116         }
117         return sdcCsarHelper;
118     }
119
120     @Override
121     public void configure(final String parameterGroupName) {
122         decoderParameters = ParameterService.get(parameterGroupName);
123     }
124 }