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
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.List;
27 import java.util.Collection;
28 import java.util.Collections;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
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;
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;
43 import org.onap.sdc.toscaparser.api.NodeTemplate;
44 import org.onap.sdc.toscaparser.api.elements.Metadata;
47 * Decodes PDP-X policies from a TOSCA file.
49 public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> {
51 private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderCsarPdpx.class);
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);
72 public boolean canHandle(PolicyInput policyInput) {
73 return policyInput.getClass().isAssignableFrom(Csar.class);
77 * Parse the input Csar by SDC tosca tool.
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
83 public ISdcCsarHelper parseCsar(Csar csar) throws PolicyDecodingException {
84 ISdcCsarHelper sdcCsarHelper ;
87 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();//Autoclosable
89 LOGGER.debug("tosca File Path = " + csar.getCsarPath());
91 File spoolFile = new File(csar.getCsarPath());
93 sdcCsarHelper = factory.getSdcCsarHelper(spoolFile.getAbsolutePath());
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);