643785fa453b703a66bb2a64d08a21208e2f1156
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2020 AT&T Inc.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.reception.decoding.policy.file;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Enumeration;
29 import java.util.zip.ZipEntry;
30 import java.util.zip.ZipFile;
31
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.distribution.model.Csar;
36 import org.onap.policy.distribution.model.PolicyInput;
37 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
38 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
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     private StandardCoder coder;
51
52     /**
53      * {@inheritDoc}.
54      */
55     @Override
56     public void configure(final String parameterGroupName) {
57         decoderParameters = ParameterService.get(parameterGroupName);
58         coder = new StandardCoder();
59     }
60
61     /**
62      * {@inheritDoc}.
63      */
64     @Override
65     public boolean canHandle(final PolicyInput policyInput) {
66         return policyInput.getClass().isAssignableFrom(Csar.class);
67     }
68
69     /**
70      * {@inheritDoc}.
71      */
72     @Override
73     public Collection<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException {
74         final Collection<ToscaEntity> policyList = new ArrayList<>();
75
76         try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) {
77             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
78             while (entries.hasMoreElements()) {
79                 final ZipEntry entry = entries.nextElement();
80                 if (entry.getName().contains(decoderParameters.getPolicyTypeFileName())
81                         || entry.getName().contains(decoderParameters.getPolicyFileName())) {
82                     final ToscaServiceTemplate policy =
83                             coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
84                     policyList.add(policy);
85                 }
86             }
87         } catch (final IOException | CoderException exp) {
88             throw new PolicyDecodingException("Failed decoding the policy", exp);
89         }
90
91         return policyList;
92     }
93 }