e88f7eb930920b57350114f0051fdb53c1bb6cfd
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Intel Crop. All rights reserved.
4  *  Modifications Copyright (C) 2020 AT&T Inc.
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.forwarding.file;
23
24 import java.io.BufferedWriter;
25 import java.io.IOException;
26 import java.nio.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.nio.file.InvalidPathException;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.Collection;
32
33 import org.onap.policy.common.parameters.ParameterService;
34 import org.onap.policy.distribution.forwarding.PolicyForwarder;
35 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to a
43  * file directory.
44  */
45 public class FilePolicyForwarder implements PolicyForwarder {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(FilePolicyForwarder.class);
48     private FilePolicyForwarderParameterGroup fileForwarderParameters;
49
50     /**
51      * {@inheritDoc}.
52      */
53     @Override
54     public void configure(final String parameterGroupName) {
55         fileForwarderParameters = ParameterService.get(parameterGroupName);
56         try {
57             final Path path = Paths.get(fileForwarderParameters.getPath());
58             if (!path.toFile().exists()) {
59                 Files.createDirectories(path);
60             }
61         } catch (final InvalidPathException | IOException e) {
62             LOGGER.error("Configuring FilePolicyForwarder failed!", e);
63         }
64     }
65
66     /**
67      * {@inheritDoc}.
68      */
69     @Override
70     public void forward(final Collection<ToscaEntity> policies) throws PolicyForwardingException {
71         for (final ToscaEntity policy : policies) {
72             if (policy instanceof ToscaPolicy) {
73                 forwardPolicy((ToscaPolicy) policy);
74             } else {
75                 final String message = "Cannot forward policy " + policy + ". Unsupported policy type "
76                         + policy.getClass().getSimpleName();
77                 LOGGER.error(message);
78                 throw new PolicyForwardingException(message);
79             }
80         }
81     }
82
83     /**
84      * Method to forward a given policy to be logged into a file.
85      *
86      * @param pol the policy
87      * @throws PolicyForwardingException if any exception occurs while forwarding policy
88      */
89     private void forwardPolicy(final ToscaPolicy pol) throws PolicyForwardingException {
90         final String name = pol.getName();
91         final Path path = Paths.get(fileForwarderParameters.getPath(), name);
92         try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
93             writer.write("policyName: " + name);
94             if (fileForwarderParameters.isVerbose()) {
95                 writer.newLine();
96                 writer.write("policy: " + pol.toString());
97             }
98             LOGGER.debug("Sucessfully forwarded the policy to store into file {}.", path);
99         } catch (final InvalidPathException | IOException exp) {
100             final String message = "Error sending policy to file under path:" + fileForwarderParameters.getPath();
101             throw new PolicyForwardingException(message, exp);
102         }
103     }
104 }
105