0cd7bc16fa483b92acb3c1ec3e806dbba4289756
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
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.List;
26
27 import java.util.Collection;
28 import java.util.Collections;
29
30
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33
34 import org.onap.policy.distribution.model.PolicyInput;
35 import org.onap.policy.distribution.model.Csar;
36 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
37 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
38
39 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
40 import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl;
41 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
42
43 import org.onap.sdc.toscaparser.api.NodeTemplate;
44 import org.onap.sdc.toscaparser.api.elements.Metadata;
45
46 /**
47  * Decodes PDP-X policies from a TOSCA file.
48  */
49 public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> {
50
51     private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderCsarPdpx.class);
52
53     @Override
54     public Collection<PdpxPolicy> decode(Csar csar) throws PolicyDecodingException {
55         // logic for generating the policies from the CSAR. 
56         List<PdpxPolicy> lPdpxPolicy = new ArrayList<>();
57         ISdcCsarHelper sdcCsarHelper = parseCsar(csar);
58         List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList();
59         LOGGER.debug("the size of Vf = " + lnodeVf.size());
60         ExtractFromNode extractFromNode = new ExtractFromNode();
61         extractFromNode.setSdcCsarHelper(sdcCsarHelper);
62         for ( NodeTemplate node : lnodeVf) {
63             PdpxPolicy ret = extractFromNode.extractInfo(node);
64             if (ret != null) {
65                 lPdpxPolicy.add(ret);
66             }
67         }
68         return lPdpxPolicy;
69     }
70
71     @Override
72     public boolean canHandle(PolicyInput policyInput) {
73         return policyInput.getClass().isAssignableFrom(Csar.class);
74     }
75
76     /** 
77      * Parse the input Csar by SDC tosca tool.
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(Csar csar)  throws PolicyDecodingException {
84         ISdcCsarHelper sdcCsarHelper ;
85         try {
86
87             SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();//Autoclosable
88
89             LOGGER.debug("tosca File Path = " + csar.getCsarPath());
90
91             File spoolFile = new File(csar.getCsarPath());
92
93             sdcCsarHelper = factory.getSdcCsarHelper(spoolFile.getAbsolutePath());
94
95         } catch (Exception e) {
96             LOGGER.error("Exception got in parseTosca",e);
97             throw new PolicyDecodingException ("Exception caught when passing the csar file to the parser ", e);
98         }
99         return sdcCsarHelper;
100     }
101
102 }