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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.decoding.policy.file;
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;
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;
42 * This class extracts policy files from a CSAR file.
44 * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
46 public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, PolicyAsString> {
48 private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderFileInCsarToPolicy.class);
49 PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters;
55 public void configure(final String parameterGroupName) {
56 decoderParameters = ParameterService.get(parameterGroupName);
63 public boolean canHandle(final PolicyInput policyInput) {
64 return policyInput.getClass().isAssignableFrom(Csar.class);
71 public Collection<PolicyAsString> decode(final Csar csar) throws PolicyDecodingException {
72 final Collection<PolicyAsString> policyList = new ArrayList<>();
73 ZipFile zipFile = null;
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);
84 } catch (final IOException exp) {
85 final String message = "Failed decoding the policy";
86 LOGGER.error(message, exp);
87 throw new PolicyDecodingException(message, exp);
89 if (zipFile != null) {
92 } catch (final IOException exp) {
93 LOGGER.error("Failed closing the zipFile", exp);
101 * Creates the policy from given input.
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
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());