2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2022 Nordix Foundation.
 
   4  *  Modifications Copyright (C) 2022 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.util.ArrayList;
 
  26 import java.util.Collection;
 
  27 import java.util.Enumeration;
 
  28 import java.util.zip.ZipEntry;
 
  29 import java.util.zip.ZipFile;
 
  30 import org.onap.policy.common.parameters.ParameterService;
 
  31 import org.onap.policy.common.utils.coder.CoderException;
 
  32 import org.onap.policy.distribution.model.Csar;
 
  33 import org.onap.policy.distribution.model.PolicyInput;
 
  34 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
 
  35 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
 
  36 import org.onap.policy.distribution.reception.util.ReceptionUtil;
 
  37 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  41  * This class extracts acm information from a CSAR file.
 
  43  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
 
  45 public class AutomationCompositionDecoderFileInCsar implements PolicyDecoder<Csar, ToscaEntity> {
 
  47     private AutomationCompositionDecoderFileInCsarParameterGroup decoderParameters;
 
  48     private static final String NODE_TYPES = "nodes.yml";
 
  49     private static final String DATA_TYPES = "data.yml";
 
  55     public void configure(final String parameterGroupName) {
 
  56         decoderParameters = ParameterService.get(parameterGroupName);
 
  63     public boolean canHandle(final PolicyInput policyInput) {
 
  64         return policyInput.getClass().isAssignableFrom(Csar.class);
 
  71     public Collection<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException {
 
  72         final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
 
  73         ToscaServiceTemplate nodeTypes = null;
 
  74         ToscaServiceTemplate dataTypes = null;
 
  76         try (var zipFile = new ZipFile(csar.getCsarFilePath())) {
 
  77             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
 
  78             while (entries.hasMoreElements()) {
 
  80                 // Sonar will flag this as a Security Hotspot
 
  81                 // "Expanding archive files is security-sensitive"
 
  82                 // isZipEntryValid ensures the file being read exists in the archive
 
  84                 final ZipEntry entry = entries.nextElement(); // NOSONAR
 
  85                 final String entryName = entry.getName();
 
  88                 if (entryName.contains(NODE_TYPES)) {
 
  89                     nodeTypes = ReceptionUtil.decodeFile(zipFile, entry);
 
  93                 if (entryName.contains(DATA_TYPES)) {
 
  94                     dataTypes = ReceptionUtil.decodeFile(zipFile, entry);
 
  97                 if (entryName.contains(decoderParameters.getAutomationCompositionType())) {
 
  98                     ReceptionUtil.validateZipEntry(entryName, csar.getCsarFilePath(), entry.getSize());
 
  99                     final ToscaServiceTemplate automationComposition = ReceptionUtil.decodeFile(zipFile, entry);
 
 100                     if (null != automationComposition.getToscaTopologyTemplate()) {
 
 101                         if (null != nodeTypes) {
 
 102                             automationComposition.setNodeTypes(nodeTypes.getNodeTypes());
 
 104                         if (null != dataTypes) {
 
 105                             automationComposition.setDataTypes(dataTypes.getDataTypes());
 
 107                         automationCompositionList.add(automationComposition);
 
 111         } catch (final IOException | CoderException exp) {
 
 112             throw new PolicyDecodingException("Failed decoding the acm", exp);
 
 115         return automationCompositionList;