4115bff348b3b0beb6aa2836b10a0cb3aa85c98b
[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 (!Files.exists(path)) {
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 = new String("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         try {
92             Path path = Paths.get(fileForwarderParameters.getPath(), name);
93             BufferedWriter writer = new BufferedWriter(new FileWriter(path.toString()));
94             writer.write("policyName: " + name);
95             if (fileForwarderParameters.isVerbose()) {
96                 writer.newLine();
97                 writer.write("policyType: " + pol.getPolicyType());
98                 writer.newLine();
99                 writer.write("policyDescription: " + pol.getPolicyDescription());
100                 writer.newLine();
101                 writer.write("onapName: " + pol.getOnapName());
102                 writer.newLine();
103                 writer.write("configBodyType: " + pol.getConfigBodyType());
104                 writer.newLine();
105                 writer.write("configBody: " + pol.getConfigBody());
106                 writer.newLine();
107                 writer.write("timetolive: " + pol.getTimetolive().toString());
108                 writer.newLine();
109                 writer.write("guard: " + pol.getGuard());
110                 writer.newLine();
111                 writer.write("riskLevel: " + pol.getRiskLevel());
112                 writer.newLine();
113                 writer.write("riskType: " + pol.getRiskType());
114             }
115             writer.close();
116             LOGGER.debug("Sucessfully forwarded the policy to store into file {}.", path);
117         } catch (final InvalidPathException | IOException exp) {
118             final String message = "Error sending policy to file under path:" + fileForwarderParameters.getPath();
119             LOGGER.error(message, exp);
120             throw new PolicyForwardingException(message, exp);
121         }
122     }
123 }
124