diff options
author | ITSERVICES\rb7147 <rb7147@att.com> | 2017-05-08 22:20:44 -0400 |
---|---|---|
committer | ITSERVICES\rb7147 <rb7147@att.com> | 2017-05-09 13:58:03 -0400 |
commit | dda032f8bb161d54eb1f59de2b4a3efb774fc4d1 (patch) | |
tree | 9a11825d59434d97bb0c7dcbf00a0b84e7e5f526 /BRMSGateway/src | |
parent | a330af579866dacbe595e2e4ad1dd29cd3c96945 (diff) |
Policy 1707 Second commit
Change-Id: I18f5b142238733d17280cf17c3d1dd28204d34e9
Signed-off-by: ITSERVICES\rb7147 <rb7147@att.com>
Diffstat (limited to 'BRMSGateway/src')
-rw-r--r-- | BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSHandler.java | 108 | ||||
-rw-r--r-- | BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSPush.java | 135 |
2 files changed, 183 insertions, 60 deletions
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 67713420e..b28624e61 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; @@ -31,11 +32,10 @@ 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.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; -//import org.apache.log4j.Logger; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; /** * BRMSHandler: Notification Handler which listens for PDP Notifications. @@ -56,7 +56,12 @@ public class BRMSHandler implements BackUpHandler{ 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"); @@ -71,6 +76,9 @@ public class BRMSHandler implements BackUpHandler{ } } + /* + * Executed when a policy is removed from PDP. + */ private void removedPolicies(Collection<RemovedPolicy> removedPolicies){ Boolean removed = false; logger.info("Removed Policies"); @@ -87,44 +95,88 @@ public class BRMSHandler implements BackUpHandler{ } } } - 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 = getPolicyEngine(); - if(policyEngine!=null){ - ConfigRequestParameters configRequestParameters = new ConfigRequestParameters(); - configRequestParameters.setPolicyName(updatedPolicy.getPolicyName()); - Collection<PolicyConfig> 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()); - 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"); - } + ArrayList<PolicyConfig> brmsPolicies = addedPolicies(notification); + Boolean successFlag = false; + for(int i=0; !successFlag && i< bRMSPush.URLListSize(); i++){ + successFlag = false; + if(i!=0 && !successFlag){ + 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<PolicyConfig> addedPolicies(PDPNotification notification) { + ArrayList<PolicyConfig> result = new ArrayList<PolicyConfig>(); + 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<PolicyConfig> 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() { diff --git a/BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSPush.java b/BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSPush.java index 60d764cb0..7e9b64c33 100644 --- a/BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSPush.java +++ b/BRMSGateway/src/main/java/org/openecomp/policy/brmsInterface/BRMSPush.java @@ -36,6 +36,7 @@ import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.List; @@ -75,6 +76,7 @@ import org.openecomp.policy.brmsInterface.jpa.DependencyInfo; import org.openecomp.policy.common.im.AdministrativeStateException; import org.openecomp.policy.common.im.IntegrityMonitor; import org.openecomp.policy.common.logging.eelf.MessageCodes; +import org.openecomp.policy.common.logging.eelf.PolicyLogger; import org.openecomp.policy.common.logging.flexlogger.FlexLogger; import org.openecomp.policy.common.logging.flexlogger.Logger; import org.openecomp.policy.utils.BackUpHandler; @@ -100,6 +102,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; * @version 0.9 */ +@SuppressWarnings("deprecation") public class BRMSPush { private static final Logger LOGGER = FlexLogger.getLogger(BRMSPush.class.getName()); private static final String PROJECTSLOCATION = "RuleProjects"; @@ -114,7 +117,7 @@ public class BRMSPush { private String defaultName = null; private String repID = null; private String repName = null; - private String repURL= null; + private ArrayList<String> repURLs= null; private String repUserName = null; private String repPassword = null; private String policyKeyID = null; @@ -132,6 +135,7 @@ public class BRMSPush { private Map<String, String> policyMap = new HashMap<String,String>(); private String brmsdependencyversion; private EntityManager em; + private boolean syncFlag = false; public BRMSPush(String propertiesFile, BackUpHandler handler) throws PolicyException{ Properties config = new Properties(); @@ -196,12 +200,17 @@ public class BRMSPush { throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryName property is missing from the property file "); } repName = repName.trim(); - repURL = config.getProperty("repositoryURL"); + String repURL = config.getProperty("repositoryURL"); if(repURL==null){ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file "); throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file "); } - repURL = repURL.trim(); + if(repURL.contains(",")){ + repURLs = new ArrayList<String>(Arrays.asList(repURL.trim().split(","))); + }else{ + repURLs = new ArrayList<>(); + repURLs.add(repURL); + } repUserName = config.getProperty("repositoryUsername"); repPassword = config.getProperty("repositoryPassword"); if(repUserName==null || repPassword==null){ @@ -216,6 +225,11 @@ public class BRMSPush { throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file "); } policyKeyID = policyKeyID.trim(); + String syncF = config.getProperty("sync", "false").trim(); + syncFlag = Boolean.parseBoolean(syncF); + if(syncFlag){ + PolicyLogger.info("SYNC Flag is turned ON. DB will be given Priority."); + } brmsdependencyversion = config.getProperty("brms.dependency.version"); if(brmsdependencyversion==null){ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "brmsdependencyversion property is missing from the property file, Using default Version."); @@ -440,6 +454,9 @@ public class BRMSPush { addToPolicy(name,selectedName); } + /* + * Add Policy to JMemory and DataBase. + */ private void addToPolicy(String policyName, String controllerName) { policyMap.put(policyName, controllerName); EntityTransaction et = em.getTransaction(); @@ -523,7 +540,11 @@ public class BRMSPush { String path = PROJECTSLOCATION+ File.separator + artifactId + File.separator + "src"+ File.separator+ "main" + File.separator + "resources" + File.separator+ "rules"; new File(path).mkdirs(); - f = new File(path + File.separator + fileName); + if(syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))){ + f = new File(path + File.separator + fileName); + }else{ + f = new File(path + File.separator + fileName); + } }else if(file.getName().endsWith("pom.xml")){ String path = PROJECTSLOCATION+ File.separator + artifactId; new File(path).mkdirs(); @@ -578,22 +599,35 @@ public class BRMSPush { private List<NexusArtifact> getArtifactFromNexus(String selectedName, String version) { final NexusClient client = new NexusRestClient(); - try { - client.connect(repURL.substring(0, repURL.indexOf(repURL.split(":[0-9]+\\/nexus")[1])), repUserName, repPassword); - final NexusArtifact template = new NexusArtifact(); - template.setGroupId(getGroupID(selectedName)); - template.setArtifactId(getArtifactID(selectedName)); - if(version!=null){ - template.setVersion(version); - } - return client.searchByGAV(template); - } catch (NexusClientException | NexusConnectionException | NullPointerException e) { - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. " +e.getMessage()); - } finally { + int i = 0 ; + boolean flag = false; + while(i<repURLs.size()){ try { - client.disconnect(); - } catch (NexusClientException | NexusConnectionException e) { - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus." +e.getMessage()); + String repURL = repURLs.get(0); + client.connect(repURL.substring(0, repURL.indexOf(repURL.split(":[0-9]+\\/nexus")[1])), repUserName, repPassword); + final NexusArtifact template = new NexusArtifact(); + template.setGroupId(getGroupID(selectedName)); + template.setArtifactId(getArtifactID(selectedName)); + if(version!=null){ + template.setVersion(version); + } + List<NexusArtifact> resultList = client.searchByGAV(template); + if(resultList!=null){ + flag = true; + return resultList; + } + } catch (NexusClientException | NexusConnectionException | NullPointerException e) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. " +e.getMessage()); + } finally { + try { + client.disconnect(); + } catch (NexusClientException | NexusConnectionException e) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus." +e.getMessage()); + } + if(!flag){ + Collections.rotate(repURLs, -1); + i++; + } } } return new ArrayList<NexusArtifact>(); @@ -637,8 +671,9 @@ public class BRMSPush { * Will Push policies to the PolicyRepo. * * @param notificationType <String> type of notification Type. + * @throws PolicyException */ - public void pushRules(){ + public void pushRules() throws PolicyException{ // Check how many groups have been updated. // Invoke their Maven process. try { @@ -651,6 +686,7 @@ public class BRMSPush { if(!modifiedGroups.isEmpty()){ Boolean flag = false; for(String group: modifiedGroups.keySet()){ + InvocationResult result = null; try{ InvocationRequest request = new DefaultInvocationRequest(); setVersion(group); @@ -658,24 +694,26 @@ public class BRMSPush { request.setPomFile(new File(PROJECTSLOCATION+File.separator+getArtifactID(group)+File.separator+"pom.xml")); request.setGoals(Arrays.asList(GOALS)); Invoker invoker = new DefaultInvoker(); - InvocationResult result = invoker.execute(request); + result = invoker.execute(request); if(result.getExecutionException()!=null){ LOGGER.error(result.getExecutionException()); }else if(result.getExitCode()!=0){ LOGGER.error("Maven Invocation failure..!"); } - if(result.getExitCode()==0){ - LOGGER.info("Build Completed..!"); - if (createFlag) { - addNotification(group, "create"); - }else{ - addNotification(group, modifiedGroups.get(group)); - } - flag = true; - } }catch(Exception e){ LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Maven Invocation issue for "+getArtifactID(group) + e.getMessage()); } + if(result!=null && result.getExitCode()==0){ + LOGGER.info("Build Completed..!"); + if (createFlag) { + addNotification(group, "create"); + }else{ + addNotification(group, modifiedGroups.get(group)); + } + flag = true; + }else{ + throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!"); + } } if(flag){ sendNotification(controllers); @@ -799,10 +837,10 @@ public class BRMSPush { DeploymentRepository repository = new DeploymentRepository(); repository.setId(repID); repository.setName(repName); - repository.setUrl(repURL); + repository.setUrl(repURLs.get(0)); distributionManagement.setRepository(repository); model.setDistributionManagement(distributionManagement); - // Depenendency Mangement goes here. + // Dependency Management goes here. List<Dependency> dependencyList= new ArrayList<Dependency>(); if(groupMap.get(name).size()>1){ @SuppressWarnings("unchecked") @@ -1007,6 +1045,7 @@ public class BRMSPush { File file = new File(ruleFolder+File.separator+ policyName +".drl"); if(file.delete()){ LOGGER.info("Deleted File.. " + file.getAbsolutePath()); + removePolicyFromGroup(policyName, controllerName); } if(new File(ruleFolder).listFiles().length == 0) { removedRuleModifiedGroup(controllerName); @@ -1016,6 +1055,26 @@ public class BRMSPush { } } + // Removes Policy from Memory and Database. + private void removePolicyFromGroup(String policyName, String controllerName) { + policyMap.remove(policyName); + EntityTransaction et = em.getTransaction(); + et.begin(); + Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn"); + query.setParameter("pn", policyName); + List<?> pList = query.getResultList(); + BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo(); + if(pList.size()>0){ + // Already exists. + brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0); + if(brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)){ + em.remove(brmsPolicyInfo); + em.flush(); + } + } + et.commit(); + } + private void setVersion(String newVersion, String controllerName) { PEDependency userController = (PEDependency) groupMap.get(controllerName).get(0); userController.setVersion(newVersion); @@ -1026,4 +1085,16 @@ public class BRMSPush { public static BackUpMonitor getBackUpMonitor(){ return bm; } + + public void rotateURLs() { + if(repURLs!=null){ + Collections.rotate(repURLs, -1); + } + } + + public int URLListSize() { + if(repURLs!=null){ + return repURLs.size(); + }else return 0; + } } |