X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=BRMSGateway%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fbrms%2Fapi%2FBrmsPush.java;h=2aa2873f1a40bba98bbd03f6065481c8882fa6af;hp=5a7b25ec3bd5ed58c6165c8d2cd88b008acaf112;hb=31aa90351064401a6ef4aaea9304b745969004e0;hpb=18d1759edf0805d060417f70cf162e56a71a4c27 diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java index 5a7b25ec3..2aa2873f1 100644 --- a/BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java +++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java @@ -2,14 +2,16 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -24,7 +26,6 @@ import com.att.nsa.cambria.client.CambriaBatchingPublisher; import com.att.nsa.cambria.client.CambriaClientBuilders; import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder; import com.fasterxml.jackson.core.JsonProcessingException; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -52,13 +53,12 @@ import java.util.concurrent.TimeUnit; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Pattern; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; -import javax.persistence.Query; - +import javax.persistence.TypedQuery; +import javax.ws.rs.ProcessingException; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringEscapeUtils; import org.apache.maven.model.Dependency; @@ -76,6 +76,10 @@ import org.codehaus.plexus.util.WriterFactory; import org.eclipse.persistence.config.PersistenceUnitProperties; import org.onap.policy.api.PEDependency; import org.onap.policy.api.PolicyException; +import org.onap.policy.brms.api.nexus.NexusRestSearchParameters; +import org.onap.policy.brms.api.nexus.NexusRestWrapper; +import org.onap.policy.brms.api.nexus.NexusRestWrapperException; +import org.onap.policy.brms.api.nexus.pojo.NexusArtifact; import org.onap.policy.brms.entity.BrmsGroupInfo; import org.onap.policy.brms.entity.BrmsPolicyInfo; import org.onap.policy.brms.entity.DependencyInfo; @@ -87,28 +91,23 @@ import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.utils.BackUpHandler; import org.onap.policy.utils.BackUpMonitor; import org.onap.policy.utils.BusPublisher; +import org.onap.policy.utils.PeCryptoUtils; import org.onap.policy.utils.PolicyUtils; import org.onap.policy.xacml.api.XACMLErrorConstants; -import org.sonatype.nexus.client.NexusClient; -import org.sonatype.nexus.client.NexusClientException; -import org.sonatype.nexus.client.NexusConnectionException; -import org.sonatype.nexus.client.rest.NexusRestClient; -import org.sonatype.nexus.rest.model.NexusArtifact; /** * BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR). * Mavenize and push policy to PR - * + * * @version 1.0 */ -@SuppressWarnings("deprecation") public class BrmsPush { 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"; + "org.onap.policy.models.policy-models-interactions.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"; @@ -118,9 +117,10 @@ public class BrmsPush { 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.2.1-SNAPSHOT"; + private static final String DEFAULT_VERSION = "1.5.1"; private static final String DEPENDENCY_FILE = "dependency.json"; - private static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml"; + private static final String PROP_AES_KEY = "org.onap.policy.encryption.aes.key"; + public static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml"; private static Map modifiedGroups = new HashMap<>(); private static IntegrityMonitor im; @@ -150,7 +150,7 @@ public class BrmsPush { /** * Responsible to push policies to the BRMS PDP Policy Repository (PR). - * + * * @param propertiesFile the properties file * @param handler the {@link BackUpHandler} * @throws PolicyException PolicyException related to the operation @@ -187,6 +187,9 @@ public class BrmsPush { throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file."); } + // init the aes key from prop or env + PeCryptoUtils.initAesKey(config.getProperty(PROP_AES_KEY)); + LOGGER.info("Trying to set up IntegrityMonitor"); String resourceName = null; try { @@ -254,7 +257,7 @@ public class BrmsPush { repUrlList.add(repUrl); } repUserName = config.getProperty("repositoryUsername"); - repPassword = config.getProperty("repositoryPassword"); + repPassword = PeCryptoUtils.decrypt(config.getProperty("repositoryPassword")); if (repUserName == null || repPassword == null) { LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repostoryUserName and respositoryPassword properties are required."); @@ -341,7 +344,7 @@ public class BrmsPush { uebList = uebList.trim(); pubTopic = pubTopic.trim(); pubBuilder = new CambriaClientBuilders.PublisherBuilder(); - pubBuilder.usingHosts(uebList).onTopic(pubTopic); + pubBuilder.usingHosts(uebList).onTopic(pubTopic).usingHttps(true); String apiKey = config.getProperty("UEB_API_KEY"); String apiSecret = config.getProperty("UEB_API_SECRET"); if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) { @@ -414,7 +417,7 @@ public class BrmsPush { final ArrayList userDependencies = new ArrayList<>(); for (final Map.Entry entry : responseAttributes.entrySet()) { final String key = entry.getKey(); - String value = entry.getValue(); + final String value = entry.getValue(); if (key.equals(policyKeyId)) { selectedName = value; } @@ -424,27 +427,9 @@ public class BrmsPush { } // Check User Specific values. if ("$controller:".equals(key)) { - try { - final PEDependency dependency = PolicyUtils.jsonStringToObject(value, PEDependency.class); - userControllerName = key.replaceFirst("$controller:", ""); - LOGGER.info("addRule: userControllerName - " + userControllerName + ", dependency: - " - + dependency); - addToGroup(userControllerName, dependency); - } catch (final Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e); - } - + userControllerName = getUserControllerName(key, value); } else if ("$dependency$".equals(key) && value.startsWith("[") && value.endsWith("]")) { - value = value.substring(1, value.length() - 1).trim(); - final List dependencyStrings = Arrays.asList(value.split(Pattern.quote("},{"))); - for (final String dependencyString : dependencyStrings) { - try { - userDependencies.add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class)); - } catch (final Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Dependencies: " - + e); - } - } + updateUserDependencies(userDependencies, value); } } if (userControllerName != null) { @@ -479,34 +464,71 @@ public class BrmsPush { } } + private String getUserControllerName(final String key, final String value) { + String userControllerName = null; + // Check User Specific values. + try { + final PEDependency dependency = PolicyUtils.jsonStringToObject(value, PEDependency.class); + userControllerName = key.replaceFirst("$controller:", ""); + LOGGER.info("addRule: userControllerName - " + userControllerName + ", dependency: - " + dependency); + addToGroup(userControllerName, dependency); + } catch (final Exception e) { + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e); + } + return userControllerName; + } + + private void updateUserDependencies(final ArrayList userDependencies, String value) { + // update the user dependencies supplied as parameter to this method + value = value.substring(1, value.length() - 1).trim(); + final List dependencyStrings = Arrays.asList(value.split(Pattern.quote("},{"))); + for (final String dependencyString : dependencyStrings) { + try { + userDependencies.add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class)); + } catch (final Exception e) { + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Dependencies: " + e); + } + } + } + private void syncGroupInfo() { // Sync DB to JMemory. final EntityTransaction et = em.getTransaction(); - et.begin(); - Query query = em.createQuery("select b from BrmsGroupInfo AS b"); - List result = query.getResultList(); - if (result.size() != groupMap.size()) { - for (final Object value : result) { - final BrmsGroupInfo brmsGroupInfo = (BrmsGroupInfo) value; - final PEDependency dependency = new PEDependency(); - dependency.setArtifactId(brmsGroupInfo.getArtifactId()); - dependency.setGroupId(brmsGroupInfo.getGroupId()); - dependency.setVersion(brmsGroupInfo.getVersion()); - final ArrayList values = new ArrayList<>(); - values.add(dependency); - groupMap.put(brmsGroupInfo.getControllerName(), values); + try { + et.begin(); + final TypedQuery groupInfoQuery = + em.createQuery("select b from BrmsGroupInfo AS b", BrmsGroupInfo.class); + final List groupInfoResult = groupInfoQuery.getResultList(); + if (groupInfoResult.size() != groupMap.size()) { + for (final BrmsGroupInfo brmsGroupInfo : groupInfoResult) { + final PEDependency dependency = new PEDependency(); + dependency.setArtifactId(brmsGroupInfo.getArtifactId()); + dependency.setGroupId(brmsGroupInfo.getGroupId()); + dependency.setVersion(brmsGroupInfo.getVersion()); + final ArrayList values = new ArrayList<>(); + values.add(dependency); + groupMap.put(brmsGroupInfo.getControllerName(), values); + } } - } - query = em.createQuery("select g from BrmsPolicyInfo AS g"); - result = query.getResultList(); - if (result.size() != policyMap.size()) { - for (final Object value : result) { - final BrmsPolicyInfo brmsPolicyInfo = (BrmsPolicyInfo) value; - policyMap.put(brmsPolicyInfo.getPolicyName(), brmsPolicyInfo.getControllerName().getControllerName()); + + final TypedQuery policyInfoQuery = + em.createQuery("select g from BrmsPolicyInfo AS g", BrmsPolicyInfo.class); + final List policyInfoResult = policyInfoQuery.getResultList(); + if (policyInfoResult.size() != policyMap.size()) { + for (final BrmsPolicyInfo brmsPolicyInfo : policyInfoResult) { + policyMap.put(brmsPolicyInfo.getPolicyName(), + brmsPolicyInfo.getControllerName().getControllerName()); + } + } + et.commit(); + LOGGER.info("Updated Local Memory values with values from database."); + } catch (final Exception exception) { + LOGGER.error("Unable to sync group info", exception); + if (et.isActive()) { + et.rollback(); } + } - et.commit(); - LOGGER.info("Updated Local Memory values with values from database."); } private void manageProject(final String selectedName, final String ksessionName, final String name, @@ -525,37 +547,46 @@ public class BrmsPush { * Add Policy to JMemory and DataBase. */ private void addToPolicy(final String policyName, final String controllerName) { - policyMap.put(policyName, controllerName); + final EntityTransaction et = em.getTransaction(); - et.begin(); - Query query = em.createQuery("select b from BrmsPolicyInfo as b where b.policyName = :pn"); - query.setParameter("pn", policyName); - final List pList = query.getResultList(); - boolean create = false; - BrmsPolicyInfo brmsPolicyInfo = new BrmsPolicyInfo(); - if (!pList.isEmpty()) { - // Already exists. - brmsPolicyInfo = (BrmsPolicyInfo) pList.get(0); - if (!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) { + try { + et.begin(); + boolean create = false; + final TypedQuery policyInfoQuery = + em.createQuery("select b from BrmsPolicyInfo as b where b.policyName = :pn", BrmsPolicyInfo.class); + policyInfoQuery.setParameter("pn", policyName); + final List policyInfoResultList = policyInfoQuery.getResultList(); + BrmsPolicyInfo brmsPolicyInfo = new BrmsPolicyInfo(); + if (!policyInfoResultList.isEmpty()) { + // Already exists. + brmsPolicyInfo = policyInfoResultList.get(0); + if (!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) { + create = true; + } + } else { create = true; } - } else { - create = true; - } - if (create) { - query = em.createQuery("select b from BrmsGroupInfo as b where b.controllerName = :cn"); - query.setParameter("cn", controllerName); - final List bList = query.getResultList(); - BrmsGroupInfo brmsGroupInfo = new BrmsGroupInfo(); - if (!bList.isEmpty()) { - brmsGroupInfo = (BrmsGroupInfo) bList.get(0); + if (create) { + final TypedQuery groupInfoQuery = em.createQuery( + "select b from BrmsGroupInfo as b where b.controllerName = :cn", BrmsGroupInfo.class); + groupInfoQuery.setParameter("cn", controllerName); + final List groupInfoResultList = groupInfoQuery.getResultList(); + BrmsGroupInfo brmsGroupInfo = new BrmsGroupInfo(); + if (!groupInfoResultList.isEmpty()) { + brmsGroupInfo = groupInfoResultList.get(0); + } + brmsPolicyInfo.setPolicyName(policyName); + brmsPolicyInfo.setControllerName(brmsGroupInfo); + em.persist(brmsPolicyInfo); + em.flush(); } - brmsPolicyInfo.setPolicyName(policyName); - brmsPolicyInfo.setControllerName(brmsGroupInfo); - em.persist(brmsPolicyInfo); - em.flush(); + et.commit(); + + policyMap.put(policyName, controllerName); + } catch (final Exception exception) { + LOGGER.error("Unable add policy to database", exception); + et.rollback(); } - et.commit(); } private void syncProject(final String selectedName) { @@ -587,7 +618,7 @@ public class BrmsPush { URL website; final String fileName = "rule.jar"; try { - website = new URL(artifact.getResourceURI()); + website = new URL(artifact.getUrlPath() + ".jar"); try (ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(fileName)) { fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); @@ -606,45 +637,48 @@ public class BrmsPush { try (JarFile jar = new JarFile(jarFileName)) { final Enumeration enumEntries = jar.entries(); while (enumEntries.hasMoreElements()) { - final JarEntry jarEntry = (JarEntry) enumEntries.nextElement(); - File file = null; - final String fileName = jarEntry.getName().substring(jarEntry.getName().lastIndexOf("/") + 1); - if (jarEntry.getName().endsWith(".drl")) { - final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" - + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES; - new File(path).mkdirs(); - if (syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))) { - file = new File(path + File.separator + fileName); - } else { - file = new File(path + File.separator + fileName); - } - } else if (jarEntry.getName().endsWith(POM_XML_FILE)) { - final String path = PROJECTSLOCATION + File.separator + artifactId; - new File(path).mkdirs(); - file = new File(path + File.separator + fileName); - } else if (jarEntry.getName().endsWith(KMODULE_XML_FILE)) { - final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" - + File.separator + "main" + File.separator + RESOURCES + File.separator + META_INF; - new File(path).mkdirs(); - file = new File(path + File.separator + fileName); - } - if (file != null) { - try (InputStream is = jar.getInputStream(jarEntry); - FileOutputStream fos = new FileOutputStream(file)) { - while (is.available() > 0) { - fos.write(is.read()); - } - LOGGER.info(fileName + " Created.."); - } catch (final IOException e) { - LOGGER.info("exception Occured" + e); - } - } + parseJarContents(artifactId, jar, enumEntries); } } catch (final IOException e) { LOGGER.info("exception Occured" + e); } } + private void parseJarContents(final String artifactId, final JarFile jar, final Enumeration enumEntries) { + final JarEntry jarEntry = (JarEntry) enumEntries.nextElement(); + File file = null; + final String fileName = jarEntry.getName().substring(jarEntry.getName().lastIndexOf("/") + 1); + if (jarEntry.getName().endsWith(".drl")) { + final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator + + "main" + File.separator + RESOURCES + File.separator + RULES; + new File(path).mkdirs(); + if (syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))) { + file = new File(path + File.separator + fileName); + } else { + file = new File(path + File.separator + fileName); + } + } else if (jarEntry.getName().endsWith(POM_XML_FILE)) { + final String path = PROJECTSLOCATION + File.separator + artifactId; + new File(path).mkdirs(); + file = new File(path + File.separator + fileName); + } else if (jarEntry.getName().endsWith(KMODULE_XML_FILE)) { + final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator + + "main" + File.separator + RESOURCES + File.separator + META_INF; + new File(path).mkdirs(); + file = new File(path + File.separator + fileName); + } + if (file != null) { + try (InputStream is = jar.getInputStream(jarEntry); FileOutputStream fos = new FileOutputStream(file)) { + while (is.available() > 0) { + fos.write(is.read()); + } + LOGGER.info(fileName + " Created.."); + } catch (final IOException e) { + LOGGER.info("exception Occured" + e); + } + } + } + private NexusArtifact getLatestArtifactFromNexus(final String selectedName) { final List artifacts = getArtifactFromNexus(selectedName, null); int bigNum = 0; @@ -685,34 +719,30 @@ public class BrmsPush { } private List getArtifactFromNexus(final String selectedName, final String version) { - final NexusClient client = new NexusRestClient(); + NexusRestWrapper restWrapper = null; int index = 0; boolean flag = false; while (index < repUrlList.size()) { try { final String repUrl = repUrlList.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); - } - final List resultList = client.searchByGAV(template); + restWrapper = + new NexusRestWrapper(repUrl.substring(0, repUrl.indexOf(repUrl.split(":[0-9]+\\/nexus")[1])), + repUserName, repPassword); + final NexusRestSearchParameters searchParameters = new NexusRestSearchParameters(); + searchParameters.useFilterSearch(getGroupId(selectedName), getArtifactId(selectedName), version, null, + null); + + final List resultList = restWrapper.findArtifact(searchParameters).getArtifactList(); if (resultList != null) { flag = true; return resultList; } - } catch (NexusClientException | NexusConnectionException | NullPointerException e) { + } catch (NexusRestWrapperException | ProcessingException e) { LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. " + e.getMessage(), e); } finally { - try { - client.disconnect(); - } catch (NexusClientException | NexusConnectionException e) { - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus." - + e.getMessage(), e); + if (null != restWrapper) { + restWrapper.close(); } if (!flag) { Collections.rotate(repUrlList, -1); @@ -745,7 +775,7 @@ public class BrmsPush { /** * Will Push policies to the PolicyRepo. - * + * * @throws PolicyException PolicyException related to the operation */ public void pushRules() throws PolicyException { @@ -757,42 +787,7 @@ public class BrmsPush { LOGGER.error("Error while starting Transaction " + e); } if (!modifiedGroups.isEmpty()) { - Boolean flag = false; - for (final Map.Entry entry : modifiedGroups.entrySet()) { - InvocationResult result = null; - final String group = entry.getKey(); - try { - LOGGER.info("PushRules: ModifiedGroups, Key: " + group + ", Value: " + entry.getValue()); - final InvocationRequest request = new DefaultInvocationRequest(); - setVersion(group); - createPom(group); - request.setPomFile(new File( - PROJECTSLOCATION + File.separator + getArtifactId(group) + File.separator + POM_XML_FILE)); - request.setGoals(Arrays.asList(GOALS)); - final Invoker invoker = new DefaultInvoker(); - result = invoker.execute(request); - if (result.getExecutionException() != null) { - LOGGER.error(result.getExecutionException()); - } else if (result.getExitCode() != 0) { - LOGGER.error("Maven Invocation failure..!"); - } - } catch (final Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation issue for " - + getArtifactId(group) + e.getMessage(), e); - } - if (result != null && result.getExitCode() == 0) { - LOGGER.info("Build Completed..!"); - if (createFlag) { - addNotification(group, "create"); - } else { - addNotification(group, entry.getValue()); - } - flag = true; - } else { - throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!"); - } - } - if (flag) { + if (buildAndGenerateJarFile()) { sendNotification(controllers); } } @@ -814,6 +809,45 @@ public class BrmsPush { getNameAndSetRemove(controllerName, name); } + private Boolean buildAndGenerateJarFile() throws PolicyException { + Boolean flag = false; + for (final Map.Entry entry : modifiedGroups.entrySet()) { + InvocationResult result = null; + final String group = entry.getKey(); + try { + LOGGER.info("PushRules: ModifiedGroups, Key: " + group + ", Value: " + entry.getValue()); + final InvocationRequest request = new DefaultInvocationRequest(); + setVersion(group); + createPom(group); + request.setPomFile(new File( + PROJECTSLOCATION + File.separator + getArtifactId(group) + File.separator + POM_XML_FILE)); + request.setGoals(Arrays.asList(GOALS)); + final Invoker invoker = new DefaultInvoker(); + result = invoker.execute(request); + if (result.getExecutionException() != null) { + LOGGER.error(result.getExecutionException()); + } else if (result.getExitCode() != 0) { + LOGGER.error("Maven Invocation failure..!"); + } + } catch (final Exception e) { + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation issue for " + + getArtifactId(group) + e.getMessage(), e); + } + if (result != null && result.getExitCode() == 0) { + LOGGER.info("Build Completed..!"); + if (createFlag) { + addNotification(group, "create"); + } else { + addNotification(group, entry.getValue()); + } + flag = true; + } else { + throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!"); + } + } + return flag; + } + private String getGroupName(final String name) { if (policyMap.containsKey(name)) { return policyMap.get(name); @@ -971,7 +1005,7 @@ public class BrmsPush { /** * Default Dependency Section. Can be changed as required. - * + * * @param controllerName the controller name * @return changed dependency list */ @@ -1015,12 +1049,6 @@ public class BrmsPush { msoDependency.setArtifactId("controlloop.common.model-impl.so"); msoDependency.setVersion(version); dependencyList.add(msoDependency); - - final Dependency trafficgeneratorDependency = new Dependency(); - trafficgeneratorDependency.setGroupId(DROOLS_APPS_MODEL_GROUP); - trafficgeneratorDependency.setArtifactId("controlloop.common.model-impl.trafficgenerator"); - trafficgeneratorDependency.setVersion(version); - dependencyList.add(trafficgeneratorDependency); return dependencyList; } @@ -1088,29 +1116,36 @@ public class BrmsPush { } private void addToGroup(final String name, final PEDependency dependency) { - final ArrayList values = new ArrayList<>(); - values.add(dependency); - groupMap.put(name, values); final EntityTransaction et = em.getTransaction(); - et.begin(); - final Query query = em.createQuery("select b from BrmsGroupInfo as b where b.controllerName = :cn"); - query.setParameter("cn", name); - final List groupList = query.getResultList(); - BrmsGroupInfo brmsGroupInfo = null; - if (!groupList.isEmpty()) { - LOGGER.info("Controller name already Existing in DB. Will be updating the DB Values" + name); - brmsGroupInfo = (BrmsGroupInfo) groupList.get(0); - } - if (brmsGroupInfo == null) { - brmsGroupInfo = new BrmsGroupInfo(); + try { + et.begin(); + final TypedQuery query = em + .createQuery("select b from BrmsGroupInfo as b where b.controllerName = :cn", BrmsGroupInfo.class); + query.setParameter("cn", name); + final List groupList = query.getResultList(); + BrmsGroupInfo brmsGroupInfo = null; + if (!groupList.isEmpty()) { + LOGGER.info("Controller name already Existing in DB. Will be updating the DB Values" + name); + brmsGroupInfo = groupList.get(0); + } + if (brmsGroupInfo == null) { + brmsGroupInfo = new BrmsGroupInfo(); + } + brmsGroupInfo.setControllerName(name); + brmsGroupInfo.setGroupId(dependency.getGroupId()); + brmsGroupInfo.setArtifactId(dependency.getArtifactId()); + brmsGroupInfo.setVersion(dependency.getVersion()); + em.persist(brmsGroupInfo); + em.flush(); + et.commit(); + + final ArrayList values = new ArrayList<>(); + values.add(dependency); + groupMap.put(name, values); + } catch (final Exception exception) { + LOGGER.error("Unable add/update policy group to database for controller name: " + name, exception); + et.rollback(); } - brmsGroupInfo.setControllerName(name); - brmsGroupInfo.setGroupId(dependency.getGroupId()); - brmsGroupInfo.setArtifactId(dependency.getArtifactId()); - brmsGroupInfo.setVersion(dependency.getVersion()); - em.persist(brmsGroupInfo); - em.flush(); - et.commit(); } private String getArtifactId(final String name) { @@ -1144,22 +1179,28 @@ public class BrmsPush { // Removes Policy from Memory and Database. private void removePolicyFromGroup(final String policyName, final String controllerName) { - policyMap.remove(policyName); final EntityTransaction et = em.getTransaction(); - et.begin(); - final Query query = em.createQuery("select b from BrmsPolicyInfo as b where b.policyName = :pn"); - query.setParameter("pn", policyName); - final List pList = query.getResultList(); - BrmsPolicyInfo brmsPolicyInfo; - if (!pList.isEmpty()) { - // Already exists. - brmsPolicyInfo = (BrmsPolicyInfo) pList.get(0); - if (brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) { - em.remove(brmsPolicyInfo); - em.flush(); + try { + et.begin(); + final TypedQuery query = + em.createQuery("select b from BrmsPolicyInfo as b where b.policyName = :pn", BrmsPolicyInfo.class); + query.setParameter("pn", policyName); + final List pList = query.getResultList(); + BrmsPolicyInfo brmsPolicyInfo; + if (!pList.isEmpty()) { + // Already exists. + brmsPolicyInfo = pList.get(0); + if (brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) { + em.remove(brmsPolicyInfo); + em.flush(); + } } + et.commit(); + policyMap.remove(policyName); + } catch (final Exception exception) { + LOGGER.error("Unable remove policy from group to database for policy name: " + policyName, exception); + et.rollback(); } - et.commit(); } private void setVersion(final String selectedName) { @@ -1198,7 +1239,7 @@ public class BrmsPush { /** * Get URL List Size. - * + * * @return URL list size */ public int urlListSize() {