517000052dda3d9709a764585edaf4e059bbe14b
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Intel Crop. 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.forwarding.file;
22
23 import java.io.BufferedWriter;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.InvalidPathException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Collection;
31
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.onap.policy.distribution.forwarding.PolicyForwarder;
34 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
35 import org.onap.policy.distribution.model.OptimizationPolicy;
36 import org.onap.policy.distribution.model.Policy;
37
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
43  * a 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             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<Policy> policies) throws PolicyForwardingException {
71         for (Policy policy : policies) {
72             if (policy instanceof OptimizationPolicy) {
73                 forwardPolicy((OptimizationPolicy) policy);
74             } else {
75                 final String message = "Cannot forward policy " + policy
76                      + ". Unsupported policy type " + 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 OptimizationPolicy pol) throws PolicyForwardingException {
90         final String name = pol.getPolicyName();
91         Path path = Paths.get(fileForwarderParameters.getPath(), name);
92         try (BufferedWriter writer = new BufferedWriter(new FileWriter(path.toString()))) {
93             writer.write("policyName: " + name);
94             if (fileForwarderParameters.isVerbose()) {
95                 writer.newLine();
96                 writer.write("policyType: " + pol.getPolicyType());
97                 writer.newLine();
98                 writer.write("policyDescription: " + pol.getPolicyDescription());
99                 writer.newLine();
100                 writer.write("onapName: " + pol.getOnapName());
101                 writer.newLine();
102                 writer.write("configBodyType: " + pol.getConfigBodyType());
103                 writer.newLine();
104                 writer.write("configBody: " + pol.getConfigBody());
105                 writer.newLine();
106                 writer.write("timetolive: " + pol.getTimetolive().toString());
107                 writer.newLine();
108                 writer.write("guard: " + pol.getGuard());
109                 writer.newLine();
110                 writer.write("riskLevel: " + pol.getRiskLevel());
111                 writer.newLine();
112                 writer.write("riskType: " + pol.getRiskType());
113             }
114             LOGGER.debug("Sucessfully forwarded the policy to store into file {}.", path);
115         } catch (final InvalidPathException | IOException exp) {
116             final String message = "Error sending policy to file under path:" + fileForwarderParameters.getPath();
117             LOGGER.error(message, exp);
118             throw new PolicyForwardingException(message, exp);
119         }
120     }
121 }
122