eadca94d0f609404a08aac854d4331b9a8ace07d
[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.pdpx;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.onap.policy.common.parameters.ParameterService;
29 import org.onap.policy.common.utils.coder.StandardCoder;
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.ToscaServiceTemplate;
35 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
36 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
37 import org.onap.sdc.toscaparser.api.NodeTemplate;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Decodes PDP-X policies from a CSAR file and creates ToscaServiceTemplate for Policy Lifecycle API's.
43  */
44 public class PolicyDecoderCsarPdpxLifecycleApi implements PolicyDecoder<Csar, ToscaServiceTemplate> {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderCsarPdpxLifecycleApi.class);
47     private final StandardCoder coder = new StandardCoder();
48     private PolicyDecoderCsarPdpxLifecycleApiParameters decoderParameters;
49
50     @Override
51     public Collection<ToscaServiceTemplate> decode(final Csar csar) throws PolicyDecodingException {
52         final List<ToscaServiceTemplate> entities = new ArrayList<>();
53         final ISdcCsarHelper sdcCsarHelper = parseCsar(csar);
54         final List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList();
55         LOGGER.debug("the size of Vf = {}", lnodeVf.size());
56         final ExtractFromNode extractFromNode = new ExtractFromNode();
57         extractFromNode.setSdcCsarHelper(sdcCsarHelper);
58         final String serviceName = sdcCsarHelper.getServiceMetadata().getValue("name");
59         LOGGER.debug("the name of the service = {}", serviceName);
60         for (final NodeTemplate node : lnodeVf) {
61             final Content content = extractFromNode.extractInfo(node);
62             if (content != null) {
63                 final ToscaServiceTemplate entity = new ToscaServiceTemplate();
64                 // TODO: add the logic for creating ToscaServiceTemplate for HPA policy
65                 entities.add(entity);
66             }
67         }
68         return entities;
69     }
70
71     @Override
72     public boolean canHandle(final PolicyInput policyInput) {
73         return Csar.class.isAssignableFrom(policyInput.getClass());
74     }
75
76     /**
77      * Parse the input Csar using SDC TOSCA parser.
78      *
79      * @param csar represents the service TOSCA Csar
80      * @return the object to represents the content of input csar
81      * @throws PolicyDecodingException if parse fails
82      */
83     public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException {
84         ISdcCsarHelper sdcCsarHelper;
85         try {
86             final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
87             LOGGER.debug("Csar File Path = {}", csar.getCsarPath());
88             final File csarFile = new File(csar.getCsarPath());
89             sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
90         } catch (final Exception exp) {
91             final String message = "Failed passing the csar file";
92             LOGGER.error(message, exp);
93             throw new PolicyDecodingException(message, exp);
94         }
95         return sdcCsarHelper;
96     }
97
98     @Override
99     public void configure(final String parameterGroupName) {
100         decoderParameters = ParameterService.get(parameterGroupName);
101     }
102 }