fb7a8d99bcbe5f4d16e2adece0c78a17039d1428
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.decoding.policy.file;
22
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;
40
41 /**
42  * This class extracts acm information from a CSAR file.
43  *
44  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
45  */
46 public class AutomationCompositionDecoderFileInCsar implements PolicyDecoder<Csar, ToscaEntity> {
47
48     private AutomationCompositionDecoderFileInCsarParameterGroup decoderParameters;
49     private static final String NODE_TYPES = "nodes.yml";
50     private static final String DATA_TYPES = "data.yml";
51
52     /**
53      * {@inheritDoc}.
54      */
55     @Override
56     public void configure(final String parameterGroupName) {
57         decoderParameters = ParameterService.get(parameterGroupName);
58     }
59
60     /**
61      * {@inheritDoc}.
62      */
63     @Override
64     public boolean canHandle(final PolicyInput policyInput) {
65         return policyInput.getClass().isAssignableFrom(Csar.class);
66     }
67
68     /**
69      * {@inheritDoc}.
70      */
71     @Override
72     public Collection<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException {
73         final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
74
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());
79
80             for (ZipEntry entry : entries) {
81                 ReceptionUtil.validateZipEntry(entry.getName(), csar.getCsarFilePath(), entry.getSize());
82                 final ToscaServiceTemplate automationComposition = ReceptionUtil.decodeFile(zipFile, entry);
83
84                 if (null != automationComposition.getToscaTopologyTemplate()) {
85                     validateTypes(zipFile, NODE_TYPES)
86                         .ifPresent(node -> automationComposition.setNodeTypes(node.getNodeTypes()));
87
88                     validateTypes(zipFile, DATA_TYPES)
89                         .ifPresent(data -> automationComposition.setDataTypes(data.getDataTypes()));
90
91                     automationCompositionList.add(automationComposition);
92                 }
93             }
94         } catch (final IOException | CoderException exp) {
95             throw new PolicyDecodingException("Failed decoding the acm", exp);
96         }
97
98         return automationCompositionList;
99     }
100
101     /**
102      * Decode and validate if node or data type is available withing ACM csar file.
103      *
104      * @param zipFile full csar file
105      * @return tosca template with parsed node/data type
106      * @throws CoderException if file can't be parsed
107      */
108     private Optional<ToscaServiceTemplate> validateTypes(final ZipFile zipFile, String type)
109         throws CoderException {
110
111         try {
112             ToscaServiceTemplate template = null;
113             final Optional<? extends ZipEntry> file = zipFile.stream()
114                 .filter(entry -> entry.getName().contains(type)).findFirst();
115
116             if (file.isPresent()) {
117                 template = ReceptionUtil.decodeFile(zipFile, file.get());
118             }
119             return Optional.ofNullable(template);
120         } catch (final IOException | CoderException exp) {
121             throw new CoderException("Couldn't decode " + type + " type", exp);
122         }
123     }
124 }