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.IOException;
25 import java.nio.charset.StandardCharsets;
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.models.tosca.authorative.concepts.ToscaEntity;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to a
44 public class FilePolicyForwarder implements PolicyForwarder {
46 private static final Logger LOGGER = LoggerFactory.getLogger(FilePolicyForwarder.class);
47 private FilePolicyForwarderParameterGroup fileForwarderParameters;
53 public void configure(final String parameterGroupName) {
54 fileForwarderParameters = ParameterService.get(parameterGroupName);
56 final Path path = Paths.get(fileForwarderParameters.getPath());
57 if (!path.toFile().exists()) {
58 Files.createDirectories(path);
60 } catch (final InvalidPathException | IOException e) {
61 LOGGER.error("Configuring FilePolicyForwarder failed!", e);
69 public void forward(final Collection<ToscaEntity> policies) throws PolicyForwardingException {
70 for (final ToscaEntity policy : policies) {
71 if (policy instanceof ToscaPolicy) {
72 forwardPolicy((ToscaPolicy) policy);
74 final String message = "Cannot forward policy " + policy + ". Unsupported policy type "
75 + policy.getClass().getSimpleName();
76 LOGGER.error(message);
77 throw new PolicyForwardingException(message);
83 * Method to forward a given policy to be logged into a file.
85 * @param pol the policy
86 * @throws PolicyForwardingException if any exception occurs while forwarding policy
88 private void forwardPolicy(final ToscaPolicy pol) throws PolicyForwardingException {
89 final String name = pol.getName();
90 final Path path = Paths.get(fileForwarderParameters.getPath(), name);
91 try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
92 writer.write("policyName: " + name);
93 if (fileForwarderParameters.isVerbose()) {
95 writer.write("policy: " + pol.toString());
97 LOGGER.debug("Sucessfully forwarded the policy to store into file {}.", path);
98 } catch (final InvalidPathException | IOException exp) {
99 final String message = "Error sending policy to file under path:" + fileForwarderParameters.getPath();
100 LOGGER.error(message, exp);
101 throw new PolicyForwardingException(message, exp);