9516ad70426d2b69de5801113686a2f6fa468d52
[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
74         try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) {
75             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
76             while (entries.hasMoreElements()) {
77                 final ZipEntry entry = entries.nextElement();
78                 if (entry.getName().contains(decoderParameters.getPolicyFileName())) {
79                     final StringWriter writer = new StringWriter();
80                     IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8");
81                     final PolicyAsString poilcy = new PolicyAsString(decoderParameters.getPolicyFileName(),
82                             decoderParameters.getPolicyType(), writer.toString());
83                     policyList.add(poilcy);
84                 }
85             }
86         } catch (final IOException exp) {
87             final String message = "Failed decoding the policy";
88             LOGGER.error(message, exp);
89             throw new PolicyDecodingException(message, exp);
90         }
91
92         return policyList;
93     }
94 }