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.policy.file;
 
  24 import java.io.IOException;
 
  25 import java.io.StringWriter;
 
  26 import java.util.ArrayList;
 
  27 import java.util.Collection;
 
  28 import java.util.Enumeration;
 
  29 import java.util.zip.ZipEntry;
 
  30 import java.util.zip.ZipFile;
 
  32 import org.apache.commons.io.IOUtils;
 
  33 import org.onap.policy.common.parameters.ParameterService;
 
  34 import org.onap.policy.distribution.model.Csar;
 
  35 import org.onap.policy.distribution.model.PolicyAsString;
 
  36 import org.onap.policy.distribution.model.PolicyInput;
 
  37 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
 
  38 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
 
  39 import org.slf4j.Logger;
 
  40 import org.slf4j.LoggerFactory;
 
  43  * This class extracts policy files from a CSAR file.
 
  45  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  47 public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, PolicyAsString> {
 
  49     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderFileInCsarToPolicy.class);
 
  50     PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters;
 
  56     public void configure(final String parameterGroupName) {
 
  57         decoderParameters = ParameterService.get(parameterGroupName);
 
  64     public boolean canHandle(final PolicyInput policyInput) {
 
  65         return policyInput.getClass().isAssignableFrom(Csar.class);
 
  72     public Collection<PolicyAsString> decode(final Csar csar) throws PolicyDecodingException {
 
  73         final Collection<PolicyAsString> policyList = new ArrayList<>();
 
  75         try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) {
 
  76             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
 
  77             while (entries.hasMoreElements()) {
 
  78                 final ZipEntry entry = entries.nextElement();
 
  79                 if (entry.getName().contains(decoderParameters.getPolicyFileName())) {
 
  80                     final StringWriter writer = new StringWriter();
 
  81                     IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8");
 
  82                     final PolicyAsString policy = new PolicyAsString(decoderParameters.getPolicyFileName(),
 
  83                             decoderParameters.getPolicyType(), writer.toString());
 
  84                     policyList.add(policy);
 
  87         } catch (final IOException exp) {
 
  88             final String message = "Failed decoding the policy";
 
  89             LOGGER.error(message, exp);
 
  90             throw new PolicyDecodingException(message, exp);