X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=BRMSGateway%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fpolicy%2FbrmsInterface%2FBRMSPush.java;h=def57e2bc5ee3d719b3516d8dcd6638036afd97f;hb=e92ff832cf993db876f22b2d27562fedf59f5043;hp=60d764cb0b049553892f19662af56cbed6280f0b;hpb=a330af579866dacbe595e2e4ad1dd29cd3c96945;p=policy%2Fengine.git 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..def57e2bc 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; @@ -97,9 +99,10 @@ import com.fasterxml.jackson.core.JsonProcessingException; * BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR). * Mavenize and push policy to PR * - * @version 0.9 + * @version 1.0 */ +@SuppressWarnings("deprecation") public class BRMSPush { private static final Logger LOGGER = FlexLogger.getLogger(BRMSPush.class.getName()); private static final String PROJECTSLOCATION = "RuleProjects"; @@ -107,14 +110,14 @@ public class BRMSPush { private static final String DEFAULT_VERSION = "1.1.0-SNAPSHOT"; private static final String DEPENDENCY_FILE = "dependency.json"; - private static Map modifiedGroups = new HashMap(); + private static Map modifiedGroups = new HashMap<>(); private static IntegrityMonitor im; private static BackUpMonitor bm; private static String resourceName = null; private String defaultName = null; private String repID = null; private String repName = null; - private String repURL= null; + private ArrayList repURLs= null; private String repUserName = null; private String repPassword = null; private String policyKeyID = null; @@ -128,10 +131,11 @@ public class BRMSPush { private Long dmaapDelay = Long.parseLong("5000"); private String notificationType = null; private ArrayList controllers; - private HashMap> groupMap = new HashMap>(); - private Map policyMap = new HashMap(); + private HashMap> groupMap = new HashMap<>(); + private Map policyMap = new HashMap<>(); 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<>(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."); @@ -244,9 +258,9 @@ public class BRMSPush { pubTopic = pubTopic.trim(); if(dmaapServers.contains(",")) { - dmaapList = new ArrayList(Arrays.asList(dmaapServers.split("\\s*,\\s*"))); + dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*"))); } else { - dmaapList = new ArrayList(); + dmaapList = new ArrayList<>(); dmaapList.add(dmaapServers); } @@ -308,8 +322,8 @@ public class BRMSPush { * Will Initialize the variables required for BRMSPush. */ public void initiate(boolean flag) { - modifiedGroups = new HashMap(); - controllers = new ArrayList(); + modifiedGroups = new HashMap<>(); + controllers = new ArrayList<>(); try { bm.updateNotification(); } catch (Exception e) { @@ -332,7 +346,7 @@ public class BRMSPush { if(!responseAttributes.isEmpty()){ // Pick selected Value String userControllerName = null; - ArrayList userDependencies = new ArrayList(); + ArrayList userDependencies = new ArrayList<>(); for(String key: responseAttributes.keySet()){ if(key.equals(policyKeyID)){ selectedName = responseAttributes.get(key); @@ -407,7 +421,7 @@ public class BRMSPush { dependency.setArtifactId(brmsGroupInfo.getArtifactId()); dependency.setGroupId(brmsGroupInfo.getGroupId()); dependency.setVersion(brmsGroupInfo.getVersion()); - ArrayList values = new ArrayList(); + ArrayList values = new ArrayList<>(); values.add(dependency); groupMap.put(brmsGroupInfo.getControllerName(), values); } @@ -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(); @@ -568,7 +589,14 @@ public class BRMSPush { result = artifact; } } - return result; + return additionalNexusLatestCheck(selectedName, result); + } + + // Additional Check due to Limitations from Nexus API to check if the artifact is the latest. + private NexusArtifact additionalNexusLatestCheck(String selectedName, NexusArtifact result) { + String nextVersion = incrementVersion(result.getVersion()); + List artifact = getArtifactFromNexus(selectedName, nextVersion); + return artifact.isEmpty()? result: additionalNexusLatestCheck(selectedName, artifact.get(0)); } private boolean checkRemoteSync(String selectedName, String version) { @@ -578,25 +606,38 @@ public class BRMSPush { private List 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 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(); + return new ArrayList<>(); } private void setVersion(String selectedName) { @@ -637,8 +678,9 @@ public class BRMSPush { * Will Push policies to the PolicyRepo. * * @param notificationType 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 +693,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 +701,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); @@ -718,7 +763,7 @@ public class BRMSPush { ControllerPOJO controllerPOJO = new ControllerPOJO(); controllerPOJO.setName(controllerName); controllerPOJO.setOperation(operation); - HashMap drools = new HashMap(); + HashMap drools = new HashMap<>(); drools.put("groupId", getGroupID(controllerName)); drools.put("artifactId", getArtifactID(controllerName)); drools.put("version", getVersion(controllerName)); @@ -738,7 +783,7 @@ public class BRMSPush { ControllerPOJO controllerPOJO = new ControllerPOJO(); controllerPOJO.setName(controllerName); controllerPOJO.setOperation("lock"); - List controllers = new ArrayList(); + List controllers = new ArrayList<>(); controllers.add(controllerPOJO); sendNotification(controllers); } @@ -799,11 +844,11 @@ 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. - List dependencyList= new ArrayList(); + // Dependency Management goes here. + List dependencyList= new ArrayList<>(); if(groupMap.get(name).size()>1){ @SuppressWarnings("unchecked") ArrayList dependencies = (ArrayList) groupMap.get(name).get(1); @@ -842,7 +887,7 @@ public class BRMSPush { if(dependencyInfo.getDependencies().containsKey(controllerName)){ controller = controllerName; } - List dependencyList = new ArrayList(); + List dependencyList = new ArrayList<>(); for(PEDependency dependency: dependencyInfo.getDependencies().get(controller)){ dependencyList.add(dependency.getDependency()); } @@ -859,47 +904,47 @@ public class BRMSPush { // Default Dependency Section. Can be changed as required. public List defaultDependencies(String controllerName) { - List dependencyList = new ArrayList(); + List dependencyList = new ArrayList<>(); String version= StringEscapeUtils.escapeJava(brmsdependencyversion); Dependency demoDependency = new Dependency(); - demoDependency.setGroupId("org.openecomp.policy.drools-applications"); + demoDependency.setGroupId("org.onap.policy.drools-applications"); demoDependency.setArtifactId("demo"); demoDependency.setVersion(version); dependencyList.add(demoDependency); Dependency controlloopDependency = new Dependency(); - controlloopDependency.setGroupId("org.openecomp.policy.drools-applications"); - controlloopDependency.setArtifactId("controlloop"); + controlloopDependency.setGroupId("org.onap.policy.drools-applications"); + controlloopDependency.setArtifactId("events"); controlloopDependency.setVersion(version); dependencyList.add(controlloopDependency); Dependency restDependency = new Dependency(); - restDependency.setGroupId("org.openecomp.policy.drools-applications"); + restDependency.setGroupId("org.onap.policy.drools-applications"); restDependency.setArtifactId("rest"); restDependency.setVersion(version); dependencyList.add(restDependency); Dependency appcDependency = new Dependency(); - appcDependency.setGroupId("org.openecomp.policy.drools-applications"); + appcDependency.setGroupId("org.onap.policy.drools-applications"); appcDependency.setArtifactId("appc"); appcDependency.setVersion(version); dependencyList.add(appcDependency); Dependency aaiDependency = new Dependency(); - aaiDependency.setGroupId("org.openecomp.policy.drools-applications"); + aaiDependency.setGroupId("org.onap.policy.drools-applications"); aaiDependency.setArtifactId("aai"); aaiDependency.setVersion(version); dependencyList.add(aaiDependency); Dependency msoDependency = new Dependency(); - msoDependency.setGroupId("org.openecomp.policy.drools-applications"); + msoDependency.setGroupId("org.onap.policy.drools-applications"); msoDependency.setArtifactId("mso"); msoDependency.setVersion(version); dependencyList.add(msoDependency); Dependency trafficgeneratorDependency = new Dependency(); - trafficgeneratorDependency.setGroupId("org.openecomp.policy.drools-applications"); + trafficgeneratorDependency.setGroupId("org.onap.policy.drools-applications"); trafficgeneratorDependency.setArtifactId("trafficgenerator"); trafficgeneratorDependency.setVersion(version); dependencyList.add(trafficgeneratorDependency); @@ -939,7 +984,7 @@ public class BRMSPush { LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "groupNames property is missing or empty from the property file "); throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "groupNames property is missing or empty from the property file "); } - groupMap = new HashMap>(); + groupMap = new HashMap<>(); for(int counter=0; counter < groupNames.length ;counter++){ String name = groupNames[counter]; String groupID = config.getProperty(name+".groupID"); @@ -961,7 +1006,7 @@ public class BRMSPush { } private void addToGroup(String name, PEDependency dependency) { - ArrayList values = new ArrayList(); + ArrayList values = new ArrayList<>(); values.add(dependency); groupMap.put(name, values); EntityTransaction et = em.getTransaction(); @@ -1007,6 +1052,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 +1062,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 +1092,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; + } }