b50d6ff384e24f19853c4229895f3a895e65b464
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Intel Crop. All rights reserved.
4  *  Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.forwarding.file;
23
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.Paths;
29 import java.util.Collection;
30 import org.onap.policy.common.parameters.ParameterService;
31 import org.onap.policy.distribution.forwarding.PolicyForwarder;
32 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to a
40  * file directory.
41  */
42 public class FilePolicyForwarder implements PolicyForwarder {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(FilePolicyForwarder.class);
45     private FilePolicyForwarderParameterGroup fileForwarderParameters;
46
47     /**
48      * {@inheritDoc}.
49      */
50     @Override
51     public void configure(final String parameterGroupName) {
52         fileForwarderParameters = ParameterService.get(parameterGroupName);
53         try {
54             final var path = Paths.get(fileForwarderParameters.getPath());
55             if (!path.toFile().exists()) {
56                 Files.createDirectories(path);
57             }
58         } catch (final InvalidPathException | IOException e) {
59             LOGGER.error("Configuring FilePolicyForwarder failed!", e);
60         }
61     }
62
63     /**
64      * {@inheritDoc}.
65      */
66     @Override
67     public void forward(final Collection<ToscaEntity> policies) throws PolicyForwardingException {
68         for (final ToscaEntity policy : policies) {
69             if (policy instanceof ToscaPolicy) {
70                 forwardPolicy((ToscaPolicy) policy);
71             } else {
72                 final String message = "Cannot forward policy " + policy + ". Unsupported policy type "
73                         + policy.getClass().getSimpleName();
74                 LOGGER.error(message);
75                 throw new PolicyForwardingException(message);
76             }
77         }
78     }
79
80     /**
81      * Method to forward a given policy to be logged into a file.
82      *
83      * @param pol the policy
84      * @throws PolicyForwardingException if any exception occurs while forwarding policy
85      */
86     private void forwardPolicy(final ToscaPolicy pol) throws PolicyForwardingException {
87         final String name = pol.getName();
88         final var path = Paths.get(fileForwarderParameters.getPath(), name);
89         try (var writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
90             writer.write("policyName: " + name);
91             if (fileForwarderParameters.isVerbose()) {
92                 writer.newLine();
93                 writer.write("policy: " + pol.toString());
94             }
95             LOGGER.debug("Sucessfully forwarded the policy to store into file {}.", path);
96         } catch (final InvalidPathException | IOException exp) {
97             final String message = "Error sending policy to file under path:" + fileForwarderParameters.getPath();
98             throw new PolicyForwardingException(message, exp);
99         }
100     }
101 }
102