30f90763ec6b5e573c403c34997112b53d2c9779
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * feature-controller-logging
4  * ================================================================================
5  * Copyright (C) 2019-2020 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.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * This class hooks the network logging implementation into DroolsPDP. It will disable the
39  * default network logger where all topic traffic is logged and segregates the topic
40  * traffic by controller for each supported control loop use case.
41  */
42
43 /*
44  * PolicyControllerFeatureAPI - the 'beforeStart' hook is used to shut off the default
45  * network logger and the 'beforeOffer' hook is used to log incoming topic messages
46  *
47  * DroolsControllerFeatureAPI - the 'afterDeliver' hook is where the outgoing topic
48  * messages are logged
49  *
50  */
51 public class ControllerLoggingFeature
52                 implements PolicyEngineFeatureApi, DroolsControllerFeatureApi, PolicyControllerFeatureApi {
53
54     private static final String LINE_SEP = System.lineSeparator();
55
56     @Override
57     public int getSequenceNumber() {
58         return 1000;
59     }
60
61     /**
62      * The 'beforeOffer' hook will intercept an incoming topic message and append it to
63      * the log file that is configured for the controller logger.
64      */
65     @Override
66     public boolean beforeOffer(PolicyController controller, CommInfrastructure protocol, String topic, String event) {
67         Logger controllerLogger = LoggerFactory.getLogger(controller.getName());
68         controllerLogger.info("[IN|{}|{}]{}{}", protocol, topic, LINE_SEP, event);
69         return false;
70     }
71
72     /**
73      * The 'afterDeliver' hook will intercept an outgoing topic message and append it to
74      * the log file that is configured for the controller logger.
75      */
76     @Override
77     public boolean afterDeliver(DroolsController controller, TopicSink sink, Object fact, String json,
78                     boolean success) {
79         if (success) {
80             Logger controllerLogger = LoggerFactory
81                             .getLogger(PolicyControllerConstants.getFactory().get(controller).getName());
82             controllerLogger.info("[OUT|{}|{}]{}{}", sink.getTopicCommInfrastructure(), sink.getTopic(),
83                             LINE_SEP, json);
84         }
85         return false;
86     }
87
88     /**
89      * The 'afterOnTopicEvent' hook will determine which controllers were updated and log
90      * the event to the appropriate controller logs.
91      */
92     @Override
93     public boolean afterOnTopicEvent(PolicyEngine engine, PdpdConfiguration configuration, CommInfrastructure commType,
94                     String topic, String event) {
95         for (ControllerConfiguration controller : configuration.getControllers()) {
96             Logger controllerLogger = LoggerFactory.getLogger(controller.getName());
97             controllerLogger.info("[IN|{}|{}]{}{}", commType, topic, LINE_SEP, event);
98         }
99         return false;
100     }
101 }