X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=BRMSGateway%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2FbrmsInterface%2FBRMSPush.java;h=ac1d0ac873601d636ce16c58127a37db76bfcdcf;hb=f36850d62ae77760daef9972a3c31042ad19480d;hp=f6f7c12149d6059f313524d40af631066546a8da;hpb=7ea81288603904f0cb6f57936da44d6a3dd521a6;p=policy%2Fengine.git diff --git a/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSPush.java b/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSPush.java index f6f7c1214..ac1d0ac87 100644 --- a/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSPush.java +++ b/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSPush.java @@ -73,7 +73,6 @@ import org.onap.policy.api.PolicyException; import org.onap.policy.brmsInterface.jpa.BRMSGroupInfo; import org.onap.policy.brmsInterface.jpa.BRMSPolicyInfo; import org.onap.policy.brmsInterface.jpa.DependencyInfo; -import org.onap.policy.common.im.AdministrativeStateException; import org.onap.policy.common.im.IntegrityMonitor; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; @@ -104,11 +103,21 @@ import com.fasterxml.jackson.core.JsonProcessingException; @SuppressWarnings("deprecation") public class BRMSPush { - private static final Logger LOGGER = FlexLogger.getLogger(BRMSPush.class.getName()); + private static final String GROUP_NAMES = "groupNames"; + private static final String DROOLS_APPS_TEMPLATE_GROUP = "org.onap.policy.drools-applications.controlloop.templates"; + private static final String DROOLS_APPS_MODEL_GROUP = "org.onap.policy.drools-applications.controlloop.common.model-impl"; + private static final String META_INF = "META-INF"; + private static final String KMODULE_XML_FILE = "kmodule.xml"; + private static final String POM_XML_FILE = "pom.xml"; + private static final String VERSION_0_1_0 = "0.1.0"; + private static final String RULES = "rules"; + private static final String RESOURCES = "resources"; + private static final Logger LOGGER = FlexLogger.getLogger(BRMSPush.class.getName()); private static final String PROJECTSLOCATION = "RuleProjects"; private static final String[] GOALS = { "clean", "deploy" }; - private static final String DEFAULT_VERSION = "1.1.0-SNAPSHOT"; + private static final String DEFAULT_VERSION = "1.2.0-SNAPSHOT"; private static final String DEPENDENCY_FILE = "dependency.json"; + private static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml"; private static Map modifiedGroups = new HashMap<>(); private static IntegrityMonitor im; @@ -137,6 +146,9 @@ public class BRMSPush { private boolean syncFlag = false; public BRMSPush(String propertiesFile, BackUpHandler handler) throws PolicyException { + if(propertiesFile==null || handler==null){ + throw new PolicyException("Error no propertiesFile or handler"); + } Properties config = new Properties(); Path file = Paths.get(propertiesFile); if (Files.notExists(file)) { @@ -185,7 +197,11 @@ public class BRMSPush { } catch (Exception e) { LOGGER.error("Error starting BackUpMonitor: " + e); } - config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml"); + if(!config.containsKey(BRMSPERSISTENCE)){ + config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml"); + } else { + config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, config.getProperty(BRMSPERSISTENCE,"META-INF/persistenceBRMS.xml")); + } EntityManagerFactory emf = Persistence.createEntityManagerFactory("BRMSGW", config); em = emf.createEntityManager(); defaultName = config.getProperty("defaultName"); @@ -378,18 +394,20 @@ public class BRMSPush { // Pick selected Value String userControllerName = null; ArrayList userDependencies = new ArrayList<>(); - for (String key : responseAttributes.keySet()) { + for (Map.Entry entry: responseAttributes.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); if (key.equals(policyKeyID)) { - selectedName = responseAttributes.get(key); + selectedName = value; } // kmodule configurations - else if (key.equals("kSessionName")) { - kSessionName = responseAttributes.get(key); + else if ("kSessionName".equals(key)) { + kSessionName = value; } // Check User Specific values. - if (key.equals("$controller:")) { + if ("$controller:".equals(key)) { try { - PEDependency dependency = PolicyUtils.jsonStringToObject(responseAttributes.get(key), + PEDependency dependency = PolicyUtils.jsonStringToObject(value, PEDependency.class); userControllerName = key.replaceFirst("$controller:", ""); addToGroup(userControllerName, dependency); @@ -397,19 +415,16 @@ public class BRMSPush { LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e); } - } else if (key.equals("$dependency$")) { - String value = responseAttributes.get(key); - if (value.startsWith("[") && value.endsWith("]")) { - value = value.substring(1, value.length() - 1).trim(); - List dependencyStrings = Arrays.asList(value.split("},{")); - for (String dependencyString : dependencyStrings) { - try { - userDependencies - .add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class)); - } catch (Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW - + "Error while resolving Dependencies: " + e); - } + } else if ("$dependency$".equals(key) && value.startsWith("[") && value.endsWith("]")) { + value = value.substring(1, value.length() - 1).trim(); + List dependencyStrings = Arrays.asList(value.split("},{")); + for (String dependencyString : dependencyStrings) { + try { + userDependencies + .add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class)); + } catch (Exception e) { + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + + "Error while resolving Dependencies: " + e); } } } @@ -476,9 +491,9 @@ public class BRMSPush { // Check if the Project is in Sync. If not get the latest Version. syncProject(selectedName); createProject(PROJECTSLOCATION + File.separator + getArtifactID(selectedName) + File.separator + "src" - + File.separator + "main" + File.separator + "resources", kSessionName); + + File.separator + "main" + File.separator + RESOURCES, kSessionName); copyDataToFile(PROJECTSLOCATION + File.separator + getArtifactID(selectedName) + File.separator + "src" - + File.separator + "main" + File.separator + "resources" + File.separator + "rules" + File.separator + + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES + File.separator + name + ".drl", rule); addToPolicy(name, selectedName); } @@ -493,23 +508,23 @@ public class BRMSPush { Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn"); query.setParameter("pn", policyName); List pList = query.getResultList(); - boolean createFlag = false; + boolean create = false; BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo(); - if (pList.size() > 0) { + if (!pList.isEmpty()) { // Already exists. brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0); if (!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) { - createFlag = true; + create = true; } } else { - createFlag = true; + create = true; } - if (createFlag) { + if (create) { query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn"); query.setParameter("cn", controllerName); List bList = query.getResultList(); BRMSGroupInfo brmsGroupInfo = new BRMSGroupInfo(); - if (bList.size() > 0) { + if (!bList.isEmpty()) { brmsGroupInfo = (BRMSGroupInfo) bList.get(0); } brmsPolicyInfo.setPolicyName(policyName); @@ -523,12 +538,12 @@ public class BRMSPush { private void syncProject(String selectedName) { boolean projectExists = checkProject(selectedName); if (projectExists) { - String version = null; + String version; version = getVersion(selectedName); if (version == null) { LOGGER.error("Error getting local version for the given Controller Name:" + selectedName + " going with Default value"); - version = "0.1.0"; + version = VERSION_0_1_0; } String nextVersion = incrementVersion(version); boolean outOfSync = checkRemoteSync(selectedName, nextVersion); @@ -569,20 +584,20 @@ public class BRMSPush { String fileName = file.getName().substring(file.getName().lastIndexOf("/") + 1); if (file.getName().endsWith(".drl")) { String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator - + "main" + File.separator + "resources" + File.separator + "rules"; + + "main" + File.separator + RESOURCES + File.separator + RULES; new File(path).mkdirs(); 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")) { + } else if (file.getName().endsWith(POM_XML_FILE)) { String path = PROJECTSLOCATION + File.separator + artifactId; new File(path).mkdirs(); f = new File(path + File.separator + fileName); - } else if (file.getName().endsWith("kmodule.xml")) { + } else if (file.getName().endsWith(KMODULE_XML_FILE)) { String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator - + "main" + File.separator + "resources" + File.separator + "META-INF"; + + "main" + File.separator + RESOURCES + File.separator + META_INF; new File(path).mkdirs(); f = new File(path + File.separator + fileName); } @@ -594,7 +609,6 @@ public class BRMSPush { } fos.close(); is.close(); - f = null; LOGGER.info(fileName + " Created.."); } } @@ -626,6 +640,9 @@ public class BRMSPush { // Additional Check due to Limitations from Nexus API to check if the artifact is the latest. private NexusArtifact additionalNexusLatestCheck(String selectedName, NexusArtifact result) { + if(result==null){ + return result; + } String nextVersion = incrementVersion(result.getVersion()); List artifact = getArtifactFromNexus(selectedName, nextVersion); return artifact.isEmpty() ? result : additionalNexusLatestCheck(selectedName, artifact.get(0)); @@ -633,7 +650,7 @@ public class BRMSPush { private boolean checkRemoteSync(String selectedName, String version) { List artifacts = getArtifactFromNexus(selectedName, version); - return (artifacts.size() == 0) ? false : true; + return artifacts.isEmpty() ? false : true; } private List getArtifactFromNexus(String selectedName, String version) { @@ -676,13 +693,13 @@ public class BRMSPush { } private void setVersion(String selectedName) { - String newVersion = "0.1.0"; + String newVersion = VERSION_0_1_0; createFlag = false; NexusArtifact artifact = getLatestArtifactFromNexus(selectedName); if (artifact != null) { newVersion = incrementVersion(artifact.getVersion()); } - if (newVersion.equals("0.1.0")) { + if (VERSION_0_1_0.equals(newVersion)) { createFlag = true; } setVersion(newVersion, selectedName); @@ -721,21 +738,20 @@ public class BRMSPush { // Invoke their Maven process. try { im.startTransaction(); - } catch (AdministrativeStateException e) { - LOGGER.error("Error while starting Transaction " + e); } catch (Exception e) { LOGGER.error("Error while starting Transaction " + e); } if (!modifiedGroups.isEmpty()) { Boolean flag = false; - for (String group : modifiedGroups.keySet()) { + for (Map.Entry entry : modifiedGroups.entrySet()) { InvocationResult result = null; + String group = entry.getKey(); try { InvocationRequest request = new DefaultInvocationRequest(); setVersion(group); createPom(group); request.setPomFile(new File( - PROJECTSLOCATION + File.separator + getArtifactID(group) + File.separator + "pom.xml")); + PROJECTSLOCATION + File.separator + getArtifactID(group) + File.separator + POM_XML_FILE)); request.setGoals(Arrays.asList(GOALS)); Invoker invoker = new DefaultInvoker(); result = invoker.execute(request); @@ -753,7 +769,7 @@ public class BRMSPush { if (createFlag) { addNotification(group, "create"); } else { - addNotification(group, modifiedGroups.get(group)); + addNotification(group, entry.getValue()); } flag = true; } else { @@ -787,7 +803,7 @@ public class BRMSPush { return policyMap.get(name); } else { syncGroupInfo(); - return (policyMap.containsKey(name)) ? policyMap.get(name) : null; + return policyMap.containsKey(name) ? policyMap.get(name) : null; } } @@ -819,9 +835,9 @@ public class BRMSPush { ControllerPOJO controllerPOJO = new ControllerPOJO(); controllerPOJO.setName(controllerName); controllerPOJO.setOperation("lock"); - List controllers = new ArrayList<>(); - controllers.add(controllerPOJO); - sendNotification(controllers); + List controllerPojos = new ArrayList<>(); + controllerPojos.add(controllerPOJO); + sendNotification(controllerPojos); } private void sendNotification(List controllers) { @@ -861,7 +877,7 @@ public class BRMSPush { pub.send("MyPartitionKey", message); final List stuck = pub.close(uebDelay, TimeUnit.SECONDS); - if (stuck.size() > 0) { + if (!stuck.isEmpty()) { LOGGER.error(stuck.size() + " messages unsent"); } else { LOGGER.debug("Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic); @@ -900,7 +916,7 @@ public class BRMSPush { Writer writer = null; try { writer = WriterFactory.newXmlWriter( - new File(PROJECTSLOCATION + File.separator + getArtifactID(name) + File.separator + "pom.xml")); + new File(PROJECTSLOCATION + File.separator + getArtifactID(name) + File.separator + POM_XML_FILE)); MavenXpp3Writer pomWriter = new MavenXpp3Writer(); pomWriter.write(writer, model); } catch (Exception e) { @@ -943,59 +959,59 @@ public class BRMSPush { String version = StringEscapeUtils.escapeJava(brmsdependencyversion); Dependency demoDependency = new Dependency(); - demoDependency.setGroupId("org.onap.policy.drools-applications"); - demoDependency.setArtifactId("demo"); + demoDependency.setGroupId(DROOLS_APPS_TEMPLATE_GROUP); + demoDependency.setArtifactId("template.demo"); demoDependency.setVersion(version); dependencyList.add(demoDependency); Dependency controlloopDependency = new Dependency(); - controlloopDependency.setGroupId("org.onap.policy.drools-applications"); + controlloopDependency.setGroupId(DROOLS_APPS_MODEL_GROUP); controlloopDependency.setArtifactId("events"); controlloopDependency.setVersion(version); dependencyList.add(controlloopDependency); Dependency restDependency = new Dependency(); - restDependency.setGroupId("org.onap.policy.drools-applications"); - restDependency.setArtifactId("rest"); + restDependency.setGroupId(DROOLS_APPS_MODEL_GROUP); + restDependency.setArtifactId("controlloop.common.model-impl.rest"); restDependency.setVersion(version); dependencyList.add(restDependency); Dependency appcDependency = new Dependency(); - appcDependency.setGroupId("org.onap.policy.drools-applications"); - appcDependency.setArtifactId("appc"); + appcDependency.setGroupId(DROOLS_APPS_MODEL_GROUP); + appcDependency.setArtifactId("controlloop.common.model-impl.appc"); appcDependency.setVersion(version); dependencyList.add(appcDependency); Dependency aaiDependency = new Dependency(); - aaiDependency.setGroupId("org.onap.policy.drools-applications"); - aaiDependency.setArtifactId("aai"); + aaiDependency.setGroupId(DROOLS_APPS_MODEL_GROUP); + aaiDependency.setArtifactId("controlloop.common.model-impl.aai"); aaiDependency.setVersion(version); dependencyList.add(aaiDependency); Dependency msoDependency = new Dependency(); - msoDependency.setGroupId("org.onap.policy.drools-applications"); - msoDependency.setArtifactId("mso"); + msoDependency.setGroupId(DROOLS_APPS_MODEL_GROUP); + msoDependency.setArtifactId("controlloop.common.model-impl.so"); msoDependency.setVersion(version); dependencyList.add(msoDependency); Dependency trafficgeneratorDependency = new Dependency(); - trafficgeneratorDependency.setGroupId("org.onap.policy.drools-applications"); - trafficgeneratorDependency.setArtifactId("trafficgenerator"); + trafficgeneratorDependency.setGroupId(DROOLS_APPS_MODEL_GROUP); + trafficgeneratorDependency.setArtifactId("controlloop.common.model-impl.trafficgenerator"); trafficgeneratorDependency.setVersion(version); dependencyList.add(trafficgeneratorDependency); return dependencyList; } private void createProject(String path, String ksessionName) { - new File(path + File.separator + "rules").mkdirs(); - new File(path + File.separator + "META-INF").mkdirs(); - if (!Files.exists(Paths.get(path + File.separator + "META-INF" + File.separator + "kmodule.xml"))) { + new File(path + File.separator + RULES).mkdirs(); + new File(path + File.separator + META_INF).mkdirs(); + if (!Files.exists(Paths.get(path + File.separator + META_INF + File.separator + KMODULE_XML_FILE))) { // Hard coding XML for PDP Drools to accept our Rules. String xml = "" + "\n" + "" + "\n" + "" + "\n" + "" + "\n" + ""; - copyDataToFile(path + File.separator + "META-INF" + File.separator + "kmodule.xml", xml); + copyDataToFile(path + File.separator + META_INF + File.separator + KMODULE_XML_FILE, xml); } } @@ -1010,17 +1026,22 @@ public class BRMSPush { } private void readGroups(Properties config) throws PolicyException { - String[] groupNames = null; - if (config.getProperty("groupNames").contains(",")) { - groupNames = config.getProperty("groupNames").replaceAll(" ", "").split(","); + String[] groupNames; + String groupNamesError = "groupNames property is missing or empty from the property file "; + if (!config.containsKey(GROUP_NAMES) || config.getProperty(GROUP_NAMES)==null){ + throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + + groupNamesError); + } + if (config.getProperty(GROUP_NAMES).contains(",")) { + groupNames = config.getProperty(GROUP_NAMES).replaceAll(" ", "").split(","); } else { - groupNames = new String[] { config.getProperty("groupNames").replaceAll(" ", "") }; + groupNames = new String[] { config.getProperty(GROUP_NAMES).replaceAll(" ", "") }; } if (groupNames == null || groupNames.length == 0) { LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE - + "groupNames property is missing or empty from the property file "); + + groupNamesError); throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE - + "groupNames property is missing or empty from the property file "); + + groupNamesError); } groupMap = new HashMap<>(); for (int counter = 0; counter < groupNames.length; counter++) { @@ -1057,7 +1078,7 @@ public class BRMSPush { query.setParameter("cn", name); List groupList = query.getResultList(); BRMSGroupInfo brmsGroupInfo = null; - if (groupList.size() > 0) { + if (!groupList.isEmpty()) { LOGGER.info("Controller name already Existing in DB. Will be updating the DB Values" + name); brmsGroupInfo = (BRMSGroupInfo) groupList.get(0); } @@ -1088,7 +1109,7 @@ public class BRMSPush { private void getNameAndSetRemove(String controllerName, String policyName) { String artifactName = getArtifactID(controllerName); String ruleFolder = PROJECTSLOCATION + File.separator + artifactName + File.separator + "src" + File.separator - + "main" + File.separator + "resources" + File.separator + "rules"; + + "main" + File.separator + RESOURCES + File.separator + RULES; File file = new File(ruleFolder + File.separator + policyName + ".drl"); if (file.delete()) { LOGGER.info("Deleted File.. " + file.getAbsolutePath()); @@ -1110,8 +1131,8 @@ public class BRMSPush { 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) { + BRMSPolicyInfo brmsPolicyInfo; + if (!pList.isEmpty()) { // Already exists. brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0); if (brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) { @@ -1139,7 +1160,7 @@ public class BRMSPush { } } - public int URLListSize() { + public int urlListSize() { if (repURLs != null) { return repURLs.size(); } else