X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=BRMSGateway%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fpolicy%2FbrmsInterface%2FBRMSHandler.java;h=f5f78a970a31f5984ed4214ac7c074d1f72952c9;hb=a084c4de015521f842a3fef0d7f1f7c340237bc3;hp=2cc17873609857ea6924ad775f507eb04ca86ae8;hpb=91d04c64771832a0b8815ffbe1f0f9920320d94d;p=policy%2Fengine.git diff --git a/BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSHandler.java b/BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSHandler.java index 2cc178736..f5f78a970 100644 --- a/BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSHandler.java +++ b/BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSHandler.java @@ -20,6 +20,7 @@ package org.openecomp.policy.brmsInterface; +import java.util.ArrayList; import java.util.Collection; import org.openecomp.policy.api.ConfigRequestParameters; @@ -29,15 +30,12 @@ import org.openecomp.policy.api.PDPNotification; import org.openecomp.policy.api.PolicyConfig; import org.openecomp.policy.api.PolicyConfigStatus; import org.openecomp.policy.api.PolicyEngine; +import org.openecomp.policy.api.PolicyException; import org.openecomp.policy.api.RemovedPolicy; -import org.openecomp.policy.utils.BackUpHandler; - -import org.openecomp.policy.xacml.api.XACMLErrorConstants; - -//import org.apache.log4j.Logger; - import org.openecomp.policy.common.logging.flexlogger.FlexLogger; import org.openecomp.policy.common.logging.flexlogger.Logger; +import org.openecomp.policy.utils.BackUpHandler; +import org.openecomp.policy.xacml.api.XACMLErrorConstants; /** * BRMSHandler: Notification Handler which listens for PDP Notifications. @@ -47,24 +45,30 @@ import org.openecomp.policy.common.logging.flexlogger.Logger; */ public class BRMSHandler implements BackUpHandler{ -// private static final Logger logger = Logger.getLogger(BRMSHandler.class.getName()); private static final Logger logger = FlexLogger.getLogger(BRMSHandler.class.getName()); - // This Values are fixed for BRMS based rules. Don't change until they are changed during creation in PAP-ADMIN. - //private static final String ecompName = "DROOLS"; - //private static final String configName = "BRMS_RAW_RULE"; private BRMSPush bRMSPush = null; - public BRMSHandler(String propertiesFile) throws Exception{ - bRMSPush = new BRMSPush(propertiesFile, this); + public BRMSHandler(String propertiesFile) throws PolicyException{ + setBRMSPush(new BRMSPush(propertiesFile, this)); } + public void setBRMSPush(BRMSPush brmsPush) { + this.bRMSPush = brmsPush; + } + + /* + * This Method is executed upon notification by the Policy Engine API Notification. + * (non-Javadoc) + * @see org.openecomp.policy.utils.BackUpHandler#notificationReceived(org.openecomp.policy.api.PDPNotification) + */ @Override public void notificationReceived(PDPNotification notification) { logger.info("Notification Recieved"); logger.info(notification.getNotificationType().toString()); - bRMSPush.initiate(); - if(BRMSPush.getBackUpMonitor().getFlag()){ + Boolean flag = BRMSPush.getBackUpMonitor().getFlag(); + bRMSPush.initiate(flag); + if(flag){ logger.info("Master Application performing on Notification "); runOnNotification(notification); }else{ @@ -72,6 +76,9 @@ public class BRMSHandler implements BackUpHandler{ } } + /* + * Executed when a policy is removed from PDP. + */ private void removedPolicies(Collection removedPolicies){ Boolean removed = false; logger.info("Removed Policies"); @@ -80,55 +87,98 @@ public class BRMSHandler implements BackUpHandler{ logger.info(removedPolicy.getVersionNo()); if(removedPolicy.getPolicyName().contains("_BRMS_")){ try{ - String name = removedPolicy.getPolicyName().substring(removedPolicy.getPolicyName().indexOf("_BRMS_")+6, removedPolicy.getPolicyName().length()); - name= name.substring(name.indexOf("_")+1,name.length()); - logger.info("Policy Removed with this policy Name : " + name); - bRMSPush.removeRule(name); + logger.info("Policy Removed with this policy Name : " + removedPolicy.getPolicyName()); + bRMSPush.removeRule(removedPolicy.getPolicyName()); removed = true; }catch(Exception e){ logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage()); } } } - if(removed){ - bRMSPush.pushRules(); - } + Boolean failureFlag = false; + int i = 0; + do{ + failureFlag = false; + if(removed){ + try{ + bRMSPush.pushRules(); + }catch(PolicyException e){ + //Upon Notification failure + failureFlag = true; + bRMSPush.rotateURLs(); + } + } + i++; + }while(failureFlag && i< bRMSPush.URLListSize()); } + /* + * This method is executed if BRMSGW is "MASTER" + * (non-Javadoc) + * @see org.openecomp.policy.utils.BackUpHandler#runOnNotification(org.openecomp.policy.api.PDPNotification) + */ public void runOnNotification(PDPNotification notification){ if(notification.getNotificationType().equals(NotificationType.REMOVE)){ removedPolicies(notification.getRemovedPolicies()); }else if(notification.getNotificationType().equals(NotificationType.UPDATE)|| notification.getNotificationType().equals(NotificationType.BOTH)){ logger.info("Updated Policies: \n"); - for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){ - logger.info("policyName : " + updatedPolicy.getPolicyName()); - logger.info("policyVersion :" + updatedPolicy.getVersionNo()); - logger.info("Matches: " + updatedPolicy.getMatches()); - // Checking the Name is correct or not. - if(updatedPolicy.getPolicyName().contains("_BRMS_")){ - try{ - PolicyEngine policyEngine = BRMSGateway.getPolicyEngine(); - if(policyEngine!=null){ - ConfigRequestParameters configRequestParameters = new ConfigRequestParameters(); - configRequestParameters.setPolicyName(updatedPolicy.getPolicyName()); - Collection policyConfigs = policyEngine.getConfig(configRequestParameters); - for(PolicyConfig policyConfig: policyConfigs){ - if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){ - String name = policyConfig.getPolicyName().substring(policyConfig.getPolicyName().indexOf("_BRMS_")+6, policyConfig.getPolicyName().length()); - name= name.substring(name.indexOf("_")+1,name.length()); - logger.info("Policy Retrieved with this Name notified: " + name); - bRMSPush.addRule(name,policyConfig.toOther(),policyConfig.getResponseAttributes()); - }else{ - logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n"); - } + ArrayList brmsPolicies = addedPolicies(notification); + Boolean successFlag = false; + for(int i=0; !successFlag && i< bRMSPush.URLListSize(); i++){ + if(i!=0){ + for(PolicyConfig policyConfig: brmsPolicies){ + logger.info("Policy Retry with this Name notified: " + policyConfig.getPolicyName()); + bRMSPush.addRule(policyConfig.getPolicyName(),policyConfig.toOther(),policyConfig.getResponseAttributes()); + } + } + try{ + bRMSPush.pushRules(); + successFlag = true; + }catch(PolicyException e){ + //Upon Notification failure + successFlag = false; + bRMSPush.rotateURLs(); + } + } + } + } + + /* + * Executed when a policy is added to PDP. + */ + private ArrayList addedPolicies(PDPNotification notification) { + ArrayList result = new ArrayList<>(); + for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){ + logger.info("policyName : " + updatedPolicy.getPolicyName()); + logger.info("policyVersion :" + updatedPolicy.getVersionNo()); + logger.info("Matches: " + updatedPolicy.getMatches()); + // Checking the Name is correct or not. + if(updatedPolicy.getPolicyName().contains("_BRMS_")){ + try{ + PolicyEngine policyEngine = getPolicyEngine(); + if(policyEngine!=null){ + ConfigRequestParameters configRequestParameters = new ConfigRequestParameters(); + configRequestParameters.setPolicyName(updatedPolicy.getPolicyName()); + Collection policyConfigs = policyEngine.getConfig(configRequestParameters); + for(PolicyConfig policyConfig: policyConfigs){ + if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){ + logger.info("Policy Retrieved with this Name notified: " + policyConfig.getPolicyName()); + result.add(policyConfig); + bRMSPush.addRule(policyConfig.getPolicyName(),policyConfig.toOther(),policyConfig.getResponseAttributes()); + }else{ + logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n"); } } - }catch(Exception e){ - logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage()); } + }catch(Exception e){ + logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage()); } } - bRMSPush.pushRules(); } + return result; + } + + public PolicyEngine getPolicyEngine() { + return BRMSGateway.getPolicyEngine(); } }