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;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
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;
42 * Decodes PDP-X policies from a CSAR file.
44 public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, OptimizationPolicy> {
46 private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderCsarPdpx.class);
47 private final Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().create();
48 private PolicyDecoderCsarPdpxConfigurationParameterGroup decoderParameters;
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 configBody.setContent(content);
73 policy.setConfigBody(gson.toJson(configBody));
81 public boolean canHandle(final PolicyInput policyInput) {
82 return policyInput.getClass().isAssignableFrom(Csar.class);
86 * Parse the input Csar using SDC TOSCA parser.
88 * @param csar represents the service TOSCA Csar
89 * @return the object to represents the content of input csar
90 * @throws PolicyDecodingException if parse fails
92 public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException {
93 ISdcCsarHelper sdcCsarHelper;
95 final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
96 LOGGER.debug("Csar File Path = " + csar.getCsarPath());
97 final File csarFile = new File(csar.getCsarPath());
98 sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
99 } catch (final Exception exp) {
100 final String message = "Failed passing the csar file";
101 LOGGER.error(message, exp);
102 throw new PolicyDecodingException(message, exp);
104 return sdcCsarHelper;
108 public void configure(final String parameterGroupName) {
109 decoderParameters = ParameterService.get(parameterGroupName);