c4ba21feabf9a83e01e2a926b8cdec1a21c371e5
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.distribution.reception.decoding.policy.file;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Enumeration;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipFile;
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.distribution.model.Csar;
35 import org.onap.policy.distribution.model.PolicyInput;
36 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
37 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
38 import org.onap.policy.distribution.reception.util.ReceptionUtil;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41
42 /**
43  * This class extracts policy files from a CSAR file.
44  *
45  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
46  */
47 public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, ToscaEntity> {
48
49     private PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters;
50
51     /**
52      * {@inheritDoc}.
53      */
54     @Override
55     public void configure(final String parameterGroupName) {
56         decoderParameters = ParameterService.get(parameterGroupName);
57     }
58
59     /**
60      * {@inheritDoc}.
61      */
62     @Override
63     public boolean canHandle(final PolicyInput policyInput) {
64         return policyInput.getClass().isAssignableFrom(Csar.class);
65     }
66
67     /**
68      * {@inheritDoc}.
69      */
70     @Override
71     public Collection<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException {
72         final Collection<ToscaEntity> policyList = new ArrayList<>();
73
74         try (var zipFile = new ZipFile(csar.getCsarFilePath())) {
75             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
76             while (entries.hasMoreElements()) {
77                 //
78                 // Sonar will flag this as a Security Hotspot
79                 // "Expanding archive files is security-sensitive"
80                 // isZipEntryValid ensures the file being read exists in the archive
81                 //
82                 final ZipEntry entry = entries.nextElement(); // NOSONAR
83                 final String entryName = entry.getName();
84
85                 //
86                 // We only care about policy types and policies
87                 //
88                 if (entryName.contains(decoderParameters.getPolicyTypeFileName())
89                         || entryName.contains(decoderParameters.getPolicyFileName())) {
90                     ReceptionUtil.validateZipEntry(entryName, csar.getCsarFilePath(), entry.getSize());
91                     final ToscaServiceTemplate policy = ReceptionUtil.decodeFile(zipFile, entry);
92                     policyList.add(policy);
93                 }
94             }
95         } catch (final IOException | CoderException exp) {
96             throw new PolicyDecodingException("Failed decoding the policy", exp);
97         }
98
99         return policyList;
100     }
101 }