97ca3f5efb648096fb92eea3ca51d823d8e8f523
[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 com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31 import org.onap.policy.common.parameters.ParameterService;
32 import org.onap.policy.distribution.model.Csar;
33 import org.onap.policy.distribution.model.OptimizationPolicy;
34 import org.onap.policy.distribution.model.PolicyInput;
35 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
36 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
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
41 /**
42  * Decodes PDP-X policies from a CSAR file.
43  */
44 public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, OptimizationPolicy> {
45
46     private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderCsarPdpx.class);
47     private final Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().create();
48     private PolicyDecoderCsarPdpxConfigurationParameterGroup decoderParameters;
49
50     @Override
51     public Collection<OptimizationPolicy> decode(final Csar csar) throws PolicyDecodingException {
52         final List<OptimizationPolicy> policys = 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         for (final NodeTemplate node : lnodeVf) {
59             final Content content = extractFromNode.extractInfo(node);
60             if (content != null) {
61                 final OptimizationPolicy policy = new OptimizationPolicy();
62                 policy.setOnapName(decoderParameters.getOnapName());
63                 policy.setPolicyName(decoderParameters.getPolicyNamePrefix() + "." + content.getIdentity());
64                 ConfigBody configBody = new ConfigBody();
65                 configBody.setService("hpaPolicy");
66                 configBody.setDescription("OOF Policy");
67                 configBody.setVersion(decoderParameters.getVersion());
68                 configBody.setPriority(decoderParameters.getPriority());
69                 configBody.setRiskLevel(decoderParameters.getRiskLevel());
70                 configBody.setRiskType(decoderParameters.getRiskType());
71                 configBody.setGuard("false");
72                 content.getPolicyScope().add("HPA");
73                 configBody.setContent(content);
74                 policy.setConfigBody(gson.toJson(configBody));
75                 policys.add(policy);
76             }
77         }
78         return policys;
79     }
80
81     @Override
82     public boolean canHandle(final PolicyInput policyInput) {
83         return policyInput.getClass().isAssignableFrom(Csar.class);
84     }
85
86     /**
87      * Parse the input Csar using SDC TOSCA parser.
88      *
89      * @param csar represents the service TOSCA Csar
90      * @return the object to represents the content of input csar
91      * @throws PolicyDecodingException if parse fails
92      */
93     public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException {
94         ISdcCsarHelper sdcCsarHelper;
95         try {
96             final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
97             LOGGER.debug("Csar File Path = " + csar.getCsarPath());
98             final File csarFile = new File(csar.getCsarPath());
99             sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
100         } catch (final Exception exp) {
101             final String message = "Failed passing the csar file";
102             LOGGER.error(message, exp);
103             throw new PolicyDecodingException(message, exp);
104         }
105         return sdcCsarHelper;
106     }
107
108     @Override
109     public void configure(final String parameterGroupName) {
110         decoderParameters = ParameterService.get(parameterGroupName);
111     }
112 }