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