Policy 1707 commit to LF
[policy/engine.git] / BRMSGateway / src / main / java / org / openecomp / policy / brmsInterface / BRMSHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP 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.openecomp.policy.brmsInterface;
22
23 import java.util.Collection;
24
25 import org.openecomp.policy.api.ConfigRequestParameters;
26 import org.openecomp.policy.api.LoadedPolicy;
27 import org.openecomp.policy.api.NotificationType;
28 import org.openecomp.policy.api.PDPNotification;
29 import org.openecomp.policy.api.PolicyConfig;
30 import org.openecomp.policy.api.PolicyConfigStatus;
31 import org.openecomp.policy.api.PolicyEngine;
32 import org.openecomp.policy.api.PolicyException;
33 import org.openecomp.policy.api.RemovedPolicy;
34 import org.openecomp.policy.common.logging.flexlogger.Logger;
35 import org.openecomp.policy.utils.BackUpHandler;
36 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
37 //import org.apache.log4j.Logger;
38 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
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         @Override
61         public void notificationReceived(PDPNotification notification) {
62                 logger.info("Notification Recieved");
63                 logger.info(notification.getNotificationType().toString());
64                 Boolean flag = BRMSPush.getBackUpMonitor().getFlag();
65                 bRMSPush.initiate(flag);
66                 if(flag){
67                         logger.info("Master Application performing on Notification ");
68                         runOnNotification(notification);
69                 }else{
70                         logger.info("Slave application Skipping Notification.. ");
71                 }
72         }
73         
74         private void removedPolicies(Collection<RemovedPolicy> removedPolicies){
75                 Boolean removed = false;
76                 logger.info("Removed Policies");
77                 for(RemovedPolicy removedPolicy: removedPolicies){
78                         logger.info(removedPolicy.getPolicyName());
79                         logger.info(removedPolicy.getVersionNo());
80                         if(removedPolicy.getPolicyName().contains("_BRMS_")){
81                                 try{
82                                         logger.info("Policy Removed with this policy Name : " + removedPolicy.getPolicyName());
83                                         bRMSPush.removeRule(removedPolicy.getPolicyName());
84                                         removed = true;
85                                 }catch(Exception e){
86                                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage());
87                                 }
88                         }
89                 }
90                 if(removed){
91                         bRMSPush.pushRules();
92                 }
93         }
94         
95         public void runOnNotification(PDPNotification notification){
96                 if(notification.getNotificationType().equals(NotificationType.REMOVE)){
97                         removedPolicies(notification.getRemovedPolicies());
98                 }else if(notification.getNotificationType().equals(NotificationType.UPDATE)|| notification.getNotificationType().equals(NotificationType.BOTH)){
99                         logger.info("Updated Policies: \n");
100                         for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
101                                 logger.info("policyName : " + updatedPolicy.getPolicyName());
102                                 logger.info("policyVersion :" + updatedPolicy.getVersionNo());
103                                 logger.info("Matches: " + updatedPolicy.getMatches());
104                                 // Checking the Name is correct or not. 
105                                 if(updatedPolicy.getPolicyName().contains("_BRMS_")){
106                                         try{
107                                                 PolicyEngine policyEngine = getPolicyEngine();
108                                                 if(policyEngine!=null){
109                                                         ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
110                                                         configRequestParameters.setPolicyName(updatedPolicy.getPolicyName());
111                                                         Collection<PolicyConfig> policyConfigs = policyEngine.getConfig(configRequestParameters);
112                                                         for(PolicyConfig policyConfig: policyConfigs){
113                                                                 if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
114                                                                         logger.info("Policy Retrieved with this Name notified: " + policyConfig.getPolicyName());
115                                                                     bRMSPush.addRule(policyConfig.getPolicyName(),policyConfig.toOther(),policyConfig.getResponseAttributes());
116                                                                 }else{
117                                                                         logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n");
118                                                                 }
119                                                         }
120                                                 }
121                                         }catch(Exception e){
122                                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage());
123                                         }
124                                 }
125                         }
126                         bRMSPush.pushRules();
127                 }
128         }
129
130         public PolicyEngine getPolicyEngine() {
131                 return BRMSGateway.getPolicyEngine();
132         }
133 }