Initial OpenECOMP policy/engine commit
[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.RemovedPolicy;
33 import org.openecomp.policy.utils.BackUpHandler;
34
35 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
36
37 //import org.apache.log4j.Logger;
38
39 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
40 import org.openecomp.policy.common.logging.flexlogger.Logger;
41
42 /**
43  * BRMSHandler: Notification Handler which listens for PDP Notifications. 
44  * Take action only for BRMS policies. 
45  * 
46  * @version 0.3
47  */
48 public class BRMSHandler implements BackUpHandler{
49         
50 //      private static final Logger logger = Logger.getLogger(BRMSHandler.class.getName());
51         private static final Logger logger = FlexLogger.getLogger(BRMSHandler.class.getName());
52         // This Values are fixed for BRMS based rules. Don't change until they are changed during creation in PAP-ADMIN. 
53         //private static final String ecompName = "DROOLS";
54         //private static final String configName = "BRMS_RAW_RULE";
55         
56         private BRMSPush bRMSPush = null;
57         
58         public BRMSHandler(String propertiesFile) throws Exception{
59                 bRMSPush = new BRMSPush(propertiesFile, this);
60         }
61
62         @Override
63         public void notificationReceived(PDPNotification notification) {
64                 logger.info("Notification Recieved");
65                 logger.info(notification.getNotificationType().toString());
66                 bRMSPush.initiate();
67                 if(BRMSPush.getBackUpMonitor().getFlag()){
68                         logger.info("Master Application performing on Notification ");
69                         runOnNotification(notification);
70                 }else{
71                         logger.info("Slave application Skipping Notification.. ");
72                 }
73         }
74         
75         private void removedPolicies(Collection<RemovedPolicy> removedPolicies){
76                 Boolean removed = false;
77                 logger.info("Removed Policies");
78                 for(RemovedPolicy removedPolicy: removedPolicies){
79                         logger.info(removedPolicy.getPolicyName());
80                         logger.info(removedPolicy.getVersionNo());
81                         if(removedPolicy.getPolicyName().contains("_BRMS_")){
82                                 try{
83                                         String name = removedPolicy.getPolicyName().substring(removedPolicy.getPolicyName().indexOf("_BRMS_")+6, removedPolicy.getPolicyName().length());
84                                         name= name.substring(name.indexOf("_")+1,name.length());
85                                         logger.info("Policy Removed with this policy Name : " + name);
86                                         bRMSPush.removeRule(name);
87                                         removed = true;
88                                 }catch(Exception e){
89                                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage());
90                                 }
91                         }
92                 }
93                 if(removed){
94                         bRMSPush.pushRules();
95                 }
96         }
97         
98         public void runOnNotification(PDPNotification notification){
99                 if(notification.getNotificationType().equals(NotificationType.REMOVE)){
100                         removedPolicies(notification.getRemovedPolicies());
101                 }else if(notification.getNotificationType().equals(NotificationType.UPDATE)|| notification.getNotificationType().equals(NotificationType.BOTH)){
102                         logger.info("Updated Policies: \n");
103                         for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
104                                 logger.info("policyName : " + updatedPolicy.getPolicyName());
105                                 logger.info("policyVersion :" + updatedPolicy.getVersionNo());
106                                 logger.info("Matches: " + updatedPolicy.getMatches());
107                                 // Checking the Name is correct or not. 
108                                 if(updatedPolicy.getPolicyName().contains("_BRMS_")){
109                                         try{
110                                                 PolicyEngine policyEngine = BRMSGateway.getPolicyEngine();
111                                                 if(policyEngine!=null){
112                                                         ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
113                                                         configRequestParameters.setPolicyName(updatedPolicy.getPolicyName());
114                                                         Collection<PolicyConfig> policyConfigs = policyEngine.getConfig(configRequestParameters);
115                                                         for(PolicyConfig policyConfig: policyConfigs){
116                                                                 if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
117                                                                         String name = policyConfig.getPolicyName().substring(policyConfig.getPolicyName().indexOf("_BRMS_")+6, policyConfig.getPolicyName().length());
118                                                                         name= name.substring(name.indexOf("_")+1,name.length());
119                                                                         logger.info("Policy Retrieved with this Name notified: " + name);
120                                                                     bRMSPush.addRule(name,policyConfig.toOther(),policyConfig.getResponseAttributes());
121                                                                 }else{
122                                                                         logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n");
123                                                                 }
124                                                         }
125                                                 }
126                                         }catch(Exception e){
127                                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage());
128                                         }
129                                 }
130                         }
131                         bRMSPush.pushRules();
132                 }
133         }
134 }