80c5172f2ca5376cf388f3ad2f5f6389201ac6e3
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
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.io.StringWriter;
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.apache.commons.io.IOUtils;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.onap.policy.distribution.model.Csar;
36 import org.onap.policy.distribution.model.PolicyAsString;
37 import org.onap.policy.distribution.model.PolicyInput;
38 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
39 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
40
41 /**
42  * This class extracts policy files from a CSAR file.
43  *
44  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
45  */
46 public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, PolicyAsString> {
47
48     private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderFileInCsarToPolicy.class);
49     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<PolicyAsString> decode(final Csar csar) throws PolicyDecodingException {
72         final Collection<PolicyAsString> policyList = new ArrayList<>();
73         ZipFile zipFile = null;
74         try {
75             zipFile = new ZipFile(csar.getCsarPath());
76             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
77             while (entries.hasMoreElements()) {
78                 final ZipEntry entry = entries.nextElement();
79                 if (entry.getName().contains(decoderParameters.getPolicyFileName())) {
80                     final PolicyAsString poilcy = createPolicy(zipFile, entry);
81                     policyList.add(poilcy);
82                 }
83             }
84         } catch (final IOException exp) {
85             final String message = "Failed decoding the policy";
86             LOGGER.error(message, exp);
87             throw new PolicyDecodingException(message, exp);
88         } finally {
89             if (zipFile != null) {
90                 try {
91                     zipFile.close();
92                 } catch (final IOException exp) {
93                     LOGGER.error("Failed closing the zipFile", exp);
94                 }
95             }
96         }
97         return policyList;
98     }
99
100     /**
101      * Creates the policy from given input.
102      *
103      * @param zipFile the csar file
104      * @param entry an entry in the csar file
105      * @return the created policy
106      * @throws IOException if policy creation fails
107      */
108     private PolicyAsString createPolicy(final ZipFile zipFile, final ZipEntry entry) throws IOException {
109         final StringWriter writer = new StringWriter();
110         IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8");
111         final PolicyAsString poilcy = new PolicyAsString(decoderParameters.getPolicyFileName(),
112                 decoderParameters.getPolicyType(), writer.toString());
113         return poilcy;
114     }
115 }