7a73c83183dd4d5ce70c8e1a7218baa07a769caf
[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         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 OptimizationPolicy policy = new OptimizationPolicy();
64                 final String policyName = decoderParameters.getPolicyNamePrefix() + "." + content.getIdentity();
65                 policy.setOnapName(decoderParameters.getOnapName());
66                 policy.setPolicyName(policyName);
67                 ConfigBody configBody = new ConfigBody();
68                 configBody.setService("hpaPolicy");
69                 configBody.setPolicyName(policyName);
70                 configBody.setDescription("OOF Policy");
71                 configBody.setTemplateVersion("OpenSource.version.1");
72                 configBody.setVersion(decoderParameters.getVersion());
73                 configBody.setPriority(decoderParameters.getPriority());
74                 configBody.setRiskLevel(decoderParameters.getRiskLevel());
75                 configBody.setRiskType(decoderParameters.getRiskType());
76                 configBody.setGuard("False");
77                 content.setPolicyType("hpa");
78                 content.getPolicyScope().add("HPA");
79                 content.getPolicyScope().add(serviceName);
80                 configBody.setContent(content);
81                 policy.setConfigBody(gson.toJson(configBody));
82                 policys.add(policy);
83             }
84         }
85         return policys;
86     }
87
88     @Override
89     public boolean canHandle(final PolicyInput policyInput) {
90         return policyInput.getClass().isAssignableFrom(Csar.class);
91     }
92
93     /**
94      * Parse the input Csar using SDC TOSCA parser.
95      *
96      * @param csar represents the service TOSCA Csar
97      * @return the object to represents the content of input csar
98      * @throws PolicyDecodingException if parse fails
99      */
100     public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException {
101         ISdcCsarHelper sdcCsarHelper;
102         try {
103             final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
104             LOGGER.debug("Csar File Path = " + csar.getCsarPath());
105             final File csarFile = new File(csar.getCsarPath());
106             sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
107         } catch (final Exception exp) {
108             final String message = "Failed passing the csar file";
109             LOGGER.error(message, exp);
110             throw new PolicyDecodingException(message, exp);
111         }
112         return sdcCsarHelper;
113     }
114
115     @Override
116     public void configure(final String parameterGroupName) {
117         decoderParameters = ParameterService.get(parameterGroupName);
118     }
119 }