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