98d9af4e723a00c1bc57d21848e27717124f5af4
[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.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.forwarding.PolicyForwarder;
36 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
37 import org.onap.policy.distribution.model.OptimizationPolicy;
38 import org.onap.policy.distribution.model.Policy;
39
40 /**
41  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
42  * a file directory.
43  */
44 public class FilePolicyForwarder implements PolicyForwarder {
45
46     private static final Logger LOGGER = FlexLogger.getLogger(FilePolicyForwarder.class);
47     private FilePolicyForwarderParameterGroup fileForwarderParameters;
48
49     /**
50      * {@inheritDoc}.
51      */
52     @Override
53     public void configure(final String parameterGroupName) {
54         fileForwarderParameters = ParameterService.get(parameterGroupName);
55         try {
56             Path path = Paths.get(fileForwarderParameters.getPath());
57             if (!Files.exists(path)) {
58                 Files.createDirectories(path);
59             }
60         } catch (final InvalidPathException | IOException e) {
61             LOGGER.error(e.toString());
62         }
63     }
64
65     /**
66      * {@inheritDoc}.
67      */
68     @Override
69     public void forward(final Collection<Policy> policies) throws PolicyForwardingException {
70         for (Policy policy : policies) {
71             if (policy instanceof OptimizationPolicy) {
72                 forwardPolicy((OptimizationPolicy) policy);
73             } else {
74                 final String message = new String("Cannot forward policy " + policy
75                         + ". Unsupported policy type " + policy.getClass().getSimpleName());
76                 LOGGER.error(message);
77                 throw new PolicyForwardingException(message);
78             }
79         }
80     }
81
82     /**
83      * Method to forward a given policy to be logged into a file.
84      *
85      * @param pol the policy
86      * @throws PolicyForwardingException if any exception occurs while forwarding policy
87      */
88     private void forwardPolicy(final OptimizationPolicy pol) throws PolicyForwardingException {
89         final String name = pol.getPolicyName();
90         try {
91             Path path = Paths.get(fileForwarderParameters.getPath(), name);
92             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             writer.close();
115             LOGGER.debug("Sucessfully forwarded the policy to store into file: " + path.toString());
116         } catch (final InvalidPathException | IOException exp) {
117             final String message = "Error sending policy to file under path:" + fileForwarderParameters.getPath();
118             LOGGER.error(message, exp);
119             throw new PolicyForwardingException(message, exp);
120         }
121     }
122 }
123