2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  *  Copyright (C) 2019 Nordix Foundation.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.distribution.reception.decoding.pdpx;
 
  24 import com.google.gson.Gson;
 
  25 import com.google.gson.GsonBuilder;
 
  28 import java.util.ArrayList;
 
  29 import java.util.Collection;
 
  30 import java.util.List;
 
  32 import org.onap.policy.common.parameters.ParameterService;
 
  33 import org.onap.policy.distribution.model.Csar;
 
  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.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  38 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
 
  39 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
 
  40 import org.onap.sdc.toscaparser.api.NodeTemplate;
 
  41 import org.slf4j.Logger;
 
  42 import org.slf4j.LoggerFactory;
 
  45  * Decodes PDP-X policies from a CSAR file.
 
  47 public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, ToscaServiceTemplate> {
 
  49     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderCsarPdpx.class);
 
  50     private final Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().create();
 
  51     private PolicyDecoderCsarPdpxConfigurationParameterGroup decoderParameters;
 
  54     public Collection<ToscaServiceTemplate> decode(final Csar csar) throws PolicyDecodingException {
 
  55         final List<ToscaServiceTemplate> policies = new ArrayList<>();
 
  56         final ISdcCsarHelper sdcCsarHelper = parseCsar(csar);
 
  57         final List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList();
 
  58         LOGGER.debug("the size of Vf = {}", lnodeVf.size());
 
  59         final ExtractFromNode extractFromNode = new ExtractFromNode();
 
  60         extractFromNode.setSdcCsarHelper(sdcCsarHelper);
 
  61         final String serviceName = sdcCsarHelper.getServiceMetadata().getValue("name");
 
  62         LOGGER.debug("the name of the service = {}", serviceName);
 
  63         for (final NodeTemplate node : lnodeVf) {
 
  64             final Content content = extractFromNode.extractInfo(node);
 
  65             if (content != null) {
 
  66                 final ToscaServiceTemplate policy = new ToscaServiceTemplate();
 
  67                 final String policyName = decoderParameters.getPolicyNamePrefix() + "." + content.getIdentity();
 
  68                 // policy.setOnapName(decoderParameters.getOnapName());
 
  69                 policy.setName(policyName);
 
  70                 final ConfigBody configBody = new ConfigBody();
 
  71                 configBody.setService("hpaPolicy");
 
  72                 configBody.setPolicyName(policyName);
 
  73                 configBody.setDescription("OOF Policy");
 
  74                 configBody.setTemplateVersion("OpenSource.version.1");
 
  75                 configBody.setVersion(decoderParameters.getVersion());
 
  76                 configBody.setPriority(decoderParameters.getPriority());
 
  77                 configBody.setRiskLevel(decoderParameters.getRiskLevel());
 
  78                 configBody.setRiskType(decoderParameters.getRiskType());
 
  79                 configBody.setGuard("False");
 
  80                 content.setPolicyType("hpa");
 
  81                 content.getPolicyScope().add("HPA");
 
  82                 content.getPolicyScope().add(serviceName);
 
  83                 configBody.setContent(content);
 
  84                 // policy.setConfigBody(gson.toJson(configBody));
 
  92     public boolean canHandle(final PolicyInput policyInput) {
 
  93         return policyInput.getClass().isAssignableFrom(Csar.class);
 
  97      * Parse the input Csar using SDC TOSCA parser.
 
  99      * @param csar represents the service TOSCA Csar
 
 100      * @return the object to represents the content of input csar
 
 101      * @throws PolicyDecodingException if parse fails
 
 103     public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException {
 
 104         ISdcCsarHelper sdcCsarHelper;
 
 106             final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
 
 107             LOGGER.debug("Csar File Path = {}", csar.getCsarPath());
 
 108             final File csarFile = new File(csar.getCsarPath());
 
 109             sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
 
 110         } catch (final Exception exp) {
 
 111             final String message = "Failed passing the csar file";
 
 112             LOGGER.error(message, exp);
 
 113             throw new PolicyDecodingException(message, exp);
 
 115         return sdcCsarHelper;
 
 119     public void configure(final String parameterGroupName) {
 
 120         decoderParameters = ParameterService.get(parameterGroupName);