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
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.forwarding.file;
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;
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;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
45 public class FilePolicyForwarder implements PolicyForwarder {
47 private static final Logger LOGGER = LoggerFactory.getLogger(FilePolicyForwarder.class);
48 private FilePolicyForwarderParameterGroup fileForwarderParameters;
54 public void configure(final String parameterGroupName) {
55 fileForwarderParameters = ParameterService.get(parameterGroupName);
57 Path path = Paths.get(fileForwarderParameters.getPath());
58 if (!Files.exists(path)) {
59 Files.createDirectories(path);
61 } catch (final InvalidPathException | IOException e) {
62 LOGGER.error("Configuring FilePolicyForwarder failed!", e);
70 public void forward(final Collection<Policy> policies) throws PolicyForwardingException {
71 for (Policy policy : policies) {
72 if (policy instanceof OptimizationPolicy) {
73 forwardPolicy((OptimizationPolicy) policy);
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);
84 * Method to forward a given policy to be logged into a file.
86 * @param pol the policy
87 * @throws PolicyForwardingException if any exception occurs while forwarding policy
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()) {
96 writer.write("policyType: " + pol.getPolicyType());
98 writer.write("policyDescription: " + pol.getPolicyDescription());
100 writer.write("onapName: " + pol.getOnapName());
102 writer.write("configBodyType: " + pol.getConfigBodyType());
104 writer.write("configBody: " + pol.getConfigBody());
106 writer.write("timetolive: " + pol.getTimetolive().toString());
108 writer.write("guard: " + pol.getGuard());
110 writer.write("riskLevel: " + pol.getRiskLevel());
112 writer.write("riskType: " + pol.getRiskType());
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);