From 80f072f60509ef3a35369a60857fe05f6c2a993a Mon Sep 17 00:00:00 2001 From: "Tej, Tarun" Date: Mon, 21 Aug 2017 20:00:50 -0400 Subject: Fixes for sonar critical issues Fixes for critical and blocker issues reported in sonar. Issue-Id: POLICY-113 Change-Id: I50969fe93a94b0497f3fb30864a6c45e63208fe6 Signed-off-by: Tej, Tarun --- .../org/onap/policy/brmsInterface/BRMSGateway.java | 101 +- .../org/onap/policy/brmsInterface/BRMSHandler.java | 6 +- .../org/onap/policy/brmsInterface/BRMSPush.java | 1865 ++++++++++---------- 3 files changed, 1004 insertions(+), 968 deletions(-) (limited to 'BRMSGateway') diff --git a/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSGateway.java b/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSGateway.java index 0b4058d59..c4fcf5454 100644 --- a/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSGateway.java +++ b/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSGateway.java @@ -22,67 +22,60 @@ package org.onap.policy.brmsInterface; import org.onap.policy.api.NotificationScheme; import org.onap.policy.api.PolicyEngine; - -//import org.apache.log4j.Logger; - -//import org.apache.commons.logging.Log; -//import org.apache.commons.logging.LogFactory; - +import org.onap.policy.api.PolicyException; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; - import org.onap.policy.xacml.api.XACMLErrorConstants; - /** - * BRMSGateway: This application acts as the Gateway interface between the PDP XACML and PDP Drools. - * The listens for BRMS based policies and pushes them to the specified Policy Repository, from where the PDP Drools reads the Rule Jar. + * BRMSGateway: This application acts as the Gateway interface between the PDP XACML and PDP Drools. The listens for + * BRMS based policies and pushes them to the specified Policy Repository, from where the PDP Drools reads the Rule Jar. * * @version 0.1 */ public class BRMSGateway { - - private static final Logger logger = FlexLogger.getLogger(BRMSGateway.class); - private static final String configFile = "config.properties"; - - private static PolicyEngine policyEngine = null; - - public static void main(String[] args) throws Exception{ - // Initialize Handler. - logger.info("Initializing BRMS Handler"); - BRMSHandler bRMSHandler = null; - try{ - bRMSHandler = new BRMSHandler(configFile); - }catch(NullPointerException e){ - logger.error("Check your property file: " + e.getMessage()); - System.exit(1); - } - - // Set Handler with Auto Notification and initialize policyEngine - try{ - logger.info("Initializing policyEngine with Auto Notifications"); - policyEngine= new PolicyEngine(configFile,NotificationScheme.AUTO_ALL_NOTIFICATIONS, bRMSHandler); - }catch(Exception e){ - logger.error(XACMLErrorConstants.ERROR_UNKNOWN+"Error while Initializing Policy Engine " + e.getMessage()); - } - - //Keep Running.... - Runnable runnable = new Runnable(){ - public void run(){ - while (true){ - try { - Thread.sleep(30000); - } catch (InterruptedException e) { - logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Thread Exception " + e.getMessage()); - } - } - } - }; - Thread thread = new Thread(runnable); - thread.start(); - } - - public static PolicyEngine getPolicyEngine(){ - return policyEngine; - } + + private static final Logger logger = FlexLogger.getLogger(BRMSGateway.class); + private static final String CONFIGFILE = "config.properties"; + + private static PolicyEngine policyEngine = null; + + public static void main(String[] args) throws Exception { + // Initialize Handler. + logger.info("Initializing BRMS Handler"); + BRMSHandler bRMSHandler = null; + try { + bRMSHandler = new BRMSHandler(CONFIGFILE); + } catch (PolicyException e) { + logger.error("Check your property file: " + e.getMessage(), e); + System.exit(1); + } + + // Set Handler with Auto Notification and initialize policyEngine + try { + logger.info("Initializing policyEngine with Auto Notifications"); + policyEngine = new PolicyEngine(CONFIGFILE, NotificationScheme.AUTO_ALL_NOTIFICATIONS, bRMSHandler); + } catch (Exception e) { + logger.error(XACMLErrorConstants.ERROR_UNKNOWN + "Error while Initializing Policy Engine " + e.getMessage(), + e); + } + + // Keep Running.... + Runnable runnable = () -> { + while (true) { + try { + Thread.sleep(30000); + } catch (InterruptedException e) { + logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Thread Exception " + e.getMessage()); + Thread.currentThread().interrupt(); + } + } + }; + Thread thread = new Thread(runnable); + thread.start(); + } + + public static PolicyEngine getPolicyEngine() { + return policyEngine; + } } diff --git a/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSHandler.java b/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSHandler.java index 305f64de1..43031861b 100644 --- a/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSHandler.java +++ b/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSHandler.java @@ -91,7 +91,7 @@ public class BRMSHandler implements BackUpHandler{ bRMSPush.removeRule(removedPolicy.getPolicyName()); removed = true; }catch(Exception e){ - logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage()); + logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage(), e); } } } @@ -106,6 +106,7 @@ public class BRMSHandler implements BackUpHandler{ //Upon Notification failure failureFlag = true; bRMSPush.rotateURLs(); + logger.error("Failure during Push Operation " , e); } } i++; @@ -138,6 +139,7 @@ public class BRMSHandler implements BackUpHandler{ //Upon Notification failure successFlag = false; bRMSPush.rotateURLs(); + logger.error("Failure during Push Operation " , e); } } } @@ -171,7 +173,7 @@ public class BRMSHandler implements BackUpHandler{ } } }catch(Exception e){ - logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage()); + logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage(), e); } } } 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 c24493882..f6f7c1214 100644 --- a/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSPush.java +++ b/BRMSGateway/src/main/java/org/onap/policy/brmsInterface/BRMSPush.java @@ -96,670 +96,708 @@ import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder; 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 + * BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR). Mavenize and push policy + * to PR * - * @version 1.0 + * @version 1.0 */ @SuppressWarnings("deprecation") 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.1.0-SNAPSHOT"; - private static final String DEPENDENCY_FILE = "dependency.json"; - - 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 ArrayList repURLs= null; - private String repUserName = null; - private String repPassword = null; - private String policyKeyID = null; - private boolean createFlag = false; - private String uebList = null; - private List dmaapList = null; - private String pubTopic = null; - private PublisherBuilder pubBuilder = null; - protected BusPublisher publisher = null; - private Long uebDelay = Long.parseLong("20"); - private Long dmaapDelay = Long.parseLong("5000"); - private String notificationType = null; - private ArrayList controllers; - 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(); - Path file = Paths.get(propertiesFile); - if(Files.notExists(file)){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Config File doesn't Exist in the specified Path " + file.toString()); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE+"Config File doesn't Exist in the specified Path " + file.toString()); - }else{ - if(file.toString().endsWith(".properties")){ - // Grab the Properties. - setProperty(file, config, handler); - } - } - } - - private void setProperty(Path file, Properties config, BackUpHandler handler) throws PolicyException{ - InputStream in; - try { - in = new FileInputStream(file.toFile()); - config.load(in); - } catch (IOException e) { - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file."); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file."); - } - LOGGER.info("Trying to set up IntegrityMonitor"); - try { - LOGGER.info("Trying to set up IntegrityMonitor"); - resourceName = config.getProperty("RESOURCE_NAME"); - if(resourceName==null){ - LOGGER.warn("RESOURCE_NAME is missing setting default value. "); - resourceName = "brmsgw_pdp01"; - } - resourceName = resourceName.trim(); - im = IntegrityMonitor.getInstance(resourceName, config); - } catch (Exception e) { - LOGGER.error("Error starting Integerity Monitor: " + e); - } - LOGGER.info("Trying to set up BackUpMonitor"); - try { - bm = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), resourceName, config, handler); - } catch (Exception e) { - LOGGER.error("Error starting BackUpMonitor: " + e); - } - config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml"); - EntityManagerFactory emf = Persistence.createEntityManagerFactory("BRMSGW", config); - em = emf.createEntityManager(); - defaultName = config.getProperty("defaultName"); - if(defaultName==null){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file "); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file"); - } - defaultName = defaultName.trim(); - repID = config.getProperty("repositoryID"); - if(repID==null){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file "); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file "); - } - repID = repID.trim(); - repName = config.getProperty("repositoryName"); - if(repName==null){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryName property is missing from the property file "); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryName property is missing from the property file "); - } - repName = repName.trim(); - 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 "); - } - 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){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repostoryUserName and respositoryPassword properties are required."); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repostoryUserName and respositoryPassword properties are required."); - } - repUserName = repUserName.trim(); - repPassword = repPassword.trim(); - policyKeyID = config.getProperty("policyKeyID"); - if(policyKeyID==null){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file "); - 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."); - brmsdependencyversion = DEFAULT_VERSION; - } - brmsdependencyversion = brmsdependencyversion.trim(); - readGroups(config); - - // Setup Publisher - notificationType = config.getProperty("NOTIFICATION_TYPE"); - if("dmaap".equalsIgnoreCase(notificationType)){ - - LOGGER.info("Notification Type being used is DMaaP... creating instance of BusPublisher."); - // Setting up the Publisher for DMaaP MR - String dmaapServers = config.getProperty("NOTIFICATION_SERVERS"); - pubTopic = config.getProperty("NOTIFICATION_TOPIC"); - String aafLogin = config.getProperty("CLIENT_ID").trim(); - String aafPassword = config.getProperty("CLIENT_KEY").trim(); - - if(dmaapServers==null || pubTopic==null){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file "); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file "); - } - - dmaapServers = dmaapServers.trim(); - pubTopic = pubTopic.trim(); - - if(dmaapServers.contains(",")) { - dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*"))); - } else { - dmaapList = new ArrayList<>(); - dmaapList.add(dmaapServers); - } - - this.publisher = - new BusPublisher.DmaapPublisherWrapper(this.dmaapList, - this.pubTopic, - aafLogin, - aafPassword); - - - String dDelay = config.getProperty("NOTIFICATION_DELAY"); - if(dDelay!=null && !dDelay.isEmpty()){ - dDelay = dDelay.trim(); - try{ - dmaapDelay = Long.parseLong(dDelay); - }catch (NumberFormatException e){ - LOGGER.error("DMAAP_DELAY not a long format number" + e); - } - } - LOGGER.info("DMAAP BusPublisher is created."); - - } else { - LOGGER.info("Notification Type being used is UEB... creating instance of PublisherBuilder."); - // Setting up the Publisher for UEB - uebList = config.getProperty("NOTIFICATION_SERVERS"); - pubTopic = config.getProperty("NOTIFICATION_TOPIC"); - String apiKey = config.getProperty("UEB_API_KEY"); - String apiSecret = config.getProperty("UEB_API_SECRET"); - if(uebList==null || pubTopic==null){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file "); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file "); - } - uebList = uebList.trim(); - pubTopic = pubTopic.trim(); - pubBuilder = new CambriaClientBuilders.PublisherBuilder(); - pubBuilder.usingHosts(uebList).onTopic(pubTopic); - if(apiKey!=null && !apiKey.isEmpty() && - apiSecret!=null && !apiSecret.isEmpty()) { - apiKey= apiKey.trim(); - apiSecret = apiSecret.trim(); - pubBuilder.authenticatedBy(apiKey, apiSecret); - } - String uDelay = config.getProperty("NOTIFICATION_DELAY"); - if(uDelay!=null && !uDelay.isEmpty()){ - uDelay = uDelay.trim(); - try{ - uebDelay = Long.parseLong(uDelay); - }catch (NumberFormatException e){ - LOGGER.error("UEB_DELAY not a long format number" + e); - } - } - LOGGER.info("UEB PublisherBuilder is created."); - - } - - } - - /** - * Will Initialize the variables required for BRMSPush. - */ - public void initiate(boolean flag) { - modifiedGroups = new HashMap<>(); - controllers = new ArrayList<>(); - try { - bm.updateNotification(); - } catch (Exception e) { - LOGGER.error("Error while updating Notification: " + e.getMessage()); - } - if(flag) syncGroupInfo(); - } - - /** - * Will Add rules to projects. Creates necessary folders if required. - */ - public void addRule(String name, String rule, Map responseAttributes) { - // 1 check the response Attributes and determine if this belongs to any projects. - // 2 if not create folder - // 3 create pom. - // 4 copy the rule. - // 5 store the groups that have been updated. - String kSessionName=null; - String selectedName = null; - if(!responseAttributes.isEmpty()){ - // Pick selected Value - String userControllerName = null; - ArrayList userDependencies = new ArrayList<>(); - for(String key: responseAttributes.keySet()){ - if(key.equals(policyKeyID)){ - selectedName = responseAttributes.get(key); - } - //kmodule configurations - else if (key.equals("kSessionName")){ - kSessionName=responseAttributes.get(key); - } - // Check User Specific values. + 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 DEPENDENCY_FILE = "dependency.json"; + + private static Map modifiedGroups = new HashMap<>(); + private static IntegrityMonitor im; + private static BackUpMonitor bm; + private String defaultName = null; + private String repID = null; + private String repName = null; + private ArrayList repURLs = null; + private String repUserName = null; + private String repPassword = null; + private String policyKeyID = null; + private boolean createFlag = false; + private String uebList = null; + private List dmaapList = null; + private String pubTopic = null; + private PublisherBuilder pubBuilder = null; + protected BusPublisher publisher = null; + private Long uebDelay = Long.parseLong("20"); + private Long dmaapDelay = Long.parseLong("5000"); + private String notificationType = null; + private ArrayList controllers; + 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(); + Path file = Paths.get(propertiesFile); + if (Files.notExists(file)) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Config File doesn't Exist in the specified Path " + + file.toString()); + throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + + "Config File doesn't Exist in the specified Path " + file.toString()); + } else { + if (file.toString().endsWith(".properties")) { + // Grab the Properties. + setProperty(file, config, handler); + } + } + } + + private void setProperty(Path file, Properties config, BackUpHandler handler) throws PolicyException { + InputStream in; + try { + in = new FileInputStream(file.toFile()); + config.load(in); + } catch (IOException e) { + LOGGER.error( + XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file.", + e); + throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + + "Data/File Read Error while reading from the property file."); + } + LOGGER.info("Trying to set up IntegrityMonitor"); + String resourceName = null; + try { + LOGGER.info("Trying to set up IntegrityMonitor"); + resourceName = config.getProperty("RESOURCE_NAME"); + if (resourceName == null) { + LOGGER.warn("RESOURCE_NAME is missing setting default value. "); + resourceName = "brmsgw_pdp01"; + } + resourceName = resourceName.trim(); + setIntegrityMonitor(IntegrityMonitor.getInstance(resourceName, config)); + } catch (Exception e) { + LOGGER.error("Error starting Integerity Monitor: " + e); + } + LOGGER.info("Trying to set up BackUpMonitor"); + try { + setBackupMonitor(BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), resourceName, config, + handler)); + } catch (Exception e) { + LOGGER.error("Error starting BackUpMonitor: " + e); + } + config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml"); + EntityManagerFactory emf = Persistence.createEntityManagerFactory("BRMSGW", config); + em = emf.createEntityManager(); + defaultName = config.getProperty("defaultName"); + if (defaultName == null) { + LOGGER.error( + XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file "); + throw new PolicyException( + XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file"); + } + defaultName = defaultName.trim(); + repID = config.getProperty("repositoryID"); + if (repID == null) { + LOGGER.error( + XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file "); + throw new PolicyException( + XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file "); + } + repID = repID.trim(); + repName = config.getProperty("repositoryName"); + if (repName == null) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + + "repositoryName property is missing from the property file "); + throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + + "repositoryName property is missing from the property file "); + } + repName = repName.trim(); + 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 "); + } + 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) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + + "repostoryUserName and respositoryPassword properties are required."); + throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + + "repostoryUserName and respositoryPassword properties are required."); + } + repUserName = repUserName.trim(); + repPassword = repPassword.trim(); + policyKeyID = config.getProperty("policyKeyID"); + if (policyKeyID == null) { + LOGGER.error( + XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file "); + 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."); + brmsdependencyversion = DEFAULT_VERSION; + } + brmsdependencyversion = brmsdependencyversion.trim(); + readGroups(config); + + // Setup Publisher + notificationType = config.getProperty("NOTIFICATION_TYPE"); + if ("dmaap".equalsIgnoreCase(notificationType)) { + + LOGGER.info("Notification Type being used is DMaaP... creating instance of BusPublisher."); + // Setting up the Publisher for DMaaP MR + String dmaapServers = config.getProperty("NOTIFICATION_SERVERS"); + pubTopic = config.getProperty("NOTIFICATION_TOPIC"); + String aafLogin = config.getProperty("CLIENT_ID").trim(); + String aafPassword = config.getProperty("CLIENT_KEY").trim(); + + if (dmaapServers == null || pubTopic == null) { + LOGGER.error( + XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file "); + throw new PolicyException( + XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file "); + } + + dmaapServers = dmaapServers.trim(); + pubTopic = pubTopic.trim(); + + if (dmaapServers.contains(",")) { + dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*"))); + } else { + dmaapList = new ArrayList<>(); + dmaapList.add(dmaapServers); + } + + this.publisher = new BusPublisher.DmaapPublisherWrapper(this.dmaapList, this.pubTopic, aafLogin, + aafPassword); + + String dDelay = config.getProperty("NOTIFICATION_DELAY"); + if (dDelay != null && !dDelay.isEmpty()) { + dDelay = dDelay.trim(); + try { + dmaapDelay = Long.parseLong(dDelay); + } catch (NumberFormatException e) { + LOGGER.error("DMAAP_DELAY not a long format number" + e); + } + } + LOGGER.info("DMAAP BusPublisher is created."); + + } else { + LOGGER.info("Notification Type being used is UEB... creating instance of PublisherBuilder."); + // Setting up the Publisher for UEB + uebList = config.getProperty("NOTIFICATION_SERVERS"); + pubTopic = config.getProperty("NOTIFICATION_TOPIC"); + String apiKey = config.getProperty("UEB_API_KEY"); + String apiSecret = config.getProperty("UEB_API_SECRET"); + if (uebList == null || pubTopic == null) { + LOGGER.error( + XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file "); + throw new PolicyException( + XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file "); + } + uebList = uebList.trim(); + pubTopic = pubTopic.trim(); + pubBuilder = new CambriaClientBuilders.PublisherBuilder(); + pubBuilder.usingHosts(uebList).onTopic(pubTopic); + if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) { + apiKey = apiKey.trim(); + apiSecret = apiSecret.trim(); + pubBuilder.authenticatedBy(apiKey, apiSecret); + } + String uDelay = config.getProperty("NOTIFICATION_DELAY"); + if (uDelay != null && !uDelay.isEmpty()) { + uDelay = uDelay.trim(); + try { + uebDelay = Long.parseLong(uDelay); + } catch (NumberFormatException e) { + LOGGER.error("UEB_DELAY not a long format number" + e); + } + } + LOGGER.info("UEB PublisherBuilder is created."); + + } + + } + + private static void setBackupMonitor(BackUpMonitor instance) { + bm = instance; + } + + private static void setIntegrityMonitor(IntegrityMonitor instance) { + im = instance; + } + + /** + * Will Initialize the variables required for BRMSPush. + */ + public void initiate(boolean flag) { + resetModifiedGroups(); + controllers = new ArrayList<>(); + try { + bm.updateNotification(); + } catch (Exception e) { + LOGGER.error("Error while updating Notification: " + e.getMessage(), e); + } + if (flag) + syncGroupInfo(); + } + + private static void resetModifiedGroups() { + modifiedGroups = new HashMap<>(); + } + + /** + * Will Add rules to projects. Creates necessary folders if required. + */ + public void addRule(String name, String rule, Map responseAttributes) { + // 1 check the response Attributes and determine if this belongs to any projects. + // 2 if not create folder + // 3 create pom. + // 4 copy the rule. + // 5 store the groups that have been updated. + String kSessionName = null; + String selectedName = null; + if (!responseAttributes.isEmpty()) { + // Pick selected Value + String userControllerName = null; + ArrayList userDependencies = new ArrayList<>(); + for (String key : responseAttributes.keySet()) { + if (key.equals(policyKeyID)) { + selectedName = responseAttributes.get(key); + } + // kmodule configurations + else if (key.equals("kSessionName")) { + kSessionName = responseAttributes.get(key); + } + // Check User Specific values. if (key.equals("$controller:")) { try { - PEDependency dependency = PolicyUtils.jsonStringToObject(responseAttributes.get(key), PEDependency.class); - userControllerName = key.replaceFirst("$controller:",""); + PEDependency dependency = PolicyUtils.jsonStringToObject(responseAttributes.get(key), + PEDependency.class); + userControllerName = key.replaceFirst("$controller:", ""); addToGroup(userControllerName, dependency); } catch (Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Error while resolving Controller: " + e); + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e); } - } - else if(key.equals("$dependency$")){ + } else if (key.equals("$dependency$")) { String value = responseAttributes.get(key); - if(value.startsWith("[") && value.endsWith("]")){ - value = value.substring(1, value.length()-1).trim(); + if (value.startsWith("[") && value.endsWith("]")) { + value = value.substring(1, value.length() - 1).trim(); List dependencyStrings = Arrays.asList(value.split("},{")); - for(String dependencyString: dependencyStrings){ + for (String dependencyString : dependencyStrings) { try { - userDependencies.add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class)); + userDependencies + .add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class)); } catch (Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Error while resolving Dependencies: " + e); + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + + "Error while resolving Dependencies: " + e); } } } } - } - if(userControllerName!=null){ - // Adding custom dependencies here. + } + if (userControllerName != null) { + // Adding custom dependencies here. ArrayList values = groupMap.get(userControllerName); values.add(userDependencies); groupMap.put(userControllerName, values); selectedName = userControllerName; } - } - // If no Match then pick Default. - if(selectedName==null){ - selectedName = defaultName; - } - if (groupMap.containsKey(selectedName)) { - //If the key is not got as parameters set by the user, setting the default value for kSessionName as closedLoop - if(kSessionName==null){ - if(selectedName==defaultName){ - kSessionName="closedloop"; - }else{ - kSessionName= "closedloop-" + selectedName; - } - } - // create directories if missing. - manageProject(selectedName, kSessionName, name, rule); + } + // If no Match then pick Default. + if (selectedName == null) { + selectedName = defaultName; + } + if (groupMap.containsKey(selectedName)) { + // If the key is not got as parameters set by the user, setting the default value for kSessionName as + // closedLoop + if (kSessionName == null) { + if (selectedName == defaultName) { + kSessionName = "closedloop"; + } else { + kSessionName = "closedloop-" + selectedName; + } + } + // create directories if missing. + manageProject(selectedName, kSessionName, name, rule); addModifiedGroup(selectedName, "update"); // Will check for Create Later after generating the Pom. - } - } - - private void syncGroupInfo() { - // Sync DB to JMemory. - EntityTransaction et = em.getTransaction(); - et.begin(); - Query query = em.createQuery("select b from BRMSGroupInfo AS b"); - List bList = query.getResultList(); - if(bList.size()!=groupMap.size()){ - for(Object value : bList){ - BRMSGroupInfo brmsGroupInfo = (BRMSGroupInfo) value; - PEDependency dependency = new PEDependency(); - dependency.setArtifactId(brmsGroupInfo.getArtifactId()); - dependency.setGroupId(brmsGroupInfo.getGroupId()); - dependency.setVersion(brmsGroupInfo.getVersion()); - ArrayList values = new ArrayList<>(); - values.add(dependency); - groupMap.put(brmsGroupInfo.getControllerName(), values); - } - } - query = em.createQuery("select g from BRMSPolicyInfo AS g"); - bList = query.getResultList(); - if(bList.size()!=policyMap.size()){ - for(Object value: bList){ - BRMSPolicyInfo brmsPolicyInfo = (BRMSPolicyInfo) value; - policyMap.put(brmsPolicyInfo.getPolicyName(), brmsPolicyInfo.getControllerName().getControllerName()); - } - } - et.commit(); - LOGGER.info("Updated Local Memory values with values from database."); - } - - private void manageProject(String selectedName, String kSessionName, String name, String rule) { - // 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); - copyDataToFile( - PROJECTSLOCATION + File.separator - + getArtifactID(selectedName) + File.separator - + "src" + File.separator + "main" + File.separator - + "resources" + File.separator + "rules" - + File.separator + name + ".drl", rule); - addToPolicy(name,selectedName); - } - - /* - * Add Policy to JMemory and DataBase. - */ - private void addToPolicy(String policyName, String controllerName) { - policyMap.put(policyName, controllerName); - 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(); - boolean createFlag = false; - BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo(); - if(pList.size()>0){ - // Already exists. - brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0); - if(!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)){ - createFlag = true; - } - }else{ - createFlag = true; - } - if(createFlag){ - 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){ - brmsGroupInfo = (BRMSGroupInfo) bList.get(0); - } - brmsPolicyInfo.setPolicyName(policyName); - brmsPolicyInfo.setControllerName(brmsGroupInfo); - em.persist(brmsPolicyInfo); - em.flush(); - } - et.commit(); - } - - private void syncProject(String selectedName) { - boolean projectExists = checkProject(selectedName); - if(projectExists){ - String version = null; - 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"; - } - String nextVersion = incrementVersion(version); - boolean outOfSync = checkRemoteSync(selectedName, nextVersion); - if(!outOfSync){ - return; - } - } - // We are out of Sync or Project is not Present. - downloadProject(selectedName); - } - - private void downloadProject(String selectedName) { - NexusArtifact artifact = getLatestArtifactFromNexus(selectedName); - if(artifact==null) return; - String dirName = getDirectoryName(selectedName); - URL website; - String fileName = "rule.jar"; - try { - website = new URL(artifact.getResourceURI()); - ReadableByteChannel rbc = Channels.newChannel(website.openStream()); - FileOutputStream fos = new FileOutputStream(fileName); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - fos.close(); - extractJar(fileName, dirName); - new File(fileName).delete(); - } catch (IOException e) { - LOGGER.error("Error while downloading the project to File System. " + e.getMessage()); - } - } - - private void extractJar(String jarFileName, String artifactId) throws IOException{ - JarFile jar = new JarFile(jarFileName); - Enumeration enumEntries = jar.entries(); - while (enumEntries.hasMoreElements()) { - JarEntry file = (JarEntry) enumEntries.nextElement(); - File f = null; - 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"; - 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")){ - String path = PROJECTSLOCATION+ File.separator + artifactId; - new File(path).mkdirs(); - f = new File(path + File.separator + fileName); - }else if(file.getName().endsWith("kmodule.xml")){ - String path = PROJECTSLOCATION+ File.separator + artifactId + File.separator + "src"+ - File.separator+ "main" + File.separator + "resources" + File.separator+ "META-INF"; - new File(path).mkdirs(); - f = new File(path + File.separator + fileName); - } - if(f!=null){ - InputStream is = jar.getInputStream(file); - FileOutputStream fos = new FileOutputStream(f); - while (is.available() > 0) { - fos.write(is.read()); - } - fos.close(); - is.close(); - f=null; - LOGGER.info(fileName + " Created.."); - } - } - jar.close(); - } - - private NexusArtifact getLatestArtifactFromNexus(String selectedName) { - List artifacts = getArtifactFromNexus(selectedName, null); - int bigNum = 0; + } + } + + private void syncGroupInfo() { + // Sync DB to JMemory. + EntityTransaction et = em.getTransaction(); + et.begin(); + Query query = em.createQuery("select b from BRMSGroupInfo AS b"); + List bList = query.getResultList(); + if (bList.size() != groupMap.size()) { + for (Object value : bList) { + BRMSGroupInfo brmsGroupInfo = (BRMSGroupInfo) value; + PEDependency dependency = new PEDependency(); + dependency.setArtifactId(brmsGroupInfo.getArtifactId()); + dependency.setGroupId(brmsGroupInfo.getGroupId()); + dependency.setVersion(brmsGroupInfo.getVersion()); + ArrayList values = new ArrayList<>(); + values.add(dependency); + groupMap.put(brmsGroupInfo.getControllerName(), values); + } + } + query = em.createQuery("select g from BRMSPolicyInfo AS g"); + bList = query.getResultList(); + if (bList.size() != policyMap.size()) { + for (Object value : bList) { + BRMSPolicyInfo brmsPolicyInfo = (BRMSPolicyInfo) value; + policyMap.put(brmsPolicyInfo.getPolicyName(), brmsPolicyInfo.getControllerName().getControllerName()); + } + } + et.commit(); + LOGGER.info("Updated Local Memory values with values from database."); + } + + private void manageProject(String selectedName, String kSessionName, String name, String rule) { + // 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); + copyDataToFile(PROJECTSLOCATION + File.separator + getArtifactID(selectedName) + File.separator + "src" + + File.separator + "main" + File.separator + "resources" + File.separator + "rules" + File.separator + + name + ".drl", rule); + addToPolicy(name, selectedName); + } + + /* + * Add Policy to JMemory and DataBase. + */ + private void addToPolicy(String policyName, String controllerName) { + policyMap.put(policyName, controllerName); + 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(); + boolean createFlag = false; + BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo(); + if (pList.size() > 0) { + // Already exists. + brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0); + if (!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) { + createFlag = true; + } + } else { + createFlag = true; + } + if (createFlag) { + 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) { + brmsGroupInfo = (BRMSGroupInfo) bList.get(0); + } + brmsPolicyInfo.setPolicyName(policyName); + brmsPolicyInfo.setControllerName(brmsGroupInfo); + em.persist(brmsPolicyInfo); + em.flush(); + } + et.commit(); + } + + private void syncProject(String selectedName) { + boolean projectExists = checkProject(selectedName); + if (projectExists) { + String version = null; + 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"; + } + String nextVersion = incrementVersion(version); + boolean outOfSync = checkRemoteSync(selectedName, nextVersion); + if (!outOfSync) { + return; + } + } + // We are out of Sync or Project is not Present. + downloadProject(selectedName); + } + + private void downloadProject(String selectedName) { + NexusArtifact artifact = getLatestArtifactFromNexus(selectedName); + if (artifact == null) + return; + String dirName = getDirectoryName(selectedName); + URL website; + String fileName = "rule.jar"; + try { + website = new URL(artifact.getResourceURI()); + ReadableByteChannel rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(fileName); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + extractJar(fileName, dirName); + new File(fileName).delete(); + } catch (IOException e) { + LOGGER.error("Error while downloading the project to File System. " + e.getMessage(), e); + } + } + + private void extractJar(String jarFileName, String artifactId) throws IOException { + JarFile jar = new JarFile(jarFileName); + Enumeration enumEntries = jar.entries(); + while (enumEntries.hasMoreElements()) { + JarEntry file = (JarEntry) enumEntries.nextElement(); + File f = null; + 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"; + 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")) { + String path = PROJECTSLOCATION + File.separator + artifactId; + new File(path).mkdirs(); + f = new File(path + File.separator + fileName); + } else if (file.getName().endsWith("kmodule.xml")) { + String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator + + "main" + File.separator + "resources" + File.separator + "META-INF"; + new File(path).mkdirs(); + f = new File(path + File.separator + fileName); + } + if (f != null) { + InputStream is = jar.getInputStream(file); + FileOutputStream fos = new FileOutputStream(f); + while (is.available() > 0) { + fos.write(is.read()); + } + fos.close(); + is.close(); + f = null; + LOGGER.info(fileName + " Created.."); + } + } + jar.close(); + } + + private NexusArtifact getLatestArtifactFromNexus(String selectedName) { + List artifacts = getArtifactFromNexus(selectedName, null); + int bigNum = 0; int smallNum = 0; NexusArtifact result = null; for (NexusArtifact artifact : artifacts) { int majorVal = Integer.parseInt(artifact.getVersion().substring(0, artifact.getVersion().indexOf("."))); - int minorVal = Integer.parseInt(artifact.getVersion().substring(artifact.getVersion().indexOf(".")+1,artifact.getVersion().lastIndexOf("."))); - if(majorVal>bigNum){ + int minorVal = Integer.parseInt(artifact.getVersion().substring(artifact.getVersion().indexOf(".") + 1, + artifact.getVersion().lastIndexOf("."))); + if (majorVal > bigNum) { bigNum = majorVal; smallNum = minorVal; } - if((bigNum==majorVal)&&(minorVal>smallNum)){ + if ((bigNum == majorVal) && (minorVal > smallNum)) { smallNum = minorVal; } - if(bigNum==majorVal && minorVal==smallNum){ + if (bigNum == majorVal && minorVal == smallNum) { result = artifact; } } 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()); + } + + // 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) { - List artifacts = getArtifactFromNexus(selectedName, version); - return (artifacts.size()==0) ? false: true; - } - - private List getArtifactFromNexus(String selectedName, String version) { - final NexusClient client = new NexusRestClient(); - 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<>(); - } - - private void setVersion(String selectedName) { - String newVersion = "0.1.0"; - createFlag = false; - NexusArtifact artifact = getLatestArtifactFromNexus(selectedName); - if(artifact!=null){ - newVersion = incrementVersion(artifact.getVersion()); - } - if(newVersion.equals("0.1.0")){ - createFlag = true; - } - setVersion(newVersion, selectedName); + return artifact.isEmpty() ? result : additionalNexusLatestCheck(selectedName, artifact.get(0)); + } + + private boolean checkRemoteSync(String selectedName, String version) { + List artifacts = getArtifactFromNexus(selectedName, version); + return (artifacts.size() == 0) ? false : true; + } + + private List getArtifactFromNexus(String selectedName, String version) { + final NexusClient client = new NexusRestClient(); + int i = 0; + boolean flag = false; + while (i < repURLs.size()) { + try { + 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 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(), 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 (!flag) { + Collections.rotate(repURLs, -1); + i++; + } + } + } + return new ArrayList<>(); + } + + private void setVersion(String selectedName) { + String newVersion = "0.1.0"; + createFlag = false; + NexusArtifact artifact = getLatestArtifactFromNexus(selectedName); + if (artifact != null) { + newVersion = incrementVersion(artifact.getVersion()); + } + if (newVersion.equals("0.1.0")) { + createFlag = true; + } + setVersion(newVersion, selectedName); LOGGER.info("Controller: " + selectedName + "is on version: " + newVersion); - } + } - private String incrementVersion(String version) { - int majorVal = Integer.parseInt(version.substring(0, version.indexOf("."))); - int minorVal = Integer.parseInt(version.substring(version.indexOf(".")+1,version.lastIndexOf("."))); - if (minorVal >= 9) { + private String incrementVersion(String version) { + int majorVal = Integer.parseInt(version.substring(0, version.indexOf("."))); + int minorVal = Integer.parseInt(version.substring(version.indexOf(".") + 1, version.lastIndexOf("."))); + if (minorVal >= 9) { majorVal += 1; minorVal = 0; } else { minorVal += 1; } - return majorVal + "." + minorVal + version.substring(version.lastIndexOf(".")); - } - - private boolean checkProject(String selectedName) { - return new File(PROJECTSLOCATION + File.separator + getDirectoryName(selectedName)).exists(); - } - - private String getDirectoryName(String selectedName) { - return getArtifactID(selectedName); - } - - /** - * Will Push policies to the PolicyRepo. - * - * @param notificationType type of notification Type. - * @throws PolicyException - */ - public void pushRules() throws PolicyException{ - // Check how many groups have been updated. - // 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()){ - InvocationResult result = null; - try{ - InvocationRequest request = new DefaultInvocationRequest(); - setVersion(group); - createPom(group); - request.setPomFile(new File(PROJECTSLOCATION+File.separator+getArtifactID(group)+File.separator+"pom.xml")); - request.setGoals(Arrays.asList(GOALS)); - 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(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); - } - } - if(im!=null){ - im.endTransaction(); - } - } - - /** - * Removes a Rule from Rule Projects. - */ - public void removeRule(String name){ - String controllerName = getGroupName(name); - if(controllerName==null){ - LOGGER.info("Error finding the controllerName for the given Policy: " + name); - return; - } - syncProject(controllerName); - getNameAndSetRemove(controllerName, name); - } - - private String getGroupName(String name) { - if(policyMap.containsKey(name)){ - return policyMap.get(name); - }else{ - syncGroupInfo(); - return (policyMap.containsKey(name)) ? policyMap.get(name):null; - } - } - - private void addModifiedGroup(String controllerName, String operation) { - if(controllerName!=null){ + return majorVal + "." + minorVal + version.substring(version.lastIndexOf(".")); + } + + private boolean checkProject(String selectedName) { + return new File(PROJECTSLOCATION + File.separator + getDirectoryName(selectedName)).exists(); + } + + private String getDirectoryName(String selectedName) { + return getArtifactID(selectedName); + } + + /** + * Will Push policies to the PolicyRepo. + * + * @param notificationType + * type of notification Type. + * @throws PolicyException + */ + public void pushRules() throws PolicyException { + // Check how many groups have been updated. + // 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()) { + InvocationResult result = null; + try { + InvocationRequest request = new DefaultInvocationRequest(); + setVersion(group); + createPom(group); + request.setPomFile(new File( + PROJECTSLOCATION + File.separator + getArtifactID(group) + File.separator + "pom.xml")); + request.setGoals(Arrays.asList(GOALS)); + 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 (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, modifiedGroups.get(group)); + } + flag = true; + } else { + throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!"); + } + } + if (flag) { + sendNotification(controllers); + } + } + if (im != null) { + im.endTransaction(); + } + } + + /** + * Removes a Rule from Rule Projects. + */ + public void removeRule(String name) { + String controllerName = getGroupName(name); + if (controllerName == null) { + LOGGER.info("Error finding the controllerName for the given Policy: " + name); + return; + } + syncProject(controllerName); + getNameAndSetRemove(controllerName, name); + } + + private String getGroupName(String name) { + if (policyMap.containsKey(name)) { + return policyMap.get(name); + } else { + syncGroupInfo(); + return (policyMap.containsKey(name)) ? policyMap.get(name) : null; + } + } + + private void addModifiedGroup(String controllerName, String operation) { + if (controllerName != null) { modifiedGroups.put(controllerName, operation); } } - private void addNotification(String controllerName, String operation) { + private void addNotification(String controllerName, String operation) { ControllerPOJO controllerPOJO = new ControllerPOJO(); controllerPOJO.setName(controllerName); controllerPOJO.setOperation(operation); @@ -770,338 +808,341 @@ public class BRMSPush { controllerPOJO.setDrools(drools); controllers.add(controllerPOJO); try { - LOGGER.debug("Notification added: " - + PolicyUtils.objectToJsonString(controllerPOJO)); + LOGGER.debug("Notification added: " + PolicyUtils.objectToJsonString(controllerPOJO)); } catch (JsonProcessingException e) { - LOGGER.error(MessageCodes.ERROR_SCHEMA_INVALID - + "Json Processing Error " + e); + LOGGER.error(MessageCodes.ERROR_SCHEMA_INVALID + "Json Processing Error " + e); } } - private void removedRuleModifiedGroup(String controllerName){ - // This will be sending Notification to PDPD directly to Lock - ControllerPOJO controllerPOJO = new ControllerPOJO(); - controllerPOJO.setName(controllerName); - controllerPOJO.setOperation("lock"); - List controllers = new ArrayList<>(); - controllers.add(controllerPOJO); - sendNotification(controllers); - } - - private void sendNotification(List controllers){ - NotificationPOJO notification = new NotificationPOJO(); - String requestId = UUID.randomUUID().toString(); - LOGGER.info("Generating notification RequestID : " + requestId); - notification.setRequestID(requestId); - notification.setEntity("controller"); - notification.setControllers(controllers); - try { - String notificationJson = PolicyUtils.objectToJsonString(notification); - LOGGER.info("Sending Notification :\n" + notificationJson); - sendMessage(notificationJson); - } catch (Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while sending notification to PDP-D " + e.getMessage()); - } - } - - private void sendMessage(String message) throws IOException, GeneralSecurityException, InterruptedException { - - if("dmaap".equalsIgnoreCase(notificationType)) { - // Sending Message through DMaaP Message Router - LOGGER.debug("DMAAP Publishing Message"); - - publisher.send( "MyPartitionKey", message); - - LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + pubTopic); - - Thread.sleep(dmaapDelay); - publisher.close(); - } else { - // Sending Message through UEB interface. - LOGGER.debug("UEB Publishing Message"); - - CambriaBatchingPublisher pub = pubBuilder.build(); - pub.send( "MyPartitionKey", message); - - final List stuck = pub.close ( uebDelay, TimeUnit.SECONDS ); - if ( stuck.size () > 0 ) { - LOGGER.error ( stuck.size() + " messages unsent" ); - }else { - LOGGER.debug ( "Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic ); - } - } - - } - - private void createPom(String name) { + private void removedRuleModifiedGroup(String controllerName) { + // This will be sending Notification to PDPD directly to Lock + ControllerPOJO controllerPOJO = new ControllerPOJO(); + controllerPOJO.setName(controllerName); + controllerPOJO.setOperation("lock"); + List controllers = new ArrayList<>(); + controllers.add(controllerPOJO); + sendNotification(controllers); + } + + private void sendNotification(List controllers) { + NotificationPOJO notification = new NotificationPOJO(); + String requestId = UUID.randomUUID().toString(); + LOGGER.info("Generating notification RequestID : " + requestId); + notification.setRequestID(requestId); + notification.setEntity("controller"); + notification.setControllers(controllers); + try { + String notificationJson = PolicyUtils.objectToJsonString(notification); + LOGGER.info("Sending Notification :\n" + notificationJson); + sendMessage(notificationJson); + } catch (Exception e) { + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while sending notification to PDP-D " + + e.getMessage(), e); + } + } + + private void sendMessage(String message) throws IOException, GeneralSecurityException, InterruptedException { + + if ("dmaap".equalsIgnoreCase(notificationType)) { + // Sending Message through DMaaP Message Router + LOGGER.debug("DMAAP Publishing Message"); + + publisher.send("MyPartitionKey", message); + + LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + pubTopic); + + Thread.sleep(dmaapDelay); + publisher.close(); + } else { + // Sending Message through UEB interface. + LOGGER.debug("UEB Publishing Message"); + + CambriaBatchingPublisher pub = pubBuilder.build(); + pub.send("MyPartitionKey", message); + + final List stuck = pub.close(uebDelay, TimeUnit.SECONDS); + if (stuck.size() > 0) { + LOGGER.error(stuck.size() + " messages unsent"); + } else { + LOGGER.debug("Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic); + } + } + + } + + private void createPom(String name) { Model model = new Model(); model.setModelVersion("4.0.0"); model.setGroupId(getGroupID(name)); model.setArtifactId(getArtifactID(name)); model.setVersion(getVersion(name)); model.setName(name); - DistributionManagement distributionManagement = new DistributionManagement(); - DeploymentRepository repository = new DeploymentRepository(); - repository.setId(repID); - repository.setName(repName); - repository.setUrl(repURLs.get(0)); - distributionManagement.setRepository(repository); - model.setDistributionManagement(distributionManagement); - // Dependency Management goes here. - List dependencyList= new ArrayList<>(); - if(groupMap.get(name).size()>1){ - @SuppressWarnings("unchecked") + DistributionManagement distributionManagement = new DistributionManagement(); + DeploymentRepository repository = new DeploymentRepository(); + repository.setId(repID); + repository.setName(repName); + repository.setUrl(repURLs.get(0)); + distributionManagement.setRepository(repository); + model.setDistributionManagement(distributionManagement); + // Dependency Management goes here. + List dependencyList = new ArrayList<>(); + if (groupMap.get(name).size() > 1) { + @SuppressWarnings("unchecked") ArrayList dependencies = (ArrayList) groupMap.get(name).get(1); - for(PEDependency dependency: dependencies){ + for (PEDependency dependency : dependencies) { dependencyList.add(dependency.getDependency()); } - }else{ - // Add Default dependencies. - dependencyList = getDependencies(name); + } else { + // Add Default dependencies. + dependencyList = getDependencies(name); + } + model.setDependencies(dependencyList); + Writer writer = null; + try { + writer = WriterFactory.newXmlWriter( + new File(PROJECTSLOCATION + File.separator + getArtifactID(name) + File.separator + "pom.xml")); + MavenXpp3Writer pomWriter = new MavenXpp3Writer(); + pomWriter.write(writer, model); + } catch (Exception e) { + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating POM for " + getArtifactID(name) + + e.getMessage(), e); + } finally { + IOUtil.close(writer); + } + } + + private List getDependencies(String controllerName) { + // Read the Dependency Information from property file. + Path file = Paths.get(DEPENDENCY_FILE); + if (!Files.notExists(file)) { + try { + String dependencyJSON = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); + DependencyInfo dependencyInfo = PolicyUtils.jsonStringToObject(dependencyJSON, DependencyInfo.class); + String controller = "default"; + if (dependencyInfo.getDependencies().containsKey(controllerName)) { + controller = controllerName; + } + List dependencyList = new ArrayList<>(); + for (PEDependency dependency : dependencyInfo.getDependencies().get(controller)) { + dependencyList.add(dependency.getDependency()); + } + return dependencyList; + } catch (IOException | NullPointerException e) { + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + + "Error while getting dependecy Information for controller: " + controllerName + + e.getMessage(), e); + } } - model.setDependencies(dependencyList); - Writer writer = null; - try{ - writer = WriterFactory.newXmlWriter(new File(PROJECTSLOCATION - + File.separator + getArtifactID(name) + File.separator - + "pom.xml")); - MavenXpp3Writer pomWriter = new MavenXpp3Writer(); - pomWriter.write(writer, model); - }catch(Exception e){ - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW - + "Error while creating POM for " + getArtifactID(name) - + e.getMessage()); - }finally{ - IOUtil.close(writer); - } - } - - private List getDependencies(String controllerName) { - // Read the Dependency Information from property file. - Path file = Paths.get(DEPENDENCY_FILE); - if(!Files.notExists(file)){ - try { - String dependencyJSON = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); - DependencyInfo dependencyInfo = PolicyUtils.jsonStringToObject(dependencyJSON, DependencyInfo.class); - String controller = "default"; - if(dependencyInfo.getDependencies().containsKey(controllerName)){ - controller = controllerName; - } - List dependencyList = new ArrayList<>(); - for(PEDependency dependency: dependencyInfo.getDependencies().get(controller)){ - dependencyList.add(dependency.getDependency()); - } - return dependencyList; - } catch (IOException| NullPointerException e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW - + "Error while getting dependecy Information for controller: " + controllerName - + e.getMessage()); - } - } - return defaultDependencies(controllerName); - } - - // Default Dependency Section. Can be changed as required. - public List defaultDependencies(String controllerName) { - - List dependencyList = new ArrayList<>(); - String version= StringEscapeUtils.escapeJava(brmsdependencyversion); - - Dependency demoDependency = new Dependency(); - demoDependency.setGroupId("org.onap.policy.drools-applications"); - demoDependency.setArtifactId("demo"); - demoDependency.setVersion(version); - dependencyList.add(demoDependency); - - Dependency controlloopDependency = new Dependency(); - controlloopDependency.setGroupId("org.onap.policy.drools-applications"); - controlloopDependency.setArtifactId("events"); - controlloopDependency.setVersion(version); - dependencyList.add(controlloopDependency); - - Dependency restDependency = new Dependency(); - restDependency.setGroupId("org.onap.policy.drools-applications"); - restDependency.setArtifactId("rest"); - restDependency.setVersion(version); - dependencyList.add(restDependency); - - Dependency appcDependency = new Dependency(); - appcDependency.setGroupId("org.onap.policy.drools-applications"); - appcDependency.setArtifactId("appc"); - appcDependency.setVersion(version); - dependencyList.add(appcDependency); - - Dependency aaiDependency = new Dependency(); - aaiDependency.setGroupId("org.onap.policy.drools-applications"); - aaiDependency.setArtifactId("aai"); - aaiDependency.setVersion(version); - dependencyList.add(aaiDependency); - - Dependency msoDependency = new Dependency(); - msoDependency.setGroupId("org.onap.policy.drools-applications"); - msoDependency.setArtifactId("mso"); - msoDependency.setVersion(version); - dependencyList.add(msoDependency); - - Dependency trafficgeneratorDependency = new Dependency(); - trafficgeneratorDependency.setGroupId("org.onap.policy.drools-applications"); - trafficgeneratorDependency.setArtifactId("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"))){ - // 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); - } - } - - private void copyDataToFile(String file, String rule) { - try{ - FileUtils.writeStringToFile(new File(file), rule); - } catch (Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Error while creating Rule for " + file + e.getMessage()); - } - } - - private void readGroups(Properties config) throws PolicyException{ - String[] groupNames = null; - if(config.getProperty("groupNames").contains(",")){ - groupNames = config.getProperty("groupNames").replaceAll(" ", "").split(","); - }else{ - groupNames = new String[]{config.getProperty("groupNames").replaceAll(" ", "")}; - } - if(groupNames==null || groupNames.length==0){ - 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<>(); - for(int counter=0; counter < groupNames.length ;counter++){ - String name = groupNames[counter]; - String groupID = config.getProperty(name+".groupID"); - if(groupID==null){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name+".groupID property is missing from the property file "); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name+".groupID property is missing from the property file "); - } - String artifactID = config.getProperty(name+".artifactID"); - if(artifactID==null){ - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name+".artifactID property is missing from the property file "); - throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name+".artifactID property is missing from the property file "); - } - PEDependency dependency = new PEDependency(); - dependency.setArtifactId(artifactID); - dependency.setGroupId(groupID); - // Add to list if we got all - addToGroup(name,dependency); - } - } - - private void addToGroup(String name, PEDependency dependency) { - ArrayList values = new ArrayList<>(); + return defaultDependencies(controllerName); + } + + // Default Dependency Section. Can be changed as required. + public List defaultDependencies(String controllerName) { + + List dependencyList = new ArrayList<>(); + String version = StringEscapeUtils.escapeJava(brmsdependencyversion); + + Dependency demoDependency = new Dependency(); + demoDependency.setGroupId("org.onap.policy.drools-applications"); + demoDependency.setArtifactId("demo"); + demoDependency.setVersion(version); + dependencyList.add(demoDependency); + + Dependency controlloopDependency = new Dependency(); + controlloopDependency.setGroupId("org.onap.policy.drools-applications"); + controlloopDependency.setArtifactId("events"); + controlloopDependency.setVersion(version); + dependencyList.add(controlloopDependency); + + Dependency restDependency = new Dependency(); + restDependency.setGroupId("org.onap.policy.drools-applications"); + restDependency.setArtifactId("rest"); + restDependency.setVersion(version); + dependencyList.add(restDependency); + + Dependency appcDependency = new Dependency(); + appcDependency.setGroupId("org.onap.policy.drools-applications"); + appcDependency.setArtifactId("appc"); + appcDependency.setVersion(version); + dependencyList.add(appcDependency); + + Dependency aaiDependency = new Dependency(); + aaiDependency.setGroupId("org.onap.policy.drools-applications"); + aaiDependency.setArtifactId("aai"); + aaiDependency.setVersion(version); + dependencyList.add(aaiDependency); + + Dependency msoDependency = new Dependency(); + msoDependency.setGroupId("org.onap.policy.drools-applications"); + msoDependency.setArtifactId("mso"); + msoDependency.setVersion(version); + dependencyList.add(msoDependency); + + Dependency trafficgeneratorDependency = new Dependency(); + trafficgeneratorDependency.setGroupId("org.onap.policy.drools-applications"); + trafficgeneratorDependency.setArtifactId("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"))) { + // 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); + } + } + + private void copyDataToFile(String file, String rule) { + try { + FileUtils.writeStringToFile(new File(file), rule); + } catch (Exception e) { + LOGGER.error( + XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating Rule for " + file + e.getMessage(), + e); + } + } + + private void readGroups(Properties config) throws PolicyException { + String[] groupNames = null; + if (config.getProperty("groupNames").contains(",")) { + groupNames = config.getProperty("groupNames").replaceAll(" ", "").split(","); + } else { + groupNames = new String[] { config.getProperty("groupNames").replaceAll(" ", "") }; + } + if (groupNames == null || groupNames.length == 0) { + 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<>(); + for (int counter = 0; counter < groupNames.length; counter++) { + String name = groupNames[counter]; + String groupID = config.getProperty(name + ".groupID"); + if (groupID == null) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name + + ".groupID property is missing from the property file "); + throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name + + ".groupID property is missing from the property file "); + } + String artifactID = config.getProperty(name + ".artifactID"); + if (artifactID == null) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name + + ".artifactID property is missing from the property file "); + throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name + + ".artifactID property is missing from the property file "); + } + PEDependency dependency = new PEDependency(); + dependency.setArtifactId(artifactID); + dependency.setGroupId(groupID); + // Add to list if we got all + addToGroup(name, dependency); + } + } + + private void addToGroup(String name, PEDependency dependency) { + ArrayList values = new ArrayList<>(); values.add(dependency); - groupMap.put(name, values); - EntityTransaction et = em.getTransaction(); - et.begin(); - Query query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn"); - query.setParameter("cn", name); - List groupList = query.getResultList(); - BRMSGroupInfo brmsGroupInfo = null; - if(groupList.size()>0){ - 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(); - } - 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(String name){ + groupMap.put(name, values); + EntityTransaction et = em.getTransaction(); + et.begin(); + Query query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn"); + query.setParameter("cn", name); + List groupList = query.getResultList(); + BRMSGroupInfo brmsGroupInfo = null; + if (groupList.size() > 0) { + 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(); + } + 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(String name) { return ((PEDependency) groupMap.get(name).get(0)).getArtifactId(); } - - private String getGroupID(String name){ + + private String getGroupID(String name) { return ((PEDependency) groupMap.get(name).get(0)).getGroupId(); } - - private String getVersion(String name){ + + private String getVersion(String name) { return ((PEDependency) groupMap.get(name).get(0)).getVersion(); } 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"; - File file = new File(ruleFolder+File.separator+ policyName +".drl"); - if(file.delete()){ - LOGGER.info("Deleted File.. " + file.getAbsolutePath()); - removePolicyFromGroup(policyName, controllerName); + String ruleFolder = PROJECTSLOCATION + File.separator + artifactName + File.separator + "src" + File.separator + + "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()); + removePolicyFromGroup(policyName, controllerName); } - if(new File(ruleFolder).listFiles().length == 0) { + if (new File(ruleFolder).listFiles().length == 0) { removedRuleModifiedGroup(controllerName); } else { // This is an update in terms of PDPD. addModifiedGroup(controllerName, "update"); } } - - // 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) { + + // 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); groupMap.get(controllerName).set(0, userController); } - // Return BackUpMonitor - 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; - } + // Return BackUpMonitor + 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; + } } -- cgit 1.2.3-korg