2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-2022 Nordix Foundation.
 
   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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.distribution.reception.decoding.policy.file;
 
  23 import java.io.IOException;
 
  24 import java.util.ArrayList;
 
  25 import java.util.Collection;
 
  26 import java.util.List;
 
  27 import java.util.Optional;
 
  28 import java.util.stream.Collectors;
 
  29 import java.util.zip.ZipEntry;
 
  30 import java.util.zip.ZipFile;
 
  31 import org.onap.policy.common.parameters.ParameterService;
 
  32 import org.onap.policy.common.utils.coder.CoderException;
 
  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.distribution.reception.util.ReceptionUtil;
 
  38 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  42  * This class extracts acm information from a CSAR file.
 
  44  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
 
  46 public class AutomationCompositionDecoderFileInCsar implements PolicyDecoder<Csar, ToscaEntity> {
 
  48     private AutomationCompositionDecoderFileInCsarParameterGroup decoderParameters;
 
  49     private static final String NODE_TYPES = "nodes.yml";
 
  50     private static final String DATA_TYPES = "data.yml";
 
  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> automationCompositionList = 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.getAutomationCompositionType()))
 
  78                 .collect(Collectors.toList());
 
  80             for (ZipEntry entry : entries) {
 
  81                 ReceptionUtil.validateZipEntry(entry.getName(), csar.getCsarFilePath(), entry.getSize());
 
  82                 final ToscaServiceTemplate automationComposition = ReceptionUtil.decodeFile(zipFile, entry);
 
  84                 if (null != automationComposition.getToscaTopologyTemplate()) {
 
  85                     validateTypes(zipFile, NODE_TYPES)
 
  86                         .ifPresent(node -> automationComposition.setNodeTypes(node.getNodeTypes()));
 
  88                     validateTypes(zipFile, DATA_TYPES)
 
  89                         .ifPresent(data -> automationComposition.setDataTypes(data.getDataTypes()));
 
  91                     automationCompositionList.add(automationComposition);
 
  94         } catch (final IOException | CoderException exp) {
 
  95             throw new PolicyDecodingException("Failed decoding the acm", exp);
 
  98         return automationCompositionList;
 
 102      * Decode and validate if node or data type is available withing ACM csar file.
 
 104      * @param zipFile full csar file
 
 105      * @return tosca template with parsed node/data type
 
 106      * @throws CoderException if file can't be parsed
 
 108     private Optional<ToscaServiceTemplate> validateTypes(final ZipFile zipFile, String type)
 
 109         throws CoderException {
 
 112             ToscaServiceTemplate template = null;
 
 113             final Optional<? extends ZipEntry> file = zipFile.stream()
 
 114                 .filter(entry -> entry.getName().contains(type)).findFirst();
 
 116             if (file.isPresent()) {
 
 117                 template = ReceptionUtil.decodeFile(zipFile, file.get());
 
 119             return Optional.ofNullable(template);
 
 120         } catch (final IOException | CoderException exp) {
 
 121             throw new CoderException("Couldn't decode " + type + " type", exp);