7bfcb0f9be4380b18ba2f910c79eaa4437827b20
[policy/distribution.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.reception.decoding.policy.file;
23
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;
39
40 /**
41  * This class extracts acm information from a CSAR file.
42  *
43  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
44  */
45 public class AutomationCompositionDecoderFileInCsar implements PolicyDecoder<Csar, ToscaEntity> {
46
47     private AutomationCompositionDecoderFileInCsarParameterGroup decoderParameters;
48     private static final String NODE_TYPES = "nodes.yml";
49     private static final String DATA_TYPES = "data.yml";
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> automationCompositionList = new ArrayList<>();
73         ToscaServiceTemplate nodeTypes = null;
74         ToscaServiceTemplate dataTypes = null;
75
76         try (var zipFile = new ZipFile(csar.getCsarFilePath())) {
77             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
78             while (entries.hasMoreElements()) {
79                 //
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
83                 //
84                 final ZipEntry entry = entries.nextElement(); // NOSONAR
85                 final String entryName = entry.getName();
86
87                 // Store node_types
88                 if (entryName.contains(NODE_TYPES)) {
89                     nodeTypes = ReceptionUtil.decodeFile(zipFile, entry);
90                 }
91
92                 // Store data_types
93                 if (entryName.contains(DATA_TYPES)) {
94                     dataTypes = ReceptionUtil.decodeFile(zipFile, entry);
95                 }
96
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());
103                         }
104                         if (null != dataTypes) {
105                             automationComposition.setDataTypes(dataTypes.getDataTypes());
106                         }
107                         automationCompositionList.add(automationComposition);
108                     }
109                 }
110             }
111         } catch (final IOException | CoderException exp) {
112             throw new PolicyDecodingException("Failed decoding the acm", exp);
113         }
114
115         return automationCompositionList;
116     }
117 }