2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  *  Modifications Copyright (C) 2019, 2021-2022 Nordix Foundation.
 
   5  *  Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
 
   6  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  20  * SPDX-License-Identifier: Apache-2.0
 
  21  * ============LICENSE_END=========================================================
 
  24 package org.onap.policy.distribution.reception.decoding.policy.file;
 
  26 import java.io.IOException;
 
  27 import java.util.ArrayList;
 
  28 import java.util.Collection;
 
  29 import java.util.List;
 
  30 import java.util.stream.Collectors;
 
  31 import java.util.zip.ZipEntry;
 
  32 import java.util.zip.ZipFile;
 
  33 import org.onap.policy.common.parameters.ParameterService;
 
  34 import org.onap.policy.common.utils.coder.CoderException;
 
  35 import org.onap.policy.distribution.model.Csar;
 
  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.onap.policy.distribution.reception.util.ReceptionUtil;
 
  40 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  44  * This class extracts policy files from a CSAR file.
 
  46  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  48 public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, ToscaEntity> {
 
  50     private 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<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException {
 
  73         final Collection<ToscaEntity> policyList = new ArrayList<>();
 
  75         try (var zipFile = new ZipFile(csar.getCsarFilePath())) {
 
  76             final List<? extends ZipEntry> entries = zipFile.stream()
 
  77                 .filter(entry -> entry.getName().contains(decoderParameters.getPolicyTypeFileName())
 
  78                     || entry.getName().contains(decoderParameters.getPolicyFileName())).collect(Collectors.toList());
 
  80             for (ZipEntry entry : entries) {
 
  81                 ReceptionUtil.validateZipEntry(entry.getName(), csar.getCsarFilePath(), entry.getSize());
 
  82                 final ToscaServiceTemplate policy = ReceptionUtil.decodeFile(zipFile, entry);
 
  83                 policyList.add(policy);
 
  85         } catch (final IOException | CoderException exp) {
 
  86             throw new PolicyDecodingException("Failed decoding the policy", exp);