Removed checkstyle warnings
[policy/engine.git] / BRMSGateway / src / main / java / org / onap / policy / brms / api / BrmsHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.brms.api;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25
26 import org.onap.policy.api.ConfigRequestParameters;
27 import org.onap.policy.api.LoadedPolicy;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.api.PolicyConfig;
31 import org.onap.policy.api.PolicyConfigStatus;
32 import org.onap.policy.api.PolicyEngine;
33 import org.onap.policy.api.PolicyException;
34 import org.onap.policy.api.RemovedPolicy;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.utils.BackUpHandler;
38 import org.onap.policy.xacml.api.XACMLErrorConstants;
39
40 /**
41  * BRMSHandler: Notification Handler which listens for PDP Notifications. Take action only for BRMS
42  * policies.
43  * 
44  * @version 0.3
45  */
46 public class BrmsHandler implements BackUpHandler {
47
48     private static final Logger logger = FlexLogger.getLogger(BrmsHandler.class.getName());
49
50     private BrmsPush brmsPush = null;
51
52     public BrmsHandler(final String propertiesFile) throws PolicyException {
53         setBrmsPush(new BrmsPush(propertiesFile, this));
54     }
55
56     public void setBrmsPush(final BrmsPush brmsPush) {
57         this.brmsPush = brmsPush;
58     }
59
60     /*
61      * This Method is executed upon notification by the Policy Engine API Notification.
62      * (non-Javadoc)
63      * 
64      * @see
65      * org.onap.policy.utils.BackUpHandler#notificationReceived(org.onap.policy.api.PDPNotification)
66      */
67     @Override
68     public void notificationReceived(final PDPNotification notification) {
69         logger.info("Notification Recieved");
70         logger.info(notification.getNotificationType().toString());
71         final Boolean flag = BrmsPush.getBackUpMonitor().getFlag();
72         brmsPush.initiate(flag);
73         if (flag) {
74             logger.info("Master Application performing on Notification ");
75             runOnNotification(notification);
76         } else {
77             logger.info("Slave application Skipping Notification.. ");
78         }
79     }
80
81     /*
82      * Executed when a policy is removed from PDP.
83      */
84     private void removedPolicies(final Collection<RemovedPolicy> removedPolicies) {
85         Boolean removed = false;
86         logger.info("Removed Policies");
87         for (final RemovedPolicy removedPolicy : removedPolicies) {
88             logger.info(removedPolicy.getPolicyName());
89             logger.info(removedPolicy.getVersionNo());
90             if (removedPolicy.getPolicyName().contains("_BRMS_")) {
91                 try {
92                     logger.info("Policy Removed with this policy Name : " + removedPolicy.getPolicyName());
93                     brmsPush.removeRule(removedPolicy.getPolicyName());
94                     removed = true;
95                 } catch (final Exception e) {
96                     logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Rertriving policy failed " + e.getMessage(),
97                             e);
98                 }
99             }
100         }
101         Boolean failureFlag;
102         int index = 0;
103         do {
104             failureFlag = false;
105             if (removed) {
106                 try {
107                     brmsPush.pushRules();
108                 } catch (final PolicyException e) {
109                     // Upon Notification failure
110                     failureFlag = true;
111                     brmsPush.rotateUrls();
112                     logger.error("Failure during Push Operation ", e);
113                 }
114             }
115             index++;
116         } 
117         while (failureFlag && index < brmsPush.urlListSize());
118     }
119
120     /*
121      * This method is executed if BRMSGW is "MASTER" (non-Javadoc)
122      * 
123      * @see
124      * org.onap.policy.utils.BackUpHandler#runOnNotification(org.onap.policy.api.PDPNotification)
125      */
126     @Override
127     public void runOnNotification(final PDPNotification notification) {
128         // reset the BRMSPush data structures
129         brmsPush.resetDs();
130         if (notification.getNotificationType().equals(NotificationType.REMOVE)) {
131             removedPolicies(notification.getRemovedPolicies());
132         } else if (notification.getNotificationType().equals(NotificationType.UPDATE)
133                 || notification.getNotificationType().equals(NotificationType.BOTH)) {
134             logger.info("Updated Policies: \n");
135             final ArrayList<PolicyConfig> brmsPolicies = addedPolicies(notification);
136             Boolean successFlag = false;
137             for (int i = 0; !successFlag && i < brmsPush.urlListSize(); i++) {
138                 if (i != 0) {
139                     for (final PolicyConfig policyConfig : brmsPolicies) {
140                         logger.info("Policy Retry with this Name notified: " + policyConfig.getPolicyName());
141                         brmsPush.addRule(policyConfig.getPolicyName(), policyConfig.toOther(),
142                                 policyConfig.getResponseAttributes());
143                     }
144                 }
145                 try {
146                     brmsPush.pushRules();
147                     successFlag = true;
148                 } catch (final PolicyException e) {
149                     // Upon Notification failure
150                     successFlag = false;
151                     brmsPush.rotateUrls();
152                     logger.error("Failure during Push Operation ", e);
153                 }
154             }
155         }
156     }
157
158     /*
159      * Executed when a policy is added to PDP.
160      */
161     private ArrayList<PolicyConfig> addedPolicies(final PDPNotification notification) {
162         final ArrayList<PolicyConfig> result = new ArrayList<>();
163         for (final LoadedPolicy updatedPolicy : notification.getLoadedPolicies()) {
164             logger.info("policyName : " + updatedPolicy.getPolicyName());
165             logger.info("policyVersion :" + updatedPolicy.getVersionNo());
166             logger.info("Matches: " + updatedPolicy.getMatches());
167             // Checking the Name is correct or not.
168             if (updatedPolicy.getPolicyName().contains("_BRMS_")) {
169                 try {
170                     final PolicyEngine policyEngine = getPolicyEngine();
171                     if (policyEngine != null) {
172                         final ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
173                         configRequestParameters.setPolicyName(updatedPolicy.getPolicyName());
174                         final Collection<PolicyConfig> policyConfigs = policyEngine.getConfig(configRequestParameters);
175                         for (final PolicyConfig policyConfig : policyConfigs) {
176                             if (policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)) {
177                                 logger.info(
178                                         "Policy Retrieved with this Name notified: " + policyConfig.getPolicyName());
179                                 result.add(policyConfig);
180                                 brmsPush.addRule(policyConfig.getPolicyName(), policyConfig.toOther(),
181                                         policyConfig.getResponseAttributes());
182                             } else {
183                                 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR
184                                         + "Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n");
185                             }
186                         }
187                     }
188                 } catch (final Exception e) {
189                     logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Rertriving policy failed " + e.getMessage(),
190                             e);
191                 }
192             }
193         }
194         return result;
195     }
196
197     public PolicyEngine getPolicyEngine() {
198         return BrmsGateway.getPolicyEngine();
199     }
200 }