6cca9cefec9e092f0cf86ca7cf931ea05ab675b1
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * feature-controller-logging
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.controller.logging;
22
23 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
24 import org.onap.policy.common.endpoints.event.comm.TopicSink;
25 import org.onap.policy.drools.controller.DroolsController;
26 import org.onap.policy.drools.features.DroolsControllerFeatureApi;
27 import org.onap.policy.drools.features.PolicyControllerFeatureApi;
28 import org.onap.policy.drools.features.PolicyEngineFeatureApi;
29 import org.onap.policy.drools.protocol.configuration.ControllerConfiguration;
30 import org.onap.policy.drools.protocol.configuration.PdpdConfiguration;
31 import org.onap.policy.drools.system.PolicyController;
32 import org.onap.policy.drools.system.PolicyControllerConstants;
33 import org.onap.policy.drools.system.PolicyEngine;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * This class hooks the network logging implementation into DroolsPDP. It will disable the
38  * default network logger where all topic traffic is logged and segregates the topic
39  * traffic by controller for each supported control loop use case.
40  */
41
42 /*
43  * PolicyControllerFeatureAPI - the 'beforeStart' hook is used to shut off the default
44  * network logger and the 'beforeOffer' hook is used to log incoming topic messages
45  *
46  * DroolsControllerFeatureAPI - the 'afterDeliver' hook is where the outgoing topic
47  * messages are logged
48  *
49  */
50 public class ControllerLoggingFeature
51                 implements PolicyEngineFeatureApi, DroolsControllerFeatureApi, PolicyControllerFeatureApi {
52
53     private static final String LINE_SEP = System.lineSeparator();
54
55     @Override
56     public int getSequenceNumber() {
57         return 1000;
58     }
59
60     /**
61      * The 'beforeOffer' hook will intercept an incoming topic message and append it to
62      * the log file that is configured for the controller logger.
63      */
64     @Override
65     public boolean beforeOffer(PolicyController controller, CommInfrastructure protocol, String topic, String event) {
66         var controllerLogger = LoggerFactory.getLogger(controller.getName());
67         controllerLogger.info("[IN|{}|{}]{}{}", protocol, topic, LINE_SEP, event);
68         return false;
69     }
70
71     /**
72      * The 'afterDeliver' hook will intercept an outgoing topic message and append it to
73      * the log file that is configured for the controller logger.
74      */
75     @Override
76     public boolean afterDeliver(DroolsController controller, TopicSink sink, Object fact, String json,
77                     boolean success) {
78         if (success) {
79             var controllerLogger = LoggerFactory
80                             .getLogger(PolicyControllerConstants.getFactory().get(controller).getName());
81             controllerLogger.info("[OUT|{}|{}]{}{}", sink.getTopicCommInfrastructure(), sink.getTopic(),
82                             LINE_SEP, json);
83         }
84         return false;
85     }
86
87     /**
88      * The 'afterOnTopicEvent' hook will determine which controllers were updated and log
89      * the event to the appropriate controller logs.
90      */
91     @Override
92     public boolean afterOnTopicEvent(PolicyEngine engine, PdpdConfiguration configuration, CommInfrastructure commType,
93                     String topic, String event) {
94         for (ControllerConfiguration controller : configuration.getControllers()) {
95             var controllerLogger = LoggerFactory.getLogger(controller.getName());
96             controllerLogger.info("[IN|{}|{}]{}{}", commType, topic, LINE_SEP, event);
97         }
98         return false;
99     }
100 }