Fixes for sonar critical issues
[policy/engine.git] / BRMSGateway / src / main / java / org / onap / policy / brmsInterface / BRMSHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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.brmsInterface;
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. 
42  * Take action only for BRMS 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(String propertiesFile) throws PolicyException{
53                 setBRMSPush(new BRMSPush(propertiesFile, this));
54         }
55
56         public void setBRMSPush(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          * @see org.onap.policy.utils.BackUpHandler#notificationReceived(org.onap.policy.api.PDPNotification)
64          */
65         @Override
66         public void notificationReceived(PDPNotification notification) {
67                 logger.info("Notification Recieved");
68                 logger.info(notification.getNotificationType().toString());
69                 Boolean flag = BRMSPush.getBackUpMonitor().getFlag();
70                 bRMSPush.initiate(flag);
71                 if(flag){
72                         logger.info("Master Application performing on Notification ");
73                         runOnNotification(notification);
74                 }else{
75                         logger.info("Slave application Skipping Notification.. ");
76                 }
77         }
78         
79         /*
80          * Executed when a policy is removed from PDP.  
81          */
82         private void removedPolicies(Collection<RemovedPolicy> removedPolicies){
83                 Boolean removed = false;
84                 logger.info("Removed Policies");
85                 for(RemovedPolicy removedPolicy: removedPolicies){
86                         logger.info(removedPolicy.getPolicyName());
87                         logger.info(removedPolicy.getVersionNo());
88                         if(removedPolicy.getPolicyName().contains("_BRMS_")){
89                                 try{
90                                         logger.info("Policy Removed with this policy Name : " + removedPolicy.getPolicyName());
91                                         bRMSPush.removeRule(removedPolicy.getPolicyName());
92                                         removed = true;
93                                 }catch(Exception e){
94                                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage(), e);
95                                 }
96                         }
97                 }
98                 Boolean failureFlag = false;
99                 int i = 0;
100                 do{
101                         failureFlag = false;
102                         if(removed){
103                                 try{
104                                         bRMSPush.pushRules();
105                                 }catch(PolicyException e){
106                                         //Upon Notification failure 
107                                         failureFlag = true;
108                                         bRMSPush.rotateURLs();
109                                         logger.error("Failure during Push Operation " , e);
110                                 }
111                         }
112                         i++;
113                 }while(failureFlag && i< bRMSPush.URLListSize());
114         }
115         
116         /*
117          * This method is executed if BRMSGW is "MASTER" 
118          * (non-Javadoc)
119          * @see org.onap.policy.utils.BackUpHandler#runOnNotification(org.onap.policy.api.PDPNotification)
120          */
121         public void runOnNotification(PDPNotification notification){
122                 if(notification.getNotificationType().equals(NotificationType.REMOVE)){
123                         removedPolicies(notification.getRemovedPolicies());
124                 }else if(notification.getNotificationType().equals(NotificationType.UPDATE)|| notification.getNotificationType().equals(NotificationType.BOTH)){
125                         logger.info("Updated Policies: \n");
126                         ArrayList<PolicyConfig> brmsPolicies = addedPolicies(notification);
127                         Boolean successFlag = false;
128                         for(int i=0; !successFlag && i< bRMSPush.URLListSize(); i++){
129                                 if(i!=0){
130                                         for(PolicyConfig policyConfig: brmsPolicies){
131                                                 logger.info("Policy Retry with this Name notified: " + policyConfig.getPolicyName());
132                                                 bRMSPush.addRule(policyConfig.getPolicyName(),policyConfig.toOther(),policyConfig.getResponseAttributes());
133                                         }
134                                 }
135                                 try{
136                                         bRMSPush.pushRules();
137                                         successFlag = true;
138                                 }catch(PolicyException e){
139                                         //Upon Notification failure 
140                                         successFlag = false;
141                                         bRMSPush.rotateURLs();
142                                         logger.error("Failure during Push Operation " , e);
143                                 }
144                         }
145                 }
146         }
147
148         /*
149          * Executed when a policy is added to PDP.  
150          */
151         private ArrayList<PolicyConfig> addedPolicies(PDPNotification notification) {
152                 ArrayList<PolicyConfig> result = new ArrayList<>();
153                 for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
154                         logger.info("policyName : " + updatedPolicy.getPolicyName());
155                         logger.info("policyVersion :" + updatedPolicy.getVersionNo());
156                         logger.info("Matches: " + updatedPolicy.getMatches());
157                         // Checking the Name is correct or not. 
158                         if(updatedPolicy.getPolicyName().contains("_BRMS_")){
159                                 try{
160                                         PolicyEngine policyEngine = getPolicyEngine();
161                                         if(policyEngine!=null){
162                                                 ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
163                                                 configRequestParameters.setPolicyName(updatedPolicy.getPolicyName());
164                                                 Collection<PolicyConfig> policyConfigs = policyEngine.getConfig(configRequestParameters);
165                                                 for(PolicyConfig policyConfig: policyConfigs){
166                                                         if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
167                                                                 logger.info("Policy Retrieved with this Name notified: " + policyConfig.getPolicyName());
168                                                                 result.add(policyConfig);
169                                                                 bRMSPush.addRule(policyConfig.getPolicyName(),policyConfig.toOther(),policyConfig.getResponseAttributes());
170                                                         }else{
171                                                                 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n");
172                                                         }
173                                                 }
174                                         }
175                                 }catch(Exception e){
176                                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage(), e);
177                                 }
178                         }
179                 }
180                 return result;
181         }
182
183         public PolicyEngine getPolicyEngine() {
184                 return BRMSGateway.getPolicyEngine();
185         }
186 }