X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=BRMSGateway%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fbrms%2Fapi%2FBrmsPush.java;h=caf49aa555c7a67d013ad1304ef41cd67486e34d;hb=a7f30f3bdd7b3118e423243e6328c4c7ffe32244;hp=63b6d2c9a7d580026fcfad954c465738ad69a01b;hpb=4720126987f4b8408281621620209a9817b8c402;p=policy%2Fengine.git 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 63b6d2c9a..caf49aa55 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 @@ -57,7 +57,8 @@ 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; @@ -72,10 +73,15 @@ import org.apache.maven.shared.invoker.InvocationRequest; import org.apache.maven.shared.invoker.InvocationResult; import org.apache.maven.shared.invoker.Invoker; import org.codehaus.plexus.util.IOUtil; + 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; @@ -89,11 +95,6 @@ import org.onap.policy.utils.BackUpMonitor; import org.onap.policy.utils.BusPublisher; 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). @@ -102,7 +103,6 @@ import org.sonatype.nexus.rest.model.NexusArtifact; * @version 1.0 */ -@SuppressWarnings("deprecation") public class BrmsPush { private static final String GROUP_NAMES = "groupNames"; private static final String DROOLS_APPS_TEMPLATE_GROUP = @@ -118,7 +118,7 @@ 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.0-SNAPSHOT"; + private static final String DEFAULT_VERSION = "1.2.3"; private static final String DEPENDENCY_FILE = "dependency.json"; private static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml"; @@ -482,31 +482,39 @@ public class BrmsPush { 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); + et.rollback(); + throw exception; } - 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 +533,47 @@ 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(); + throw exception; } - et.commit(); } private void syncProject(final String selectedName) { @@ -587,7 +605,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); @@ -685,34 +703,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); @@ -1088,29 +1102,37 @@ 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(); + throw exception; } - 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 +1166,29 @@ 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(); + throw exception; } - et.commit(); } private void setVersion(final String selectedName) {