4b58d2dadd2a73c9635b5b5158b5b18481fa3e57
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 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
31 import org.onap.policy.common.parameters.ParameterService;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
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.models.tosca.authorative.concepts.ToscaEntity;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * This class extracts policy files from a CSAR file.
45  *
46  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
47  */
48 public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, ToscaEntity> {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderFileInCsarToPolicy.class);
51     private PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters;
52     private StandardCoder coder;
53
54     /**
55      * {@inheritDoc}.
56      */
57     @Override
58     public void configure(final String parameterGroupName) {
59         decoderParameters = ParameterService.get(parameterGroupName);
60         coder = new StandardCoder();
61     }
62
63     /**
64      * {@inheritDoc}.
65      */
66     @Override
67     public boolean canHandle(final PolicyInput policyInput) {
68         return policyInput.getClass().isAssignableFrom(Csar.class);
69     }
70
71     /**
72      * {@inheritDoc}.
73      */
74     @Override
75     public Collection<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException {
76         final Collection<ToscaEntity> policyList = new ArrayList<>();
77
78         try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) {
79             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
80             while (entries.hasMoreElements()) {
81                 final ZipEntry entry = entries.nextElement();
82                 if (entry.getName().contains(decoderParameters.getPolicyTypeFileName())
83                         || entry.getName().contains(decoderParameters.getPolicyFileName())) {
84                     final ToscaServiceTemplate policy =
85                             coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
86                     policyList.add(policy);
87                 }
88             }
89         } catch (final IOException | CoderException exp) {
90             final String message = "Failed decoding the policy";
91             LOGGER.error(message, exp);
92             throw new PolicyDecodingException(message, exp);
93         }
94
95         return policyList;
96     }
97 }