diff options
115 files changed, 9949 insertions, 9684 deletions
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java index 932ebb2d..9e481f01 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java @@ -29,13 +29,13 @@ import java.util.Properties; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.onap.policy.drools.core.PolicySessionFeatureAPI; import org.onap.policy.drools.features.PolicyEngineFeatureAPI; import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI; import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.drools.utils.PropertyUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * If this feature is supported, there is a single instance of it. @@ -43,197 +43,193 @@ import org.onap.policy.drools.utils.PropertyUtil; * active/standby state management and IntegrityMonitor. For now, they are * all treated as a single feature, but it would be nice to separate them. * - * The bulk of the code here was once in other classes, such as + * <p>The bulk of the code here was once in other classes, such as * 'PolicyContainer' and 'Main'. It was moved here as part of making this * a separate optional feature. */ public class ActiveStandbyFeature implements ActiveStandbyFeatureAPI, - PolicySessionFeatureAPI, PolicyEngineFeatureAPI -{ - // get an instance of logger - private static final Logger logger = - LoggerFactory.getLogger(ActiveStandbyFeature.class); - - private static DroolsPdp myPdp; - private static Object myPdpSync = new Object(); - private static DroolsPdpsElectionHandler electionHandler; - - private StateManagementFeatureAPI stateManagementFeature; - - public static final int SEQ_NUM = 1; - - - /**************************/ - /* 'FeatureAPI' interface */ - /**************************/ - - /** - * {@inheritDoc} - */ - @Override - public int getSequenceNumber() - { - return(SEQ_NUM); - } - - /** - * {@inheritDoc} - */ - @Override - public void globalInit(String[] args, String configDir) - { - // This must come first since it initializes myPdp - initializePersistence(configDir); - - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - if (feature.getResourceName().equals(myPdp.getPdpId())) - { - if(logger.isDebugEnabled()){ - logger.debug("ActiveStandbyFeature.globalInit: Found StateManagementFeature" - + " with resourceName: {}", myPdp.getPdpId()); - } - stateManagementFeature = feature; - break; - } - } - if(stateManagementFeature == null){ - if(logger.isDebugEnabled()){ - logger.debug("ActiveStandbyFeature failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", myPdp.getPdpId()); - } - logger.error("ActiveStandbyFeature failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", myPdp.getPdpId()); - // - // Cannot add observer since stateManagementFeature is null - // - return; - } - - - - //Create an instance of the Observer - PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier(); - - //Register the PMStandbyStateChangeNotifier Observer - stateManagementFeature.addObserver(pmNotifier); - if(logger.isDebugEnabled()){ - logger.debug("ActiveStandbyFeature.globalInit() exit"); - } - } - - - /** - * {@inheritDoc} - */ - @Override - public boolean afterStart(PolicyEngine engine) - { - // ASSERTION: engine == PolicyEngine.manager - PolicyEngine.manager.lock(); - return false; - } - - /** - * Read in the persistence properties, determine whether persistence is - * enabled or disabled, and initialize persistence if enabled. - */ - private static void initializePersistence(String configDir) - { - //Get the Active Standby properties - try { - Properties activeStandbyProperties = - PropertyUtil.getProperties(configDir + "/feature-active-standby-management.properties"); - ActiveStandbyProperties.initProperties(activeStandbyProperties); - logger.info("initializePersistence: ActiveStandbyProperties success"); - } catch (IOException e) { - logger.error("ActiveStandbyFeature: initializePersistence ActiveStandbyProperties", e); - } - - DroolsPdpsConnector conn = getDroolsPdpsConnector("activeStandbyPU"); - String resourceName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME); - if(resourceName == null){ - throw new NullPointerException(); - } - - /* - * In a JUnit test environment, one or more PDPs may already have been - * inserted in the DB, so we need to check for this. - */ - DroolsPdp existingPdp = conn.getPdp(resourceName); - if (existingPdp != null) { - logger.info("Found existing PDP record, pdpId=" - + existingPdp.getPdpId() + ", isDesignated=" - + existingPdp.isDesignated() + ", updatedDate=" - + existingPdp.getUpdatedDate()); - myPdp = existingPdp; - } - - synchronized(myPdpSync){ - if(myPdp == null){ - - myPdp = new DroolsPdpImpl(resourceName,false,4,new Date()); - } - String siteName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.SITE_NAME); - if (siteName == null) { - siteName = ""; - }else{ - siteName = siteName.trim(); - } - myPdp.setSiteName(siteName); - if(electionHandler == null){ - electionHandler = new DroolsPdpsElectionHandler(conn,myPdp); - } - } - logger.info("\n\nThis controller is a standby, waiting to be chosen as primary...\n\n"); - } - - - /* - * Moved code to instantiate a JpaDroolsPdpsConnector object from main() to - * this method, so it can also be accessed from StandbyStateChangeNotifier - * class. - */ - public static DroolsPdpsConnector getDroolsPdpsConnector(String pu) { - - Map<String, Object> propMap = new HashMap<>(); - propMap.put("javax.persistence.jdbc.driver", ActiveStandbyProperties - .getProperty(ActiveStandbyProperties.DB_DRIVER)); - propMap.put("javax.persistence.jdbc.url", - ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_URL)); - propMap.put("javax.persistence.jdbc.user", ActiveStandbyProperties - .getProperty(ActiveStandbyProperties.DB_USER)); - propMap.put("javax.persistence.jdbc.password", - ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_PWD)); - - EntityManagerFactory emf = Persistence.createEntityManagerFactory( - pu, propMap); - return new JpaDroolsPdpsConnector(emf); - } - - /** - * {@inheritDoc} - */ - @Override - public String getPdpdNowActive(){ - return electionHandler.getPdpdNowActive(); - } - - /** - * {@inheritDoc} - */ - @Override - public String getPdpdLastActive(){ - return electionHandler.getPdpdLastActive(); - } - - /** - * {@inheritDoc} - */ - @Override - public String getResourceName() { - return myPdp.getPdpId(); - } + PolicySessionFeatureAPI, PolicyEngineFeatureAPI { + // get an instance of logger + private static final Logger logger = + LoggerFactory.getLogger(ActiveStandbyFeature.class); + + private static DroolsPdp myPdp; + private static Object myPdpSync = new Object(); + private static DroolsPdpsElectionHandler electionHandler; + + private StateManagementFeatureAPI stateManagementFeature; + + public static final int SEQ_NUM = 1; + + + /**************************/ + /* 'FeatureAPI' interface */ + /**************************/ + + /** + * {@inheritDoc} + */ + @Override + public int getSequenceNumber() { + return SEQ_NUM; + } + + /** + * {@inheritDoc} + */ + @Override + public void globalInit(String[] args, String configDir) { + // This must come first since it initializes myPdp + initializePersistence(configDir); + + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + if (feature.getResourceName().equals(myPdp.getPdpId())) { + if (logger.isDebugEnabled()) { + logger.debug("ActiveStandbyFeature.globalInit: Found StateManagementFeature" + + " with resourceName: {}", myPdp.getPdpId()); + } + stateManagementFeature = feature; + break; + } + } + if (stateManagementFeature == null) { + if (logger.isDebugEnabled()) { + logger.debug("ActiveStandbyFeature failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", myPdp.getPdpId()); + } + logger.error("ActiveStandbyFeature failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", myPdp.getPdpId()); + // + // Cannot add observer since stateManagementFeature is null + // + return; + } + + + + //Create an instance of the Observer + PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier(); + + //Register the PMStandbyStateChangeNotifier Observer + stateManagementFeature.addObserver(pmNotifier); + if (logger.isDebugEnabled()) { + logger.debug("ActiveStandbyFeature.globalInit() exit"); + } + } + + + /** + * {@inheritDoc} + */ + @Override + public boolean afterStart(PolicyEngine engine) { + // ASSERTION: engine == PolicyEngine.manager + PolicyEngine.manager.lock(); + return false; + } + + /** + * Read in the persistence properties, determine whether persistence is + * enabled or disabled, and initialize persistence if enabled. + */ + private static void initializePersistence(String configDir) { + //Get the Active Standby properties + try { + Properties activeStandbyProperties = + PropertyUtil.getProperties(configDir + "/feature-active-standby-management.properties"); + ActiveStandbyProperties.initProperties(activeStandbyProperties); + logger.info("initializePersistence: ActiveStandbyProperties success"); + } catch (IOException e) { + logger.error("ActiveStandbyFeature: initializePersistence ActiveStandbyProperties", e); + } + + DroolsPdpsConnector conn = getDroolsPdpsConnector("activeStandbyPU"); + String resourceName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME); + if (resourceName == null) { + throw new NullPointerException(); + } + + /* + * In a JUnit test environment, one or more PDPs may already have been + * inserted in the DB, so we need to check for this. + */ + DroolsPdp existingPdp = conn.getPdp(resourceName); + if (existingPdp != null) { + logger.info("Found existing PDP record, pdpId=" + + existingPdp.getPdpId() + ", isDesignated=" + + existingPdp.isDesignated() + ", updatedDate=" + + existingPdp.getUpdatedDate()); + myPdp = existingPdp; + } + + synchronized (myPdpSync) { + if (myPdp == null) { + + myPdp = new DroolsPdpImpl(resourceName,false,4,new Date()); + } + String siteName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.SITE_NAME); + if (siteName == null) { + siteName = ""; + } else { + siteName = siteName.trim(); + } + myPdp.setSiteName(siteName); + if (electionHandler == null) { + electionHandler = new DroolsPdpsElectionHandler(conn,myPdp); + } + } + logger.info("\n\nThis controller is a standby, waiting to be chosen as primary...\n\n"); + } + + + /** + * Moved code to instantiate a JpaDroolsPdpsConnector object from main() to + * this method, so it can also be accessed from StandbyStateChangeNotifier + * class. + * + * @param pu string + * @return connector object + */ + public static DroolsPdpsConnector getDroolsPdpsConnector(String pu) { + + Map<String, Object> propMap = new HashMap<>(); + propMap.put("javax.persistence.jdbc.driver", ActiveStandbyProperties + .getProperty(ActiveStandbyProperties.DB_DRIVER)); + propMap.put("javax.persistence.jdbc.url", + ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_URL)); + propMap.put("javax.persistence.jdbc.user", ActiveStandbyProperties + .getProperty(ActiveStandbyProperties.DB_USER)); + propMap.put("javax.persistence.jdbc.password", + ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_PWD)); + + EntityManagerFactory emf = Persistence.createEntityManagerFactory( + pu, propMap); + return new JpaDroolsPdpsConnector(emf); + } + + /** + * {@inheritDoc} + */ + @Override + public String getPdpdNowActive() { + return electionHandler.getPdpdNowActive(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getPdpdLastActive() { + return electionHandler.getPdpdLastActive(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getResourceName() { + return myPdp.getPdpId(); + } } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java index a70c71f6..009ff8c6 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,51 +25,53 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ActiveStandbyProperties { - // get an instance of logger - private static final Logger logger = LoggerFactory.getLogger(ActiveStandbyProperties.class); - - public static final String PDP_CHECK_INVERVAL = "pdp.checkInterval"; - public static final String PDP_UPDATE_INTERVAL = "pdp.updateInterval"; - public static final String PDP_TIMEOUT = "pdp.timeout"; - public static final String PDP_INITIAL_WAIT_PERIOD = "pdp.initialWait"; + // get an instance of logger + private static final Logger logger = LoggerFactory.getLogger(ActiveStandbyProperties.class); - public static final String NODE_NAME = "resource.name"; - public static final String SITE_NAME = "site_name"; - - /* - * feature-active-standby-management.properties parameter key values - */ - public static final String DB_DRIVER = "javax.persistence.jdbc.driver"; - public static final String DB_URL = "javax.persistence.jdbc.url"; - public static final String DB_USER = "javax.persistence.jdbc.user"; - public static final String DB_PWD = "javax.persistence.jdbc.password"; - - private static Properties properties = null; - - private ActiveStandbyProperties() { - throw new IllegalStateException("Utility class"); - } - /* - * Initialize the parameter values from the droolsPersitence.properties file values - * - * This is designed so that the Properties object is obtained from properties - * file and then is passed to this method to initialize the value of the parameters. - * This allows the flexibility of JUnit tests using getProperties(filename) to get the - * properties while runtime methods can use getPropertiesFromClassPath(filename). - * - */ - public static void initProperties (Properties prop){ - logger.info("ActiveStandbyProperties.initProperties(Properties): entry"); - logger.info("\n\nActiveStandbyProperties.initProperties: Properties = \n{}\n\n", prop); - - properties = prop; - } + public static final String PDP_CHECK_INVERVAL = "pdp.checkInterval"; + public static final String PDP_UPDATE_INTERVAL = "pdp.updateInterval"; + public static final String PDP_TIMEOUT = "pdp.timeout"; + public static final String PDP_INITIAL_WAIT_PERIOD = "pdp.initialWait"; - public static String getProperty(String key){ - return properties.getProperty(key); - } - - public static Properties getProperties() { - return properties; - } + public static final String NODE_NAME = "resource.name"; + public static final String SITE_NAME = "site_name"; + + /* + * feature-active-standby-management.properties parameter key values + */ + public static final String DB_DRIVER = "javax.persistence.jdbc.driver"; + public static final String DB_URL = "javax.persistence.jdbc.url"; + public static final String DB_USER = "javax.persistence.jdbc.user"; + public static final String DB_PWD = "javax.persistence.jdbc.password"; + + private static Properties properties = null; + + private ActiveStandbyProperties() { + throw new IllegalStateException("Utility class"); + } + + /** + * Initialize the parameter values from the droolsPersitence.properties file values. + * + * <p>This is designed so that the Properties object is obtained from properties + * file and then is passed to this method to initialize the value of the parameters. + * This allows the flexibility of JUnit tests using getProperties(filename) to get the + * properties while runtime methods can use getPropertiesFromClassPath(filename). + * + * @param prop properties + */ + public static void initProperties(Properties prop) { + logger.info("ActiveStandbyProperties.initProperties(Properties): entry"); + logger.info("\n\nActiveStandbyProperties.initProperties: Properties = \n{}\n\n", prop); + + properties = prop; + } + + public static String getProperty(String key) { + return properties.getProperty(key); + } + + public static Properties getProperties() { + return properties; + } } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java index a440a7e1..46096bdd 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,16 +24,27 @@ import java.util.Date; public interface DroolsPdp { - public String getPdpId(); - public boolean isDesignated(); - public int getPriority(); - public Date getUpdatedDate(); - public void setDesignated(boolean isDesignated); - public void setUpdatedDate(Date updatedDate); - public int comparePriority(DroolsPdp other); - public int comparePriority(DroolsPdp other,String previousSite); - public String getSiteName(); - public void setSiteName(String siteName); - public Date getDesignatedDate(); - public void setDesignatedDate(Date designatedDate); + public String getPdpId(); + + public boolean isDesignated(); + + public int getPriority(); + + public Date getUpdatedDate(); + + public void setDesignated(boolean isDesignated); + + public void setUpdatedDate(Date updatedDate); + + public int comparePriority(DroolsPdp other); + + public int comparePriority(DroolsPdp other,String previousSite); + + public String getSiteName(); + + public void setSiteName(String siteName); + + public Date getDesignatedDate(); + + public void setDesignatedDate(Date designatedDate); } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java index ed10f4c2..f0fd2789 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,121 +37,123 @@ import org.onap.policy.drools.activestandby.DroolsPdpObject; //@Table(name="DroolsPdpEntity") @NamedQueries({ - @NamedQuery(name="DroolsPdpEntity.findAll", query="SELECT e FROM DroolsPdpEntity e "), - @NamedQuery(name="DroolsPdpEntity.deleteAll", query="DELETE FROM DroolsPdpEntity WHERE 1=1") + @NamedQuery(name = "DroolsPdpEntity.findAll", query = "SELECT e FROM DroolsPdpEntity e "), + @NamedQuery(name = "DroolsPdpEntity.deleteAll", query = "DELETE FROM DroolsPdpEntity WHERE 1=1") }) -public class DroolsPdpEntity extends DroolsPdpObject implements Serializable{ - - private static final long serialVersionUID = 1L; - - @Id - @Column(name="pdpId", nullable=false) - private String pdpId="-1"; - - @Column(name="designated", nullable=false) - private boolean designated=false; - - @Column(name="priority", nullable=false) - private int priority=0; - - @Temporal(TemporalType.TIMESTAMP) - @Column(name="updatedDate", nullable=false) - private Date updatedDate; - - @Temporal(TemporalType.TIMESTAMP) - @Column(name="designatedDate",nullable=false) - private Date designatedDate; - - @Column(name="site", nullable=true, length = 50) - private String site; - - - public DroolsPdpEntity(){ - updatedDate = new Date(); - //When this is translated to a TimeStamp in MySQL, it assumes the date is relative - //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969 - //which is an invalid value for the MySql TimeStamp - designatedDate = new Date(864000000); - } - - @Override - public String getPdpId() { - return this.pdpId; - } - - public void setPdpId(String pdpId) { - this.pdpId = pdpId; - } - - @Override - public boolean isDesignated() { - return this.designated; - } - - @Override - public int getPriority() { - return this.priority; - } - - public void setPriority(int priority) { - this.priority = priority; - } - - @Override - public Date getUpdatedDate() { - return this.updatedDate; - } - - @Override - public void setDesignated(boolean isDesignated) { - this.designated=isDesignated; - } - - @Override - public void setUpdatedDate(Date updatedDate) { - this.updatedDate=updatedDate; - } - - - @Override - public String getSiteName() { - return site; - } - - @Override - public void setSiteName(String siteName) { - site = siteName; - - } - - @Override - public Date getDesignatedDate() { - return designatedDate; - } - - @Override - public void setDesignatedDate(Date designatedDate) { - this.designatedDate = designatedDate; - } - - @Override - public boolean equals(Object obj){ - - if (obj instanceof DroolsPdp) { - DroolsPdpEntity d = (DroolsPdpEntity) obj; - return this.pdpId.equals(d.getPdpId()); - } else { - return false; - } - - } - - @Override - public int hashCode() { +public class DroolsPdpEntity extends DroolsPdpObject implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @Column(name = "pdpId", nullable = false) + private String pdpId = "-1"; + + @Column(name = "designated", nullable = false) + private boolean designated = false; + + @Column(name = "priority", nullable = false) + private int priority = 0; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "updatedDate", nullable = false) + private Date updatedDate; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "designatedDate",nullable = false) + private Date designatedDate; + + @Column(name = "site", nullable = true, length = 50) + private String site; + + /** + * Constructor. + */ + public DroolsPdpEntity() { + updatedDate = new Date(); + //When this is translated to a TimeStamp in MySQL, it assumes the date is relative + //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969 + //which is an invalid value for the MySql TimeStamp + designatedDate = new Date(864000000); + } + + @Override + public String getPdpId() { + return this.pdpId; + } + + public void setPdpId(String pdpId) { + this.pdpId = pdpId; + } + + @Override + public boolean isDesignated() { + return this.designated; + } + + @Override + public int getPriority() { + return this.priority; + } + + public void setPriority(int priority) { + this.priority = priority; + } + + @Override + public Date getUpdatedDate() { + return this.updatedDate; + } + + @Override + public void setDesignated(boolean isDesignated) { + this.designated = isDesignated; + } + + @Override + public void setUpdatedDate(Date updatedDate) { + this.updatedDate = updatedDate; + } + + + @Override + public String getSiteName() { + return site; + } + + @Override + public void setSiteName(String siteName) { + site = siteName; + + } + + @Override + public Date getDesignatedDate() { + return designatedDate; + } + + @Override + public void setDesignatedDate(Date designatedDate) { + this.designatedDate = designatedDate; + } + + @Override + public boolean equals(Object obj) { + + if (obj instanceof DroolsPdp) { + DroolsPdpEntity entity = (DroolsPdpEntity) obj; + return this.pdpId.equals(entity.getPdpId()); + } else { + return false; + } + + } + + @Override + public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (this.pdpId == null ? 0 : this.pdpId.hashCode()); - return result; - } + return result; + } } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java index f54a18cc..31728bc2 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,89 +24,104 @@ import java.util.Date; public class DroolsPdpImpl extends DroolsPdpObject { - private boolean designated; - private int priority; - private Date updatedDate; - private Date designatedDate; - private String pdpId; - private String site; - - public DroolsPdpImpl(String pdpId, boolean designated, int priority, Date updatedDate){ - this.pdpId = pdpId; - this.designated = designated; - this.priority = priority; - this.updatedDate = updatedDate; - //When this is translated to a TimeStamp in MySQL, it assumes the date is relative - //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969 - //which is an invalid value for the MySql TimeStamp - this.designatedDate = new Date(864000000); - - } - @Override - public boolean isDesignated() { - - return designated; - } - - @Override - public int getPriority() { - return priority; - } - @Override - public void setUpdatedDate(Date date){ - this.updatedDate = date; - } - @Override - public Date getUpdatedDate() { - return updatedDate; - } - - @Override - public String getPdpId() { - return pdpId; - } - @Override - public void setDesignated(boolean isDesignated) { - this.designated = isDesignated; - - } - - @Override - public String getSiteName() { - return site; - } - @Override - public void setSiteName(String siteName) { - this.site = siteName; - - } - @Override - public Date getDesignatedDate() { - return designatedDate; - } - @Override - public void setDesignatedDate(Date designatedDate) { - this.designatedDate = designatedDate; - - } - - @Override - public boolean equals(Object obj){ - - - if (obj instanceof DroolsPdp) { - DroolsPdpImpl p = (DroolsPdpImpl) obj; - return this.pdpId.equals(p.getPdpId()); - } else { - return false; - } - } - - @Override - public int hashCode() { + private boolean designated; + private int priority; + private Date updatedDate; + private Date designatedDate; + private String pdpId; + private String site; + + /** + * Contructor. + * + * @param pdpId ID for the PDP + * @param designated is designated + * @param priority priority + * @param updatedDate date updated + */ + public DroolsPdpImpl(String pdpId, boolean designated, int priority, Date updatedDate) { + this.pdpId = pdpId; + this.designated = designated; + this.priority = priority; + this.updatedDate = updatedDate; + //When this is translated to a TimeStamp in MySQL, it assumes the date is relative + //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969 + //which is an invalid value for the MySql TimeStamp + this.designatedDate = new Date(864000000); + + } + + @Override + public boolean isDesignated() { + + return designated; + } + + @Override + public int getPriority() { + return priority; + } + + @Override + public void setUpdatedDate(Date date) { + this.updatedDate = date; + } + + @Override + public Date getUpdatedDate() { + return updatedDate; + } + + @Override + public String getPdpId() { + return pdpId; + } + + @Override + public void setDesignated(boolean isDesignated) { + this.designated = isDesignated; + + } + + @Override + public String getSiteName() { + return site; + } + + @Override + public void setSiteName(String siteName) { + this.site = siteName; + + } + + @Override + public Date getDesignatedDate() { + return designatedDate; + } + + @Override + public void setDesignatedDate(Date designatedDate) { + this.designatedDate = designatedDate; + + } + + @Override + public boolean equals(Object obj) { + + + if (obj instanceof DroolsPdp) { + DroolsPdpImpl temp = (DroolsPdpImpl) obj; + return this.pdpId.equals(temp.getPdpId()); + } else { + return false; + } + } + + @Override + public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (this.pdpId == null ? 0 : this.pdpId.hashCode()); - return result; - } + return result; + } } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java index 690b260c..49f6b02d 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java @@ -21,60 +21,64 @@ package org.onap.policy.drools.activestandby; -public abstract class DroolsPdpObject implements DroolsPdp{ - - @Override - public boolean equals(Object other){ - if(other instanceof DroolsPdp){ - return this.getPdpId().equals(((DroolsPdp)other).getPdpId()); - }else{ - return false; - } - } - @Override - public int hashCode() { +public abstract class DroolsPdpObject implements DroolsPdp { + + @Override + public boolean equals(Object other) { + if (other instanceof DroolsPdp) { + return this.getPdpId().equals(((DroolsPdp)other).getPdpId()); + } else { + return false; + } + } + + @Override + public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (this.getPdpId() == null ? 0 : this.getPdpId().hashCode()); result = prime * result + (this.getSiteName() == null ? 0 : this.getSiteName().hashCode()); result = prime * result + this.getPriority(); - return super.hashCode(); - } - private int nullSafeCompare(String one, String two){ - if(one != null && two != null){ - return one.compareTo(two); - } - if(one == null && two != null){ - return -1; - } - if(one != null && two == null){ - return 1; - } - return 0; - } - @Override - public int comparePriority(DroolsPdp other){ - if(nullSafeCompare(this.getSiteName(),other.getSiteName()) == 0){ - if(this.getPriority() != other.getPriority()){ - return this.getPriority() - other.getPriority(); - } - return this.getPdpId().compareTo(other.getPdpId()); - } else { - return nullSafeCompare(this.getSiteName(),other.getSiteName()); - } - } - @Override - public int comparePriority(DroolsPdp other, String previousSite){ - if(previousSite == null || previousSite.isEmpty()){ - return comparePriority(other); - } - if(nullSafeCompare(this.getSiteName(),other.getSiteName()) == 0){ - if(this.getPriority() != other.getPriority()){ - return this.getPriority() - other.getPriority(); - } - return this.getPdpId().compareTo(other.getPdpId()); - } else { - return nullSafeCompare(this.getSiteName(),other.getSiteName()); - } - } + return super.hashCode(); + } + + private int nullSafeCompare(String one, String two) { + if (one != null && two != null) { + return one.compareTo(two); + } + if (one == null && two != null) { + return -1; + } + if (one != null && two == null) { + return 1; + } + return 0; + } + + @Override + public int comparePriority(DroolsPdp other) { + if (nullSafeCompare(this.getSiteName(),other.getSiteName()) == 0) { + if (this.getPriority() != other.getPriority()) { + return this.getPriority() - other.getPriority(); + } + return this.getPdpId().compareTo(other.getPdpId()); + } else { + return nullSafeCompare(this.getSiteName(),other.getSiteName()); + } + } + + @Override + public int comparePriority(DroolsPdp other, String previousSite) { + if (previousSite == null || previousSite.isEmpty()) { + return comparePriority(other); + } + if (nullSafeCompare(this.getSiteName(),other.getSiteName()) == 0) { + if (this.getPriority() != other.getPriority()) { + return this.getPriority() - other.getPriority(); + } + return this.getPdpId().compareTo(other.getPdpId()); + } else { + return nullSafeCompare(this.getSiteName(),other.getSiteName()); + } + } } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java index d0d33f0f..fdb87378 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,40 +24,41 @@ import java.util.Collection; public interface DroolsPdpsConnector { - - //return a list of PDPs, NOT including this PDP - public Collection<DroolsPdp> getDroolsPdps(); - - public void update(DroolsPdp pdp); - - //determines if the DroolsPdp parameter is considered "current" or expired (has it been too long since the Pdp sent an update) - public boolean isPdpCurrent(DroolsPdp pdp); - - // Updates DESIGNATED boolean in PDP record. - public void setDesignated(DroolsPdp pdp, boolean designated); - - // Marks droolspdpentity.DESIGNATED=false, so another PDP-D will go active. - public void standDownPdp(String pdpId); - - // This is used in a JUnit test environment to manually - // insert a PDP - public void insertPdp(DroolsPdp pdp); - - // This is used in a JUnit test environment to manually - // delete a PDP - public void deletePdp(String pdpId); - - // This is used in a JUnit test environment to manually - // clear the droolspdpentity table. - public void deleteAllPdps(); - - // This is used in a JUnit test environment to manually - // get a PDP - public DroolsPdpEntity getPdp(String pdpId); - - // Used by DroolsPdpsElectionHandler to determine if the currently designated - // PDP has failed. - public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps); - - + + //return a list of PDPs, NOT including this PDP + public Collection<DroolsPdp> getDroolsPdps(); + + public void update(DroolsPdp pdp); + + //determines if the DroolsPdp parameter is considered "current" or expired + //(has it been too long since the Pdp sent an update) + public boolean isPdpCurrent(DroolsPdp pdp); + + // Updates DESIGNATED boolean in PDP record. + public void setDesignated(DroolsPdp pdp, boolean designated); + + // Marks droolspdpentity.DESIGNATED=false, so another PDP-D will go active. + public void standDownPdp(String pdpId); + + // This is used in a JUnit test environment to manually + // insert a PDP + public void insertPdp(DroolsPdp pdp); + + // This is used in a JUnit test environment to manually + // delete a PDP + public void deletePdp(String pdpId); + + // This is used in a JUnit test environment to manually + // clear the droolspdpentity table. + public void deleteAllPdps(); + + // This is used in a JUnit test environment to manually + // get a PDP + public DroolsPdpEntity getPdp(String pdpId); + + // Used by DroolsPdpsElectionHandler to determine if the currently designated + // PDP has failed. + public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps); + + } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java index 9b172e13..08592f23 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java @@ -28,991 +28,996 @@ import java.util.Timer; import java.util.TimerTask; import org.onap.policy.common.im.StateManagement; +import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI; public class DroolsPdpsElectionHandler implements ThreadRunningChecker { - // get an instance of logger - private static final Logger logger = LoggerFactory.getLogger(DroolsPdpsElectionHandler.class); - private DroolsPdpsConnector pdpsConnector; - private Object checkWaitTimerLock = new Object(); - private Object designationWaiterLock = new Object(); - - /* - * Must be static, so it can be referenced by JpaDroolsPdpsConnector, - * without requiring a reference to the election handler instantiation. - */ - private static DroolsPdp myPdp; - - private DesignationWaiter designationWaiter; - private Timer updateWorker; - private Timer waitTimer; - private Date waitTimerLastRunDate; - - // The interval between checks of the DesignationWaiter to be sure it is running. - private int pdpCheckInterval; - - // The interval between runs of the DesignationWaiter - private int pdpUpdateInterval; - - private volatile boolean isDesignated; - - private String pdpdNowActive; - private String pdpdLastActive; - - /* - * Start allSeemsWell with a value of null so that, on the first run - * of the checkWaitTimer it will set the value in IntegrityMonitor - * regardless of whether it needs to be set to true or false. - */ - private Boolean allSeemsWell=null; - - private StateManagementFeatureAPI stateManagementFeature; - - private static boolean isUnitTesting = false; - private static boolean isStalled = false; - - public DroolsPdpsElectionHandler(DroolsPdpsConnector pdps, DroolsPdp myPdp){ - if (pdps == null) { - logger.error("DroolsPdpsElectinHandler(): pdpsConnector==null"); - throw new IllegalArgumentException("DroolsPdpsElectinHandler(): pdpsConnector==null"); - } - if (myPdp == null){ - logger.error("DroolsPdpsElectinHandler(): droolsPdp==null"); - throw new IllegalArgumentException("DroolsPdpsElectinHandler(): DroolsPdp==null"); - } - - pdpdNowActive = null; - pdpdLastActive = null; - this.pdpsConnector = pdps; - DroolsPdpsElectionHandler.myPdp = myPdp; - this.isDesignated = false; - pdpCheckInterval = 3000; - try{ - pdpCheckInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_CHECK_INVERVAL)); - }catch(Exception e){ - logger.error - ("Could not get pdpCheckInterval property. Using default {}",pdpCheckInterval, e); - } - pdpUpdateInterval = 2000; - try{ - pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL)); - }catch(Exception e){ - logger.error - ("Could not get pdpUpdateInterval property. Using default {} ", pdpUpdateInterval, e); - } - - Date now = new Date(); - - // Retrieve the ms since the epoch - long nowMs = now.getTime(); - - // Create the timer which will update the updateDate in DroolsPdpEntity table. - // This is the heartbeat - updateWorker = new Timer(); - - // Schedule the TimerUpdateClass to run at 100 ms and run at pdpCheckInterval ms thereafter - // NOTE: The first run of the TimerUpdateClass results in myPdp being added to the - // drools droolsPdpEntity table. - updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval); - - // Create the timer which will run the election algorithm - waitTimer = new Timer(); - - // Schedule it to start in startMs ms (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter - long startMs = getDWaiterStartMs(); - designationWaiter = new DesignationWaiter(); - waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval); - waitTimerLastRunDate = new Date(nowMs + startMs); - - //Get the StateManagementFeature instance - - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - if (feature.getResourceName().equals(myPdp.getPdpId())) - { - if(logger.isDebugEnabled()){ - logger.debug("DroolsPdpsElectionHandler: Found StateManagementFeature" - + " with resourceName: {}", myPdp.getPdpId()); - } - stateManagementFeature = feature; - break; - } - } - if(stateManagementFeature == null){ - logger.error("DroolsPdpsElectionHandler failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", myPdp.getPdpId()); - } - } - - public static void setIsUnitTesting(boolean val){ - isUnitTesting = val; - } - public static void setIsStalled(boolean val){ - isStalled = val; - } - - /* - * When the JpaDroolsPdpsConnector.standDown() method is invoked, it needs - * access to myPdp, so it can keep its designation status in sync with the - * DB. - */ - public static void setMyPdpDesignated(boolean designated) { - if(logger.isDebugEnabled()){ - logger.debug - ("setMyPdpDesignated: designated= {}", designated); - } - myPdp.setDesignated(designated); - } - - private class DesignationWaiter extends TimerTask { - // get an instance of logger - private final Logger logger = LoggerFactory.getLogger(DesignationWaiter.class); - - @Override - public void run() { - try{ - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: Entering"); - } - - //This is for testing the checkWaitTimer - if(isUnitTesting && isStalled){ - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWaiter.run: isUnitTesting = {} isStalled = {}", isUnitTesting, isStalled); - } - return; - } - - synchronized (designationWaiterLock) { - - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: Entering synchronized block"); - } - - //It is possible that multiple PDPs are designated lead. So, we will make a list of all designated - //PDPs and then decide which one really should be designated at the end. - List<DroolsPdp> listOfDesignated = new ArrayList<>(); - - Collection<DroolsPdp> pdps = pdpsConnector.getDroolsPdps(); - DroolsPdp designatedPdp = null; - - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: pdps.size= {}", pdps.size()); - } - - //This is only true if all designated PDPs have failed - boolean designatedPdpHasFailed = pdpsConnector.hasDesignatedPdpFailed(pdps); - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: designatedPdpHasFailed= {}", designatedPdpHasFailed); - } - for (DroolsPdp pdp : pdps) { - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: evaluating pdp ID: {}", pdp.getPdpId()); - } - - /* - * Note: side effect of isPdpCurrent is that any stale but - * designated PDPs will be marked as un-designated. - */ - boolean isCurrent = pdpsConnector.isPdpCurrent(pdp); - - /* - * We can't use stateManagement.getStandbyStatus() here, because - * we need the standbyStatus, not for this PDP, but for the PDP - * being processed by this loop iteration. - */ - String standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId()); - if(standbyStatus==null){ - // Treat this case as a cold standby -- if we - // abort here, no sessions will be created in a - // single-node test environment. - standbyStatus = StateManagement.COLD_STANDBY; - } - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: PDP= {}, isCurrent= {}", pdp.getPdpId(), isCurrent); - } - - /* - * There are 4 combinations of isDesignated and isCurrent. We will examine each one in-turn - * and evaluate the each pdp in the list of pdps against each combination. - * - * This is the first combination of isDesignated and isCurrent - */ - if (pdp.isDesignated() && isCurrent) { - //It is current, but it could have a standbystatus=coldstandby / hotstandby - //If so, we need to stand it down and demote it - if(!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)){ - if(pdp.getPdpId().equals(myPdp.getPdpId())){ - if(logger.isDebugEnabled()){ - logger.debug - ("\n\nDesignatedWaiter.run: myPdp {} is current and designated, " - + "butstandbystatus is not providingservice. " - + " Executing stateManagement.demote()" + "\n\n", myPdp.getPdpId()); - } - // So, we must demote it - try { - //Keep the order like this. StateManagement is last since it triggers controller shutdown - //This will change isDesignated and it can enter another if(combination) below - pdpsConnector.standDownPdp(pdp.getPdpId()); - myPdp.setDesignated(false); - isDesignated = false; - if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) || - standbyStatus.equals(StateManagement.COLD_STANDBY))){ - /* - * Only demote it if it appears it has not already been demoted. Don't worry - * about synching with the topic endpoint states. That is done by the - * refreshStateAudit - */ - stateManagementFeature.demote(); - } - //update the standbystatus to check in a later combination of isDesignated and isCurrent - standbyStatus=stateManagementFeature.getStandbyStatus(pdp.getPdpId()); - } catch (Exception e) { - logger.error - ("DesignatedWaiter.run: myPdp: {} " - + "Caught Exception attempting to demote myPdp," - + "message= {}", myPdp.getPdpId(), e); - } - }else{ - // Don't demote a remote PDP that is current. It should catch itself - if(logger.isDebugEnabled()){ - logger.debug - ("\n\nDesignatedWaiter.run: myPdp {} is current and designated, " - + "but standbystatus is not providingservice. " - + " Cannot execute stateManagement.demote() since it it is not myPdp\n\n", myPdp.getPdpId()); - } - } - - }else{ - // If we get here, it is ok to be on the list - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: PDP= {} is designated, current and {} Noting PDP as " - + "designated, standbyStatus= {}", pdp.getPdpId(), standbyStatus, standbyStatus); - } - listOfDesignated.add(pdp); - } - - - } - - - /* - * The second combination of isDesignated and isCurrent - * - * PDP is designated but not current; it has failed. So we stand it down (it doesn't matter what - * its standbyStatus is). None of these go on the list. - */ - if (pdp.isDesignated() && !isCurrent) { - if(logger.isDebugEnabled()){ - logger.debug - ("INFO: DesignatedWaiter.run: PDP= {} is currently designated but is not current; " - + "it has failed. Standing down. standbyStatus= {}", pdp.getPdpId(), standbyStatus); - } - /* - * Changes designated to 0 but it is still potentially providing service - * Will affect isDesignated, so, it can enter an if(combination) below - */ - pdpsConnector.standDownPdp(pdp.getPdpId()); - - //need to change standbystatus to coldstandby - if (pdp.getPdpId().equals(myPdp.getPdpId())){ - if(logger.isDebugEnabled()){ - logger.debug - ("\n\nDesignatedWaiter.run: myPdp {} is not Current. " - + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId()); - } - // We found that myPdp is designated but not current - // So, we must cause it to disableFail - try { - myPdp.setDesignated(false); - pdpsConnector.setDesignated(myPdp, false); - isDesignated = false; - stateManagementFeature.disableFailed(); - } catch (Exception e) { - logger.error - ("DesignatedWaiter.run: myPdp: {} Caught Exception " - + "attempting to disableFail myPdp {}, message= {}", - myPdp.getPdpId(), myPdp.getPdpId(), e); - } - } else { //it is a remote PDP that is failed - if(logger.isDebugEnabled()){ - logger.debug - ("\n\nDesignatedWaiter.run: PDP {} is not Current. " - + " Executing stateManagement.disableFailed(otherResourceName)\n\n", pdp.getPdpId() ); - } - // We found a PDP is designated but not current - // We already called standdown(pdp) which will change designated to false - // Now we need to disableFail it to get its states in synch. The standbyStatus - // should equal coldstandby - try { - stateManagementFeature.disableFailed(pdp.getPdpId()); - } catch (Exception e) { - logger.error - ("DesignatedWaiter.run: for PDP {} Caught Exception attempting to " - + "disableFail({}), message= {}", - pdp.getPdpId(), pdp.getPdpId(), e); - } - - } - continue; //we are not going to do anything else with this pdp - } - - /* - * The third combination of isDesignated and isCurrent - * /* - * If a PDP is not currently designated but is providing service (erroneous, but recoverable) or hot standby - * we can add it to the list of possible designated if all the designated have failed - */ - if (!pdp.isDesignated() && isCurrent){ - if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) || - standbyStatus.equals(StateManagement.COLD_STANDBY))){ - if(logger.isDebugEnabled()){ - logger.debug("\n\nDesignatedWaiter.run: PDP {}" - + " is NOT designated but IS current and" - + " has a standbystatus= {}", pdp.getPdpId(), standbyStatus); - } - // Since it is current, we assume it can adjust its own state. - // We will demote if it is myPdp - if(pdp.getPdpId().equals(myPdp.getPdpId())){ - //demote it - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWaiter.run: PDP {} going to " - + "setDesignated = false and calling stateManagement.demote", pdp.getPdpId()); - } - try { - //Keep the order like this. StateManagement is last since it triggers controller shutdown - pdpsConnector.setDesignated(myPdp, false); - myPdp.setDesignated(false); - isDesignated = false; - //This is definitely not a redundant call. It is attempting to correct a problem - stateManagementFeature.demote(); - //recheck the standbystatus - standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId()); - } catch (Exception e) { - logger.error - ("DesignatedWaiter.run: myPdp: {} Caught Exception " - + "attempting to demote myPdp {}, message = {}", myPdp.getPdpId(), - myPdp.getPdpId(), e); - } - - } - } - if(standbyStatus.equals(StateManagement.HOT_STANDBY) && designatedPdpHasFailed){ - //add it to the list - if(logger.isDebugEnabled()){ - logger.debug - ("INFO: DesignatedWaiter.run: PDP= {}" - + " is not designated but is {} and designated PDP " - + "has failed. standbyStatus= {}", pdp.getPdpId(), - standbyStatus, standbyStatus); - } - listOfDesignated.add(pdp); - } - continue; //done with this one - } - - /* - * The fourth combination of isDesignated and isCurrent - * - * We are not going to put any of these on the list since it appears they have failed. - - * - */ - if(!pdp.isDesignated() && !isCurrent) { - if(logger.isDebugEnabled()){ - logger.debug - ("INFO: DesignatedWaiter.run: PDP= {} " - + "designated= {}, current= {}, " - + "designatedPdpHasFailed= {}, " - + "standbyStatus= {}",pdp.getPdpId(), - pdp.isDesignated(), isCurrent, designatedPdpHasFailed, standbyStatus); - } - if(!standbyStatus.equals(StateManagement.COLD_STANDBY)){ - //stand it down - //disableFail it - pdpsConnector.standDownPdp(pdp.getPdpId()); - if(pdp.getPdpId().equals(myPdp.getPdpId())){ - /* - * I don't actually know how this condition could happen, but if it did, we would want - * to declare it failed. - */ - if(logger.isDebugEnabled()){ - logger.debug - ("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, " - + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId()); - } - // So, we must disableFail it - try { - //Keep the order like this. StateManagement is last since it triggers controller shutdown - pdpsConnector.setDesignated(myPdp, false); - myPdp.setDesignated(false); - isDesignated = false; - stateManagementFeature.disableFailed(); - } catch (Exception e) { - logger.error - ("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to " - + "disableFail myPdp {}, message= {}", - myPdp.getPdpId(), myPdp.getPdpId(), e); - } - }else{//it is remote - if(logger.isDebugEnabled()){ - logger.debug - ("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, " - + " Executing stateManagement.disableFailed({})\n\n", - myPdp.getPdpId(), pdp.getPdpId()); - } - // We already called standdown(pdp) which will change designated to false - // Now we need to disableFail it to get its states in sync. StandbyStatus = coldstandby - try { - stateManagementFeature.disableFailed(pdp.getPdpId()); - } catch (Exception e) { - logger.error - ("DesignatedWaiter.run: for PDP {}" - + " Caught Exception attempting to disableFail({})" - + ", message=", pdp.getPdpId(), pdp.getPdpId(), e); - } - } - } - } - - - } // end pdps loop - - /* - * We have checked the four combinations of isDesignated and isCurrent. Where appropriate, - * we added the PDPs to the potential list of designated pdps - * - * We need to give priority to pdps on the same site that is currently being used - * First, however, we must sanitize the list of designated to make sure their are - * only designated members or non-designated members. There should not be both in - * the list. Because there are real time delays, it is possible that both types could - * be on the list. - */ - - listOfDesignated = santizeDesignatedList(listOfDesignated); - - /* - * We need to figure out the last pdp that was the primary so we can get the last site - * name and the last session numbers. We need to create a "dummy" droolspdp since - * it will be used in later comparisons and cannot be null. - */ - - DroolsPdp mostRecentPrimary = computeMostRecentPrimary(pdps, listOfDesignated); - - if(mostRecentPrimary != null){ - pdpdLastActive = mostRecentPrimary.getPdpId(); - } - - - /* - * It is possible to get here with more than one pdp designated and providingservice. This normally - * occurs when there is a race condition with multiple nodes coming up at the same time. If that is - * the case we must determine which one is the one that should be designated and which one should - * be demoted. - * - * It is possible to have 0, 1, 2 or more but not all, or all designated. - * If we have one designated and current, we chose it and are done - * If we have 2 or more, but not all, we must determine which one is in the same site as - * the previously designated pdp. - */ - - designatedPdp = computeDesignatedPdp(listOfDesignated, mostRecentPrimary); - if(designatedPdp != null){ - pdpdNowActive = designatedPdp.getPdpId(); - } - - if (designatedPdp == null) { - logger.warn - ("WARNING: DesignatedWaiter.run: No viable PDP found to be Designated. designatedPdp still null."); - // Just to be sure the parameters are correctly set - myPdp.setDesignated(false); - pdpsConnector.setDesignated(myPdp,false); - isDesignated = false; - - waitTimerLastRunDate = new Date(); - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWaiter.run (designatedPdp == null) waitTimerLastRunDate = {}", waitTimerLastRunDate); - } - myPdp.setUpdatedDate(waitTimerLastRunDate); - pdpsConnector.update(myPdp); - - return; - - } else if (designatedPdp.getPdpId().equals(myPdp.getPdpId())) { - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: designatedPdp is PDP={}", myPdp.getPdpId()); - } - /* - * update function expects myPdp.isDesignated to be true. - */ - try { - //Keep the order like this. StateManagement is last since it triggers controller init - myPdp.setDesignated(true); - myPdp.setDesignatedDate(new Date()); - pdpsConnector.setDesignated(myPdp, true); - isDesignated = true; - String standbyStatus = stateManagementFeature.getStandbyStatus(); - if(!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)){ - /* - * Only call promote if it is not already in the right state. Don't worry about - * synching the lower level topic endpoint states. That is done by the - * refreshStateAudit. - * Note that we need to fetch the session list from 'mostRecentPrimary' - * at this point -- soon, 'mostRecentPrimary' will be set to this host. - */ - //this.sessions = mostRecentPrimary.getSessions(); - stateManagementFeature.promote(); - } - } catch (Exception e) { - logger.error - ("ERROR: DesignatedWaiter.run: Caught Exception attempting to promote PDP={}" - + ", message=", myPdp.getPdpId(), e); - myPdp.setDesignated(false); - pdpsConnector.setDesignated(myPdp,false); - isDesignated = false; - //If you can't promote it, demote it - try { - String standbyStatus = stateManagementFeature.getStandbyStatus(); - if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) || - standbyStatus.equals(StateManagement.COLD_STANDBY))){ - /* - * Only call demote if it is not already in the right state. Don't worry about - * synching the lower level topic endpoint states. That is done by the - * refreshStateAudit. - */ - stateManagementFeature.demote(); - } - } catch (Exception e1) { - logger.error - ("ERROR: DesignatedWaiter.run: Caught StandbyStatusException " - + "attempting to promote then demote PDP={}, message=", - myPdp.getPdpId(), e1); - } - - } - waitTimerLastRunDate = new Date(); - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) " - + "waitTimerLastRunDate = " + waitTimerLastRunDate); - } - myPdp.setUpdatedDate(waitTimerLastRunDate); - pdpsConnector.update(myPdp); - - return; - } - isDesignated = false; - - } // end synchronized - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: myPdp: {}; Returning, isDesignated= {}", - isDesignated, myPdp.getPdpId()); - } - - Date tmpDate = new Date(); - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWaiter.run (end of run) waitTimerLastRunDate = {}", tmpDate); - } - - waitTimerLastRunDate = tmpDate; - myPdp.setUpdatedDate(waitTimerLastRunDate); - pdpsConnector.update(myPdp); - - }catch(Exception e){ - logger.error("DesignatedWaiter.run caught an unexpected exception: ", e); - } - } // end run - } - - public List<DroolsPdp> santizeDesignatedList(List<DroolsPdp> listOfDesignated){ - - boolean containsDesignated = false; - boolean containsHotStandby = false; - List<DroolsPdp> listForRemoval = new ArrayList<>(); - for(DroolsPdp pdp : listOfDesignated){ - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run sanitizing: pdp = {}" - + " isDesignated = {}",pdp.getPdpId(), pdp.isDesignated()); - } - if(pdp.isDesignated()){ - containsDesignated = true; - }else { - containsHotStandby = true; - listForRemoval.add(pdp); - } - } - if(containsDesignated && containsHotStandby){ - //remove the hot standby from the list - listOfDesignated.removeAll(listForRemoval); - } - return listOfDesignated; - } - - public DroolsPdp computeMostRecentPrimary(Collection<DroolsPdp> pdps, List<DroolsPdp> listOfDesignated){ - boolean containsDesignated = false; - for(DroolsPdp pdp : listOfDesignated){ - if(pdp.isDesignated()){ - containsDesignated = true; - } - } - DroolsPdp mostRecentPrimary = new DroolsPdpImpl(null, true, 1, new Date(0)); - mostRecentPrimary.setSiteName(null); - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run listOfDesignated.size() = {}", listOfDesignated.size()); - } - if(listOfDesignated.size() <=1){ - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWainter.run: listOfDesignated.size <=1"); - } - //Only one or none is designated or hot standby. Choose the latest designated date - for(DroolsPdp pdp : pdps){ - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run pdp = {}" - + " pdp.getDesignatedDate() = {}", pdp.getPdpId(), pdp.getDesignatedDate()); - } - if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){ - mostRecentPrimary = pdp; - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); - } - } - } - }else if(listOfDesignated.size() == pdps.size()){ - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWainter.run: listOfDesignated.size = pdps.size() which is {}", pdps.size()); - } - //They are all designated or all hot standby. - mostRecentPrimary = null; - for(DroolsPdp pdp : pdps){ - if(mostRecentPrimary == null){ - mostRecentPrimary = pdp; - continue; - } - if(containsDesignated){ //Choose the site of the first designated date - if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) < 0){ - mostRecentPrimary = pdp; - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); - } - } - }else{ //Choose the site with the latest designated date - if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){ - mostRecentPrimary = pdp; - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); - } - } - } - } - }else{ - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWainter.run: Some but not all are designated or hot standby. "); - } - //Some but not all are designated or hot standby. - if(containsDesignated){ - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated); - } - /* - * The list only contains designated. This is a problem. It is most likely a race - * condition that resulted in two thinking they should be designated. Choose the - * site with the latest designated date for the pdp not included on the designated list. - * This should be the site that had the last designation before this race condition - * occurred. - */ - for(DroolsPdp pdp : pdps){ - if(listOfDesignated.contains(pdp)){ - continue; //Don't consider this entry - } - if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){ - mostRecentPrimary = pdp; - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); - } - } - } - }else{ - if(logger.isDebugEnabled()){ - logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated); - } - //The list only contains hot standby. Choose the site of the latest designated date - for(DroolsPdp pdp : pdps){ - if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){ - mostRecentPrimary = pdp; - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); - } - } - } - } - } - return mostRecentPrimary; - } - - public DroolsPdp computeDesignatedPdp(List<DroolsPdp> listOfDesignated, DroolsPdp mostRecentPrimary){ - DroolsPdp designatedPdp = null; - DroolsPdp lowestPriorityPdp = null; - if(listOfDesignated.size() > 1){ - if(logger.isDebugEnabled()){ - logger.debug - ("DesignatedWaiter.run: myPdp: {} listOfDesignated.size(): {}", myPdp.getPdpId(), listOfDesignated.size()); - } - DroolsPdp rejectedPdp = null; - DroolsPdp lowestPrioritySameSite = null; - DroolsPdp lowestPriorityDifferentSite = null; - for(DroolsPdp pdp : listOfDesignated){ - // We need to determine if another PDP is the lowest priority - if(nullSafeEquals(pdp.getSiteName(),mostRecentPrimary.getSiteName())){ - if(lowestPrioritySameSite == null){ - if(lowestPriorityDifferentSite != null){ - rejectedPdp = lowestPriorityDifferentSite; - } - lowestPrioritySameSite = pdp; - }else{ - if(pdp.getPdpId().equals((lowestPrioritySameSite.getPdpId()))){ - continue;//nothing to compare - } - if(pdp.comparePriority(lowestPrioritySameSite) <0){ - if(logger.isDebugEnabled()){ - logger.debug - ("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}" - + " has lower priority than pdp ID: {}",myPdp.getPdpId(), pdp.getPdpId(), - lowestPrioritySameSite.getPdpId()); - } - //we need to reject lowestPrioritySameSite - rejectedPdp = lowestPrioritySameSite; - lowestPrioritySameSite = pdp; - } else{ - //we need to reject pdp and keep lowestPrioritySameSite - if(logger.isDebugEnabled()){ - logger.debug - ("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {} " - + " has higher priority than pdp ID: {}", myPdp.getPdpId(),pdp.getPdpId(), - lowestPrioritySameSite.getPdpId()); - } - rejectedPdp = pdp; - } - } - } else{ - if(lowestPrioritySameSite != null){ - //if we already have a candidate for same site, we don't want to bother with different sites - rejectedPdp = pdp; - } else{ - if(lowestPriorityDifferentSite == null){ - lowestPriorityDifferentSite = pdp; - continue; - } - if(pdp.getPdpId().equals((lowestPriorityDifferentSite.getPdpId()))){ - continue;//nothing to compare - } - if(pdp.comparePriority(lowestPriorityDifferentSite) <0){ - if(logger.isDebugEnabled()){ - logger.debug - ("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}" - + " has lower priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(), - lowestPriorityDifferentSite.getPdpId()); - } - //we need to reject lowestPriorityDifferentSite - rejectedPdp = lowestPriorityDifferentSite; - lowestPriorityDifferentSite = pdp; - } else{ - //we need to reject pdp and keep lowestPriorityDifferentSite - if(logger.isDebugEnabled()){ - logger.debug - ("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}" - + " has higher priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(), - lowestPriorityDifferentSite.getPdpId()); - } - rejectedPdp = pdp; - } - } - } - // If the rejectedPdp is myPdp, we need to stand it down and demote it. Each pdp is responsible - // for demoting itself - if(rejectedPdp != null && nullSafeEquals(rejectedPdp.getPdpId(),myPdp.getPdpId())){ - if(logger.isDebugEnabled()){ - logger.debug - ("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated myPdp ID: {}" - + " is NOT the lowest priority. Executing stateManagement.demote()\n\n", myPdp.getPdpId(), - myPdp.getPdpId()); - } - // We found that myPdp is on the listOfDesignated and it is not the lowest priority - // So, we must demote it - try { - //Keep the order like this. StateManagement is last since it triggers controller shutdown - myPdp.setDesignated(false); - pdpsConnector.setDesignated(myPdp, false); - isDesignated = false; - String standbyStatus = stateManagementFeature.getStandbyStatus(); - if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) || - standbyStatus.equals(StateManagement.COLD_STANDBY))){ - /* - * Only call demote if it is not already in the right state. Don't worry about - * synching the lower level topic endpoint states. That is done by the - * refreshStateAudit. - */ - stateManagementFeature.demote(); - } - } catch (Exception e) { - myPdp.setDesignated(false); - pdpsConnector.setDesignated(myPdp, false); - isDesignated = false; - logger.error - ("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to " - + "demote myPdp {} myPdp.getPdpId(), message= {}", myPdp.getPdpId(), - e); - } - } - } //end: for(DroolsPdp pdp : listOfDesignated) - if(lowestPrioritySameSite != null){ - lowestPriorityPdp = lowestPrioritySameSite; - } else { - lowestPriorityPdp = lowestPriorityDifferentSite; - } - //now we have a valid value for lowestPriorityPdp - if(logger.isDebugEnabled()){ - logger.debug - ("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated " - + "found the LOWEST priority pdp ID: {} " - + " It is now the designatedPpd from the perspective of myPdp ID: {} \n\n", - myPdp.getPdpId(), lowestPriorityPdp.getPdpId(), myPdp); - } - designatedPdp = lowestPriorityPdp; - - } else if(listOfDesignated.isEmpty()){ - if(logger.isDebugEnabled()){ - logger.debug - ("\nDesignatedWaiter.run: myPdp: {} listOfDesignated is: EMPTY.", myPdp.getPdpId()); - } - designatedPdp = null; - } else{ //only one in listOfDesignated - if(logger.isDebugEnabled()){ - logger.debug - ("\nDesignatedWaiter.run: myPdp: {} listOfDesignated " - + "has ONE entry. PDP ID: {}", myPdp.getPdpId(), listOfDesignated.get(0).getPdpId()); - } - designatedPdp = listOfDesignated.get(0); - } - return designatedPdp; - - } - - private class TimerUpdateClass extends TimerTask{ - - @Override - public void run() { - try{ - if(logger.isDebugEnabled()){ - logger.debug("TimerUpdateClass.run: entry"); - } - checkWaitTimer(); - }catch(Exception e){ - logger.error("TimerUpdateClass.run caught an unexpected exception: ", e); - } - if(logger.isDebugEnabled()){ - logger.debug("TimerUpdateClass.run.exit"); - } - } - } - @Override - public void checkThreadStatus() { - checkWaitTimer(); - } - - private void checkWaitTimer(){ - synchronized(checkWaitTimerLock){ - try{ - if(logger.isDebugEnabled()){ - logger.debug("checkWaitTimer: entry"); - } - Date now = new Date(); - long nowMs = now.getTime(); - long waitTimerMs = waitTimerLastRunDate.getTime(); - - //give it 10 times leeway - if((nowMs - waitTimerMs) > 10*pdpUpdateInterval){ - if(allSeemsWell==null || allSeemsWell){ - allSeemsWell = false; - if(logger.isDebugEnabled()){ - logger.debug("checkWaitTimer: calling allSeemsWell with ALLNOTWELL param"); - } - stateManagementFeature.allSeemsWell(this.getClass().getName(), - StateManagementFeatureAPI.ALLNOTWELL_STATE, - "DesignationWaiter/ElectionHandler has STALLED"); - } - logger.error("checkWaitTimer: nowMs - waitTimerMs = {}" - + ", exceeds 10* pdpUpdateInterval = {}" - + " DesignationWaiter is STALLED!", (nowMs - waitTimerMs), (10*pdpUpdateInterval)); - }else if(allSeemsWell==null || !allSeemsWell){ - allSeemsWell = true; - stateManagementFeature.allSeemsWell(this.getClass().getName(), - StateManagementFeatureAPI.ALLSEEMSWELL_STATE, - "DesignationWaiter/ElectionHandler has RESUMED"); - logger.info("DesignationWaiter/ElectionHandler has RESUMED"); - } - if(logger.isDebugEnabled()){ - logger.debug("checkWaitTimer: exit"); - } - }catch(Exception e){ - logger.error("checkWaitTimer: caught unexpected exception: ", e); - } - } - } - - private long getDWaiterStartMs(){ - Date now = new Date(); - - // Retrieve the ms since the epoch - long nowMs = now.getTime(); - - // Time since the end of the last pdpUpdateInterval multiple - long nowModMs = nowMs % pdpUpdateInterval; - - // Time to the start of the next pdpUpdateInterval multiple - long startMs = 2*pdpUpdateInterval - nowModMs; - - // Give the start time a minimum of a 5 second cushion - if(startMs < 5000){ - // Start at the beginning of following interval - startMs = pdpUpdateInterval + startMs; - } - return startMs; - } - - private boolean nullSafeEquals(Object one, Object two){ - if(one == null && two == null){ - return true; - } - if(one != null && two != null){ - return one.equals(two); - } - return false; - } - - public String getPdpdNowActive(){ - return pdpdNowActive; - } - - public String getPdpdLastActive(){ - return pdpdLastActive; - } + // get an instance of logger + private static final Logger logger = LoggerFactory.getLogger(DroolsPdpsElectionHandler.class); + private DroolsPdpsConnector pdpsConnector; + private Object checkWaitTimerLock = new Object(); + private Object designationWaiterLock = new Object(); + + /* + * Must be static, so it can be referenced by JpaDroolsPdpsConnector, + * without requiring a reference to the election handler instantiation. + */ + private static DroolsPdp myPdp; + + private DesignationWaiter designationWaiter; + private Timer updateWorker; + private Timer waitTimer; + private Date waitTimerLastRunDate; + + // The interval between checks of the DesignationWaiter to be sure it is running. + private int pdpCheckInterval; + + // The interval between runs of the DesignationWaiter + private int pdpUpdateInterval; + + private volatile boolean isDesignated; + + private String pdpdNowActive; + private String pdpdLastActive; + + /* + * Start allSeemsWell with a value of null so that, on the first run + * of the checkWaitTimer it will set the value in IntegrityMonitor + * regardless of whether it needs to be set to true or false. + */ + private Boolean allSeemsWell = null; + + private StateManagementFeatureAPI stateManagementFeature; + + private static boolean isUnitTesting = false; + private static boolean isStalled = false; + + /** + * Constructor. + * + * @param pdps connectors + * @param myPdp pdp + */ + public DroolsPdpsElectionHandler(DroolsPdpsConnector pdps, DroolsPdp myPdp) { + if (pdps == null) { + logger.error("DroolsPdpsElectinHandler(): pdpsConnector==null"); + throw new IllegalArgumentException("DroolsPdpsElectinHandler(): pdpsConnector==null"); + } + if (myPdp == null) { + logger.error("DroolsPdpsElectinHandler(): droolsPdp==null"); + throw new IllegalArgumentException("DroolsPdpsElectinHandler(): DroolsPdp==null"); + } + + pdpdNowActive = null; + pdpdLastActive = null; + this.pdpsConnector = pdps; + DroolsPdpsElectionHandler.myPdp = myPdp; + this.isDesignated = false; + pdpCheckInterval = 3000; + try { + pdpCheckInterval = Integer.parseInt(ActiveStandbyProperties.getProperty( + ActiveStandbyProperties.PDP_CHECK_INVERVAL)); + } catch (Exception e) { + logger.error("Could not get pdpCheckInterval property. Using default {}",pdpCheckInterval, e); + } + pdpUpdateInterval = 2000; + try { + pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty( + ActiveStandbyProperties.PDP_UPDATE_INTERVAL)); + } catch (Exception e) { + logger.error("Could not get pdpUpdateInterval property. Using default {} ", pdpUpdateInterval, e); + } + + Date now = new Date(); + + // Retrieve the ms since the epoch + final long nowMs = now.getTime(); + + // Create the timer which will update the updateDate in DroolsPdpEntity table. + // This is the heartbeat + updateWorker = new Timer(); + + // Schedule the TimerUpdateClass to run at 100 ms and run at pdpCheckInterval ms thereafter + // NOTE: The first run of the TimerUpdateClass results in myPdp being added to the + // drools droolsPdpEntity table. + updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval); + + // Create the timer which will run the election algorithm + waitTimer = new Timer(); + + // Schedule it to start in startMs ms + // (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter + long startMs = getDWaiterStartMs(); + designationWaiter = new DesignationWaiter(); + waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval); + waitTimerLastRunDate = new Date(nowMs + startMs); + + //Get the StateManagementFeature instance + + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + if (feature.getResourceName().equals(myPdp.getPdpId())) { + if (logger.isDebugEnabled()) { + logger.debug("DroolsPdpsElectionHandler: Found StateManagementFeature" + + " with resourceName: {}", myPdp.getPdpId()); + } + stateManagementFeature = feature; + break; + } + } + if (stateManagementFeature == null) { + logger.error("DroolsPdpsElectionHandler failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", myPdp.getPdpId()); + } + } + + public static void setIsUnitTesting(boolean val) { + isUnitTesting = val; + } + + public static void setIsStalled(boolean val) { + isStalled = val; + } + + /** + * When the JpaDroolsPdpsConnector.standDown() method is invoked, it needs + * access to myPdp, so it can keep its designation status in sync with the + * DB. + * + * @param designated is designated value + */ + public static void setMyPdpDesignated(boolean designated) { + if (logger.isDebugEnabled()) { + logger.debug("setMyPdpDesignated: designated= {}", designated); + } + myPdp.setDesignated(designated); + } + + private class DesignationWaiter extends TimerTask { + // get an instance of logger + private final Logger logger = LoggerFactory.getLogger(DesignationWaiter.class); + + @Override + public void run() { + try { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: Entering"); + } + + //This is for testing the checkWaitTimer + if (isUnitTesting && isStalled) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: isUnitTesting = {} isStalled = {}", + isUnitTesting, isStalled); + } + return; + } + + synchronized (designationWaiterLock) { + + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: Entering synchronized block"); + } + + //It is possible that multiple PDPs are designated lead. So, we will make a list of all designated + //PDPs and then decide which one really should be designated at the end. + List<DroolsPdp> listOfDesignated = new ArrayList<>(); + + Collection<DroolsPdp> pdps = pdpsConnector.getDroolsPdps(); + + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: pdps.size= {}", pdps.size()); + } + + //This is only true if all designated PDPs have failed + boolean designatedPdpHasFailed = pdpsConnector.hasDesignatedPdpFailed(pdps); + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: designatedPdpHasFailed= {}", designatedPdpHasFailed); + } + for (DroolsPdp pdp : pdps) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: evaluating pdp ID: {}", pdp.getPdpId()); + } + + /* + * Note: side effect of isPdpCurrent is that any stale but + * designated PDPs will be marked as un-designated. + */ + boolean isCurrent = pdpsConnector.isPdpCurrent(pdp); + + /* + * We can't use stateManagement.getStandbyStatus() here, because + * we need the standbyStatus, not for this PDP, but for the PDP + * being processed by this loop iteration. + */ + String standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId()); + if (standbyStatus == null) { + // Treat this case as a cold standby -- if we + // abort here, no sessions will be created in a + // single-node test environment. + standbyStatus = StateManagement.COLD_STANDBY; + } + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: PDP= {}, isCurrent= {}", pdp.getPdpId(), isCurrent); + } + + /* + * There are 4 combinations of isDesignated and isCurrent. We will examine each one in-turn + * and evaluate the each pdp in the list of pdps against each combination. + * + * This is the first combination of isDesignated and isCurrent + */ + if (pdp.isDesignated() && isCurrent) { + //It is current, but it could have a standbystatus=coldstandby / hotstandby + //If so, we need to stand it down and demote it + if (!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)) { + if (pdp.getPdpId().equals(myPdp.getPdpId())) { + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: myPdp {} is current and designated, " + + "butstandbystatus is not providingservice. " + + " Executing stateManagement.demote()" + "\n\n", myPdp.getPdpId()); + } + // So, we must demote it + try { + //Keep the order like this. StateManagement is last since it + //triggers controller shutdown + //This will change isDesignated and it can enter another if(combination) below + pdpsConnector.standDownPdp(pdp.getPdpId()); + myPdp.setDesignated(false); + isDesignated = false; + if (!(standbyStatus.equals(StateManagement.HOT_STANDBY) + || standbyStatus.equals(StateManagement.COLD_STANDBY))) { + /* + * Only demote it if it appears it has not already been demoted. Don't worry + * about synching with the topic endpoint states. That is done by the + * refreshStateAudit + */ + stateManagementFeature.demote(); + } + //update the standbystatus to check in a later + //combination of isDesignated and isCurrent + standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId()); + } catch (Exception e) { + logger.error("DesignatedWaiter.run: myPdp: {} " + + "Caught Exception attempting to demote myPdp," + + "message= {}", myPdp.getPdpId(), e); + } + } else { + // Don't demote a remote PDP that is current. It should catch itself + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: myPdp {} is current and designated, " + + "but standbystatus is not providingservice. " + + " Cannot execute stateManagement.demote() " + + "since it it is not myPdp\n\n", + myPdp.getPdpId()); + } + } + + } else { + // If we get here, it is ok to be on the list + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: PDP= {} is designated, " + + "current and {} Noting PDP as " + + "designated, standbyStatus= {}", + pdp.getPdpId(), standbyStatus, standbyStatus); + } + listOfDesignated.add(pdp); + } + + + } + + + /* + * The second combination of isDesignated and isCurrent + * + * PDP is designated but not current; it has failed. + * So we stand it down (it doesn't matter what + * its standbyStatus is). None of these go on the list. + */ + if (pdp.isDesignated() && !isCurrent) { + if (logger.isDebugEnabled()) { + logger.debug("INFO: DesignatedWaiter.run: PDP= {} is currently " + + "designated but is not current; " + + "it has failed. Standing down. standbyStatus= {}", + pdp.getPdpId(), standbyStatus); + } + /* + * Changes designated to 0 but it is still potentially providing service + * Will affect isDesignated, so, it can enter an if(combination) below + */ + pdpsConnector.standDownPdp(pdp.getPdpId()); + + //need to change standbystatus to coldstandby + if (pdp.getPdpId().equals(myPdp.getPdpId())) { + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: myPdp {} is not Current. " + + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId()); + } + // We found that myPdp is designated but not current + // So, we must cause it to disableFail + try { + myPdp.setDesignated(false); + pdpsConnector.setDesignated(myPdp, false); + isDesignated = false; + stateManagementFeature.disableFailed(); + } catch (Exception e) { + logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception " + + "attempting to disableFail myPdp {}, message= {}", + myPdp.getPdpId(), myPdp.getPdpId(), e); + } + } else { //it is a remote PDP that is failed + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: PDP {} is not Current. " + + " Executing stateManagement.disableFailed(otherResourceName)\n\n", + pdp.getPdpId() ); + } + // We found a PDP is designated but not current + // We already called standdown(pdp) which will change designated to false + // Now we need to disableFail it to get its states in synch. The standbyStatus + // should equal coldstandby + try { + stateManagementFeature.disableFailed(pdp.getPdpId()); + } catch (Exception e) { + logger.error("DesignatedWaiter.run: for PDP {} Caught Exception attempting to " + + "disableFail({}), message= {}", + pdp.getPdpId(), pdp.getPdpId(), e); + } + + } + continue; //we are not going to do anything else with this pdp + } + + /* + * The third combination of isDesignated and isCurrent + * /* + * If a PDP is not currently designated but is providing service + * (erroneous, but recoverable) or hot standby + * we can add it to the list of possible designated if all the designated have failed + */ + if (!pdp.isDesignated() && isCurrent) { + if (!(standbyStatus.equals(StateManagement.HOT_STANDBY) + || standbyStatus.equals(StateManagement.COLD_STANDBY))) { + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: PDP {}" + + " is NOT designated but IS current and" + + " has a standbystatus= {}", pdp.getPdpId(), standbyStatus); + } + // Since it is current, we assume it can adjust its own state. + // We will demote if it is myPdp + if (pdp.getPdpId().equals(myPdp.getPdpId())) { + //demote it + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: PDP {} going to " + + "setDesignated = false and calling stateManagement.demote", + pdp.getPdpId()); + } + try { + //Keep the order like this. + //StateManagement is last since it triggers controller shutdown + pdpsConnector.setDesignated(myPdp, false); + myPdp.setDesignated(false); + isDesignated = false; + //This is definitely not a redundant call. + //It is attempting to correct a problem + stateManagementFeature.demote(); + //recheck the standbystatus + standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId()); + } catch (Exception e) { + logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception " + + "attempting to demote myPdp {}, message = {}", myPdp.getPdpId(), + myPdp.getPdpId(), e); + } + + } + } + if (standbyStatus.equals(StateManagement.HOT_STANDBY) && designatedPdpHasFailed) { + //add it to the list + if (logger.isDebugEnabled()) { + logger.debug("INFO: DesignatedWaiter.run: PDP= {}" + + " is not designated but is {} and designated PDP " + + "has failed. standbyStatus= {}", pdp.getPdpId(), + standbyStatus, standbyStatus); + } + listOfDesignated.add(pdp); + } + continue; //done with this one + } + + /* + * The fourth combination of isDesignated and isCurrent + * + * We are not going to put any of these on the list since it appears they have failed. + + * + */ + if (!pdp.isDesignated() && !isCurrent) { + if (logger.isDebugEnabled()) { + logger.debug("INFO: DesignatedWaiter.run: PDP= {} " + + "designated= {}, current= {}, " + + "designatedPdpHasFailed= {}, " + + "standbyStatus= {}",pdp.getPdpId(), + pdp.isDesignated(), isCurrent, designatedPdpHasFailed, standbyStatus); + } + if (!standbyStatus.equals(StateManagement.COLD_STANDBY)) { + //stand it down + //disableFail it + pdpsConnector.standDownPdp(pdp.getPdpId()); + if (pdp.getPdpId().equals(myPdp.getPdpId())) { + /* + * I don't actually know how this condition could happen, + * but if it did, we would want + * to declare it failed. + */ + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, " + + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId()); + } + // So, we must disableFail it + try { + //Keep the order like this. + //StateManagement is last since it triggers controller shutdown + pdpsConnector.setDesignated(myPdp, false); + myPdp.setDesignated(false); + isDesignated = false; + stateManagementFeature.disableFailed(); + } catch (Exception e) { + logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to " + + "disableFail myPdp {}, message= {}", + myPdp.getPdpId(), myPdp.getPdpId(), e); + } + } else { //it is remote + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, " + + " Executing stateManagement.disableFailed({})\n\n", + myPdp.getPdpId(), pdp.getPdpId()); + } + // We already called standdown(pdp) which will change designated to false + // Now we need to disableFail it to get its states in sync. + // StandbyStatus = coldstandby + try { + stateManagementFeature.disableFailed(pdp.getPdpId()); + } catch (Exception e) { + logger.error("DesignatedWaiter.run: for PDP {}" + + " Caught Exception attempting to disableFail({})" + + ", message=", pdp.getPdpId(), pdp.getPdpId(), e); + } + } + } + } + + + } // end pdps loop + + /* + * We have checked the four combinations of isDesignated and isCurrent. Where appropriate, + * we added the PDPs to the potential list of designated pdps + * + * We need to give priority to pdps on the same site that is currently being used + * First, however, we must sanitize the list of designated to make sure their are + * only designated members or non-designated members. There should not be both in + * the list. Because there are real time delays, it is possible that both types could + * be on the list. + */ + + listOfDesignated = santizeDesignatedList(listOfDesignated); + + /* + * We need to figure out the last pdp that was the primary so we can get the last site + * name and the last session numbers. We need to create a "dummy" droolspdp since + * it will be used in later comparisons and cannot be null. + */ + + DroolsPdp mostRecentPrimary = computeMostRecentPrimary(pdps, listOfDesignated); + + if (mostRecentPrimary != null) { + pdpdLastActive = mostRecentPrimary.getPdpId(); + } + + + /* + * It is possible to get here with more than one pdp designated and providingservice. This normally + * occurs when there is a race condition with multiple nodes coming up at the same time. If that is + * the case we must determine which one is the one that should be designated and which one should + * be demoted. + * + * It is possible to have 0, 1, 2 or more but not all, or all designated. + * If we have one designated and current, we chose it and are done + * If we have 2 or more, but not all, we must determine which one is in the same site as + * the previously designated pdp. + */ + DroolsPdp designatedPdp = computeDesignatedPdp(listOfDesignated, mostRecentPrimary); + if (designatedPdp != null) { + pdpdNowActive = designatedPdp.getPdpId(); + } + + if (designatedPdp == null) { + logger.warn("WARNING: DesignatedWaiter.run: No viable PDP found to be Designated. " + + "designatedPdp still null."); + // Just to be sure the parameters are correctly set + myPdp.setDesignated(false); + pdpsConnector.setDesignated(myPdp,false); + isDesignated = false; + + waitTimerLastRunDate = new Date(); + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run (designatedPdp == null) waitTimerLastRunDate = {}", + waitTimerLastRunDate); + } + myPdp.setUpdatedDate(waitTimerLastRunDate); + pdpsConnector.update(myPdp); + + return; + + } else if (designatedPdp.getPdpId().equals(myPdp.getPdpId())) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: designatedPdp is PDP={}", myPdp.getPdpId()); + } + /* + * update function expects myPdp.isDesignated to be true. + */ + try { + //Keep the order like this. StateManagement is last since it triggers controller init + myPdp.setDesignated(true); + myPdp.setDesignatedDate(new Date()); + pdpsConnector.setDesignated(myPdp, true); + isDesignated = true; + String standbyStatus = stateManagementFeature.getStandbyStatus(); + if (!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)) { + /* + * Only call promote if it is not already in the right state. Don't worry about + * synching the lower level topic endpoint states. That is done by the + * refreshStateAudit. + * Note that we need to fetch the session list from 'mostRecentPrimary' + * at this point -- soon, 'mostRecentPrimary' will be set to this host. + */ + //this.sessions = mostRecentPrimary.getSessions(); + stateManagementFeature.promote(); + } + } catch (Exception e) { + logger.error("ERROR: DesignatedWaiter.run: Caught Exception attempting to promote PDP={}" + + ", message=", myPdp.getPdpId(), e); + myPdp.setDesignated(false); + pdpsConnector.setDesignated(myPdp,false); + isDesignated = false; + //If you can't promote it, demote it + try { + String standbyStatus = stateManagementFeature.getStandbyStatus(); + if (!(standbyStatus.equals(StateManagement.HOT_STANDBY) + || standbyStatus.equals(StateManagement.COLD_STANDBY))) { + /* + * Only call demote if it is not already in the right state. Don't worry about + * synching the lower level topic endpoint states. That is done by the + * refreshStateAudit. + */ + stateManagementFeature.demote(); + } + } catch (Exception e1) { + logger.error("ERROR: DesignatedWaiter.run: Caught StandbyStatusException " + + "attempting to promote then demote PDP={}, message=", + myPdp.getPdpId(), e1); + } + + } + waitTimerLastRunDate = new Date(); + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) " + + "waitTimerLastRunDate = " + waitTimerLastRunDate); + } + myPdp.setUpdatedDate(waitTimerLastRunDate); + pdpsConnector.update(myPdp); + + return; + } + isDesignated = false; + + } // end synchronized + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: myPdp: {}; Returning, isDesignated= {}", + isDesignated, myPdp.getPdpId()); + } + + Date tmpDate = new Date(); + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run (end of run) waitTimerLastRunDate = {}", tmpDate); + } + + waitTimerLastRunDate = tmpDate; + myPdp.setUpdatedDate(waitTimerLastRunDate); + pdpsConnector.update(myPdp); + + } catch (Exception e) { + logger.error("DesignatedWaiter.run caught an unexpected exception: ", e); + } + } // end run + } + + /** + * Sanitize designated list. + * + * @param listOfDesignated list of designated pdps + * @return + */ + public List<DroolsPdp> santizeDesignatedList(List<DroolsPdp> listOfDesignated) { + + boolean containsDesignated = false; + boolean containsHotStandby = false; + List<DroolsPdp> listForRemoval = new ArrayList<>(); + for (DroolsPdp pdp : listOfDesignated) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run sanitizing: pdp = {}" + + " isDesignated = {}",pdp.getPdpId(), pdp.isDesignated()); + } + if (pdp.isDesignated()) { + containsDesignated = true; + } else { + containsHotStandby = true; + listForRemoval.add(pdp); + } + } + if (containsDesignated && containsHotStandby) { + //remove the hot standby from the list + listOfDesignated.removeAll(listForRemoval); + } + return listOfDesignated; + } + + /** + * Compute most recent primary. + * + * @param pdps collection of pdps + * @param listOfDesignated list of designated pdps + * @return + */ + public DroolsPdp computeMostRecentPrimary(Collection<DroolsPdp> pdps, List<DroolsPdp> listOfDesignated) { + boolean containsDesignated = false; + for (DroolsPdp pdp : listOfDesignated) { + if (pdp.isDesignated()) { + containsDesignated = true; + } + } + DroolsPdp mostRecentPrimary = new DroolsPdpImpl(null, true, 1, new Date(0)); + mostRecentPrimary.setSiteName(null); + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run listOfDesignated.size() = {}", listOfDesignated.size()); + } + if (listOfDesignated.size() <= 1) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWainter.run: listOfDesignated.size <=1"); + } + //Only one or none is designated or hot standby. Choose the latest designated date + for (DroolsPdp pdp : pdps) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run pdp = {}" + + " pdp.getDesignatedDate() = {}", + pdp.getPdpId(), pdp.getDesignatedDate()); + } + if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) { + mostRecentPrimary = pdp; + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", + mostRecentPrimary.getPdpId()); + } + } + } + } else if (listOfDesignated.size() == pdps.size()) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWainter.run: listOfDesignated.size = pdps.size() which is {}", pdps.size()); + } + //They are all designated or all hot standby. + mostRecentPrimary = null; + for (DroolsPdp pdp : pdps) { + if (mostRecentPrimary == null) { + mostRecentPrimary = pdp; + continue; + } + if (containsDesignated) { //Choose the site of the first designated date + if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) < 0) { + mostRecentPrimary = pdp; + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); + } + } + } else { //Choose the site with the latest designated date + if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) { + mostRecentPrimary = pdp; + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); + } + } + } + } + } else { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWainter.run: Some but not all are designated or hot standby. "); + } + //Some but not all are designated or hot standby. + if (containsDesignated) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated); + } + /* + * The list only contains designated. This is a problem. It is most likely a race + * condition that resulted in two thinking they should be designated. Choose the + * site with the latest designated date for the pdp not included on the designated list. + * This should be the site that had the last designation before this race condition + * occurred. + */ + for (DroolsPdp pdp : pdps) { + if (listOfDesignated.contains(pdp)) { + continue; //Don't consider this entry + } + if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) { + mostRecentPrimary = pdp; + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); + } + } + } + } else { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated); + } + //The list only contains hot standby. Choose the site of the latest designated date + for (DroolsPdp pdp : pdps) { + if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) { + mostRecentPrimary = pdp; + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId()); + } + } + } + } + } + return mostRecentPrimary; + } + + /** + * Compue designated pdp. + * + * @param listOfDesignated list of designated pdps + * @param mostRecentPrimary most recent primary pdpd + * @return + */ + public DroolsPdp computeDesignatedPdp(List<DroolsPdp> listOfDesignated, DroolsPdp mostRecentPrimary) { + DroolsPdp designatedPdp = null; + DroolsPdp lowestPriorityPdp = null; + if (listOfDesignated.size() > 1) { + if (logger.isDebugEnabled()) { + logger.debug("DesignatedWaiter.run: myPdp: {} listOfDesignated.size(): {}", myPdp.getPdpId(), + listOfDesignated.size()); + } + DroolsPdp rejectedPdp = null; + DroolsPdp lowestPrioritySameSite = null; + DroolsPdp lowestPriorityDifferentSite = null; + for (DroolsPdp pdp : listOfDesignated) { + // We need to determine if another PDP is the lowest priority + if (nullSafeEquals(pdp.getSiteName(),mostRecentPrimary.getSiteName())) { + if (lowestPrioritySameSite == null) { + if (lowestPriorityDifferentSite != null) { + rejectedPdp = lowestPriorityDifferentSite; + } + lowestPrioritySameSite = pdp; + } else { + if (pdp.getPdpId().equals((lowestPrioritySameSite.getPdpId()))) { + continue;//nothing to compare + } + if (pdp.comparePriority(lowestPrioritySameSite) < 0) { + if (logger.isDebugEnabled()) { + logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}" + + " has lower priority than pdp ID: {}",myPdp.getPdpId(), pdp.getPdpId(), + lowestPrioritySameSite.getPdpId()); + } + //we need to reject lowestPrioritySameSite + rejectedPdp = lowestPrioritySameSite; + lowestPrioritySameSite = pdp; + } else { + //we need to reject pdp and keep lowestPrioritySameSite + if (logger.isDebugEnabled()) { + logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {} " + + " has higher priority than pdp ID: {}", myPdp.getPdpId(),pdp.getPdpId(), + lowestPrioritySameSite.getPdpId()); + } + rejectedPdp = pdp; + } + } + } else { + if (lowestPrioritySameSite != null) { + //if we already have a candidate for same site, we don't want to bother with different sites + rejectedPdp = pdp; + } else { + if (lowestPriorityDifferentSite == null) { + lowestPriorityDifferentSite = pdp; + continue; + } + if (pdp.getPdpId().equals((lowestPriorityDifferentSite.getPdpId()))) { + continue;//nothing to compare + } + if (pdp.comparePriority(lowestPriorityDifferentSite) < 0) { + if (logger.isDebugEnabled()) { + logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}" + + " has lower priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(), + lowestPriorityDifferentSite.getPdpId()); + } + //we need to reject lowestPriorityDifferentSite + rejectedPdp = lowestPriorityDifferentSite; + lowestPriorityDifferentSite = pdp; + } else { + //we need to reject pdp and keep lowestPriorityDifferentSite + if (logger.isDebugEnabled()) { + logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}" + + " has higher priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(), + lowestPriorityDifferentSite.getPdpId()); + } + rejectedPdp = pdp; + } + } + } + // If the rejectedPdp is myPdp, we need to stand it down and demote it. Each pdp is responsible + // for demoting itself + if (rejectedPdp != null && nullSafeEquals(rejectedPdp.getPdpId(),myPdp.getPdpId())) { + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated myPdp ID: {}" + + " is NOT the lowest priority. Executing stateManagement.demote()\n\n", + myPdp.getPdpId(), + myPdp.getPdpId()); + } + // We found that myPdp is on the listOfDesignated and it is not the lowest priority + // So, we must demote it + try { + //Keep the order like this. StateManagement is last since it triggers controller shutdown + myPdp.setDesignated(false); + pdpsConnector.setDesignated(myPdp, false); + isDesignated = false; + String standbyStatus = stateManagementFeature.getStandbyStatus(); + if (!(standbyStatus.equals(StateManagement.HOT_STANDBY) + || standbyStatus.equals(StateManagement.COLD_STANDBY))) { + /* + * Only call demote if it is not already in the right state. Don't worry about + * synching the lower level topic endpoint states. That is done by the + * refreshStateAudit. + */ + stateManagementFeature.demote(); + } + } catch (Exception e) { + myPdp.setDesignated(false); + pdpsConnector.setDesignated(myPdp, false); + isDesignated = false; + logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to " + + "demote myPdp {} myPdp.getPdpId(), message= {}", myPdp.getPdpId(), + e); + } + } + } //end: for(DroolsPdp pdp : listOfDesignated) + if (lowestPrioritySameSite != null) { + lowestPriorityPdp = lowestPrioritySameSite; + } else { + lowestPriorityPdp = lowestPriorityDifferentSite; + } + //now we have a valid value for lowestPriorityPdp + if (logger.isDebugEnabled()) { + logger.debug("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated " + + "found the LOWEST priority pdp ID: {} " + + " It is now the designatedPpd from the perspective of myPdp ID: {} \n\n", + myPdp.getPdpId(), lowestPriorityPdp.getPdpId(), myPdp); + } + designatedPdp = lowestPriorityPdp; + + } else if (listOfDesignated.isEmpty()) { + if (logger.isDebugEnabled()) { + logger.debug("\nDesignatedWaiter.run: myPdp: {} listOfDesignated is: EMPTY.", myPdp.getPdpId()); + } + designatedPdp = null; + } else { //only one in listOfDesignated + if (logger.isDebugEnabled()) { + logger.debug("\nDesignatedWaiter.run: myPdp: {} listOfDesignated " + + "has ONE entry. PDP ID: {}", myPdp.getPdpId(), listOfDesignated.get(0).getPdpId()); + } + designatedPdp = listOfDesignated.get(0); + } + return designatedPdp; + + } + + private class TimerUpdateClass extends TimerTask { + + @Override + public void run() { + try { + if (logger.isDebugEnabled()) { + logger.debug("TimerUpdateClass.run: entry"); + } + checkWaitTimer(); + } catch (Exception e) { + logger.error("TimerUpdateClass.run caught an unexpected exception: ", e); + } + if (logger.isDebugEnabled()) { + logger.debug("TimerUpdateClass.run.exit"); + } + } + } + + @Override + public void checkThreadStatus() { + checkWaitTimer(); + } + + private void checkWaitTimer() { + synchronized (checkWaitTimerLock) { + try { + if (logger.isDebugEnabled()) { + logger.debug("checkWaitTimer: entry"); + } + Date now = new Date(); + long nowMs = now.getTime(); + long waitTimerMs = waitTimerLastRunDate.getTime(); + + //give it 10 times leeway + if ((nowMs - waitTimerMs) > 10 * pdpUpdateInterval) { + if (allSeemsWell == null || allSeemsWell) { + allSeemsWell = false; + if (logger.isDebugEnabled()) { + logger.debug("checkWaitTimer: calling allSeemsWell with ALLNOTWELL param"); + } + stateManagementFeature.allSeemsWell(this.getClass().getName(), + StateManagementFeatureAPI.ALLNOTWELL_STATE, + "DesignationWaiter/ElectionHandler has STALLED"); + } + logger.error("checkWaitTimer: nowMs - waitTimerMs = {}" + + ", exceeds 10* pdpUpdateInterval = {}" + + " DesignationWaiter is STALLED!", (nowMs - waitTimerMs), (10 * pdpUpdateInterval)); + } else if (allSeemsWell == null || !allSeemsWell) { + allSeemsWell = true; + stateManagementFeature.allSeemsWell(this.getClass().getName(), + StateManagementFeatureAPI.ALLSEEMSWELL_STATE, + "DesignationWaiter/ElectionHandler has RESUMED"); + logger.info("DesignationWaiter/ElectionHandler has RESUMED"); + } + if (logger.isDebugEnabled()) { + logger.debug("checkWaitTimer: exit"); + } + } catch (Exception e) { + logger.error("checkWaitTimer: caught unexpected exception: ", e); + } + } + } + + private long getDWaiterStartMs() { + Date now = new Date(); + + // Retrieve the ms since the epoch + long nowMs = now.getTime(); + + // Time since the end of the last pdpUpdateInterval multiple + long nowModMs = nowMs % pdpUpdateInterval; + + // Time to the start of the next pdpUpdateInterval multiple + long startMs = 2 * pdpUpdateInterval - nowModMs; + + // Give the start time a minimum of a 5 second cushion + if (startMs < 5000) { + // Start at the beginning of following interval + startMs = pdpUpdateInterval + startMs; + } + return startMs; + } + + private boolean nullSafeEquals(Object one, Object two) { + if (one == null && two == null) { + return true; + } + if (one != null && two != null) { + return one.equals(two); + } + return false; + } + + public String getPdpdNowActive() { + return pdpdNowActive; + } + + public String getPdpdLastActive() { + return pdpdLastActive; + } } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java index dc907b27..04448052 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java @@ -36,599 +36,599 @@ import org.slf4j.LoggerFactory; public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { - // get an instance of logger - private static final Logger logger = LoggerFactory.getLogger(JpaDroolsPdpsConnector.class); - private EntityManagerFactory emf; - - - //not sure if we want to use the same entity manager factory for drools session and pass it in here, or create a new one - public JpaDroolsPdpsConnector(EntityManagerFactory emf){ - this.emf = emf; - } - @Override - public Collection<DroolsPdp> getDroolsPdps() { - //return a list of all the DroolsPdps in the database - EntityManager em = emf.createEntityManager(); - try { - em.getTransaction().begin(); - Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p"); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - LinkedList<DroolsPdp> droolsPdpsReturnList = new LinkedList<>(); - for(Object o : droolsPdpsList){ - if(o instanceof DroolsPdp){ - //Make sure it is not a cached version - em.refresh((DroolsPdpEntity)o); - droolsPdpsReturnList.add((DroolsPdp)o); - if (logger.isDebugEnabled()) { - DroolsPdp droolsPdp = (DroolsPdp)o; - logger.debug("getDroolsPdps: PDP= {}" - + ", isDesignated= {}" - + ", updatedDate= {}" - + ", priority= {}", droolsPdp.getPdpId(), droolsPdp.isDesignated(), - droolsPdp.getUpdatedDate(), droolsPdp.getPriority()); - } - } - } - try{ - em.getTransaction().commit(); - }catch(Exception e){ - logger.error - ("Cannot commit getDroolsPdps() transaction", e); - } - return droolsPdpsReturnList; - } finally { - cleanup(em, "getDroolsPdps"); - } - } - - private boolean nullSafeEquals(Object one, Object two){ - if(one == null && two == null){ - return true; - } - if(one != null && two != null){ - return one.equals(two); - } - return false; - } - - @Override - public void update(DroolsPdp pdp) { - - if (logger.isDebugEnabled()) { - logger.debug("update: Entering, pdpId={}", pdp.getPdpId()); - } - - //this is to update our own pdp in the database - EntityManager em = emf.createEntityManager(); - try { - em.getTransaction().begin(); - Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); - droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId()); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - DroolsPdpEntity droolsPdpEntity; - if(droolsPdpsList.size() == 1 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)){ - droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0); - em.refresh(droolsPdpEntity); //Make sure we have current values - Date currentDate = new Date(); - long difference = currentDate.getTime()-droolsPdpEntity.getUpdatedDate().getTime(); - //just set some kind of default here - long pdpTimeout = 15000; - try{ - pdpTimeout = Long.parseLong(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_TIMEOUT)); - }catch(Exception e){ - logger.error - ("Could not get PDP timeout property, using default.", e); - } - boolean isCurrent = difference<pdpTimeout; - if (logger.isDebugEnabled()) { - logger.debug("update: PDP= {}, isCurrent={}" - + " difference= {}" - + ", pdpTimeout= {}, designated= {}", - pdp.getPdpId(), isCurrent, difference, pdpTimeout, droolsPdpEntity.isDesignated()); - } - } else { - if (logger.isDebugEnabled()) { - logger.debug("update: For PDP={}" - + ", instantiating new DroolsPdpEntity", pdp.getPdpId()); - } - droolsPdpEntity = new DroolsPdpEntity(); - em.persist(droolsPdpEntity); - droolsPdpEntity.setPdpId(pdp.getPdpId()); - } - if(droolsPdpEntity.getPriority() != pdp.getPriority()){ - droolsPdpEntity.setPriority(pdp.getPriority()); - } - if(!droolsPdpEntity.getUpdatedDate().equals(pdp.getUpdatedDate())){ - droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate()); - } - /*if(!droolsPdpEntity.getDesignatedDate().equals(pdp.getDesignatedDate())){ - droolsPdpEntity.setDesignatedDate(pdp.getDesignatedDate()); - } The designated date is only set below when this first becomes designated*/ - if(!nullSafeEquals(droolsPdpEntity.getSiteName(),pdp.getSiteName())){ - droolsPdpEntity.setSiteName(pdp.getSiteName()); - } - - if(droolsPdpEntity.isDesignated() != pdp.isDesignated()){ - if (logger.isDebugEnabled()) { - logger.debug("update: pdpId={}" - + ", pdp.isDesignated={}" - + ", droolsPdpEntity.pdpId= {}" - + ", droolsPdpEntity.isDesignated={}", - pdp.getPdpId(), pdp.isDesignated(),droolsPdpEntity.getPdpId(), droolsPdpEntity.isDesignated()); - } - droolsPdpEntity.setDesignated(pdp.isDesignated()); - //The isDesignated value is not the same and the new one == true - if(pdp.isDesignated()){ - droolsPdpEntity.setDesignatedDate(new Date()); - } - } - em.getTransaction().commit(); - } finally { - cleanup(em, "update"); - } - - if (logger.isDebugEnabled()) { - logger.debug("update: Exiting"); - } - - } - - /* - * Note: A side effect of this boolean method is that if the PDP is designated but not current, the - * droolspdpentity.DESIGNATED column will be set to false (the PDP will be un-designated, i.e. marked as - * being in standby mode) - */ - @Override - public boolean isPdpCurrent(DroolsPdp pdp) { - - boolean isCurrent = isCurrent(pdp); - - EntityManager em = emf.createEntityManager(); - try{ - if(!isCurrent && pdp.isDesignated()){ - em.getTransaction().begin(); - Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); - droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId()); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - if(droolsPdpsList.size() == 1 && droolsPdpsList.get(0) instanceof DroolsPdpEntity){ - if (logger.isDebugEnabled()) { - logger.debug("isPdpCurrent: PDP={} designated but not current; setting designated to false", pdp.getPdpId()); - } - DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0); - droolsPdpEntity.setDesignated(false); - em.getTransaction().commit(); - } else { - logger.warn("isPdpCurrent: PDP={} is designated but not current; " - + "however it does not have a DB entry, so cannot set DESIGNATED to false!", pdp.getPdpId()); - } - } else { - if (logger.isDebugEnabled()) { - logger.debug("isPdpCurrent: For PDP= {}, " - + "designated={}, isCurrent={}", pdp.getPdpId(), pdp.isDesignated(), isCurrent); - } - } - }catch(Exception e){ - logger.error - ("Could not update expired record marked as designated in the database", e); - } finally { - cleanup(em, "isPdpCurrent"); - } - return isCurrent; - - } - - @Override - public void setDesignated(DroolsPdp pdp, boolean designated) { - - if (logger.isDebugEnabled()) { - logger.debug("setDesignated: Entering, pdpId={}" - + ", designated={}", pdp.getPdpId(), designated); - } - - EntityManager em = null; - try { - em = emf.createEntityManager(); - em.getTransaction().begin(); - Query droolsPdpsListQuery = em - .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); - droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId()); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( - LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - if (droolsPdpsList.size() == 1 - && droolsPdpsList.get(0) instanceof DroolsPdpEntity) { - DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList - .get(0); - - if (logger.isDebugEnabled()) { - logger.debug("setDesignated: PDP={}" - + " found, designated= {}" - + ", setting to {}", pdp.getPdpId(), droolsPdpEntity.isDesignated(), - designated); - } - droolsPdpEntity.setDesignated(designated); - if(designated){ - em.refresh(droolsPdpEntity); //make sure we get the DB value - if(!droolsPdpEntity.isDesignated()){ - droolsPdpEntity.setDesignatedDate(new Date()); - } - - } - em.getTransaction().commit(); - } else { - logger.error("setDesignated: PDP={}" - + " not in DB; cannot update designation", pdp.getPdpId()); - } - } catch (Exception e) { - logger.error("setDesignated: Caught Exception", e); - } finally { - cleanup(em, "setDesignated"); - } - - if (logger.isDebugEnabled()) { - logger.debug("setDesignated: Exiting"); - } - - } - - - @Override - public void standDownPdp(String pdpId) { - if(logger.isDebugEnabled()){ - logger.debug("standDownPdp: Entering, pdpId={}", pdpId); - } - - EntityManager em = null; - try { - /* - * Start transaction. - */ - em = emf.createEntityManager(); - em.getTransaction().begin(); - - /* - * Get droolspdpentity record for this PDP and mark DESIGNATED as - * false. - */ - Query droolsPdpsListQuery = em - .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); - droolsPdpsListQuery.setParameter("pdpId", pdpId); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( - LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - DroolsPdpEntity droolsPdpEntity; - if (droolsPdpsList.size() == 1 - && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) { - droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0); - droolsPdpEntity.setDesignated(false); - em.persist(droolsPdpEntity); - if(logger.isDebugEnabled()){ - logger.debug("standDownPdp: PDP={} persisted as non-designated.", pdpId ); - } - } else { - logger.error("standDownPdp: Missing record in droolspdpentity for pdpId={}" - + "; cannot stand down PDP", pdpId); - } - - /* - * End transaction. - */ - em.getTransaction().commit(); - cleanup(em, "standDownPdp"); - em = null; - - // Keep the election handler in sync with the DB - DroolsPdpsElectionHandler.setMyPdpDesignated(false); - - } catch (Exception e) { - logger.error("standDownPdp: Unexpected Exception attempting to mark " - + "DESIGNATED as false for droolspdpentity, pdpId={}" - + ". Cannot stand down PDP; message={}", pdpId, e.getMessage(), e); - } finally { - cleanup(em, "standDownPdp"); - } - if(logger.isDebugEnabled()){ - logger.debug("standDownPdp: Exiting"); - } - - } - - /* - * Determines whether or not a designated PDP has failed. - * - * Note: The update method, which is run periodically by the - * TimerUpdateClass, will un-designate a PDP that is stale. - */ - @Override - public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps) { - - if (logger.isDebugEnabled()) { - logger.debug("hasDesignatedPdpFailed: Entering, pdps.size()={}", pdps.size()); - } - - boolean failed = true; - boolean foundDesignatedPdp = false; - - for (DroolsPdp pdp : pdps) { - - /* - * Normally, the update method will un-designate any stale PDP, but - * we check here to see if the PDP has gone stale since the update - * method was run. - * - * Even if we determine that the designated PDP is current, we keep - * going (we don't break), so we can get visibility into the other - * PDPs, when in DEBUG mode. - */ - if (pdp.isDesignated() && isCurrent(pdp)) { - if (logger.isDebugEnabled()) { - logger.debug("hasDesignatedPdpFailed: Designated PDP={} is current", pdp.getPdpId()); - } - failed = false; - foundDesignatedPdp = true; - } else if (pdp.isDesignated() && !isCurrent(pdp)) { - logger.error("hasDesignatedPdpFailed: Designated PDP={} has failed", pdp.getPdpId()); - foundDesignatedPdp = true; - } else { - if (logger.isDebugEnabled()) { - logger.debug("hasDesignatedPdpFailed: PDP={} is not designated", pdp.getPdpId()); - } - } - } - - if (logger.isDebugEnabled()) { - logger.debug("hasDesignatedPdpFailed: Exiting and returning, foundDesignatedPdp={}", - foundDesignatedPdp); - } - return failed; - } - - - private boolean isCurrent(DroolsPdp pdp) { - - if (logger.isDebugEnabled()) { - logger.debug("isCurrent: Entering, pdpId={}", pdp.getPdpId()); - } - - boolean current = false; - - // Return if the current PDP is considered "current" based on whatever - // time box that may be. - // If the the PDP is not current, we should mark it as not primary in - // the database - Date currentDate = new Date(); - long difference = currentDate.getTime() - - pdp.getUpdatedDate().getTime(); - // just set some kind of default here - long pdpTimeout = 15000; - try { - pdpTimeout = Long.parseLong(ActiveStandbyProperties - .getProperty(ActiveStandbyProperties.PDP_TIMEOUT)); - if (logger.isDebugEnabled()) { - logger.debug("isCurrent: pdp.timeout={}", pdpTimeout); - } - } catch (Exception e) { - logger.error - ("isCurrent: Could not get PDP timeout property, using default.", e); - } - current = difference < pdpTimeout; - - if (logger.isDebugEnabled()) { - logger.debug("isCurrent: Exiting, difference={}, pdpTimeout={}" - + "; returning current={}", difference, pdpTimeout, current); - } - - return current; - } - - - /* - * Currently this method is only used in a JUnit test environment. Gets a - * PDP record from droolspdpentity table. - */ - @Override - public DroolsPdpEntity getPdp(String pdpId) { - - if (logger.isDebugEnabled()) { - logger.debug("getPdp: Entering and getting PDP with pdpId={}", pdpId); - } - - DroolsPdpEntity droolsPdpEntity = null; - - EntityManager em = null; - try { - em = emf.createEntityManager(); - em.getTransaction().begin(); - Query droolsPdpsListQuery = em - .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); - droolsPdpsListQuery.setParameter("pdpId", pdpId); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( - LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - if (droolsPdpsList.size() == 1 - && droolsPdpsList.get(0) instanceof DroolsPdpEntity) { - droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0); - if (logger.isDebugEnabled()) { - logger.debug("getPdp: PDP={}" - + " found, isDesignated={}," - + " updatedDate={}, " - + "priority={}", pdpId, - droolsPdpEntity.isDesignated(), droolsPdpEntity.getUpdatedDate(), - droolsPdpEntity.getPriority()); - } - - // Make sure the droolsPdpEntity is not a cached version - em.refresh(droolsPdpEntity); - - em.getTransaction().commit(); - } else { - logger.error("getPdp: PDP={} not found!?", pdpId); - } - } catch (Exception e) { - logger.error - ("getPdp: Caught Exception attempting to get PDP", e); - } finally { - cleanup(em, "getPdp"); - } - - if (logger.isDebugEnabled()) { - logger.debug("getPdp: Returning droolsPdpEntity={}", droolsPdpEntity); - } - return droolsPdpEntity; - - } - - /* - * Normally this method should only be used in a JUnit test environment. - * Manually inserts a PDP record in droolspdpentity table. - */ - @Override - public void insertPdp(DroolsPdp pdp) { - if(logger.isDebugEnabled()){ - logger.debug("insertPdp: Entering and manually inserting PDP"); - } - - /* - * Start transaction - */ - EntityManager em = emf.createEntityManager(); - try { - em.getTransaction().begin(); - - /* - * Insert record. - */ - DroolsPdpEntity droolsPdpEntity = new DroolsPdpEntity(); - em.persist(droolsPdpEntity); - droolsPdpEntity.setPdpId(pdp.getPdpId()); - droolsPdpEntity.setDesignated(pdp.isDesignated()); - droolsPdpEntity.setPriority(pdp.getPriority()); - droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate()); - droolsPdpEntity.setSiteName(pdp.getSiteName()); - - /* - * End transaction. - */ - em.getTransaction().commit(); - } finally { - cleanup(em, "insertPdp"); - } - if(logger.isDebugEnabled()){ - logger.debug("insertPdp: Exiting"); - } - - } - - /* - * Normally this method should only be used in a JUnit test environment. - * Manually deletes all PDP records in droolspdpentity table. - */ - @Override - public void deleteAllPdps() { - - if(logger.isDebugEnabled()){ - logger.debug("deleteAllPdps: Entering"); - } - - /* - * Start transaction - */ - EntityManager em = emf.createEntityManager(); - try { - em.getTransaction().begin(); - - Query droolsPdpsListQuery = em - .createQuery("SELECT p FROM DroolsPdpEntity p"); - @SuppressWarnings("unchecked") - List<DroolsPdp> droolsPdpsList = droolsPdpsListQuery.setLockMode( - LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - if(logger.isDebugEnabled()){ - logger.debug("deleteAllPdps: Deleting {} PDPs", droolsPdpsList.size()); - } - for (DroolsPdp droolsPdp : droolsPdpsList) { - String pdpId = droolsPdp.getPdpId(); - deletePdp(pdpId); - } - - /* - * End transaction. - */ - em.getTransaction().commit(); - } finally { - cleanup(em, "deleteAllPdps"); - } - if(logger.isDebugEnabled()){ - logger.debug("deleteAllPdps: Exiting"); - } - - } - - /* - * Normally this method should only be used in a JUnit test environment. - * Manually deletes a PDP record in droolspdpentity table. - */ - @Override - public void deletePdp(String pdpId) { - if(logger.isDebugEnabled()){ - logger.debug("deletePdp: Entering and manually deleting pdpId={}", pdpId); - } - - /* - * Start transaction - */ - EntityManager em = emf.createEntityManager(); - try { - em.getTransaction().begin(); - - /* - * Delete record. - */ - DroolsPdpEntity droolsPdpEntity = em.find(DroolsPdpEntity.class, pdpId); - if (droolsPdpEntity != null) { - if(logger.isDebugEnabled()){ - logger.debug("deletePdp: Removing PDP"); - } - em.remove(droolsPdpEntity); - } else { - if(logger.isDebugEnabled()){ - logger.debug("deletePdp: PDP with ID={} not currently in DB", pdpId); - } - } - - /* - * End transaction. - */ - em.getTransaction().commit(); - } finally { - cleanup(em, "deletePdp"); - } - if(logger.isDebugEnabled()){ - logger.debug("deletePdp: Exiting"); - } - - } - - /* - * Close the specified EntityManager, rolling back any pending transaction - * - * @param em the EntityManager to close ('null' is OK) - * @param method the invoking Java method (used for log messages) - */ - private static void cleanup(EntityManager em, String method) - { - if (em != null && em.isOpen()) { - if (em.getTransaction().isActive()) { - // there is an active EntityTransaction -- roll it back - try { - em.getTransaction().rollback(); - } catch (Exception e) { - logger.error(method + ": Caught Exception attempting to rollback EntityTransaction,", e); - } - } - - // now, close the EntityManager - try { - em.close(); - } catch (Exception e) { - logger.error(method + ": Caught Exception attempting to close EntityManager, ", e); - } - } - } + // get an instance of logger + private static final Logger logger = LoggerFactory.getLogger(JpaDroolsPdpsConnector.class); + private EntityManagerFactory emf; + + + //not sure if we want to use the same entity manager factory + //for drools session and pass it in here, or create a new one + public JpaDroolsPdpsConnector(EntityManagerFactory emf) { + this.emf = emf; + } + + @Override + public Collection<DroolsPdp> getDroolsPdps() { + //return a list of all the DroolsPdps in the database + EntityManager em = emf.createEntityManager(); + try { + em.getTransaction().begin(); + Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p"); + List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) + .setFlushMode(FlushModeType.COMMIT).getResultList(); + LinkedList<DroolsPdp> droolsPdpsReturnList = new LinkedList<>(); + for (Object o : droolsPdpsList) { + if (o instanceof DroolsPdp) { + //Make sure it is not a cached version + em.refresh((DroolsPdpEntity)o); + droolsPdpsReturnList.add((DroolsPdp)o); + if (logger.isDebugEnabled()) { + DroolsPdp droolsPdp = (DroolsPdp)o; + logger.debug("getDroolsPdps: PDP= {}" + + ", isDesignated= {}" + + ", updatedDate= {}" + + ", priority= {}", droolsPdp.getPdpId(), droolsPdp.isDesignated(), + droolsPdp.getUpdatedDate(), droolsPdp.getPriority()); + } + } + } + try { + em.getTransaction().commit(); + } catch (Exception e) { + logger.error("Cannot commit getDroolsPdps() transaction", e); + } + return droolsPdpsReturnList; + } finally { + cleanup(em, "getDroolsPdps"); + } + } + + private boolean nullSafeEquals(Object one, Object two) { + if (one == null && two == null) { + return true; + } + if (one != null && two != null) { + return one.equals(two); + } + return false; + } + + @Override + public void update(DroolsPdp pdp) { + + if (logger.isDebugEnabled()) { + logger.debug("update: Entering, pdpId={}", pdp.getPdpId()); + } + + //this is to update our own pdp in the database + EntityManager em = emf.createEntityManager(); + try { + em.getTransaction().begin(); + Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); + droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId()); + List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) + .setFlushMode(FlushModeType.COMMIT).getResultList(); + DroolsPdpEntity droolsPdpEntity; + if (droolsPdpsList.size() == 1 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) { + droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0); + em.refresh(droolsPdpEntity); //Make sure we have current values + Date currentDate = new Date(); + long difference = currentDate.getTime() - droolsPdpEntity.getUpdatedDate().getTime(); + //just set some kind of default here + long pdpTimeout = 15000; + try { + pdpTimeout = Long.parseLong( + ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_TIMEOUT)); + } catch (Exception e) { + logger.error("Could not get PDP timeout property, using default.", e); + } + boolean isCurrent = difference < pdpTimeout; + if (logger.isDebugEnabled()) { + logger.debug("update: PDP= {}, isCurrent={}" + + " difference= {}" + + ", pdpTimeout= {}, designated= {}", + pdp.getPdpId(), isCurrent, difference, pdpTimeout, droolsPdpEntity.isDesignated()); + } + } else { + if (logger.isDebugEnabled()) { + logger.debug("update: For PDP={}" + + ", instantiating new DroolsPdpEntity", pdp.getPdpId()); + } + droolsPdpEntity = new DroolsPdpEntity(); + em.persist(droolsPdpEntity); + droolsPdpEntity.setPdpId(pdp.getPdpId()); + } + if (droolsPdpEntity.getPriority() != pdp.getPriority()) { + droolsPdpEntity.setPriority(pdp.getPriority()); + } + if (!droolsPdpEntity.getUpdatedDate().equals(pdp.getUpdatedDate())) { + droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate()); + } + if (!nullSafeEquals(droolsPdpEntity.getSiteName(),pdp.getSiteName())) { + droolsPdpEntity.setSiteName(pdp.getSiteName()); + } + + if (droolsPdpEntity.isDesignated() != pdp.isDesignated()) { + if (logger.isDebugEnabled()) { + logger.debug("update: pdpId={}" + + ", pdp.isDesignated={}" + + ", droolsPdpEntity.pdpId= {}" + + ", droolsPdpEntity.isDesignated={}", + pdp.getPdpId(), pdp.isDesignated(), + droolsPdpEntity.getPdpId(), droolsPdpEntity.isDesignated()); + } + droolsPdpEntity.setDesignated(pdp.isDesignated()); + //The isDesignated value is not the same and the new one == true + if (pdp.isDesignated()) { + droolsPdpEntity.setDesignatedDate(new Date()); + } + } + em.getTransaction().commit(); + } finally { + cleanup(em, "update"); + } + + if (logger.isDebugEnabled()) { + logger.debug("update: Exiting"); + } + + } + + /* + * Note: A side effect of this boolean method is that if the PDP is designated but not current, the + * droolspdpentity.DESIGNATED column will be set to false (the PDP will be un-designated, i.e. marked as + * being in standby mode) + */ + @Override + public boolean isPdpCurrent(DroolsPdp pdp) { + + boolean isCurrent = isCurrent(pdp); + + EntityManager em = emf.createEntityManager(); + try { + if (!isCurrent && pdp.isDesignated()) { + em.getTransaction().begin(); + Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); + droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId()); + List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) + .setFlushMode(FlushModeType.COMMIT).getResultList(); + if (droolsPdpsList.size() == 1 && droolsPdpsList.get(0) instanceof DroolsPdpEntity) { + if (logger.isDebugEnabled()) { + logger.debug("isPdpCurrent: PDP={} designated but not current; setting designated to false", + pdp.getPdpId()); + } + DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0); + droolsPdpEntity.setDesignated(false); + em.getTransaction().commit(); + } else { + logger.warn("isPdpCurrent: PDP={} is designated but not current; " + + "however it does not have a DB entry, so cannot set DESIGNATED to false!", + pdp.getPdpId()); + } + } else { + if (logger.isDebugEnabled()) { + logger.debug("isPdpCurrent: For PDP= {}, " + + "designated={}, isCurrent={}", pdp.getPdpId(), pdp.isDesignated(), isCurrent); + } + } + } catch (Exception e) { + logger.error("Could not update expired record marked as designated in the database", e); + } finally { + cleanup(em, "isPdpCurrent"); + } + return isCurrent; + + } + + @Override + public void setDesignated(DroolsPdp pdp, boolean designated) { + + if (logger.isDebugEnabled()) { + logger.debug("setDesignated: Entering, pdpId={}" + + ", designated={}", pdp.getPdpId(), designated); + } + + EntityManager em = null; + try { + em = emf.createEntityManager(); + em.getTransaction().begin(); + Query droolsPdpsListQuery = em + .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); + droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId()); + List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( + LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + if (droolsPdpsList.size() == 1 + && droolsPdpsList.get(0) instanceof DroolsPdpEntity) { + DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList + .get(0); + + if (logger.isDebugEnabled()) { + logger.debug("setDesignated: PDP={}" + + " found, designated= {}" + + ", setting to {}", pdp.getPdpId(), droolsPdpEntity.isDesignated(), + designated); + } + droolsPdpEntity.setDesignated(designated); + if (designated) { + em.refresh(droolsPdpEntity); //make sure we get the DB value + if (!droolsPdpEntity.isDesignated()) { + droolsPdpEntity.setDesignatedDate(new Date()); + } + + } + em.getTransaction().commit(); + } else { + logger.error("setDesignated: PDP={}" + + " not in DB; cannot update designation", pdp.getPdpId()); + } + } catch (Exception e) { + logger.error("setDesignated: Caught Exception", e); + } finally { + cleanup(em, "setDesignated"); + } + + if (logger.isDebugEnabled()) { + logger.debug("setDesignated: Exiting"); + } + + } + + + @Override + public void standDownPdp(String pdpId) { + if (logger.isDebugEnabled()) { + logger.debug("standDownPdp: Entering, pdpId={}", pdpId); + } + + EntityManager em = null; + try { + /* + * Start transaction. + */ + em = emf.createEntityManager(); + em.getTransaction().begin(); + + /* + * Get droolspdpentity record for this PDP and mark DESIGNATED as + * false. + */ + Query droolsPdpsListQuery = em + .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); + droolsPdpsListQuery.setParameter("pdpId", pdpId); + List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( + LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + DroolsPdpEntity droolsPdpEntity; + if (droolsPdpsList.size() == 1 + && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) { + droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0); + droolsPdpEntity.setDesignated(false); + em.persist(droolsPdpEntity); + if (logger.isDebugEnabled()) { + logger.debug("standDownPdp: PDP={} persisted as non-designated.", pdpId ); + } + } else { + logger.error("standDownPdp: Missing record in droolspdpentity for pdpId={}" + + "; cannot stand down PDP", pdpId); + } + + /* + * End transaction. + */ + em.getTransaction().commit(); + cleanup(em, "standDownPdp"); + em = null; + + // Keep the election handler in sync with the DB + DroolsPdpsElectionHandler.setMyPdpDesignated(false); + + } catch (Exception e) { + logger.error("standDownPdp: Unexpected Exception attempting to mark " + + "DESIGNATED as false for droolspdpentity, pdpId={}" + + ". Cannot stand down PDP; message={}", pdpId, e.getMessage(), e); + } finally { + cleanup(em, "standDownPdp"); + } + if (logger.isDebugEnabled()) { + logger.debug("standDownPdp: Exiting"); + } + + } + + /* + * Determines whether or not a designated PDP has failed. + * + * Note: The update method, which is run periodically by the + * TimerUpdateClass, will un-designate a PDP that is stale. + */ + @Override + public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps) { + + if (logger.isDebugEnabled()) { + logger.debug("hasDesignatedPdpFailed: Entering, pdps.size()={}", pdps.size()); + } + + boolean failed = true; + boolean foundDesignatedPdp = false; + + for (DroolsPdp pdp : pdps) { + + /* + * Normally, the update method will un-designate any stale PDP, but + * we check here to see if the PDP has gone stale since the update + * method was run. + * + * Even if we determine that the designated PDP is current, we keep + * going (we don't break), so we can get visibility into the other + * PDPs, when in DEBUG mode. + */ + if (pdp.isDesignated() && isCurrent(pdp)) { + if (logger.isDebugEnabled()) { + logger.debug("hasDesignatedPdpFailed: Designated PDP={} is current", pdp.getPdpId()); + } + failed = false; + foundDesignatedPdp = true; + } else if (pdp.isDesignated() && !isCurrent(pdp)) { + logger.error("hasDesignatedPdpFailed: Designated PDP={} has failed", pdp.getPdpId()); + foundDesignatedPdp = true; + } else { + if (logger.isDebugEnabled()) { + logger.debug("hasDesignatedPdpFailed: PDP={} is not designated", pdp.getPdpId()); + } + } + } + + if (logger.isDebugEnabled()) { + logger.debug("hasDesignatedPdpFailed: Exiting and returning, foundDesignatedPdp={}", + foundDesignatedPdp); + } + return failed; + } + + + private boolean isCurrent(DroolsPdp pdp) { + + if (logger.isDebugEnabled()) { + logger.debug("isCurrent: Entering, pdpId={}", pdp.getPdpId()); + } + + boolean current = false; + + // Return if the current PDP is considered "current" based on whatever + // time box that may be. + // If the the PDP is not current, we should mark it as not primary in + // the database + Date currentDate = new Date(); + long difference = currentDate.getTime() + - pdp.getUpdatedDate().getTime(); + // just set some kind of default here + long pdpTimeout = 15000; + try { + pdpTimeout = Long.parseLong(ActiveStandbyProperties + .getProperty(ActiveStandbyProperties.PDP_TIMEOUT)); + if (logger.isDebugEnabled()) { + logger.debug("isCurrent: pdp.timeout={}", pdpTimeout); + } + } catch (Exception e) { + logger.error("isCurrent: Could not get PDP timeout property, using default.", e); + } + current = difference < pdpTimeout; + + if (logger.isDebugEnabled()) { + logger.debug("isCurrent: Exiting, difference={}, pdpTimeout={}" + + "; returning current={}", difference, pdpTimeout, current); + } + + return current; + } + + + /* + * Currently this method is only used in a JUnit test environment. Gets a + * PDP record from droolspdpentity table. + */ + @Override + public DroolsPdpEntity getPdp(String pdpId) { + + if (logger.isDebugEnabled()) { + logger.debug("getPdp: Entering and getting PDP with pdpId={}", pdpId); + } + + DroolsPdpEntity droolsPdpEntity = null; + + EntityManager em = null; + try { + em = emf.createEntityManager(); + em.getTransaction().begin(); + Query droolsPdpsListQuery = em + .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId"); + droolsPdpsListQuery.setParameter("pdpId", pdpId); + List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( + LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + if (droolsPdpsList.size() == 1 + && droolsPdpsList.get(0) instanceof DroolsPdpEntity) { + droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0); + if (logger.isDebugEnabled()) { + logger.debug("getPdp: PDP={}" + + " found, isDesignated={}," + + " updatedDate={}, " + + "priority={}", pdpId, + droolsPdpEntity.isDesignated(), droolsPdpEntity.getUpdatedDate(), + droolsPdpEntity.getPriority()); + } + + // Make sure the droolsPdpEntity is not a cached version + em.refresh(droolsPdpEntity); + + em.getTransaction().commit(); + } else { + logger.error("getPdp: PDP={} not found!?", pdpId); + } + } catch (Exception e) { + logger.error("getPdp: Caught Exception attempting to get PDP", e); + } finally { + cleanup(em, "getPdp"); + } + + if (logger.isDebugEnabled()) { + logger.debug("getPdp: Returning droolsPdpEntity={}", droolsPdpEntity); + } + return droolsPdpEntity; + + } + + /* + * Normally this method should only be used in a JUnit test environment. + * Manually inserts a PDP record in droolspdpentity table. + */ + @Override + public void insertPdp(DroolsPdp pdp) { + if (logger.isDebugEnabled()) { + logger.debug("insertPdp: Entering and manually inserting PDP"); + } + + /* + * Start transaction + */ + EntityManager em = emf.createEntityManager(); + try { + em.getTransaction().begin(); + + /* + * Insert record. + */ + DroolsPdpEntity droolsPdpEntity = new DroolsPdpEntity(); + em.persist(droolsPdpEntity); + droolsPdpEntity.setPdpId(pdp.getPdpId()); + droolsPdpEntity.setDesignated(pdp.isDesignated()); + droolsPdpEntity.setPriority(pdp.getPriority()); + droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate()); + droolsPdpEntity.setSiteName(pdp.getSiteName()); + + /* + * End transaction. + */ + em.getTransaction().commit(); + } finally { + cleanup(em, "insertPdp"); + } + if (logger.isDebugEnabled()) { + logger.debug("insertPdp: Exiting"); + } + + } + + /* + * Normally this method should only be used in a JUnit test environment. + * Manually deletes all PDP records in droolspdpentity table. + */ + @Override + public void deleteAllPdps() { + + if (logger.isDebugEnabled()) { + logger.debug("deleteAllPdps: Entering"); + } + + /* + * Start transaction + */ + EntityManager em = emf.createEntityManager(); + try { + em.getTransaction().begin(); + + Query droolsPdpsListQuery = em + .createQuery("SELECT p FROM DroolsPdpEntity p"); + @SuppressWarnings("unchecked") + List<DroolsPdp> droolsPdpsList = droolsPdpsListQuery.setLockMode( + LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); + if (logger.isDebugEnabled()) { + logger.debug("deleteAllPdps: Deleting {} PDPs", droolsPdpsList.size()); + } + for (DroolsPdp droolsPdp : droolsPdpsList) { + String pdpId = droolsPdp.getPdpId(); + deletePdp(pdpId); + } + + /* + * End transaction. + */ + em.getTransaction().commit(); + } finally { + cleanup(em, "deleteAllPdps"); + } + if (logger.isDebugEnabled()) { + logger.debug("deleteAllPdps: Exiting"); + } + + } + + /* + * Normally this method should only be used in a JUnit test environment. + * Manually deletes a PDP record in droolspdpentity table. + */ + @Override + public void deletePdp(String pdpId) { + if (logger.isDebugEnabled()) { + logger.debug("deletePdp: Entering and manually deleting pdpId={}", pdpId); + } + + /* + * Start transaction + */ + EntityManager em = emf.createEntityManager(); + try { + em.getTransaction().begin(); + + /* + * Delete record. + */ + DroolsPdpEntity droolsPdpEntity = em.find(DroolsPdpEntity.class, pdpId); + if (droolsPdpEntity != null) { + if (logger.isDebugEnabled()) { + logger.debug("deletePdp: Removing PDP"); + } + em.remove(droolsPdpEntity); + } else { + if (logger.isDebugEnabled()) { + logger.debug("deletePdp: PDP with ID={} not currently in DB", pdpId); + } + } + + /* + * End transaction. + */ + em.getTransaction().commit(); + } finally { + cleanup(em, "deletePdp"); + } + if (logger.isDebugEnabled()) { + logger.debug("deletePdp: Exiting"); + } + + } + + /* + * Close the specified EntityManager, rolling back any pending transaction + * + * @param em the EntityManager to close ('null' is OK) + * @param method the invoking Java method (used for log messages) + */ + private static void cleanup(EntityManager em, String method) { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + // there is an active EntityTransaction -- roll it back + try { + em.getTransaction().rollback(); + } catch (Exception e) { + logger.error(method + ": Caught Exception attempting to rollback EntityTransaction,", e); + } + } + + // now, close the EntityManager + try { + em.close(); + } catch (Exception e) { + logger.error(method + ": Caught Exception attempting to close EntityManager, ", e); + } + } + } } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java index 805c4b80..554376ce 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java @@ -51,24 +51,32 @@ import org.slf4j.LoggerFactory; /* * Some background: + * + * Originally, there was a "StandbyStateChangeNotifier" that belonged to policy-core, and this class's + * handleStateChange() method used to take care of invoking conn.standDownPdp(). * - * Originally, there was a "StandbyStateChangeNotifier" that belonged to policy-core, and this class's handleStateChange() method - * used to take care of invoking conn.standDownPdp(). But testing revealed that when a state change to hot standby occurred - * from a demote() operation, first the PMStandbyStateChangeNotifier.handleStateChange() method would be invoked and then the - * StandbyStateChangeNotifier.handleStateChange() method would be invoked, and this ordering was creating the following problem: + * But testing revealed that when a state change to hot standby + * occurred from a demote() operation, first the PMStandbyStateChangeNotifier.handleStateChange() method + * would be invoked and then the StandbyStateChangeNotifier.handleStateChange() method would be invoked, + * and this ordering was creating the following problem: + * + * When PMStandbyStateChangeNotifier.handleStateChange() was invoked it would take a long time to finish, + * because it would result in SingleThreadedUebTopicSource.stop() being invoked, which can potentially do a + * 5 second sleep for each controller being stopped. * - * When PMStandbyStateChangeNotifier.handleStateChange() was invoked it would take a long time to finish, because it would result - * in SingleThreadedUebTopicSource.stop() being invoked, which can potentially do a 5 second sleep for each controller being stopped. - * Meanwhile, as these controller stoppages and their associated sleeps were occurring, the election handler would discover the - * demoted PDP in hotstandby (but still designated!) and promote it, resulting in the standbyStatus going from hotstandby - * to providingservice. So then, by the time that PMStandbyStateChangeNotifier.handleStateChange() finished its work and - * StandbyStateChangeNotifier.handleStateChange() started executing, the standbyStatus was no longer hotstandby (as effected by - * the demote), but providingservice (as reset by the election handling logic) and conn.standDownPdp() would not get called! - * - * To fix this bug, we consolidated StandbyStateChangeNotifier and PMStandbyStateChangeNotifier, with the standDownPdp() always - * being invoked prior to the TopicEndpoint.manager.lock(). In this way, when the election handling logic is invoked + * Meanwhile, as these controller stoppages and their associated sleeps were occurring, the election handler + * would discover the demoted PDP in hotstandby (but still designated!) and promote it, resulting in the + * standbyStatus going from hotstandby to providingservice. So then, by the time that + * PMStandbyStateChangeNotifier.handleStateChange() finished its work and + * StandbyStateChangeNotifier.handleStateChange() started executing, the standbyStatus was no longer hotstandby + * (as effected by the demote), but providingservice (as reset by the election handling logic) and + * conn.standDownPdp() would not get called! + * + * To fix this bug, we consolidated StandbyStateChangeNotifier and PMStandbyStateChangeNotifier, + * with the standDownPdp() always + * being invoked prior to the TopicEndpoint.manager.lock(). In this way, when the election handling logic is invoked * during the controller stoppages, the PDP is in hotstandby and the standdown occurs. - * + * */ public class PMStandbyStateChangeNotifier extends StateChangeNotifier { // get an instance of logger @@ -84,6 +92,10 @@ public class PMStandbyStateChangeNotifier extends StateChangeNotifier { public static final String UNSUPPORTED = "unsupported"; public static final String HOTSTANDBY_OR_COLDSTANDBY = "hotstandby_or_coldstandby"; + /** + * Constructor. + * + */ public PMStandbyStateChangeNotifier() { pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL)); @@ -122,8 +134,9 @@ public class PMStandbyStateChangeNotifier extends StateChangeNotifier { // We were just here and did this successfully if (logger.isDebugEnabled()) { logger.debug( - "handleStateChange: Is returning because standbyStatus is null and was previously 'null'; PDP={}", - pdpId); + "handleStateChange: " + + "Is returning because standbyStatus is null and was previously 'null'; PDP={}", + pdpId); } return; } @@ -230,16 +243,16 @@ public class PMStandbyStateChangeNotifier extends StateChangeNotifier { if (waitTimeMs > 3 * waitInterval) { if (logger.isDebugEnabled()) { logger.debug( - "handleStateChange: PROVIDING_SERVICE looks like the activation wait timer may be hung," - + " waitTimeMs = {} and allowable waitInterval = {}" - + " Checking whether it is currently in activation. isNowActivating = {}", - waitTimeMs, waitInterval, isNowActivating); + "handleStateChange: PROVIDING_SERVICE looks like the activation wait timer may be hung," + + " waitTimeMs = {} and allowable waitInterval = {}" + + " Checking whether it is currently in activation. isNowActivating = {}", + waitTimeMs, waitInterval, isNowActivating); } // Now check that it is not currently executing an activation if (!isNowActivating) { if (logger.isDebugEnabled()) { logger.debug( - "handleStateChange: PROVIDING_SERVICE looks like the activation wait timer died"); + "handleStateChange: PROVIDING_SERVICE looks like the activation wait timer died"); } // This will assure the timer is cancelled and rescheduled. isWaitingForActivation = false; diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java index b5fe3071..373c373c 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java @@ -22,6 +22,6 @@ package org.onap.policy.drools.activestandby; @FunctionalInterface public interface ThreadRunningChecker { - public void checkThreadStatus(); + public void checkThreadStatus(); } diff --git a/feature-active-standby-management/src/main/resources/META-INF/persistence.xml b/feature-active-standby-management/src/main/resources/META-INF/persistence.xml index 4a625b55..36f65ad8 100644 --- a/feature-active-standby-management/src/main/resources/META-INF/persistence.xml +++ b/feature-active-standby-management/src/main/resources/META-INF/persistence.xml @@ -3,7 +3,7 @@ ============LICENSE_START======================================================= feature-active-standby-management ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,14 +20,16 @@ --> <persistence version="2.1" - xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> - <persistence-unit name="activeStandbyPU" transaction-type="RESOURCE_LOCAL"> - <!-- This is for database access by non-drools methods --> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - <class>org.onap.policy.drools.activestandby.DroolsPdpEntity</class> - <properties> - <!-- Properties are passed in --> + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + <persistence-unit name="activeStandbyPU" + transaction-type="RESOURCE_LOCAL"> + <!-- This is for database access by non-drools methods --> + <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + <class>org.onap.policy.drools.activestandby.DroolsPdpEntity</class> + <properties> + <!-- Properties are passed in --> </properties> - </persistence-unit> + </persistence-unit> </persistence> diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java index 19265206..dfc69304 100644 --- a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,265 +55,280 @@ import org.slf4j.LoggerFactory; * Testing the allSeemsWell interface to verify that it correctly affects the * operational state. */ - + public class AllSeemsWellTest { - private static final Logger logger = LoggerFactory.getLogger(AllSeemsWellTest.class); - /* - * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting - * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion - * to ensure that we wait for the DesignationWaiter to do its job, before - * checking the results. Add a few seconds for safety - */ - - long sleepTime = 10000; - - /* - * DroolsPdpsElectionHandler runs every 1 seconds, so it takes 10 seconds for the - * checkWaitTimer() method to time out and call allSeemsWell which then requires - * the forward progress counter to go stale which should add an additional 5 sec. - */ - - long stalledElectionHandlerSleepTime = 15000; - - /* - * As soon as the election hander successfully runs, it will resume the forward progress. - * If the election handler runs ever 1 sec and test transaction is run every 1 sec and - * then fpc is written every 1 sec and then the fpc is checked every 2 sec, that could - * take a total of 5 sec to recognize the resumption of progress. So, add 1 for safety. - */ - long resumedElectionHandlerSleepTime = 6000; - - private static EntityManagerFactory emfx; - private static EntityManagerFactory emfd; - private static EntityManager emx; - private static EntityManager emd; - private static EntityTransaction et; - - private final String configDir = "src/test/resources/asw"; - - /* - * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing. - */ - - @BeforeClass - public static void setUpClass() throws Exception { - - String userDir = System.getProperty("user.dir"); - logger.debug("setUpClass: userDir={}", userDir); - System.setProperty("com.sun.management.jmxremote.port", "9980"); - System.setProperty("com.sun.management.jmxremote.authenticate","false"); - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - //Create teh data access for xaml db - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); - - emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties); - - // Create an entity manager to use the DB - emx = emfx.createEntityManager(); - - //Create the data access for drools db - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - - emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties); - - // Create an entity manager to use the DB - emd = emfd.createEntityManager(); - - DroolsPdpsElectionHandler.setIsUnitTesting(true); - } - - @After - public void tearDown() throws Exception { - - } - - public void cleanXacmlDb(){ - et = emx.getTransaction(); - - et.begin(); - // Make sure we leave the DB clean - emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate(); - emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate(); - emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate(); - emx.flush(); - et.commit(); - } - - public void cleanDroolsDb(){ - et = emd.getTransaction(); - - et.begin(); - // Make sure we leave the DB clean - emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate(); - emd.flush(); - et.commit(); - } - - - // Tests hot standby when there is only one PDP. - - //@Ignore - @Test - public void testAllSeemsWell() throws Exception { - - logger.debug("\n\ntestAllSeemsWell: Entering\n\n"); - cleanXacmlDb(); - cleanDroolsDb(); - - logger.debug("testAllSeemsWell: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); - - logger.debug("testAllSeemsWell: Creating emfXacml"); - EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( - "junitXacmlPU", stateManagementProperties); - - logger.debug("testAllSeemsWell: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); - - logger.debug("testAllSeemsWell: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); - - logger.debug("testAllSeemsWell: Cleaning up tables"); - conn.deleteAllPdps(); - - /* - * Insert this PDP as not designated. Initial standby state will be - * either null or cold standby. Demoting should transit state to - * hot standby. - */ - - logger.debug("testAllSeemsWell: Inserting PDP={} as not designated", thisPdpId); - Date yesterday = DateUtils.addDays(new Date(), -1); - DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday); - conn.insertPdp(pdp); - DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); - logger.debug("testAllSeemsWell: After insertion, PDP={} has DESIGNATED={}", - thisPdpId, droolsPdpEntity.isDesignated()); - assertTrue(droolsPdpEntity.isDesignated() == false); - - logger.debug("testAllSeemsWell: Instantiating stateManagement object"); - StateManagement sm = new StateManagement(emfXacml, "dummy"); - sm.deleteAllStateManagementEntities(); - - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI smf = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - smf = feature; - logger.debug("testAllSeemsWell stateManagementFeature.getResourceName(): {}", smf.getResourceName()); - break; - } - if(smf == null){ - logger.error("testAllSeemsWell failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testAllSeemsWell failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature - // that has been created. - ActiveStandbyFeatureAPI activeStandbyFeature = null; - for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - activeStandbyFeature = feature; - logger.debug("testAllSeemsWell activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); - break; - } - if(activeStandbyFeature == null){ - logger.error("testAllSeemsWell failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testAllSeemsWell failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - - logger.debug("testAllSeemsWell: Demoting PDP={}", thisPdpId); - // demoting should cause state to transit to hotstandby - smf.demote(); - - - logger.debug("testAllSeemsWell: Sleeping {} ms, to allow JpaDroolsPdpsConnector " - + "time to check droolspdpentity table", sleepTime); - sleep(sleepTime); - - - // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service. - - droolsPdpEntity = conn.getPdp(thisPdpId); - logger.debug("testAllSeemsWell: After sm.demote() invoked, DESIGNATED= {} " - + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId); - assertTrue(droolsPdpEntity.isDesignated() == true); - String standbyStatus = smf.getStandbyStatus(thisPdpId); - logger.debug("testAllSeemsWell: After demotion, PDP= {} " - + "has standbyStatus= {}", thisPdpId, standbyStatus); - assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE)); - - //Now we want to stall the election handler and see the if AllSeemsWell will make the - //standbystatus = coldstandby - - DroolsPdpsElectionHandler.setIsStalled(true); - - logger.debug("testAllSeemsWell: Sleeping {} ms, to allow checkWaitTimer to recognize " - + "the election handler has stalled and for the testTransaction to fail to " - + "increment forward progress and for the lack of forward progress to be recognized.", - stalledElectionHandlerSleepTime); - - - //It takes 10x the update interval (1 sec) before the watcher will declare the election handler dead - //and that just stops forward progress counter. So, the fp monitor must then run to determine - //if the fpc has stalled. That will take about another 5 sec. - sleep(stalledElectionHandlerSleepTime); - - logger.debug("testAllSeemsWell: After isStalled=true, PDP= {} " - + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId)); - - assertTrue(smf.getStandbyStatus().equals(StateManagement.COLD_STANDBY)); - - //Now lets resume the election handler - DroolsPdpsElectionHandler.setIsStalled(false); - - sleep(resumedElectionHandlerSleepTime); - - logger.debug("testAllSeemsWell: After isStalled=false, PDP= {} " - + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId)); - - assertTrue(smf.getStandbyStatus().equals(StateManagement.PROVIDING_SERVICE)); - - //resumedElectionHandlerSleepTime = 5000; - logger.debug("\n\ntestAllSeemsWell: Exiting\n\n"); - - } - - private void sleep(long sleepms) throws InterruptedException { - Thread.sleep(sleepms); - } + private static final Logger logger = LoggerFactory.getLogger(AllSeemsWellTest.class); + /* + * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting + * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion + * to ensure that we wait for the DesignationWaiter to do its job, before + * checking the results. Add a few seconds for safety + */ + + long sleepTime = 10000; + + /* + * DroolsPdpsElectionHandler runs every 1 seconds, so it takes 10 seconds for the + * checkWaitTimer() method to time out and call allSeemsWell which then requires + * the forward progress counter to go stale which should add an additional 5 sec. + */ + + long stalledElectionHandlerSleepTime = 15000; + + /* + * As soon as the election hander successfully runs, it will resume the forward progress. + * If the election handler runs ever 1 sec and test transaction is run every 1 sec and + * then fpc is written every 1 sec and then the fpc is checked every 2 sec, that could + * take a total of 5 sec to recognize the resumption of progress. So, add 1 for safety. + */ + long resumedElectionHandlerSleepTime = 6000; + + private static EntityManagerFactory emfx; + private static EntityManagerFactory emfd; + private static EntityManager emx; + private static EntityManager emd; + private static EntityTransaction et; + + private final String configDir = "src/test/resources/asw"; + + /* + * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing. + */ + + /** + * Setup the class. + * + * @throws Exception exception + */ + @BeforeClass + public static void setUpClass() throws Exception { + + String userDir = System.getProperty("user.dir"); + logger.debug("setUpClass: userDir={}", userDir); + System.setProperty("com.sun.management.jmxremote.port", "9980"); + System.setProperty("com.sun.management.jmxremote.authenticate","false"); + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + /** + * Setup. + * + * @throws Exception exception + */ + @Before + public void setUp() throws Exception { + //Create teh data access for xaml db + Properties stateManagementProperties = new Properties(); + stateManagementProperties.load(new FileInputStream(new File( + configDir + "/feature-state-management.properties"))); + + emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties); + + // Create an entity manager to use the DB + emx = emfx.createEntityManager(); + + //Create the data access for drools db + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + + emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties); + + // Create an entity manager to use the DB + emd = emfd.createEntityManager(); + + DroolsPdpsElectionHandler.setIsUnitTesting(true); + } + + @After + public void tearDown() throws Exception { + + } + + /** + * Clean the xacml database. + */ + public void cleanXacmlDb() { + et = emx.getTransaction(); + + et.begin(); + // Make sure we leave the DB clean + emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate(); + emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate(); + emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate(); + emx.flush(); + et.commit(); + } + + /** + * Clean the drools database. + */ + public void cleanDroolsDb() { + et = emd.getTransaction(); + + et.begin(); + // Make sure we leave the DB clean + emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate(); + emd.flush(); + et.commit(); + } + + + // Tests hot standby when there is only one PDP. + + //@Ignore + @Test + public void testAllSeemsWell() throws Exception { + + logger.debug("\n\ntestAllSeemsWell: Entering\n\n"); + cleanXacmlDb(); + cleanDroolsDb(); + + logger.debug("testAllSeemsWell: Reading stateManagementProperties"); + Properties stateManagementProperties = new Properties(); + stateManagementProperties.load(new FileInputStream(new File( + configDir + "/feature-state-management.properties"))); + + logger.debug("testAllSeemsWell: Creating emfXacml"); + final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( + "junitXacmlPU", stateManagementProperties); + + logger.debug("testAllSeemsWell: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + final String thisPdpId = activeStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + logger.debug("testAllSeemsWell: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); + + logger.debug("testAllSeemsWell: Cleaning up tables"); + conn.deleteAllPdps(); + + /* + * Insert this PDP as not designated. Initial standby state will be + * either null or cold standby. Demoting should transit state to + * hot standby. + */ + + logger.debug("testAllSeemsWell: Inserting PDP={} as not designated", thisPdpId); + Date yesterday = DateUtils.addDays(new Date(), -1); + DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday); + conn.insertPdp(pdp); + DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); + logger.debug("testAllSeemsWell: After insertion, PDP={} has DESIGNATED={}", + thisPdpId, droolsPdpEntity.isDesignated()); + assertTrue(droolsPdpEntity.isDesignated() == false); + + logger.debug("testAllSeemsWell: Instantiating stateManagement object"); + StateManagement sm = new StateManagement(emfXacml, "dummy"); + sm.deleteAllStateManagementEntities(); + + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI smf = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + smf = feature; + logger.debug("testAllSeemsWell stateManagementFeature.getResourceName(): {}", smf.getResourceName()); + break; + } + if (smf == null) { + logger.error("testAllSeemsWell failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testAllSeemsWell failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature + // that has been created. + ActiveStandbyFeatureAPI activeStandbyFeature = null; + for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + activeStandbyFeature = feature; + logger.debug("testAllSeemsWell activeStandbyFeature.getResourceName(): {}", + activeStandbyFeature.getResourceName()); + break; + } + if (activeStandbyFeature == null) { + logger.error("testAllSeemsWell failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testAllSeemsWell failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + + logger.debug("testAllSeemsWell: Demoting PDP={}", thisPdpId); + // demoting should cause state to transit to hotstandby + smf.demote(); + + + logger.debug("testAllSeemsWell: Sleeping {} ms, to allow JpaDroolsPdpsConnector " + + "time to check droolspdpentity table", sleepTime); + sleep(sleepTime); + + + // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service. + + droolsPdpEntity = conn.getPdp(thisPdpId); + logger.debug("testAllSeemsWell: After sm.demote() invoked, DESIGNATED= {} " + + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId); + assertTrue(droolsPdpEntity.isDesignated() == true); + String standbyStatus = smf.getStandbyStatus(thisPdpId); + logger.debug("testAllSeemsWell: After demotion, PDP= {} " + + "has standbyStatus= {}", thisPdpId, standbyStatus); + assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE)); + + //Now we want to stall the election handler and see the if AllSeemsWell will make the + //standbystatus = coldstandby + + DroolsPdpsElectionHandler.setIsStalled(true); + + logger.debug("testAllSeemsWell: Sleeping {} ms, to allow checkWaitTimer to recognize " + + "the election handler has stalled and for the testTransaction to fail to " + + "increment forward progress and for the lack of forward progress to be recognized.", + stalledElectionHandlerSleepTime); + + + //It takes 10x the update interval (1 sec) before the watcher will declare the election handler dead + //and that just stops forward progress counter. So, the fp monitor must then run to determine + //if the fpc has stalled. That will take about another 5 sec. + sleep(stalledElectionHandlerSleepTime); + + logger.debug("testAllSeemsWell: After isStalled=true, PDP= {} " + + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId)); + + assertTrue(smf.getStandbyStatus().equals(StateManagement.COLD_STANDBY)); + + //Now lets resume the election handler + DroolsPdpsElectionHandler.setIsStalled(false); + + sleep(resumedElectionHandlerSleepTime); + + logger.debug("testAllSeemsWell: After isStalled=false, PDP= {} " + + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId)); + + assertTrue(smf.getStandbyStatus().equals(StateManagement.PROVIDING_SERVICE)); + + //resumedElectionHandlerSleepTime = 5000; + logger.debug("\n\ntestAllSeemsWell: Exiting\n\n"); + + } + + private void sleep(long sleepms) throws InterruptedException { + Thread.sleep(sleepms); + } } diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java index b79deeb0..aca2021d 100644 --- a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -69,1473 +69,1545 @@ import org.slf4j.LoggerFactory; */ public class StandbyStateManagementTest { - private static final Logger logger = LoggerFactory.getLogger(StandbyStateManagementTest.class); - /* - * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting - * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion - * to ensure that we wait for the DesignationWaiter to do its job, before - * checking the results. Add a few seconds for safety - */ - - long sleepTime = 10000; - - /* - * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be - * plenty to ensure it has time to re-promote this PDP. - */ - - long electionWaitSleepTime = 6000; - - /* - * Sleep 1 seconds after each test to allow interrupt (shutdown) recovery. - */ - - long interruptRecoveryTime = 5000; - - private static EntityManagerFactory emfx; - private static EntityManagerFactory emfd; - private static EntityManager emx; - private static EntityManager emd; - private static EntityTransaction et; - - private final String configDir = "src/test/resources"; - - /* - * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing. - */ - - @BeforeClass - public static void setUpClass() throws Exception { - - String userDir = System.getProperty("user.dir"); - logger.debug("setUpClass: userDir={}", userDir); - System.setProperty("com.sun.management.jmxremote.port", "9980"); - System.setProperty("com.sun.management.jmxremote.authenticate","false"); - - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - //Create teh data access for xaml db - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - "src/test/resources/feature-state-management.properties"))); - - emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties); - - // Create an entity manager to use the DB - emx = emfx.createEntityManager(); - - //Create the data access for drools db - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - "src/test/resources/feature-active-standby-management.properties"))); - - emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties); - - // Create an entity manager to use the DB - emd = emfd.createEntityManager(); - } - - @After - public void tearDown() throws Exception { - - } - - public void cleanXacmlDb(){ - et = emx.getTransaction(); - - et.begin(); - // Make sure we leave the DB clean - emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate(); - emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate(); - emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate(); - emx.flush(); - et.commit(); - } - - public void cleanDroolsDb(){ - et = emd.getTransaction(); - - et.begin(); - // Make sure we leave the DB clean - emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate(); - emd.flush(); - et.commit(); - } - - /* - * These JUnit tests must be run one at a time in an eclipse environment - * by right-clicking StandbyStateManagementTest and selecting - * "Run As" -> "JUnit Test". - * - * They will run successfully when you run all of them under runAllTests(), - * however, you will get all sorts of non-fatal errors in the log and on the - * console that result from overlapping threads that are not terminated at the - * end of each test. The problem is that the JUnit environment does not terminate - * all the test threads between tests. This is true even if you break each JUnit - * into a separate file. Consequently, all the tests would have to be refactored - * so all test object initializations are coordinated. In other words, you - * retrieve the ActiveStandbyFeature instance and other class instances only once - * at the beginning of the JUnits and then reuse them throughout the tests. - * Initialization of the state of the objects is pretty straight forward as it - * just amounts to manipulating the entries in StateManagementEntity and - * DroolsPdpEntity tables. However, some thought needs to be given to how to - * "pause" the processing in ActiveStandbyFeature class. I think we could "pause" - * it by calling globalInit() which will, I think, restart it. So long as it - * does not create a new instance, it will force it to go through an initialization - * cycle which includes a "pause" at the beginning of proecessing. We just must - * be sure it does not create another instance - which may mean we need to add - * a factory interface instead of calling the constructor directly. - */ - - - //@Ignore - @Test - public void runAllTests() throws Exception { - testColdStandby(); - testHotStandby1(); - testHotStandby2(); - testLocking1(); - testLocking2(); - testPMStandbyStateChangeNotifier(); - testSanitizeDesignatedList(); - testComputeMostRecentPrimary(); - testComputeDesignatedPdp(); - } - - //@Ignore - //@Test - public void testPMStandbyStateChangeNotifier() throws Exception { - logger.debug("\n\ntestPMStandbyStateChangeNotifier: Entering\n\n"); - cleanXacmlDb(); - - logger.debug("testPMStandbyStateChangeNotifier: Reading activeStandbyProperties"); - - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - - String resourceName = "testPMS"; - activeStandbyProperties.setProperty("resource.name", resourceName); - ActiveStandbyProperties.initProperties(activeStandbyProperties); - - logger.debug("testPMStandbyStateChangeNotifier: Getting StateManagement instance"); - - StateManagement sm = new StateManagement(emfx, resourceName); - - //Create an instance of the Observer - PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier(); - - //Register the PMStandbyStateChangeNotifier Observer - sm.addObserver(pmNotifier); - - //At this point the standbystatus = 'null' - sm.lock(); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE)); - - sm.unlock(); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE)); - - //Adding standbystatus=hotstandby - sm.demote(); - System.out.println(pmNotifier.getPreviousStandbyStatus()); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); - - //Now making standbystatus=coldstandby - sm.lock(); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); - - //standbystatus = hotstandby - sm.unlock(); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); - - //standbystatus = providingservice - sm.promote(); - //The previousStandbyStatus is not updated until after the delay activation expires - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); - - //Sleep long enough for the delayActivationTimer to run - sleep(5000); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE)); - - //standbystatus = providingservice - sm.promote(); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE)); - - //standbystatus = coldstandby - sm.lock(); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); - - //standbystatus = hotstandby - sm.unlock(); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); - - //standbystatus = hotstandby - sm.demote(); - assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); - } - - //@Ignore - //@Test - public void testSanitizeDesignatedList() throws Exception { - - logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n"); - - // Get a DroolsPdpsConnector - - logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); - - logger.debug("testSanitizeDesignatedList: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools); - - // Create 4 pdpd all not designated - - DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); - DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); - DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); - DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); - - List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>(); - listOfDesignated.add(pdp1); - listOfDesignated.add(pdp2); - listOfDesignated.add(pdp3); - listOfDesignated.add(pdp4); - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI stateManagementFeature = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - stateManagementFeature = feature; - logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName()); - break; - } - if(stateManagementFeature == null){ - logger.error("testColdStandby failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testColdStandby failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - - DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); - - listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated); - - logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size()); - - assertTrue(listOfDesignated.size()==4); - - // Now make 2 designated - - pdp1.setDesignated(true); - pdp2.setDesignated(true); - - listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated); - - logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n", listOfDesignated.size()); - - assertTrue(listOfDesignated.size()==2); - assertTrue(listOfDesignated.contains(pdp1)); - assertTrue(listOfDesignated.contains(pdp2)); - - - // Now all are designated. But, we have to add back the previously non-designated nodes - - pdp3.setDesignated(true); - pdp4.setDesignated(true); - listOfDesignated.add(pdp3); - listOfDesignated.add(pdp4); - - listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated); - - logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n", listOfDesignated.size()); - - assertTrue(listOfDesignated.size()==4); - - } - - - //@Ignore - //@Test - public void testComputeMostRecentPrimary() throws Exception { - - logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n"); - - logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); - - logger.debug("testComputeMostRecentPrimary: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools); - - - // Create 4 pdpd all not designated - - - long designatedDateMS = new Date().getTime(); - DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); - pdp1.setDesignatedDate(new Date(designatedDateMS - 2)); - - DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); - //oldest - pdp2.setDesignatedDate(new Date(designatedDateMS - 3)); - - DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); - pdp3.setDesignatedDate(new Date(designatedDateMS - 1)); - - DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); - //most recent - pdp4.setDesignatedDate(new Date(designatedDateMS)); - - ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>(); - listOfAllPdps.add(pdp1); - listOfAllPdps.add(pdp2); - listOfAllPdps.add(pdp3); - listOfAllPdps.add(pdp4); - - - ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>(); - listOfDesignated.add(pdp1); - listOfDesignated.add(pdp2); - listOfDesignated.add(pdp3); - listOfDesignated.add(pdp4); - - // Because the way we sanitize the listOfDesignated, it will always contain all hot standby - // or all designated members. - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI stateManagementFeature = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - stateManagementFeature = feature; - logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName()); - break; - } - if(stateManagementFeature == null){ - logger.error("testComputeMostRecentPrimary failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testComputeMostRecentPrimary failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); - - DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); - - logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId()); - - - // If all of the pdps are included in the listOfDesignated and none are designated, it will choose - // the one which has the most recent designated date. - - - assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); - - - // Now let's designate all of those on the listOfDesignated. It will choose the first one designated - - - pdp1.setDesignated(true); - pdp2.setDesignated(true); - pdp3.setDesignated(true); - pdp4.setDesignated(true); - - mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); - - logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId()); - - - // If all of the pdps are included in the listOfDesignated and all are designated, it will choose - // the one which was designated first - - - assertTrue(mostRecentPrimary.getPdpId().equals("pdp2")); - - - // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now - // look for the most recently designated pdp which is not currently designated. - - - pdp3.setDesignated(false); - pdp4.setDesignated(false); - - listOfDesignated.remove(pdp3); - listOfDesignated.remove(pdp4); - - mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); - - logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId()); - - assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); - - - - // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now - // look for the most recently designated pdp regardless of whether it is currently marked as designated. - - - pdp1.setDesignated(false); - pdp2.setDesignated(false); - - mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); - - logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId()); - - assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); - - - // If we have only one pdp on in the listOfDesignated, the most recently designated pdp will be chosen, regardless - // of its designation status - - - listOfDesignated.remove(pdp1); - - mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); - - logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId()); - - assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); - - - // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp. - - - listOfDesignated.remove(pdp2); - - mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); - - logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId()); - - assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); - - } - - //@Ignore - //@Test - public void testComputeDesignatedPdp() throws Exception{ - - logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n"); - - logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); - - - logger.debug("testComputeDesignatedPdp: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools); - - - // Create 4 pdpd all not designated. Two on site1. Two on site2 - - - long designatedDateMS = new Date().getTime(); - DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); - pdp1.setDesignatedDate(new Date(designatedDateMS - 2)); - pdp1.setSiteName("site1"); - - DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); - pdp2.setDesignatedDate(new Date(designatedDateMS - 3)); - pdp2.setSiteName("site1"); - - //oldest - DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); - pdp3.setDesignatedDate(new Date(designatedDateMS - 4)); - pdp3.setSiteName("site2"); - - DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); - //most recent - pdp4.setDesignatedDate(new Date(designatedDateMS)); - pdp4.setSiteName("site2"); - - ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>(); - listOfAllPdps.add(pdp1); - listOfAllPdps.add(pdp2); - listOfAllPdps.add(pdp3); - listOfAllPdps.add(pdp4); - - - ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>(); - - - // We will first test an empty listOfDesignated. As we know from the previous JUnit, - // the pdp with the most designated date will be chosen for mostRecentPrimary - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI stateManagementFeature = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - stateManagementFeature = feature; - logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName()); - break; - } - if(stateManagementFeature == null){ - logger.error("testComputeDesignatedPdp failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testComputeDesignatedPdp failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - - DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); - - DroolsPdp mostRecentPrimary = pdp4; - - DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary); - - - // The designatedPdp should be null - - assertTrue(designatedPdp==null); - - - // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary - - listOfDesignated.add(pdp2); - - designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary); - - - // Now the designatedPdp should be the one and only selection in the listOfDesignated - - - assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId())); - - - // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary - - - listOfDesignated.add(pdp1); - - designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary); - - - // The designatedPdp should now be the one with the lowest lexiographic score - pdp1 - - - assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId())); - - - // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary - - - listOfDesignated.remove(pdp1); - listOfDesignated.add(pdp3); - - designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary); - - - // The designatedPdp should now be the one on the same site as the mostRecentPrimary - - - assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId())); - } - - //@Ignore - //@Test - public void testColdStandby() throws Exception { - - logger.debug("\n\ntestColdStandby: Entering\n\n"); - cleanXacmlDb(); - cleanDroolsDb(); - - logger.debug("testColdStandby: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); - - logger.debug("testColdStandby: Creating emfXacml"); - EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( - "junitXacmlPU", stateManagementProperties); - - logger.debug("testColdStandby: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME); - - logger.debug("testColdStandby: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); - - logger.debug("testColdStandby: Cleaning up tables"); - conn.deleteAllPdps(); - - logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId); - DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date()); - conn.insertPdp(pdp); - DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); - logger.debug("testColdStandby: After insertion, DESIGNATED= {} " - + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId); - assertTrue(droolsPdpEntity.isDesignated() == true); - - /* - * When the Standby Status changes (from providingservice) to hotstandby - * or coldstandby,the Active/Standby selection algorithm must stand down - * if thePDP-D is currently the lead/active node and allow another PDP-D - * to take over. - * - * It must also call lock on all engines in the engine management. - */ - - - /* - * Yes, this is kludgy, but we have a chicken and egg problem here: we - * need a StateManagement object to invoke the - * deleteAllStateManagementEntities method. - */ - logger.debug("testColdStandby: Instantiating stateManagement object"); - - StateManagement sm = new StateManagement(emfXacml, "dummy"); - sm.deleteAllStateManagementEntities(); - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI smf = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - smf = feature; - logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName()); - break; - } - if(smf == null){ - logger.error("testColdStandby failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testColdStandby failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature - // that has been created. - ActiveStandbyFeatureAPI activeStandbyFeature = null; - for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - activeStandbyFeature = feature; - logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); - break; - } - if(activeStandbyFeature == null){ - logger.error("testColdStandby failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID:{}", thisPdpId); - logger.debug("testColdStandby failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID:{}", thisPdpId); - } - - // Artificially putting a PDP into service is really a two step process, 1) - // inserting it as designated and 2) promoting it so that its standbyStatus - // is providing service. - - logger.debug("testColdStandby: Runner started; Sleeping " - + interruptRecoveryTime + "ms before promoting PDP= {}", - thisPdpId); - sleep(interruptRecoveryTime); - - logger.debug("testColdStandby: Promoting PDP={}", thisPdpId); - smf.promote(); - - String standbyStatus = sm.getStandbyStatus(thisPdpId); - logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}", - thisPdpId, standbyStatus); - - logger.debug("testColdStandby: Locking smf"); - smf.lock(); - - sleep(interruptRecoveryTime); - - // Verify that the PDP is no longer designated. - - droolsPdpEntity = conn.getPdp(thisPdpId); - logger.debug("testColdStandby: After lock sm.lock() invoked, " - + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId); - assertTrue(droolsPdpEntity.isDesignated() == false); - - logger.debug("\n\ntestColdStandby: Exiting\n\n"); - sleep(interruptRecoveryTime); - - } - - // Tests hot standby when there is only one PDP. - - //@Ignore - //@Test - public void testHotStandby1() throws Exception { - - logger.debug("\n\ntestHotStandby1: Entering\n\n"); - cleanXacmlDb(); - cleanDroolsDb(); - - logger.debug("testHotStandby1: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); - - logger.debug("testHotStandby1: Creating emfXacml"); - EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( - "junitXacmlPU", stateManagementProperties); - - logger.debug("testHotStandby1: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); - - logger.debug("testHotStandby1: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); - - logger.debug("testHotStandby1: Cleaning up tables"); - conn.deleteAllPdps(); - - /* - * Insert this PDP as not designated. Initial standby state will be - * either null or cold standby. Demoting should transit state to - * hot standby. - */ - - logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId); - Date yesterday = DateUtils.addDays(new Date(), -1); - DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday); - conn.insertPdp(pdp); - DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); - logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}", - thisPdpId, droolsPdpEntity.isDesignated()); - assertTrue(droolsPdpEntity.isDesignated() == false); - - logger.debug("testHotStandby1: Instantiating stateManagement object"); - StateManagement sm = new StateManagement(emfXacml, "dummy"); - sm.deleteAllStateManagementEntities(); - - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI smf = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - smf = feature; - logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName()); - break; - } - if(smf == null){ - logger.error("testHotStandby1 failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testHotStandby1 failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature - // that has been created. - ActiveStandbyFeatureAPI activeStandbyFeature = null; - for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - activeStandbyFeature = feature; - logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); - break; - } - if(activeStandbyFeature == null){ - logger.error("testHotStandby1 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testHotStandby1 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - - logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId); - // demoting should cause state to transit to hotstandby - smf.demote(); - - - logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector " - + "time to check droolspdpentity table", sleepTime); - sleep(sleepTime); - - - // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service. - - droolsPdpEntity = conn.getPdp(thisPdpId); - logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} " - + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId); - assertTrue(droolsPdpEntity.isDesignated() == true); - String standbyStatus = smf.getStandbyStatus(thisPdpId); - logger.debug("testHotStandby1: After demotion, PDP= {} " - + "has standbyStatus= {}", thisPdpId, standbyStatus); - assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE)); - - logger.debug("testHotStandby1: Stopping policyManagementRunner"); - //policyManagementRunner.stopRunner(); - - logger.debug("\n\ntestHotStandby1: Exiting\n\n"); - sleep(interruptRecoveryTime); - - } - - /* - * Tests hot standby when two PDPs are involved. - */ - - //@Ignore - //@Test - public void testHotStandby2() throws Exception { - - logger.info("\n\ntestHotStandby2: Entering\n\n"); - cleanXacmlDb(); - cleanDroolsDb(); - - logger.info("testHotStandby2: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); - - logger.info("testHotStandby2: Creating emfXacml"); - EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( - "junitXacmlPU", stateManagementProperties); - - logger.info("testHotStandby2: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); - - logger.info("testHotStandby2: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); - - logger.info("testHotStandby2: Cleaning up tables"); - conn.deleteAllPdps(); - - - // Insert a PDP that's designated but not current. - - String activePdpId = "pdp2"; - logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId); - Date yesterday = DateUtils.addDays(new Date(), -1); - DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday); - conn.insertPdp(pdp); - DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId); - logger.info("testHotStandby2: After insertion, PDP= {}, which is " - + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated()); - assertTrue(droolsPdpEntity.isDesignated() == true); - - /* - * Promote the designated PDP. - * - * We have a chicken and egg problem here: we need a StateManagement - * object to invoke the deleteAllStateManagementEntities method. - */ - - - logger.info("testHotStandby2: Promoting PDP={}", activePdpId); - StateManagement sm = new StateManagement(emfXacml, "dummy"); - sm.deleteAllStateManagementEntities(); - - - sm = new StateManagement(emfXacml, activePdpId);//pdp2 - - // Artificially putting a PDP into service is really a two step process, 1) - // inserting it as designated and 2) promoting it so that its standbyStatus - // is providing service. - - /* - * Insert this PDP as not designated. Initial standby state will be - * either null or cold standby. Demoting should transit state to - * hot standby. - */ - - - logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId); - pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday); - conn.insertPdp(pdp); - droolsPdpEntity = conn.getPdp(thisPdpId); - logger.info("testHotStandby2: After insertion, PDP={} " - + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated()); - assertTrue(droolsPdpEntity.isDesignated() == false); - - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI sm2 = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - sm2 = feature; - logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName()); - break; - } - if(sm2 == null){ - logger.error("testHotStandby2 failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testHotStandby2 failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature - // that has been created. - ActiveStandbyFeatureAPI activeStandbyFeature = null; - for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - activeStandbyFeature = feature; - logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); - break; - } - if(activeStandbyFeature == null){ - logger.error("testHotStandby2 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testHotStandby2 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - logger.info("testHotStandby2: Runner started; Sleeping {} " - + "ms before promoting/demoting", interruptRecoveryTime); - sleep(interruptRecoveryTime); - - logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId); - //At this point, the newly created pdp will have set the state to disabled/failed/cold standby - //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we - //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice - sm.enableNotFailed();//pdp2 - sm.promote(); - String standbyStatus = sm.getStandbyStatus(activePdpId); - logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus); - - // demoting PDP should ensure that state transits to hotstandby - logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId); - sm2.demote();//pdp1 - standbyStatus = sm.getStandbyStatus(thisPdpId); - logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus); - - logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector " - + "time to check droolspdpentity table", sleepTime); - sleep(sleepTime); - - /* - * Verify that this PDP, demoted to HOT_STANDBY, is now - * re-designated and providing service. - */ - - droolsPdpEntity = conn.getPdp(thisPdpId); - logger.info("testHotStandby2: After demoting PDP={}" - + ", DESIGNATED= {}" - + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId); - assertTrue(droolsPdpEntity.isDesignated() == true); - standbyStatus = sm2.getStandbyStatus(thisPdpId); - logger.info("testHotStandby2: After demoting PDP={}" - + ", PDP={} has standbyStatus= {}", - activePdpId, thisPdpId, standbyStatus); - assertTrue(standbyStatus != null - && standbyStatus.equals(StateManagement.PROVIDING_SERVICE)); - - logger.info("testHotStandby2: Stopping policyManagementRunner"); - //policyManagementRunner.stopRunner(); - - logger.info("\n\ntestHotStandby2: Exiting\n\n"); - sleep(interruptRecoveryTime); - - } - - /* - * 1) Inserts and designates this PDP, then verifies that startTransaction - * is successful. - * - * 2) Demotes PDP, and verifies that because there is only one PDP, it will - * be immediately re-promoted, thus allowing startTransaction to be - * successful. - * - * 3) Locks PDP and verifies that startTransaction results in - * AdministrativeStateException. - * - * 4) Unlocks PDP and verifies that startTransaction results in - * StandbyStatusException. - * - * 5) Promotes PDP and verifies that startTransaction is once again - * successful. - */ - - //@Ignore - //@Test - public void testLocking1() throws Exception { - logger.debug("testLocking1: Entry"); - cleanXacmlDb(); - cleanDroolsDb(); - - logger.debug("testLocking1: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); - - logger.debug("testLocking1: Creating emfXacml"); - EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( - "junitXacmlPU", stateManagementProperties); - - logger.debug("testLocking1: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); - - logger.debug("testLocking1: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); - - logger.debug("testLocking1: Cleaning up tables"); - conn.deleteAllPdps(); - - /* - * Insert this PDP as designated. Initial standby state will be - * either null or cold standby. - */ - - logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId); - DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date()); - conn.insertPdp(pdp); - DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); - logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}", - thisPdpId, droolsPdpEntity.isDesignated()); - assertTrue(droolsPdpEntity.isDesignated() == true); - - logger.debug("testLocking1: Instantiating stateManagement object"); - StateManagement smDummy = new StateManagement(emfXacml, "dummy"); - smDummy.deleteAllStateManagementEntities(); - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI sm = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - sm = feature; - logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName()); - break; - } - if(sm == null){ - logger.error("testLocking1 failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testLocking1 failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature - // that has been created. - ActiveStandbyFeatureAPI activeStandbyFeature = null; - for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - activeStandbyFeature = feature; - logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); - break; - } - if(activeStandbyFeature == null){ - logger.error("testLocking1 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testLocking1 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - logger.debug("testLocking1: Runner started; Sleeping " - + interruptRecoveryTime + "ms before promoting PDP={}", - thisPdpId); - sleep(interruptRecoveryTime); - - logger.debug("testLocking1: Promoting PDP={}", thisPdpId); - sm.promote(); - - logger.debug("testLocking1: Sleeping {} ms, to allow time for " - + "policy-management.Main class to come up, designated= {}", - sleepTime, conn.getPdp(thisPdpId).isDesignated()); - sleep(sleepTime); - - logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}" - + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated()); - - - IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance(); - try { - droolsPdpIntegrityMonitor.startTransaction(); - droolsPdpIntegrityMonitor.endTransaction(); - logger.debug("testLocking1: As expected, transaction successful"); - } catch (AdministrativeStateException e) { - logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e); - assertTrue(false); - } catch (StandbyStatusException e) { - logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e); - assertTrue(false); - } catch (Exception e) { - logger.error("testLocking1: Unexpectedly caught Exception, ", e); - assertTrue(false); - } - - // demoting should cause state to transit to hotstandby, followed by re-promotion, - // since there is only one PDP. - logger.debug("testLocking1: demoting PDP={}", thisPdpId); - sm.demote(); - - logger.debug("testLocking1: sleeping" + electionWaitSleepTime - + " to allow election handler to re-promote PDP={}", thisPdpId); - sleep(electionWaitSleepTime); - - logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}" - + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated()); - try { - droolsPdpIntegrityMonitor.startTransaction(); - droolsPdpIntegrityMonitor.endTransaction(); - logger.debug("testLocking1: As expected, transaction successful"); - } catch (AdministrativeStateException e) { - logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e); - assertTrue(false); - } catch (StandbyStatusException e) { - logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e); - assertTrue(false); - } catch (Exception e) { - logger.error("testLocking1: Unexpectedly caught Exception, ", e); - assertTrue(false); - } - - // locking should cause state to transit to cold standby - logger.debug("testLocking1: locking PDP={}", thisPdpId); - sm.lock(); - - // Just to avoid any race conditions, sleep a little after locking - logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition"); - sleep(100); - - logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}" - + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated()); - try { - droolsPdpIntegrityMonitor.startTransaction(); - logger.error("testLocking1: startTransaction unexpectedly successful"); - assertTrue(false); - } catch (AdministrativeStateException e) { - logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e); - } catch (StandbyStatusException e) { - logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e); - assertTrue(false); - } catch (Exception e) { - logger.error("testLocking1: Unexpectedly caught Exception, ", e); - assertTrue(false); - } finally { - droolsPdpIntegrityMonitor.endTransaction(); - } - - // unlocking should cause state to transit to hot standby and then providing service - logger.debug("testLocking1: unlocking PDP={}", thisPdpId); - sm.unlock(); - - // Just to avoid any race conditions, sleep a little after locking - logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition"); - sleep(electionWaitSleepTime); - - logger.debug("testLocking1: Invoking startTransaction on unlocked PDP=" - + thisPdpId - + ", designated=" - + conn.getPdp(thisPdpId).isDesignated()); - try { - droolsPdpIntegrityMonitor.startTransaction(); - logger.error("testLocking1: startTransaction successful as expected"); - } catch (AdministrativeStateException e) { - logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e); - assertTrue(false); - } catch (StandbyStatusException e) { - logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e); - assertTrue(false); - } catch (Exception e) { - logger.error("testLocking1: Unexpectedly caught Exception, ", e); - assertTrue(false); - } finally { - droolsPdpIntegrityMonitor.endTransaction(); - } - - // demoting should cause state to transit to hot standby - logger.debug("testLocking1: demoting PDP={}", thisPdpId); - sm.demote(); - - // Just to avoid any race conditions, sleep a little after promoting - logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition"); - sleep(100); - - logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}" - + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated()); - try { - droolsPdpIntegrityMonitor.startTransaction(); - droolsPdpIntegrityMonitor.endTransaction(); - logger.debug("testLocking1: Unexpectedly, transaction successful"); - assertTrue(false); - } catch (AdministrativeStateException e) { - logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e); - assertTrue(false); - } catch (StandbyStatusException e) { - logger.error("testLocking1: As expected caught StandbyStatusException, ", e); - } catch (Exception e) { - logger.error("testLocking1: Unexpectedly caught Exception, ", e); - assertTrue(false); - } - - logger.debug("\n\ntestLocking1: Exiting\n\n"); - sleep(interruptRecoveryTime); - - } - - - /* - * 1) Inserts and designates this PDP, then verifies that startTransaction - * is successful. - * - * 2) Inserts another PDP in hotstandby. - * - * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one - * PDP cannot promote another PDP) and 2) that this PDP is re-promoted. - */ - - //@Ignore - //@Test - public void testLocking2() throws Exception { - - logger.debug("\n\ntestLocking2: Entering\n\n"); - cleanXacmlDb(); - cleanDroolsDb(); - - logger.debug("testLocking2: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); - - logger.debug("testLocking2: Creating emfXacml"); - EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( - "junitXacmlPU", stateManagementProperties); - - logger.debug("testLocking2: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); - - logger.debug("testLocking2: Creating emfDrools"); - EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( - "junitDroolsPU", activeStandbyProperties); - - DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); - - logger.debug("testLocking2: Cleaning up tables"); - conn.deleteAllPdps(); - - /* - * Insert this PDP as designated. Initial standby state will be - * either null or cold standby. Demoting should transit state to - * hot standby. - */ - - logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId); - DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date()); - conn.insertPdp(pdp); - DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); - logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}", - thisPdpId, droolsPdpEntity.isDesignated()); - assertTrue(droolsPdpEntity.isDesignated() == true); - - logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId); - StateManagement smDummy = new StateManagement(emfXacml, "dummy"); - smDummy.deleteAllStateManagementEntities(); - - // Now we want to create a StateManagementFeature and initialize it. It will be - // discovered by the ActiveStandbyFeature when the election handler initializes. - - StateManagementFeatureAPI sm = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - sm = feature; - logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName()); - break; - } - if(sm == null){ - logger.error("testLocking2 failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testLocking2 failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature - // that has been created. - ActiveStandbyFeatureAPI activeStandbyFeature = null; - for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - activeStandbyFeature = feature; - logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); - break; - } - if(activeStandbyFeature == null){ - logger.error("testLocking2 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testLocking2 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } - - /* - * Insert another PDP as not designated. Initial standby state will be - * either null or cold standby. Demoting should transit state to - * hot standby. - */ - - String standbyPdpId = "pdp2"; - logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId); - Date yesterday = DateUtils.addDays(new Date(), -1); - pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday); - conn.insertPdp(pdp); - droolsPdpEntity = conn.getPdp(standbyPdpId); - logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}", - standbyPdpId, droolsPdpEntity.isDesignated()); - assertTrue(droolsPdpEntity.isDesignated() == false); - - logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId); - StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId); - - logger.debug("testLocking2: Runner started; Sleeping {} ms " - + "before promoting/demoting", interruptRecoveryTime); - sleep(interruptRecoveryTime); - - logger.debug("testLocking2: Promoting PDP= {}", thisPdpId); - sm.promote(); - - // demoting PDP should ensure that state transits to hotstandby - logger.debug("testLocking2: Demoting PDP={}", standbyPdpId); - sm2.demote(); - - logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime); - sleep(sleepTime); - - logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}" - + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated()); - - IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance(); - - try { - droolsPdpIntegrityMonitor.startTransaction(); - droolsPdpIntegrityMonitor.endTransaction(); - logger.debug("testLocking2: As expected, transaction successful"); - } catch (AdministrativeStateException e) { - logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e); - assertTrue(false); - } catch (StandbyStatusException e) { - logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e); - assertTrue(false); - } catch (Exception e) { - logger.error("testLocking2: Unexpectedly caught Exception, ", e); - assertTrue(false); - } - - // demoting should cause state to transit to hotstandby followed by re-promotion. - logger.debug("testLocking2: demoting PDP={}", thisPdpId); - sm.demote(); - - logger.debug("testLocking2: sleeping {}" - + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId); - sleep(electionWaitSleepTime); - - logger.debug("testLocking2: Waking up and invoking startTransaction " - + "on re-promoted PDP= {}, designated= {}", - thisPdpId, conn.getPdp(thisPdpId).isDesignated()); - try { - droolsPdpIntegrityMonitor.startTransaction(); - droolsPdpIntegrityMonitor.endTransaction(); - logger.debug("testLocking2: As expected, transaction successful"); - } catch (AdministrativeStateException e) { - logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e); - assertTrue(false); - } catch (StandbyStatusException e) { - logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e); - assertTrue(false); - } catch (Exception e) { - logger.error("testLocking2: Unexpectedly caught Exception, ", e); - assertTrue(false); - } - - logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId); - boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated(); - assertTrue(standbyPdpDesignated == false); - - logger.debug("\n\ntestLocking2: Exiting\n\n"); - sleep(interruptRecoveryTime); - } - - private void sleep(long sleepms) throws InterruptedException { - Thread.sleep(sleepms); - } + private static final Logger logger = LoggerFactory.getLogger(StandbyStateManagementTest.class); + /* + * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting + * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion + * to ensure that we wait for the DesignationWaiter to do its job, before + * checking the results. Add a few seconds for safety + */ + + long sleepTime = 10000; + + /* + * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be + * plenty to ensure it has time to re-promote this PDP. + */ + + long electionWaitSleepTime = 6000; + + /* + * Sleep 1 seconds after each test to allow interrupt (shutdown) recovery. + */ + + long interruptRecoveryTime = 5000; + + private static EntityManagerFactory emfx; + private static EntityManagerFactory emfd; + private static EntityManager emx; + private static EntityManager emd; + private static EntityTransaction et; + + private final String configDir = "src/test/resources"; + + /* + * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing. + */ + + /** + * Setup the class. + * + * @throws Exception exception + */ + @BeforeClass + public static void setUpClass() throws Exception { + + String userDir = System.getProperty("user.dir"); + logger.debug("setUpClass: userDir={}", userDir); + System.setProperty("com.sun.management.jmxremote.port", "9980"); + System.setProperty("com.sun.management.jmxremote.authenticate","false"); + + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + /** + * Setup. + * + * @throws Exception exception + */ + @Before + public void setUp() throws Exception { + //Create teh data access for xaml db + Properties stateManagementProperties = new Properties(); + stateManagementProperties.load(new FileInputStream(new File( + "src/test/resources/feature-state-management.properties"))); + + emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties); + + // Create an entity manager to use the DB + emx = emfx.createEntityManager(); + + //Create the data access for drools db + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + "src/test/resources/feature-active-standby-management.properties"))); + + emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties); + + // Create an entity manager to use the DB + emd = emfd.createEntityManager(); + } + + @After + public void tearDown() throws Exception { + + } + + /** + * Clean up the xacml database. + * + */ + public void cleanXacmlDb() { + et = emx.getTransaction(); + + et.begin(); + // Make sure we leave the DB clean + emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate(); + emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate(); + emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate(); + emx.flush(); + et.commit(); + } + + /** + * Clean up the drools db. + */ + public void cleanDroolsDb() { + et = emd.getTransaction(); + + et.begin(); + // Make sure we leave the DB clean + emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate(); + emd.flush(); + et.commit(); + } + + /* + * These JUnit tests must be run one at a time in an eclipse environment + * by right-clicking StandbyStateManagementTest and selecting + * "Run As" -> "JUnit Test". + * + * They will run successfully when you run all of them under runAllTests(), + * however, you will get all sorts of non-fatal errors in the log and on the + * console that result from overlapping threads that are not terminated at the + * end of each test. The problem is that the JUnit environment does not terminate + * all the test threads between tests. This is true even if you break each JUnit + * into a separate file. Consequently, all the tests would have to be refactored + * so all test object initializations are coordinated. In other words, you + * retrieve the ActiveStandbyFeature instance and other class instances only once + * at the beginning of the JUnits and then reuse them throughout the tests. + * Initialization of the state of the objects is pretty straight forward as it + * just amounts to manipulating the entries in StateManagementEntity and + * DroolsPdpEntity tables. However, some thought needs to be given to how to + * "pause" the processing in ActiveStandbyFeature class. I think we could "pause" + * it by calling globalInit() which will, I think, restart it. So long as it + * does not create a new instance, it will force it to go through an initialization + * cycle which includes a "pause" at the beginning of proecessing. We just must + * be sure it does not create another instance - which may mean we need to add + * a factory interface instead of calling the constructor directly. + */ + + + //@Ignore + @Test + public void runAllTests() throws Exception { + testColdStandby(); + testHotStandby1(); + testHotStandby2(); + testLocking1(); + testLocking2(); + testPmStandbyStateChangeNotifier(); + testSanitizeDesignatedList(); + testComputeMostRecentPrimary(); + testComputeDesignatedPdp(); + } + + /** + * Test the standby state change notifier. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testPmStandbyStateChangeNotifier() throws Exception { + logger.debug("\n\ntestPMStandbyStateChangeNotifier: Entering\n\n"); + cleanXacmlDb(); + + logger.debug("testPMStandbyStateChangeNotifier: Reading activeStandbyProperties"); + + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + + String resourceName = "testPMS"; + activeStandbyProperties.setProperty("resource.name", resourceName); + ActiveStandbyProperties.initProperties(activeStandbyProperties); + + logger.debug("testPMStandbyStateChangeNotifier: Getting StateManagement instance"); + + StateManagement sm = new StateManagement(emfx, resourceName); + + //Create an instance of the Observer + PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier(); + + //Register the PMStandbyStateChangeNotifier Observer + sm.addObserver(pmNotifier); + + //At this point the standbystatus = 'null' + sm.lock(); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE)); + + sm.unlock(); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE)); + + //Adding standbystatus=hotstandby + sm.demote(); + System.out.println(pmNotifier.getPreviousStandbyStatus()); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals( + PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); + + //Now making standbystatus=coldstandby + sm.lock(); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals( + PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); + + //standbystatus = hotstandby + sm.unlock(); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals( + PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); + + //standbystatus = providingservice + sm.promote(); + //The previousStandbyStatus is not updated until after the delay activation expires + assertTrue(pmNotifier.getPreviousStandbyStatus().equals( + PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); + + //Sleep long enough for the delayActivationTimer to run + sleep(5000); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE)); + + //standbystatus = providingservice + sm.promote(); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE)); + + //standbystatus = coldstandby + sm.lock(); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals( + PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); + + //standbystatus = hotstandby + sm.unlock(); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals( + PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); + + //standbystatus = hotstandby + sm.demote(); + assertTrue(pmNotifier.getPreviousStandbyStatus().equals( + PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)); + } + + /** + * Test sanitize designated list. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testSanitizeDesignatedList() throws Exception { + + logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n"); + + // Get a DroolsPdpsConnector + + logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + String thisPdpId = activeStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + logger.debug("testSanitizeDesignatedList: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools); + + // Create 4 pdpd all not designated + + DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); + DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); + DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); + DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); + + List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>(); + listOfDesignated.add(pdp1); + listOfDesignated.add(pdp2); + listOfDesignated.add(pdp3); + listOfDesignated.add(pdp4); + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI stateManagementFeature = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + stateManagementFeature = feature; + logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", + stateManagementFeature.getResourceName()); + break; + } + if (stateManagementFeature == null) { + logger.error("testColdStandby failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testColdStandby failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + + DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); + + listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated); + + logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size()); + + assertTrue(listOfDesignated.size() == 4); + + // Now make 2 designated + + pdp1.setDesignated(true); + pdp2.setDesignated(true); + + listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated); + + logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n", + listOfDesignated.size()); + + assertTrue(listOfDesignated.size() == 2); + assertTrue(listOfDesignated.contains(pdp1)); + assertTrue(listOfDesignated.contains(pdp2)); + + + // Now all are designated. But, we have to add back the previously non-designated nodes + + pdp3.setDesignated(true); + pdp4.setDesignated(true); + listOfDesignated.add(pdp3); + listOfDesignated.add(pdp4); + + listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated); + + logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n", + listOfDesignated.size()); + + assertTrue(listOfDesignated.size() == 4); + + } + + /** + * Test Compute most recent primary. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testComputeMostRecentPrimary() throws Exception { + + logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n"); + + logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + String thisPdpId = activeStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + logger.debug("testComputeMostRecentPrimary: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools); + + + // Create 4 pdpd all not designated + + + long designatedDateMs = new Date().getTime(); + DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); + pdp1.setDesignatedDate(new Date(designatedDateMs - 2)); + + DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); + //oldest + pdp2.setDesignatedDate(new Date(designatedDateMs - 3)); + + DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); + pdp3.setDesignatedDate(new Date(designatedDateMs - 1)); + + DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); + //most recent + pdp4.setDesignatedDate(new Date(designatedDateMs)); + + ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>(); + listOfAllPdps.add(pdp1); + listOfAllPdps.add(pdp2); + listOfAllPdps.add(pdp3); + listOfAllPdps.add(pdp4); + + + ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>(); + listOfDesignated.add(pdp1); + listOfDesignated.add(pdp2); + listOfDesignated.add(pdp3); + listOfDesignated.add(pdp4); + + // Because the way we sanitize the listOfDesignated, it will always contain all hot standby + // or all designated members. + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI stateManagementFeature = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + stateManagementFeature = feature; + logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}", + stateManagementFeature.getResourceName()); + break; + } + if (stateManagementFeature == null) { + logger.error("testComputeMostRecentPrimary failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testComputeMostRecentPrimary failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); + + DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary( + listOfAllPdps, listOfDesignated); + + logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", + mostRecentPrimary.getPdpId()); + + + // If all of the pdps are included in the listOfDesignated and none are designated, it will choose + // the one which has the most recent designated date. + + + assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); + + + // Now let's designate all of those on the listOfDesignated. It will choose the first one designated + + + pdp1.setDesignated(true); + pdp2.setDesignated(true); + pdp3.setDesignated(true); + pdp4.setDesignated(true); + + mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); + + logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, " + + "mostRecentPrimary.getPdpId() = {}\n\n", + mostRecentPrimary.getPdpId()); + + + // If all of the pdps are included in the listOfDesignated and all are designated, it will choose + // the one which was designated first + + + assertTrue(mostRecentPrimary.getPdpId().equals("pdp2")); + + + // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now + // look for the most recently designated pdp which is not currently designated. + + + pdp3.setDesignated(false); + pdp4.setDesignated(false); + + listOfDesignated.remove(pdp3); + listOfDesignated.remove(pdp4); + + mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); + + logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", + mostRecentPrimary.getPdpId()); + + assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); + + + + // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now + // look for the most recently designated pdp regardless of whether it is currently marked as designated. + + + pdp1.setDesignated(false); + pdp2.setDesignated(false); + + mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); + + logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n", + mostRecentPrimary.getPdpId()); + + assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); + + + // If we have only one pdp on in the listOfDesignated, + // the most recently designated pdp will be chosen, regardless + // of its designation status + + + listOfDesignated.remove(pdp1); + + mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); + + logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n", + mostRecentPrimary.getPdpId()); + + assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); + + + // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp. + + + listOfDesignated.remove(pdp2); + + mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated); + + logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n", + mostRecentPrimary.getPdpId()); + + assertTrue(mostRecentPrimary.getPdpId().equals("pdp4")); + + } + + /** + * Test compute designated PDP. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testComputeDesignatedPdp() throws Exception { + + logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n"); + + logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + String thisPdpId = activeStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + + logger.debug("testComputeDesignatedPdp: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools); + + + // Create 4 pdpd all not designated. Two on site1. Two on site2 + + + long designatedDateMs = new Date().getTime(); + DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); + pdp1.setDesignatedDate(new Date(designatedDateMs - 2)); + pdp1.setSiteName("site1"); + + DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); + pdp2.setDesignatedDate(new Date(designatedDateMs - 3)); + pdp2.setSiteName("site1"); + + //oldest + DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); + pdp3.setDesignatedDate(new Date(designatedDateMs - 4)); + pdp3.setSiteName("site2"); + + DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); + //most recent + pdp4.setDesignatedDate(new Date(designatedDateMs)); + pdp4.setSiteName("site2"); + + ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>(); + listOfAllPdps.add(pdp1); + listOfAllPdps.add(pdp2); + listOfAllPdps.add(pdp3); + listOfAllPdps.add(pdp4); + + + ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>(); + + + // We will first test an empty listOfDesignated. As we know from the previous JUnit, + // the pdp with the most designated date will be chosen for mostRecentPrimary + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI stateManagementFeature = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + stateManagementFeature = feature; + logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}", + stateManagementFeature.getResourceName()); + break; + } + if (stateManagementFeature == null) { + logger.error("testComputeDesignatedPdp failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testComputeDesignatedPdp failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + + DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); + + DroolsPdp mostRecentPrimary = pdp4; + + DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary); + + + // The designatedPdp should be null + + assertTrue(designatedPdp == null); + + + // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary + + listOfDesignated.add(pdp2); + + designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary); + + + // Now the designatedPdp should be the one and only selection in the listOfDesignated + + + assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId())); + + + // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary + + + listOfDesignated.add(pdp1); + + designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary); + + + // The designatedPdp should now be the one with the lowest lexiographic score - pdp1 + + + assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId())); + + + // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary + + + listOfDesignated.remove(pdp1); + listOfDesignated.add(pdp3); + + designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary); + + + // The designatedPdp should now be the one on the same site as the mostRecentPrimary + + + assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId())); + } + + /** + * Test cold standby. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testColdStandby() throws Exception { + + logger.debug("\n\ntestColdStandby: Entering\n\n"); + cleanXacmlDb(); + cleanDroolsDb(); + + logger.debug("testColdStandby: Reading stateManagementProperties"); + Properties stateManagementProperties = new Properties(); + stateManagementProperties.load(new FileInputStream(new File( + configDir + "/feature-state-management.properties"))); + + logger.debug("testColdStandby: Creating emfXacml"); + final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( + "junitXacmlPU", stateManagementProperties); + + logger.debug("testColdStandby: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + final String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME); + + logger.debug("testColdStandby: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); + + logger.debug("testColdStandby: Cleaning up tables"); + conn.deleteAllPdps(); + + logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId); + DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date()); + conn.insertPdp(pdp); + DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); + logger.debug("testColdStandby: After insertion, DESIGNATED= {} " + + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId); + assertTrue(droolsPdpEntity.isDesignated() == true); + + /* + * When the Standby Status changes (from providingservice) to hotstandby + * or coldstandby,the Active/Standby selection algorithm must stand down + * if thePDP-D is currently the lead/active node and allow another PDP-D + * to take over. + * + * It must also call lock on all engines in the engine management. + */ + + + /* + * Yes, this is kludgy, but we have a chicken and egg problem here: we + * need a StateManagement object to invoke the + * deleteAllStateManagementEntities method. + */ + logger.debug("testColdStandby: Instantiating stateManagement object"); + + StateManagement sm = new StateManagement(emfXacml, "dummy"); + sm.deleteAllStateManagementEntities(); + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI smf = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + smf = feature; + logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName()); + break; + } + if (smf == null) { + logger.error("testColdStandby failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testColdStandby failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature + // that has been created. + ActiveStandbyFeatureAPI activeStandbyFeature = null; + for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + activeStandbyFeature = feature; + logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}", + activeStandbyFeature.getResourceName()); + break; + } + if (activeStandbyFeature == null) { + logger.error("testColdStandby failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID:{}", thisPdpId); + logger.debug("testColdStandby failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID:{}", thisPdpId); + } + + // Artificially putting a PDP into service is really a two step process, 1) + // inserting it as designated and 2) promoting it so that its standbyStatus + // is providing service. + + logger.debug("testColdStandby: Runner started; Sleeping " + + interruptRecoveryTime + "ms before promoting PDP= {}", + thisPdpId); + sleep(interruptRecoveryTime); + + logger.debug("testColdStandby: Promoting PDP={}", thisPdpId); + smf.promote(); + + String standbyStatus = sm.getStandbyStatus(thisPdpId); + logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}", + thisPdpId, standbyStatus); + + logger.debug("testColdStandby: Locking smf"); + smf.lock(); + + sleep(interruptRecoveryTime); + + // Verify that the PDP is no longer designated. + + droolsPdpEntity = conn.getPdp(thisPdpId); + logger.debug("testColdStandby: After lock sm.lock() invoked, " + + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId); + assertTrue(droolsPdpEntity.isDesignated() == false); + + logger.debug("\n\ntestColdStandby: Exiting\n\n"); + sleep(interruptRecoveryTime); + + } + + // Tests hot standby when there is only one PDP. + + /** + * Test hot standby 1. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testHotStandby1() throws Exception { + + logger.debug("\n\ntestHotStandby1: Entering\n\n"); + cleanXacmlDb(); + cleanDroolsDb(); + + logger.debug("testHotStandby1: Reading stateManagementProperties"); + Properties stateManagementProperties = new Properties(); + stateManagementProperties.load(new FileInputStream(new File( + configDir + "/feature-state-management.properties"))); + + logger.debug("testHotStandby1: Creating emfXacml"); + final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( + "junitXacmlPU", stateManagementProperties); + + logger.debug("testHotStandby1: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + final String thisPdpId = activeStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + logger.debug("testHotStandby1: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); + + logger.debug("testHotStandby1: Cleaning up tables"); + conn.deleteAllPdps(); + + /* + * Insert this PDP as not designated. Initial standby state will be + * either null or cold standby. Demoting should transit state to + * hot standby. + */ + + logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId); + Date yesterday = DateUtils.addDays(new Date(), -1); + DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday); + conn.insertPdp(pdp); + DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); + logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}", + thisPdpId, droolsPdpEntity.isDesignated()); + assertTrue(droolsPdpEntity.isDesignated() == false); + + logger.debug("testHotStandby1: Instantiating stateManagement object"); + StateManagement sm = new StateManagement(emfXacml, "dummy"); + sm.deleteAllStateManagementEntities(); + + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI smf = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + smf = feature; + logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName()); + break; + } + if (smf == null) { + logger.error("testHotStandby1 failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testHotStandby1 failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature + // that has been created. + ActiveStandbyFeatureAPI activeStandbyFeature = null; + for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + activeStandbyFeature = feature; + logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}", + activeStandbyFeature.getResourceName()); + break; + } + if (activeStandbyFeature == null) { + logger.error("testHotStandby1 failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testHotStandby1 failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + + logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId); + // demoting should cause state to transit to hotstandby + smf.demote(); + + + logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector " + + "time to check droolspdpentity table", sleepTime); + sleep(sleepTime); + + + // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service. + + droolsPdpEntity = conn.getPdp(thisPdpId); + logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} " + + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId); + assertTrue(droolsPdpEntity.isDesignated() == true); + String standbyStatus = smf.getStandbyStatus(thisPdpId); + logger.debug("testHotStandby1: After demotion, PDP= {} " + + "has standbyStatus= {}", thisPdpId, standbyStatus); + assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE)); + + logger.debug("testHotStandby1: Stopping policyManagementRunner"); + + logger.debug("\n\ntestHotStandby1: Exiting\n\n"); + sleep(interruptRecoveryTime); + + } + + /* + * Tests hot standby when two PDPs are involved. + */ + + /** + * Test hot standby 2. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testHotStandby2() throws Exception { + + logger.info("\n\ntestHotStandby2: Entering\n\n"); + cleanXacmlDb(); + cleanDroolsDb(); + + logger.info("testHotStandby2: Reading stateManagementProperties"); + Properties stateManagementProperties = new Properties(); + stateManagementProperties.load(new FileInputStream(new File( + configDir + "/feature-state-management.properties"))); + + logger.info("testHotStandby2: Creating emfXacml"); + final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( + "junitXacmlPU", stateManagementProperties); + + logger.info("testHotStandby2: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + final String thisPdpId = activeStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + logger.info("testHotStandby2: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); + + logger.info("testHotStandby2: Cleaning up tables"); + conn.deleteAllPdps(); + + + // Insert a PDP that's designated but not current. + + String activePdpId = "pdp2"; + logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId); + Date yesterday = DateUtils.addDays(new Date(), -1); + DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday); + conn.insertPdp(pdp); + DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId); + logger.info("testHotStandby2: After insertion, PDP= {}, which is " + + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated()); + assertTrue(droolsPdpEntity.isDesignated() == true); + + /* + * Promote the designated PDP. + * + * We have a chicken and egg problem here: we need a StateManagement + * object to invoke the deleteAllStateManagementEntities method. + */ + + + logger.info("testHotStandby2: Promoting PDP={}", activePdpId); + StateManagement sm = new StateManagement(emfXacml, "dummy"); + sm.deleteAllStateManagementEntities(); + + + sm = new StateManagement(emfXacml, activePdpId);//pdp2 + + // Artificially putting a PDP into service is really a two step process, 1) + // inserting it as designated and 2) promoting it so that its standbyStatus + // is providing service. + + /* + * Insert this PDP as not designated. Initial standby state will be + * either null or cold standby. Demoting should transit state to + * hot standby. + */ + + + logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId); + pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday); + conn.insertPdp(pdp); + droolsPdpEntity = conn.getPdp(thisPdpId); + logger.info("testHotStandby2: After insertion, PDP={} " + + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated()); + assertTrue(droolsPdpEntity.isDesignated() == false); + + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI sm2 = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + sm2 = feature; + logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName()); + break; + } + if (sm2 == null) { + logger.error("testHotStandby2 failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testHotStandby2 failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature + // that has been created. + ActiveStandbyFeatureAPI activeStandbyFeature = null; + for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + activeStandbyFeature = feature; + logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}", + activeStandbyFeature.getResourceName()); + break; + } + if (activeStandbyFeature == null) { + logger.error("testHotStandby2 failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testHotStandby2 failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + logger.info("testHotStandby2: Runner started; Sleeping {} " + + "ms before promoting/demoting", interruptRecoveryTime); + sleep(interruptRecoveryTime); + + logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId); + //At this point, the newly created pdp will have set the state to disabled/failed/cold standby + //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we + //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice + sm.enableNotFailed();//pdp2 + sm.promote(); + String standbyStatus = sm.getStandbyStatus(activePdpId); + logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus); + + // demoting PDP should ensure that state transits to hotstandby + logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId); + sm2.demote();//pdp1 + standbyStatus = sm.getStandbyStatus(thisPdpId); + logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus); + + logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector " + + "time to check droolspdpentity table", sleepTime); + sleep(sleepTime); + + /* + * Verify that this PDP, demoted to HOT_STANDBY, is now + * re-designated and providing service. + */ + + droolsPdpEntity = conn.getPdp(thisPdpId); + logger.info("testHotStandby2: After demoting PDP={}" + + ", DESIGNATED= {}" + + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId); + assertTrue(droolsPdpEntity.isDesignated() == true); + standbyStatus = sm2.getStandbyStatus(thisPdpId); + logger.info("testHotStandby2: After demoting PDP={}" + + ", PDP={} has standbyStatus= {}", + activePdpId, thisPdpId, standbyStatus); + assertTrue(standbyStatus != null + && standbyStatus.equals(StateManagement.PROVIDING_SERVICE)); + + logger.info("testHotStandby2: Stopping policyManagementRunner"); + + logger.info("\n\ntestHotStandby2: Exiting\n\n"); + sleep(interruptRecoveryTime); + + } + + /* + * 1) Inserts and designates this PDP, then verifies that startTransaction + * is successful. + * + * 2) Demotes PDP, and verifies that because there is only one PDP, it will + * be immediately re-promoted, thus allowing startTransaction to be + * successful. + * + * 3) Locks PDP and verifies that startTransaction results in + * AdministrativeStateException. + * + * 4) Unlocks PDP and verifies that startTransaction results in + * StandbyStatusException. + * + * 5) Promotes PDP and verifies that startTransaction is once again + * successful. + */ + + /** + * Test locking. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testLocking1() throws Exception { + logger.debug("testLocking1: Entry"); + cleanXacmlDb(); + cleanDroolsDb(); + + logger.debug("testLocking1: Reading stateManagementProperties"); + Properties stateManagementProperties = new Properties(); + stateManagementProperties.load(new FileInputStream(new File( + configDir + "/feature-state-management.properties"))); + + logger.debug("testLocking1: Creating emfXacml"); + final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( + "junitXacmlPU", stateManagementProperties); + + logger.debug("testLocking1: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + final String thisPdpId = activeStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + logger.debug("testLocking1: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); + + logger.debug("testLocking1: Cleaning up tables"); + conn.deleteAllPdps(); + + /* + * Insert this PDP as designated. Initial standby state will be + * either null or cold standby. + */ + + logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId); + DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date()); + conn.insertPdp(pdp); + DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); + logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}", + thisPdpId, droolsPdpEntity.isDesignated()); + assertTrue(droolsPdpEntity.isDesignated() == true); + + logger.debug("testLocking1: Instantiating stateManagement object"); + StateManagement smDummy = new StateManagement(emfXacml, "dummy"); + smDummy.deleteAllStateManagementEntities(); + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI sm = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + sm = feature; + logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName()); + break; + } + if (sm == null) { + logger.error("testLocking1 failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testLocking1 failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature + // that has been created. + ActiveStandbyFeatureAPI activeStandbyFeature = null; + for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + activeStandbyFeature = feature; + logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}", + activeStandbyFeature.getResourceName()); + break; + } + if (activeStandbyFeature == null) { + logger.error("testLocking1 failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testLocking1 failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + logger.debug("testLocking1: Runner started; Sleeping " + + interruptRecoveryTime + "ms before promoting PDP={}", + thisPdpId); + sleep(interruptRecoveryTime); + + logger.debug("testLocking1: Promoting PDP={}", thisPdpId); + sm.promote(); + + logger.debug("testLocking1: Sleeping {} ms, to allow time for " + + "policy-management.Main class to come up, designated= {}", + sleepTime, conn.getPdp(thisPdpId).isDesignated()); + sleep(sleepTime); + + logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}" + + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated()); + + + IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance(); + try { + droolsPdpIntegrityMonitor.startTransaction(); + droolsPdpIntegrityMonitor.endTransaction(); + logger.debug("testLocking1: As expected, transaction successful"); + } catch (AdministrativeStateException e) { + logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e); + assertTrue(false); + } catch (StandbyStatusException e) { + logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e); + assertTrue(false); + } catch (Exception e) { + logger.error("testLocking1: Unexpectedly caught Exception, ", e); + assertTrue(false); + } + + // demoting should cause state to transit to hotstandby, followed by re-promotion, + // since there is only one PDP. + logger.debug("testLocking1: demoting PDP={}", thisPdpId); + sm.demote(); + + logger.debug("testLocking1: sleeping" + electionWaitSleepTime + + " to allow election handler to re-promote PDP={}", thisPdpId); + sleep(electionWaitSleepTime); + + logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}" + + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated()); + try { + droolsPdpIntegrityMonitor.startTransaction(); + droolsPdpIntegrityMonitor.endTransaction(); + logger.debug("testLocking1: As expected, transaction successful"); + } catch (AdministrativeStateException e) { + logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e); + assertTrue(false); + } catch (StandbyStatusException e) { + logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e); + assertTrue(false); + } catch (Exception e) { + logger.error("testLocking1: Unexpectedly caught Exception, ", e); + assertTrue(false); + } + + // locking should cause state to transit to cold standby + logger.debug("testLocking1: locking PDP={}", thisPdpId); + sm.lock(); + + // Just to avoid any race conditions, sleep a little after locking + logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition"); + sleep(100); + + logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}" + + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated()); + try { + droolsPdpIntegrityMonitor.startTransaction(); + logger.error("testLocking1: startTransaction unexpectedly successful"); + assertTrue(false); + } catch (AdministrativeStateException e) { + logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e); + } catch (StandbyStatusException e) { + logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e); + assertTrue(false); + } catch (Exception e) { + logger.error("testLocking1: Unexpectedly caught Exception, ", e); + assertTrue(false); + } finally { + droolsPdpIntegrityMonitor.endTransaction(); + } + + // unlocking should cause state to transit to hot standby and then providing service + logger.debug("testLocking1: unlocking PDP={}", thisPdpId); + sm.unlock(); + + // Just to avoid any race conditions, sleep a little after locking + logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition"); + sleep(electionWaitSleepTime); + + logger.debug("testLocking1: Invoking startTransaction on unlocked PDP=" + + thisPdpId + + ", designated=" + + conn.getPdp(thisPdpId).isDesignated()); + try { + droolsPdpIntegrityMonitor.startTransaction(); + logger.error("testLocking1: startTransaction successful as expected"); + } catch (AdministrativeStateException e) { + logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e); + assertTrue(false); + } catch (StandbyStatusException e) { + logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e); + assertTrue(false); + } catch (Exception e) { + logger.error("testLocking1: Unexpectedly caught Exception, ", e); + assertTrue(false); + } finally { + droolsPdpIntegrityMonitor.endTransaction(); + } + + // demoting should cause state to transit to hot standby + logger.debug("testLocking1: demoting PDP={}", thisPdpId); + sm.demote(); + + // Just to avoid any race conditions, sleep a little after promoting + logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition"); + sleep(100); + + logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}" + + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated()); + try { + droolsPdpIntegrityMonitor.startTransaction(); + droolsPdpIntegrityMonitor.endTransaction(); + logger.debug("testLocking1: Unexpectedly, transaction successful"); + assertTrue(false); + } catch (AdministrativeStateException e) { + logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e); + assertTrue(false); + } catch (StandbyStatusException e) { + logger.error("testLocking1: As expected caught StandbyStatusException, ", e); + } catch (Exception e) { + logger.error("testLocking1: Unexpectedly caught Exception, ", e); + assertTrue(false); + } + + logger.debug("\n\ntestLocking1: Exiting\n\n"); + sleep(interruptRecoveryTime); + + } + + + /* + * 1) Inserts and designates this PDP, then verifies that startTransaction + * is successful. + * + * 2) Inserts another PDP in hotstandby. + * + * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one + * PDP cannot promote another PDP) and 2) that this PDP is re-promoted. + */ + + /** + * Test locking 2. + * + * @throws Exception exception + */ + //@Ignore + //@Test + public void testLocking2() throws Exception { + + logger.debug("\n\ntestLocking2: Entering\n\n"); + cleanXacmlDb(); + cleanDroolsDb(); + + logger.debug("testLocking2: Reading stateManagementProperties"); + Properties stateManagementProperties = new Properties(); + stateManagementProperties.load(new FileInputStream(new File( + configDir + "/feature-state-management.properties"))); + + logger.debug("testLocking2: Creating emfXacml"); + final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( + "junitXacmlPU", stateManagementProperties); + + logger.debug("testLocking2: Reading activeStandbyProperties"); + Properties activeStandbyProperties = new Properties(); + activeStandbyProperties.load(new FileInputStream(new File( + configDir + "/feature-active-standby-management.properties"))); + final String thisPdpId = activeStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + logger.debug("testLocking2: Creating emfDrools"); + EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( + "junitDroolsPU", activeStandbyProperties); + + DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools); + + logger.debug("testLocking2: Cleaning up tables"); + conn.deleteAllPdps(); + + /* + * Insert this PDP as designated. Initial standby state will be + * either null or cold standby. Demoting should transit state to + * hot standby. + */ + + logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId); + DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date()); + conn.insertPdp(pdp); + DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); + logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}", + thisPdpId, droolsPdpEntity.isDesignated()); + assertTrue(droolsPdpEntity.isDesignated() == true); + + logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId); + StateManagement smDummy = new StateManagement(emfXacml, "dummy"); + smDummy.deleteAllStateManagementEntities(); + + // Now we want to create a StateManagementFeature and initialize it. It will be + // discovered by the ActiveStandbyFeature when the election handler initializes. + + StateManagementFeatureAPI sm = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + sm = feature; + logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName()); + break; + } + if (sm == null) { + logger.error("testLocking2 failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testLocking2 failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature + // that has been created. + ActiveStandbyFeatureAPI activeStandbyFeature = null; + for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + activeStandbyFeature = feature; + logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}", + activeStandbyFeature.getResourceName()); + break; + } + if (activeStandbyFeature == null) { + logger.error("testLocking2 failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + logger.debug("testLocking2 failed to initialize. " + + "Unable to get instance of ActiveStandbyFeatureAPI " + + "with resourceID: {}", thisPdpId); + } + + /* + * Insert another PDP as not designated. Initial standby state will be + * either null or cold standby. Demoting should transit state to + * hot standby. + */ + + String standbyPdpId = "pdp2"; + logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId); + Date yesterday = DateUtils.addDays(new Date(), -1); + pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday); + conn.insertPdp(pdp); + droolsPdpEntity = conn.getPdp(standbyPdpId); + logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}", + standbyPdpId, droolsPdpEntity.isDesignated()); + assertTrue(droolsPdpEntity.isDesignated() == false); + + logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId); + final StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId); + + logger.debug("testLocking2: Runner started; Sleeping {} ms " + + "before promoting/demoting", interruptRecoveryTime); + sleep(interruptRecoveryTime); + + logger.debug("testLocking2: Promoting PDP= {}", thisPdpId); + sm.promote(); + + // demoting PDP should ensure that state transits to hotstandby + logger.debug("testLocking2: Demoting PDP={}", standbyPdpId); + sm2.demote(); + + logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime); + sleep(sleepTime); + + logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}" + + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated()); + + IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance(); + + try { + droolsPdpIntegrityMonitor.startTransaction(); + droolsPdpIntegrityMonitor.endTransaction(); + logger.debug("testLocking2: As expected, transaction successful"); + } catch (AdministrativeStateException e) { + logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e); + assertTrue(false); + } catch (StandbyStatusException e) { + logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e); + assertTrue(false); + } catch (Exception e) { + logger.error("testLocking2: Unexpectedly caught Exception, ", e); + assertTrue(false); + } + + // demoting should cause state to transit to hotstandby followed by re-promotion. + logger.debug("testLocking2: demoting PDP={}", thisPdpId); + sm.demote(); + + logger.debug("testLocking2: sleeping {}" + + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId); + sleep(electionWaitSleepTime); + + logger.debug("testLocking2: Waking up and invoking startTransaction " + + "on re-promoted PDP= {}, designated= {}", + thisPdpId, conn.getPdp(thisPdpId).isDesignated()); + try { + droolsPdpIntegrityMonitor.startTransaction(); + droolsPdpIntegrityMonitor.endTransaction(); + logger.debug("testLocking2: As expected, transaction successful"); + } catch (AdministrativeStateException e) { + logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e); + assertTrue(false); + } catch (StandbyStatusException e) { + logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e); + assertTrue(false); + } catch (Exception e) { + logger.error("testLocking2: Unexpectedly caught Exception, ", e); + assertTrue(false); + } + + logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId); + boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated(); + assertTrue(standbyPdpDesignated == false); + + logger.debug("\n\ntestLocking2: Exiting\n\n"); + sleep(interruptRecoveryTime); + } + + private void sleep(long sleepms) throws InterruptedException { + Thread.sleep(sleepms); + } } diff --git a/feature-active-standby-management/src/test/resources/META-INF/persistence.xml b/feature-active-standby-management/src/test/resources/META-INF/persistence.xml index ff6ac58a..549f01de 100644 --- a/feature-active-standby-management/src/test/resources/META-INF/persistence.xml +++ b/feature-active-standby-management/src/test/resources/META-INF/persistence.xml @@ -3,7 +3,7 @@ ============LICENSE_START======================================================= feature-active-standby-management ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,28 +20,47 @@ --> <persistence version="2.1" - xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> - <persistence-unit name="junitDroolsPU" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - <class>org.onap.policy.drools.activestandby.DroolsPdpEntity</class> - <properties> - <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> - <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/> - <property name="javax.persistence.schema-generation.scripts.create-target" value="./sql/generatedCreateDrools.ddl"/> - <property name="javax.persistence.schema-generation.scripts.drop-target" value="./sql/generatedDropDrools.ddl"/> + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + <persistence-unit name="junitDroolsPU" + transaction-type="RESOURCE_LOCAL"> + <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + <class>org.onap.policy.drools.activestandby.DroolsPdpEntity</class> + <properties> + <property + name="javax.persistence.schema-generation.database.action" + value="drop-and-create" /> + <property + name="javax.persistence.schema-generation.scripts.action" + value="drop-and-create" /> + <property + name="javax.persistence.schema-generation.scripts.create-target" + value="./sql/generatedCreateDrools.ddl" /> + <property + name="javax.persistence.schema-generation.scripts.drop-target" + value="./sql/generatedDropDrools.ddl" /> </properties> - </persistence-unit> - <persistence-unit name="junitXacmlPU" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - <class>org.onap.policy.common.im.jpa.StateManagementEntity</class> - <class>org.onap.policy.common.im.jpa.ForwardProgressEntity</class> - <class>org.onap.policy.common.im.jpa.ResourceRegistrationEntity</class> - <properties> - <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> - <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/> - <property name="javax.persistence.schema-generation.scripts.create-target" value="./sql/generatedCreateXacml.ddl"/> - <property name="javax.persistence.schema-generation.scripts.drop-target" value="./sql/generatedDropXacml.ddl"/> + </persistence-unit> + <persistence-unit name="junitXacmlPU" + transaction-type="RESOURCE_LOCAL"> + <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + <class>org.onap.policy.common.im.jpa.StateManagementEntity</class> + <class>org.onap.policy.common.im.jpa.ForwardProgressEntity</class> + <class>org.onap.policy.common.im.jpa.ResourceRegistrationEntity</class> + <properties> + <property + name="javax.persistence.schema-generation.database.action" + value="drop-and-create" /> + <property + name="javax.persistence.schema-generation.scripts.action" + value="drop-and-create" /> + <property + name="javax.persistence.schema-generation.scripts.create-target" + value="./sql/generatedCreateXacml.ddl" /> + <property + name="javax.persistence.schema-generation.scripts.drop-target" + value="./sql/generatedDropXacml.ddl" /> </properties> - </persistence-unit> + </persistence-unit> </persistence> diff --git a/feature-active-standby-management/src/test/resources/logback-test.xml b/feature-active-standby-management/src/test/resources/logback-test.xml index a36bdb16..583b9667 100644 --- a/feature-active-standby-management/src/test/resources/logback-test.xml +++ b/feature-active-standby-management/src/test/resources/logback-test.xml @@ -2,7 +2,7 @@ ============LICENSE_START======================================================= feature-state-management ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -22,25 +22,28 @@ <configuration> - <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> - <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> - <Pattern> - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n - </Pattern> - </encoder> - </appender> - <appender name="FILE" class="ch.qos.logback.core.FileAppender"> - <file>logs/debug.log</file> - <encoder> - <Pattern> - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n - </Pattern> - </encoder> - </appender> - - <root level="debug"> - <appender-ref ref="STDOUT" /> - <appender-ref ref="FILE" /> - </root> + <appender name="STDOUT" + class="ch.qos.logback.core.ConsoleAppender"> + <encoder + class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> + <Pattern> + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n + </Pattern> + </encoder> + </appender> + <appender name="FILE" + class="ch.qos.logback.core.FileAppender"> + <file>logs/debug.log</file> + <encoder> + <Pattern> + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n + </Pattern> + </encoder> + </appender> + + <root level="debug"> + <appender-ref ref="STDOUT" /> + <appender-ref ref="FILE" /> + </root> </configuration> diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java index f1c8b687..17f65459 100644 --- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java +++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.distributed.locking; import java.sql.Connection; @@ -35,102 +36,105 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DistributedLockingFeature implements PolicyEngineFeatureAPI, PolicyResourceLockFeatureAPI { - - /** - * Logger instance - */ - private static final Logger logger = LoggerFactory.getLogger(DistributedLockingFeature.class); - - /** - * Properties Configuration Name - */ - public static final String CONFIGURATION_PROPERTIES_NAME = "feature-distributed-locking"; - - /** - * Properties for locking feature - */ - private DistributedLockingProperties lockProps; - - /** - * Data source used to connect to the DB containing locks. - */ - private BasicDataSource dataSource; - - /** - * UUID - */ - private static final UUID uuid = UUID.randomUUID(); - - @Override - public int getSequenceNumber() { + + /** + * Logger instance. + */ + private static final Logger logger = LoggerFactory.getLogger(DistributedLockingFeature.class); + + /** + * Properties Configuration Name. + */ + public static final String CONFIGURATION_PROPERTIES_NAME = "feature-distributed-locking"; + + /** + * Properties for locking feature. + */ + private DistributedLockingProperties lockProps; + + /** + * Data source used to connect to the DB containing locks. + */ + private BasicDataSource dataSource; + + /** + * UUID. + */ + private static final UUID uuid = UUID.randomUUID(); + + @Override + public int getSequenceNumber() { return 1000; - } - - @Override + } + + @Override public OperResult beforeLock(String resourceId, String owner, int holdSec) { - - TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource); - return(tLock.lock(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); - } - + TargetLock lock = new TargetLock(resourceId, uuid, owner, dataSource); + + return (lock.lock(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); + } + @Override public OperResult beforeRefresh(String resourceId, String owner, int holdSec) { - - TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource); - return(tLock.refresh(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); + TargetLock lock = new TargetLock(resourceId, uuid, owner, dataSource); + + return (lock.refresh(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); + } + + @Override + public OperResult beforeUnlock(String resourceId, String owner) { + TargetLock lock = new TargetLock(resourceId, uuid, owner, dataSource); + + return (lock.unlock() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); + } + + @Override + public OperResult beforeIsLockedBy(String resourceId, String owner) { + TargetLock lock = new TargetLock(resourceId, uuid, owner, dataSource); + + return (lock.isActive() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); + } + + @Override + public OperResult beforeIsLocked(String resourceId) { + TargetLock lock = new TargetLock(resourceId, uuid, "dummyOwner", dataSource); + + return (lock.isLocked() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); } - @Override - public OperResult beforeUnlock(String resourceId, String owner) { - TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource); - - return(tLock.unlock() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); - } - - @Override - public OperResult beforeIsLockedBy(String resourceId, String owner) { - TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource); - - return(tLock.isActive() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); - } - - @Override - public OperResult beforeIsLocked(String resourceId) { - TargetLock tLock = new TargetLock(resourceId, uuid, "dummyOwner", dataSource); - - return(tLock.isLocked() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); - } - - @Override - public boolean afterStart(PolicyEngine engine) { - - try { - this.lockProps = new DistributedLockingProperties(SystemPersistence.manager.getProperties(DistributedLockingFeature.CONFIGURATION_PROPERTIES_NAME)); - this.dataSource = makeDataSource(); - } catch (PropertyException e) { - logger.error("DistributedLockingFeature feature properies have not been loaded", e); - throw new DistributedLockingFeatureException(e); - } catch(InterruptedException e) { + @Override + public boolean afterStart(PolicyEngine engine) { + + try { + this.lockProps = new DistributedLockingProperties( + SystemPersistence.manager.getProperties(DistributedLockingFeature.CONFIGURATION_PROPERTIES_NAME)); + this.dataSource = makeDataSource(); + } catch (PropertyException e) { + logger.error("DistributedLockingFeature feature properies have not been loaded", e); + throw new DistributedLockingFeatureException(e); + } catch (InterruptedException e) { logger.error("DistributedLockingFeature failed to create data source", e); - Thread.currentThread().interrupt(); + Thread.currentThread().interrupt(); throw new DistributedLockingFeatureException(e); - } catch(Exception e) { + } catch (Exception e) { logger.error("DistributedLockingFeature failed to create data source", e); throw new DistributedLockingFeatureException(e); - } - - cleanLockTable(); - - return false; - } - - /** - * @return a new, pooled data source - * @throws Exception - */ - private BasicDataSource makeDataSource() throws Exception { + } + + cleanLockTable(); + + return false; + } + + /** + * Make data source. + * + * @return a new, pooled data source + * @throws Exception exception + */ + private BasicDataSource makeDataSource() throws Exception { Properties props = new Properties(); props.put("driverClassName", lockProps.getDbDriver()); props.put("url", lockProps.getDbUrl()); @@ -140,36 +144,36 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy props.put("poolPreparedStatements", "true"); // additional properties are listed in the GenericObjectPool API - + return BasicDataSourceFactory.createDataSource(props); } /** - * This method kills the heartbeat thread and calls refreshLockTable which removes - * any records from the db where the current host is the owner. - */ - @Override - public boolean beforeShutdown(PolicyEngine engine) { - cleanLockTable(); - return false; - } - - /** - * This method removes all records owned by the current host from the db. - */ - private void cleanLockTable() { - - try (Connection conn = dataSource.getConnection(); - PreparedStatement statement = conn.prepareStatement( - "DELETE FROM pooling.locks WHERE host = ? OR expirationTime < now()"); - ){ - - statement.setString(1, uuid.toString()); - statement.executeUpdate(); - - } catch (SQLException e) { - logger.error("error in refreshLockTable()", e); - } - - } + * This method kills the heartbeat thread and calls refreshLockTable which removes + * any records from the db where the current host is the owner. + */ + @Override + public boolean beforeShutdown(PolicyEngine engine) { + cleanLockTable(); + return false; + } + + /** + * This method removes all records owned by the current host from the db. + */ + private void cleanLockTable() { + + try (Connection conn = dataSource.getConnection(); + PreparedStatement statement = conn.prepareStatement( + "DELETE FROM pooling.locks WHERE host = ? OR expirationTime < now()"); + ) { + + statement.setString(1, uuid.toString()); + statement.executeUpdate(); + + } catch (SQLException e) { + logger.error("error in refreshLockTable()", e); + } + + } } diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeatureException.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeatureException.java index f28ccbc9..55fc4fab 100644 --- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeatureException.java +++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeatureException.java @@ -21,14 +21,14 @@ package org.onap.policy.distributed.locking; public class DistributedLockingFeatureException extends RuntimeException { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - /** - * - * @param e - * exception to be wrapped - */ - public DistributedLockingFeatureException(Exception e) { - super(e); - } + /** + * Constructor. + * + * @param ex exception to be wrapped + */ + public DistributedLockingFeatureException(Exception ex) { + super(ex); + } } diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingProperties.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingProperties.java index 1f55a4cb..00db5733 100644 --- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingProperties.java +++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingProperties.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.distributed.locking; import java.util.Properties; @@ -42,25 +43,25 @@ public class DistributedLockingProperties extends PropertyConfiguration { private Properties source; /** - * Database driver + * Database driver. */ @Property(name = DB_DRIVER) private String dbDriver; /** - * Database url + * Database url. */ @Property(name = DB_URL) private String dbUrl; /** - * Database user + * Database user. */ @Property(name = DB_USER) private String dbUser; /** - * Database password + * Database password. */ @Property(name = DB_PWD) private String dbPwd; diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java index 0652897c..42e1f92f 100644 --- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java +++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.distributed.locking; import java.sql.Connection; @@ -29,145 +30,149 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TargetLock { - - private static final Logger logger = LoggerFactory.getLogger(TargetLock.class); - - /** - * The Target resource we want to lock - */ - private String resourceId; - + + private static final Logger logger = LoggerFactory.getLogger(TargetLock.class); + + /** + * The Target resource we want to lock. + */ + private String resourceId; + /** * Data source used to connect to the DB containing locks. */ private BasicDataSource dataSource; - /** - * UUID - */ - private UUID uuid; - - /** - * Owner - */ - private String owner; - - /** - * Constructs a TargetLock object. - * - * @param resourceId ID of the entity we want to lock - * @param dataSource used to connect to the DB containing locks - */ - public TargetLock (String resourceId, UUID uuid, String owner, BasicDataSource dataSource) { - this.resourceId = resourceId; - this.uuid = uuid; - this.owner = owner; - this.dataSource = dataSource; - } - - /** - * obtain a lock + /** + * UUID . + */ + private UUID uuid; + + /** + * Owner. + */ + private String owner; + + /** + * Constructs a TargetLock object. + * + * @param resourceId ID of the entity we want to lock + * @param dataSource used to connect to the DB containing locks + */ + public TargetLock(String resourceId, UUID uuid, String owner, BasicDataSource dataSource) { + this.resourceId = resourceId; + this.uuid = uuid; + this.owner = owner; + this.dataSource = dataSource; + } + + /** + * Obtain a lock. * @param holdSec the amount of time, in seconds, that the lock should be held - */ - public boolean lock(int holdSec) { - - return grabLock(holdSec); - } + */ + public boolean lock(int holdSec) { + + return grabLock(holdSec); + } /** - * refresh a lock + * Refresh a lock. * * @param holdSec the amount of time, in seconds, that the lock should be held * @return {@code true} if the lock was refreshed, {@code false} if the resource is * not currently locked by the given owner */ - public boolean refresh(int holdSec) { - return updateLock(holdSec); - } - - /** - * Unlock a resource by deleting it's associated record in the db - */ - public boolean unlock() { - return deleteLock(); - } - - /** - * "Grabs" lock by attempting to insert a new record in the db. - * If the insert fails due to duplicate key error resource is already locked - * so we call secondGrab. - * @param holdSec the amount of time, in seconds, that the lock should be held - */ - private boolean grabLock(int holdSec) { - - // try to insert a record into the table(thereby grabbing the lock) - try (Connection conn = dataSource.getConnection(); - - PreparedStatement statement = conn.prepareStatement( - "INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) values (?, ?, ?, timestampadd(second, ?, now()))")) { - - int i = 1; - statement.setString(i++, this.resourceId); - statement.setString(i++, this.uuid.toString()); - statement.setString(i++, this.owner); - statement.setInt(i++, holdSec); - statement.executeUpdate(); - } - - catch (SQLException e) { - logger.error("error in TargetLock.grabLock()", e); - return secondGrab(holdSec); - } - - return true; - } - - /** - * A second attempt at grabbing a lock. It first attempts to update the lock in case it is expired. - * If that fails, it attempts to insert a new record again + public boolean refresh(int holdSec) { + return updateLock(holdSec); + } + + /** + * Unlock a resource by deleting it's associated record in the db. + */ + public boolean unlock() { + return deleteLock(); + } + + /** + * "Grabs" lock by attempting to insert a new record in the db. + * If the insert fails due to duplicate key error resource is already locked + * so we call secondGrab. * @param holdSec the amount of time, in seconds, that the lock should be held - */ - private boolean secondGrab(int holdSec) { + */ + private boolean grabLock(int holdSec) { - try (Connection conn = dataSource.getConnection(); + // try to insert a record into the table(thereby grabbing the lock) + try (Connection conn = dataSource.getConnection(); - PreparedStatement updateStatement = conn.prepareStatement( - "UPDATE pooling.locks SET host = ?, owner = ?, expirationTime = timestampadd(second, ?, now()) WHERE resourceId = ? AND expirationTime < now()"); + PreparedStatement statement = conn.prepareStatement( + "INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) " + + "values (?, ?, ?, timestampadd(second, ?, now()))")) { - PreparedStatement insertStatement = conn.prepareStatement( - "INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) values (?, ?, ?, timestampadd(second, ?, now()))");) { + int index = 1; + statement.setString(index++, this.resourceId); + statement.setString(index++, this.uuid.toString()); + statement.setString(index++, this.owner); + statement.setInt(index++, holdSec); + statement.executeUpdate(); + } - int i = 1; - updateStatement.setString(i++, this.uuid.toString()); - updateStatement.setString(i++, this.owner); - updateStatement.setInt(i++, holdSec); - updateStatement.setString(i++, this.resourceId); + catch (SQLException e) { + logger.error("error in TargetLock.grabLock()", e); + return secondGrab(holdSec); + } + + return true; + } - // The lock was expired and we grabbed it. - // return true - if (updateStatement.executeUpdate() == 1) { - return true; - } + /** + * A second attempt at grabbing a lock. It first attempts to update the lock in case it is expired. + * If that fails, it attempts to insert a new record again + * @param holdSec the amount of time, in seconds, that the lock should be held + */ + private boolean secondGrab(int holdSec) { - // If our update does not return 1 row, the lock either has not expired - // or it was removed. Try one last grab - else { - i = 1; - insertStatement.setString(i++, this.resourceId); - insertStatement.setString(i++, this.uuid.toString()); - insertStatement.setString(i++, this.owner); - insertStatement.setInt(i++, holdSec); + try (Connection conn = dataSource.getConnection(); - // If our insert returns 1 we successfully grabbed the lock - return (insertStatement.executeUpdate() == 1); - } + PreparedStatement updateStatement = conn.prepareStatement( + "UPDATE pooling.locks SET host = ?, owner = ?, " + + "expirationTime = timestampadd(second, ?, now()) " + + "WHERE resourceId = ? AND expirationTime < now()"); + + PreparedStatement insertStatement = conn.prepareStatement( + "INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) " + + "values (?, ?, ?, timestampadd(second, ?, now()))");) { + + int index = 1; + updateStatement.setString(index++, this.uuid.toString()); + updateStatement.setString(index++, this.owner); + updateStatement.setInt(index++, holdSec); + updateStatement.setString(index++, this.resourceId); + + // The lock was expired and we grabbed it. + // return true + if (updateStatement.executeUpdate() == 1) { + return true; + } + + // If our update does not return 1 row, the lock either has not expired + // or it was removed. Try one last grab + else { + index = 1; + insertStatement.setString(index++, this.resourceId); + insertStatement.setString(index++, this.uuid.toString()); + insertStatement.setString(index++, this.owner); + insertStatement.setInt(index++, holdSec); + + // If our insert returns 1 we successfully grabbed the lock + return (insertStatement.executeUpdate() == 1); + } - } catch (SQLException e) { - logger.error("error in TargetLock.secondGrab()", e); - return false; - } + } catch (SQLException e) { + logger.error("error in TargetLock.secondGrab()", e); + return false; + } - } + } /** * Updates the DB record associated with the lock. @@ -179,15 +184,17 @@ public class TargetLock { try (Connection conn = dataSource.getConnection(); - PreparedStatement updateStatement = conn.prepareStatement( - "UPDATE pooling.locks SET host = ?, owner = ?, expirationTime = timestampadd(second, ?, now()) WHERE resourceId = ? AND owner = ? AND expirationTime >= now()")) { + PreparedStatement updateStatement = conn.prepareStatement( + "UPDATE pooling.locks SET host = ?, owner = ?, " + + "expirationTime = timestampadd(second, ?, now()) " + + "WHERE resourceId = ? AND owner = ? AND expirationTime >= now()")) { - int i = 1; - updateStatement.setString(i++, this.uuid.toString()); - updateStatement.setString(i++, this.owner); - updateStatement.setInt(i++, holdSec); - updateStatement.setString(i++, this.resourceId); - updateStatement.setString(i++, this.owner); + int index = 1; + updateStatement.setString(index++, this.uuid.toString()); + updateStatement.setString(index++, this.owner); + updateStatement.setInt(index++, holdSec); + updateStatement.setString(index++, this.resourceId); + updateStatement.setString(index++, this.owner); // refresh succeeded iff a record was updated return (updateStatement.executeUpdate() == 1); @@ -198,80 +205,78 @@ public class TargetLock { } } - - /** - *To remove a lock we simply delete the record from the db - */ - private boolean deleteLock() { - - try (Connection conn = dataSource.getConnection(); - - PreparedStatement deleteStatement = conn.prepareStatement( - "DELETE FROM pooling.locks WHERE resourceId = ? AND owner = ? AND host = ?")) { - - deleteStatement.setString(1, this.resourceId); - deleteStatement.setString(2, this.owner); - deleteStatement.setString(3, this.uuid.toString()); - - return (deleteStatement.executeUpdate() == 1); - - } catch (SQLException e) { - logger.error("error in TargetLock.deleteLock()", e); - return false; - } - - } - - /** - * Is the lock active - */ - public boolean isActive() { - try (Connection conn = dataSource.getConnection(); - - PreparedStatement selectStatement = conn.prepareStatement( - "SELECT * FROM pooling.locks WHERE resourceId = ? AND host = ? AND owner= ? AND expirationTime >= now()")) { - - selectStatement.setString(1, this.resourceId); - selectStatement.setString(2, this.uuid.toString()); - selectStatement.setString(3, this.owner); - try (ResultSet result = selectStatement.executeQuery()) { - - // This will return true if the - // query returned at least one row - return result.first(); - } - - } - - catch (SQLException e) { - logger.error("error in TargetLock.isActive()", e); - return false; - } - - } - - /** - * Is the resource locked - */ - public boolean isLocked() { - - try (Connection conn = dataSource.getConnection(); - - PreparedStatement selectStatement = conn - .prepareStatement("SELECT * FROM pooling.locks WHERE resourceId = ? AND expirationTime >= now()")) { - - selectStatement.setString(1, this.resourceId); - try (ResultSet result = selectStatement.executeQuery()) { - // This will return true if the - // query returned at least one row - return result.first(); - } - } - - catch (SQLException e) { - logger.error("error in TargetLock.isActive()", e); - return false; - } - } + + /** + *To remove a lock we simply delete the record from the db . + */ + private boolean deleteLock() { + + try (Connection conn = dataSource.getConnection(); + + PreparedStatement deleteStatement = conn.prepareStatement( + "DELETE FROM pooling.locks WHERE resourceId = ? AND owner = ? AND host = ?")) { + + deleteStatement.setString(1, this.resourceId); + deleteStatement.setString(2, this.owner); + deleteStatement.setString(3, this.uuid.toString()); + + return (deleteStatement.executeUpdate() == 1); + + } catch (SQLException e) { + logger.error("error in TargetLock.deleteLock()", e); + return false; + } + + } + + /** + * Is the lock active. + */ + public boolean isActive() { + try (Connection conn = dataSource.getConnection(); + + PreparedStatement selectStatement = conn.prepareStatement( + "SELECT * FROM pooling.locks " + + "WHERE resourceId = ? AND host = ? AND owner= ? AND expirationTime >= now()")) { + + selectStatement.setString(1, this.resourceId); + selectStatement.setString(2, this.uuid.toString()); + selectStatement.setString(3, this.owner); + try (ResultSet result = selectStatement.executeQuery()) { + + // This will return true if the + // query returned at least one row + return result.first(); + } + + } + + catch (SQLException e) { + logger.error("error in TargetLock.isActive()", e); + return false; + } + + } + + /** + * Is the resource locked. + */ + public boolean isLocked() { + + try (Connection conn = dataSource.getConnection(); + PreparedStatement selectStatement = conn + .prepareStatement("SELECT * FROM pooling.locks WHERE resourceId = ? AND expirationTime >= now()")) { + + selectStatement.setString(1, this.resourceId); + try (ResultSet result = selectStatement.executeQuery()) { + // This will return true if the + // query returned at least one row + return result.first(); + } + } catch (SQLException e) { + logger.error("error in TargetLock.isActive()", e); + return false; + } + } } diff --git a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockingFeatureExceptionTest.java b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockingFeatureExceptionTest.java index 5bc3ae5e..7ba2384a 100644 --- a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockingFeatureExceptionTest.java +++ b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockingFeatureExceptionTest.java @@ -26,11 +26,11 @@ import org.junit.Test; import org.onap.policy.common.utils.test.ExceptionsTester; import org.onap.policy.distributed.locking.DistributedLockingFeatureException; -public class DistributedLockingFeatureExceptionTest extends ExceptionsTester{ - - @Test - public void test() { - assertEquals(1, test(DistributedLockingFeatureException.class)); - } +public class DistributedLockingFeatureExceptionTest extends ExceptionsTester { + + @Test + public void test() { + assertEquals(1, test(DistributedLockingFeatureException.class)); + } } diff --git a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java index 42c8a742..60a721b7 100644 --- a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java +++ b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,6 +21,7 @@ package org.onap.policy.distributed.locking; import static org.junit.Assert.assertEquals; + import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -36,191 +37,211 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TargetLockTest { - private static final Logger logger = LoggerFactory.getLogger(TargetLockTest.class); + private static final Logger logger = LoggerFactory.getLogger(TargetLockTest.class); private static final int MAX_AGE_SEC = 4 * 60; - private static final String DB_CONNECTION = "jdbc:h2:mem:pooling;INIT=CREATE SCHEMA IF NOT EXISTS pooling\\;SET SCHEMA pooling"; - private static final String DB_USER = "user"; - private static final String DB_PASSWORD = "password"; - private static Connection conn = null; - private static DistributedLockingFeature distLockFeat; - - @BeforeClass - public static void setup() { - getDBConnection(); - createTable(); - SystemPersistence.manager.setConfigurationDir("src/test/resources"); - distLockFeat = new DistributedLockingFeature(); - distLockFeat.afterStart(null); - - } - - @AfterClass - public static void cleanUp() { - distLockFeat.beforeShutdown(null); - try { - conn.close(); - } catch (SQLException e) { - logger.error("Error in TargetLockTest.cleanUp()", e); - } - } - - @Before - public void wipeDb() { - try (PreparedStatement lockDelete = conn.prepareStatement("DELETE FROM pooling.locks");){ - lockDelete.executeUpdate(); - } catch (SQLException e) { - logger.error("Error in TargetLockTest.wipeDb()", e); - throw new RuntimeException(e); - } - - } - - @Test - public void testGrabLockSuccess() throws InterruptedException, ExecutionException { - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC)); - - //attempt to grab expiredLock - try (PreparedStatement updateStatement = conn.prepareStatement("UPDATE pooling.locks SET expirationTime = timestampadd(second, -1, now()) WHERE resourceId = ?");) - { - updateStatement.setString(1, "resource1"); - updateStatement.executeUpdate(); - - } catch (SQLException e) { - logger.error("Error in TargetLockTest.testGrabLockSuccess()", e); - throw new RuntimeException(e); - } - - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC)); - - // cannot re-lock - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC)); + private static final String DB_CONNECTION = + "jdbc:h2:mem:pooling;INIT=CREATE SCHEMA IF NOT EXISTS pooling\\;SET SCHEMA pooling"; + private static final String DB_USER = "user"; + private static final String DB_PASSWORD = "password"; + private static Connection conn = null; + private static DistributedLockingFeature distLockFeat; + + /** + * Setup the database. + */ + @BeforeClass + public static void setup() { + getDbConnection(); + createTable(); + SystemPersistence.manager.setConfigurationDir("src/test/resources"); + distLockFeat = new DistributedLockingFeature(); + distLockFeat.afterStart(null); + } + + /** + * Cleanup the lock. + */ + @AfterClass + public static void cleanUp() { + distLockFeat.beforeShutdown(null); + try { + conn.close(); + } catch (SQLException e) { + logger.error("Error in TargetLockTest.cleanUp()", e); + } + } + + /** + * Wipe the database. + */ + @Before + public void wipeDb() { + try (PreparedStatement lockDelete = conn.prepareStatement("DELETE FROM pooling.locks"); ) { + lockDelete.executeUpdate(); + } catch (SQLException e) { + logger.error("Error in TargetLockTest.wipeDb()", e); + throw new RuntimeException(e); + } + } + + @Test + public void testGrabLockSuccess() throws InterruptedException, ExecutionException { + assertEquals( + OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC)); + + // attempt to grab expiredLock + try (PreparedStatement updateStatement = + conn.prepareStatement( + "UPDATE pooling.locks SET expirationTime = timestampadd(second, -1, now()) WHERE resourceId = ?"); ) { + updateStatement.setString(1, "resource1"); + updateStatement.executeUpdate(); + + } catch (SQLException e) { + logger.error("Error in TargetLockTest.testGrabLockSuccess()", e); + throw new RuntimeException(e); + } + + assertEquals( + OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC)); + + // cannot re-lock + assertEquals( + OperResult.OPER_DENIED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC)); assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); - } - - @Test - public void testExpiredLocks() throws Exception { - - //grab lock - distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); - - //force lock to expire - try (PreparedStatement lockExpire = conn.prepareStatement("UPDATE pooling.locks SET expirationTime = timestampadd(second, -?, now())");) { + } + + @Test + public void testExpiredLocks() throws Exception { + + // grab lock + distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); + + // force lock to expire + try (PreparedStatement lockExpire = + conn.prepareStatement( + "UPDATE pooling.locks SET expirationTime = timestampadd(second, -?, now())"); ) { lockExpire.setInt(1, MAX_AGE_SEC + 1); lockExpire.executeUpdate(); } - - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner2", MAX_AGE_SEC)); - } - - @Test - public void testGrabLockFail() throws InterruptedException, ExecutionException { - - distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); - - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeLock("resource1", "owner2", MAX_AGE_SEC)); - - } - + + assertEquals( + OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner2", MAX_AGE_SEC)); + } + + @Test + public void testGrabLockFail() throws InterruptedException, ExecutionException { + + distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); + + assertEquals( + OperResult.OPER_DENIED, distLockFeat.beforeLock("resource1", "owner2", MAX_AGE_SEC)); + } + @Test public void testUpdateLock() throws InterruptedException, ExecutionException { // not locked yet - refresh should fail - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC)); - + assertEquals( + OperResult.OPER_DENIED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC)); + // now lock it - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC)); + assertEquals( + OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC)); // refresh should work now - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC)); - + assertEquals( + OperResult.OPER_ACCEPTED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC)); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); - + // expire the lock - try (PreparedStatement updateStatement = conn.prepareStatement("UPDATE pooling.locks SET expirationTime = timestampadd(second, -1, now()) WHERE resourceId = ?");) - { + try (PreparedStatement updateStatement = + conn.prepareStatement( + "UPDATE pooling.locks SET expirationTime = timestampadd(second, -1, now()) WHERE resourceId = ?"); ) { updateStatement.setString(1, "resource1"); updateStatement.executeUpdate(); - + } catch (SQLException e) { logger.error("Error in TargetLockTest.testGrabLockSuccess()", e); throw new RuntimeException(e); } - + // refresh should fail now - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC)); + assertEquals( + OperResult.OPER_DENIED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC)); assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); } - - - @Test - public void testUnlock() throws InterruptedException, ExecutionException { - distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); - - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeUnlock("resource1", "owner1")); - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner2", MAX_AGE_SEC)); - } - - @Test - public void testIsActive() { - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); - distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner2")); - - // isActive on expiredLock - try (PreparedStatement updateStatement = conn - .prepareStatement("UPDATE pooling.locks SET expirationTime = timestampadd(second, -5, now()) WHERE resourceId = ?");) { - updateStatement.setString(1, "resource1"); - updateStatement.executeUpdate(); - - } catch (SQLException e) { - logger.error("Error in TargetLockTest.testIsActive()", e); - throw new RuntimeException(e); - } - - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); - - distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); - //Unlock record, next isActive attempt should fail - distLockFeat.beforeUnlock("resource1", "owner1"); - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); - - } - - @Test - public void unlockBeforeLock() { - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeUnlock("resource1", "owner1")); - distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeUnlock("resource1", "owner1")); - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeUnlock("resource1", "owner1")); - } - - @Test - public void testIsLocked() { - assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLocked("resource1")); - distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); - assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLocked("resource1")); - - } - - private static void getDBConnection() { - try { - conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); - } catch (SQLException e) { - logger.error("Error in TargetLockTest.getDBConnection()", e); - } - } - - private static void createTable() { - String createString = "create table if not exists pooling.locks (resourceId VARCHAR(128), host VARCHAR(128), owner VARCHAR(128), expirationTime TIMESTAMP DEFAULT 0, PRIMARY KEY (resourceId))"; - try (PreparedStatement createStmt = conn.prepareStatement(createString);) { - createStmt.executeUpdate(); - - } catch (SQLException e) { - logger.error("Error in TargetLockTest.createTable()", e); - } - } - + @Test + public void testUnlock() throws InterruptedException, ExecutionException { + distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); + + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeUnlock("resource1", "owner1")); + assertEquals( + OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner2", MAX_AGE_SEC)); + } + + @Test + public void testIsActive() { + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); + distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner2")); + + // isActive on expiredLock + try (PreparedStatement updateStatement = + conn.prepareStatement( + "UPDATE pooling.locks SET expirationTime = timestampadd(second, -5, now()) WHERE resourceId = ?"); ) { + updateStatement.setString(1, "resource1"); + updateStatement.executeUpdate(); + + } catch (SQLException e) { + logger.error("Error in TargetLockTest.testIsActive()", e); + throw new RuntimeException(e); + } + + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); + + distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); + // Unlock record, next isActive attempt should fail + distLockFeat.beforeUnlock("resource1", "owner1"); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); + } + + @Test + public void unlockBeforeLock() { + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeUnlock("resource1", "owner1")); + distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeUnlock("resource1", "owner1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeUnlock("resource1", "owner1")); + } + + @Test + public void testIsLocked() { + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLocked("resource1")); + distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLocked("resource1")); + } + + private static void getDbConnection() { + try { + conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); + } catch (SQLException e) { + logger.error("Error in TargetLockTest.getDBConnection()", e); + } + } + + private static void createTable() { + String createString = + "create table if not exists pooling.locks " + + "(resourceId VARCHAR(128), host VARCHAR(128), owner VARCHAR(128), " + + "expirationTime TIMESTAMP DEFAULT 0, PRIMARY KEY (resourceId))"; + try (PreparedStatement createStmt = conn.prepareStatement(createString); ) { + createStmt.executeUpdate(); + + } catch (SQLException e) { + logger.error("Error in TargetLockTest.createTable()", e); + } + } } diff --git a/feature-eelf/src/main/java/org/onap/policy/drools/eelf/EelfFeature.java b/feature-eelf/src/main/java/org/onap/policy/drools/eelf/EelfFeature.java index aac456e2..bf0f0000 100644 --- a/feature-eelf/src/main/java/org/onap/policy/drools/eelf/EelfFeature.java +++ b/feature-eelf/src/main/java/org/onap/policy/drools/eelf/EelfFeature.java @@ -31,43 +31,45 @@ import org.onap.policy.drools.system.Main; import org.onap.policy.drools.system.PolicyEngine; /** - * Feature EELF : Enables EELF Logging Libraries + * Feature EELF : Enables EELF Logging Libraries . */ public class EelfFeature implements PolicyEngineFeatureAPI { - @Override - public final boolean beforeBoot(PolicyEngine engine, String[] cliArgs) { - - String logback = System.getProperty(Main.LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, - Main.LOGBACK_CONFIGURATION_FILE_DEFAULT); - Path logbackPath = Paths.get(logback); - - if (System.getProperty(Configuration.PROPERTY_LOGGING_FILE_PATH) == null) - System.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, - logbackPath.toAbsolutePath().getParent().toString()); - - if (System.getProperty(Configuration.PROPERTY_LOGGING_FILE_NAME) == null) - System.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, - logbackPath.getFileName().toString()); - - Logger logger = FlexLogger.getLogger(this.getClass(), true); - - if (logger.isInfoEnabled()) { - logProperty(logger, Main.LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY); - logProperty(logger, Configuration.PROPERTY_LOGGING_FILE_PATH); - logProperty(logger, Configuration.PROPERTY_LOGGING_FILE_NAME); - } - - return false; - } - - private void logProperty(Logger logger, String propnm) { - logger.info("eelf-feature: Property " + propnm + "=" + System.getProperty(propnm)); - } - - @Override - public int getSequenceNumber() { - return 0; - } + @Override + public final boolean beforeBoot(PolicyEngine engine, String[] cliArgs) { + + String logback = System.getProperty(Main.LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, + Main.LOGBACK_CONFIGURATION_FILE_DEFAULT); + Path logbackPath = Paths.get(logback); + + if (System.getProperty(Configuration.PROPERTY_LOGGING_FILE_PATH) == null) { + System.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, + logbackPath.toAbsolutePath().getParent().toString()); + } + + if (System.getProperty(Configuration.PROPERTY_LOGGING_FILE_NAME) == null) { + System.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, + logbackPath.getFileName().toString()); + } + + Logger logger = FlexLogger.getLogger(this.getClass(), true); + + if (logger.isInfoEnabled()) { + logProperty(logger, Main.LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY); + logProperty(logger, Configuration.PROPERTY_LOGGING_FILE_PATH); + logProperty(logger, Configuration.PROPERTY_LOGGING_FILE_NAME); + } + + return false; + } + + private void logProperty(Logger logger, String propnm) { + logger.info("eelf-feature: Property " + propnm + "=" + System.getProperty(propnm)); + } + + @Override + public int getSequenceNumber() { + return 0; + } } diff --git a/feature-eelf/src/test/java/org/onap/policy/drools/eelf/test/EElfTest.java b/feature-eelf/src/test/java/org/onap/policy/drools/eelf/test/EElfTest.java index f7003706..7f9e81cc 100644 --- a/feature-eelf/src/test/java/org/onap/policy/drools/eelf/test/EElfTest.java +++ b/feature-eelf/src/test/java/org/onap/policy/drools/eelf/test/EElfTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-eelf * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,11 +17,19 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.drools.eelf.test; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import ch.qos.logback.classic.LoggerContext; + +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFLogger.Level; +import com.att.eelf.configuration.EELFManager; + import java.util.ArrayList; import java.util.List; @@ -33,114 +41,108 @@ import org.onap.policy.drools.system.PolicyEngine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.att.eelf.configuration.Configuration; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFLogger.Level; -import com.att.eelf.configuration.EELFManager; - -import ch.qos.logback.classic.LoggerContext; - /** - * Logger Tests + * Logger Tests. */ public class EElfTest { - - /** - * logback configuration location - */ - public final static String LOGBACK_CONFIGURATION_FILE_DEFAULT = "src/main/feature/config/logback-eelf.xml"; - - /** - * SLF4J Logger - */ - private final Logger slf4jLogger = LoggerFactory.getLogger(EElfTest.class); - - /** - * get all loggers - * @return list of all loggers - */ - protected List<String> loggers() { - List<String> loggers = new ArrayList<String>(); - LoggerContext context = - (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory(); - for (org.slf4j.Logger logger: context.getLoggerList()) { - loggers.add(logger.getName()); - } - slf4jLogger.info(loggers.toString()); - return loggers; - } - - /** - * Assert Log Levels are the same between an EELF Logger and an SLF4J Logger - * - * @param eelfLogger EELF Logger - * @param slf4jLogger SLF4J Logger - */ - protected void assertLogLevels(EELFLogger eelfLogger, Logger slf4jLogger) { - assertTrue(slf4jLogger.isDebugEnabled() == eelfLogger.isDebugEnabled()); - assertTrue(slf4jLogger.isInfoEnabled() == eelfLogger.isInfoEnabled()); - assertTrue(slf4jLogger.isErrorEnabled() == eelfLogger.isErrorEnabled()); - assertTrue(slf4jLogger.isWarnEnabled() == eelfLogger.isWarnEnabled()); - assertTrue(slf4jLogger.isTraceEnabled() == eelfLogger.isTraceEnabled()); - } - - @Test - public void slf4jLog() { - - /* standard slf4j using defaults */ - - slf4jLogger.info("slf4j info"); - - List<String> loggers = loggers(); - - assertFalse(loggers.contains(Configuration.DEBUG_LOGGER_NAME)); - assertFalse(loggers.contains(Configuration.AUDIT_LOGGER_NAME)); - assertFalse(loggers.contains(Configuration.ERROR_LOGGER_NAME)); - assertFalse(loggers.contains(Configuration.METRICS_LOGGER_NAME)); - - /* set logback configuration */ - - System.setProperty(Main.LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, - LOGBACK_CONFIGURATION_FILE_DEFAULT); - - /* set up eelf throuth common loggings library */ - - EelfFeature feature = new EelfFeature(); - feature.beforeBoot(PolicyEngine.manager, null); - - loggers = loggers(); - assertTrue(loggers.contains(Configuration.DEBUG_LOGGER_NAME)); - assertTrue(loggers.contains(Configuration.AUDIT_LOGGER_NAME)); - assertTrue(loggers.contains(Configuration.ERROR_LOGGER_NAME)); - assertTrue(loggers.contains(Configuration.METRICS_LOGGER_NAME)); - - EELFLogger eelfAuditLogger = EELFManager.getInstance().getAuditLogger(); - Logger slf4jAuditLogger = org.slf4j.LoggerFactory.getLogger(Configuration.AUDIT_LOGGER_NAME); - org.onap.policy.common.logging.flexlogger.Logger flexLogger = - FlexLogger.getLogger(EElfTest.class, true); - - /* generate an error entry */ - - Exception testException = new IllegalStateException("exception test"); - flexLogger.error("flex-logger exception", testException); - EELFManager.getInstance().getErrorLogger().error("eelf-logger exception", testException); - org.slf4j.LoggerFactory.getLogger(Configuration.ERROR_LOGGER_NAME).error("slf4j-logger", testException); - - - /* generate an audit entry through all logs */ - - flexLogger.audit("flexlogger audit"); - eelfAuditLogger.info("eelf audit"); - slf4jAuditLogger.info("slf4j audit"); - - /* check log levels in eelf and standard slf4j change in both directions */ - - /* eelf initiated */ - eelfAuditLogger.setLevel(Level.ERROR); - assertLogLevels(eelfAuditLogger, slf4jAuditLogger); - - /* slf4j initiated */ - ((ch.qos.logback.classic.Logger) slf4jLogger).setLevel((ch.qos.logback.classic.Level.INFO)); - assertLogLevels(eelfAuditLogger, slf4jAuditLogger); - } + + /** + * logback configuration location. + */ + public static final String LOGBACK_CONFIGURATION_FILE_DEFAULT = "src/main/feature/config/logback-eelf.xml"; + + /** + * SLF4J Logger. + */ + private final Logger slf4jLogger = LoggerFactory.getLogger(EElfTest.class); + + /** + * Get all loggers. + * + * @return list of all loggers + */ + protected List<String> loggers() { + List<String> loggers = new ArrayList<String>(); + LoggerContext context = + (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory(); + for (org.slf4j.Logger logger: context.getLoggerList()) { + loggers.add(logger.getName()); + } + slf4jLogger.info(loggers.toString()); + return loggers; + } + + /** + * Assert Log Levels are the same between an EELF Logger and an SLF4J Logger. + * + * @param eelfLogger EELF Logger + * @param slf4jLogger SLF4J Logger + */ + protected void assertLogLevels(EELFLogger eelfLogger, Logger slf4jLogger) { + assertTrue(slf4jLogger.isDebugEnabled() == eelfLogger.isDebugEnabled()); + assertTrue(slf4jLogger.isInfoEnabled() == eelfLogger.isInfoEnabled()); + assertTrue(slf4jLogger.isErrorEnabled() == eelfLogger.isErrorEnabled()); + assertTrue(slf4jLogger.isWarnEnabled() == eelfLogger.isWarnEnabled()); + assertTrue(slf4jLogger.isTraceEnabled() == eelfLogger.isTraceEnabled()); + } + + @Test + public void slf4jLog() { + + /* standard slf4j using defaults */ + + slf4jLogger.info("slf4j info"); + + List<String> loggers = loggers(); + + assertFalse(loggers.contains(Configuration.DEBUG_LOGGER_NAME)); + assertFalse(loggers.contains(Configuration.AUDIT_LOGGER_NAME)); + assertFalse(loggers.contains(Configuration.ERROR_LOGGER_NAME)); + assertFalse(loggers.contains(Configuration.METRICS_LOGGER_NAME)); + + /* set logback configuration */ + + System.setProperty(Main.LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, + LOGBACK_CONFIGURATION_FILE_DEFAULT); + + /* set up eelf throuth common loggings library */ + + EelfFeature feature = new EelfFeature(); + feature.beforeBoot(PolicyEngine.manager, null); + + loggers = loggers(); + assertTrue(loggers.contains(Configuration.DEBUG_LOGGER_NAME)); + assertTrue(loggers.contains(Configuration.AUDIT_LOGGER_NAME)); + assertTrue(loggers.contains(Configuration.ERROR_LOGGER_NAME)); + assertTrue(loggers.contains(Configuration.METRICS_LOGGER_NAME)); + + final EELFLogger eelfAuditLogger = EELFManager.getInstance().getAuditLogger(); + final Logger slf4jAuditLogger = org.slf4j.LoggerFactory.getLogger(Configuration.AUDIT_LOGGER_NAME); + org.onap.policy.common.logging.flexlogger.Logger flexLogger = + FlexLogger.getLogger(EElfTest.class, true); + + /* generate an error entry */ + + Exception testException = new IllegalStateException("exception test"); + flexLogger.error("flex-logger exception", testException); + EELFManager.getInstance().getErrorLogger().error("eelf-logger exception", testException); + org.slf4j.LoggerFactory.getLogger(Configuration.ERROR_LOGGER_NAME).error("slf4j-logger", testException); + + + /* generate an audit entry through all logs */ + + flexLogger.audit("flexlogger audit"); + eelfAuditLogger.info("eelf audit"); + slf4jAuditLogger.info("slf4j audit"); + + /* check log levels in eelf and standard slf4j change in both directions */ + + /* eelf initiated */ + eelfAuditLogger.setLevel(Level.ERROR); + assertLogLevels(eelfAuditLogger, slf4jAuditLogger); + + /* slf4j initiated */ + ((ch.qos.logback.classic.Logger) slf4jLogger).setLevel((ch.qos.logback.classic.Level.INFO)); + assertLogLevels(eelfAuditLogger, slf4jAuditLogger); + } } diff --git a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java index f4cb1cc6..d35b7371 100644 --- a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java +++ b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java @@ -35,41 +35,41 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Healthcheck + * Healthcheck. */ public interface HealthCheck extends Startable { /** - * Healthcheck Monitor + * Healthcheck Monitor. */ public static final HealthCheck monitor = new HealthCheckMonitor(); /** - * Healthcheck Report + * Healthcheck Report. */ public static class Report { /** - * Named Entity in the report + * Named Entity in the report. */ private String name; /** - * URL queried + * URL queried. */ private String url; /** - * healthy? + * healthy. */ private boolean healthy; /** - * return code + * return code. */ private int code; /** - * Message from remote entity + * Message from remote entity. */ private String message; @@ -132,7 +132,7 @@ public interface HealthCheck extends Startable { } /** - * Report aggregation + * Report aggregation. */ public static class Reports { private boolean healthy; @@ -167,7 +167,7 @@ public interface HealthCheck extends Startable { } /** - * perform a healthcheck + * Perform a healthcheck. * * @return a report */ @@ -176,27 +176,27 @@ public interface HealthCheck extends Startable { /** - * Healthcheck Monitor + * Healthcheck Monitor. */ class HealthCheckMonitor implements HealthCheck { /** - * Logger + * Logger. */ private static Logger logger = LoggerFactory.getLogger(HealthCheckMonitor.class); /** - * attached http servers + * attached http servers. */ protected volatile List<HttpServletServer> servers = new ArrayList<>(); /** - * attached http clients + * attached http clients. */ protected volatile List<HttpClient> clients = new ArrayList<>(); /** - * healthcheck configuration + * healthcheck configuration. */ protected volatile Properties healthCheckProperties = null; @@ -306,6 +306,8 @@ class HealthCheckMonitor implements HealthCheck { } /** + * Get servers. + * * @return list of attached Http Servers */ public List<HttpServletServer> getServers() { @@ -313,6 +315,8 @@ class HealthCheckMonitor implements HealthCheck { } /** + * Get clients. + * * @return list of attached Http Clients */ public List<HttpClient> getClients() { diff --git a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java index 34c1fe3a..3a79a55c 100644 --- a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java +++ b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-healthcheck * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,53 +26,54 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * This feature provides healthcheck verification of remotely associated RESTful components + * This feature provides healthcheck verification of remotely associated RESTful components. */ -public class HealthCheckFeature implements PolicyEngineFeatureAPI { - - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(HealthCheckFeature.class); - - /** - * Properties Configuration Name - */ - public static final String CONFIGURATION_PROPERTIES_NAME = "feature-healthcheck"; +public class HealthCheckFeature implements PolicyEngineFeatureAPI { - @Override - public int getSequenceNumber() { - return 1000; - } + /** + * Logger. + */ + private static Logger logger = LoggerFactory.getLogger(HealthCheckFeature.class); - @Override - public boolean afterStart(PolicyEngine engine) { - try { - HealthCheck.monitor.start(); - } catch (IllegalStateException e) { - logger.error("Healthcheck Monitor cannot be started", e); - } - - return false; - } + /** + * Properties Configuration Name. + */ + public static final String CONFIGURATION_PROPERTIES_NAME = "feature-healthcheck"; - @Override - public boolean afterShutdown(PolicyEngine engine) { - try { - HealthCheck.monitor.stop(); - } catch (IllegalStateException e) { - logger.error("Healthcheck Monitor cannot be stopped", e); - } - - return false; - } - - /** - * gets the monitor - * @return the healthcheck monitor - */ - public HealthCheck getMonitor() { - return HealthCheck.monitor; - } + @Override + public int getSequenceNumber() { + return 1000; + } + + @Override + public boolean afterStart(PolicyEngine engine) { + try { + HealthCheck.monitor.start(); + } catch (IllegalStateException e) { + logger.error("Healthcheck Monitor cannot be started", e); + } + + return false; + } + + @Override + public boolean afterShutdown(PolicyEngine engine) { + try { + HealthCheck.monitor.stop(); + } catch (IllegalStateException e) { + logger.error("Healthcheck Monitor cannot be stopped", e); + } + + return false; + } + + /** + * Gets the monitor. + * + * @return the healthcheck monitor + */ + public HealthCheck getMonitor() { + return HealthCheck.monitor; + } } diff --git a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/RestHealthCheck.java b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/RestHealthCheck.java index 3f51ba46..734a75e0 100644 --- a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/RestHealthCheck.java +++ b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/RestHealthCheck.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-healthcheck * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,15 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.drools.healthcheck; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.Info; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; + import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @@ -27,12 +34,6 @@ import javax.ws.rs.core.Response; import org.onap.policy.drools.healthcheck.HealthCheck.Reports; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.Info; -import io.swagger.annotations.SwaggerDefinition; -import io.swagger.annotations.Tag; - @Path("/") @Api @Produces(MediaType.APPLICATION_JSON) @@ -50,28 +51,29 @@ import io.swagger.annotations.Tag; } ) public class RestHealthCheck { - + @GET @Path("healthcheck") @Produces(MediaType.APPLICATION_JSON) @ApiOperation( - value="Perform a system healthcheck", - notes="Provides healthy status of the PDP-D plus the components defined in its configuration by using a REST interface", - response=Reports.class - ) + value = "Perform a system healthcheck", + notes = "Provides healthy status of the PDP-D plus the components defined in its " + + "configuration by using a REST interface", + response = Reports.class + ) public Response healthcheck() { - return Response.status(Response.Status.OK).entity(HealthCheck.monitor.healthCheck()).build(); + return Response.status(Response.Status.OK).entity(HealthCheck.monitor.healthCheck()).build(); } - + @GET @Path("healthcheck/configuration") @Produces(MediaType.APPLICATION_JSON) @ApiOperation( - value="Configuration", - notes="Provides the Healthcheck server configuration and monitored REST clients", - response=HealthCheck.class - ) + value = "Configuration", + notes = "Provides the Healthcheck server configuration and monitored REST clients", + response = HealthCheck.class + ) public HealthCheck configuration() { - return HealthCheck.monitor; + return HealthCheck.monitor; } } diff --git a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java index 578ce5d5..8ae73435 100644 --- a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java +++ b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-healthcheck * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ package org.onap.policy.drools.healthcheck; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -45,7 +44,7 @@ import org.slf4j.LoggerFactory; public class HealthCheckFeatureTest { /** - * Healthcheck Configuration File + * Healthcheck Configuration File. */ private static final String HEALTH_CHECK_PROPERTIES_FILE = "feature-healthcheck.properties"; @@ -57,13 +56,15 @@ public class HealthCheckFeatureTest { /** - * logger + * logger. */ private static Logger logger = LoggerFactory.getLogger(HealthCheckFeatureTest.class); private static Properties httpProperties = new Properties(); - + /** + * Set up. + */ @BeforeClass public static void setup() { @@ -105,6 +106,9 @@ public class HealthCheckFeatureTest { } + /** + * Tear down. + */ @AfterClass public static void tearDown() { logger.info("-- tearDown() --"); @@ -135,7 +139,7 @@ public class HealthCheckFeatureTest { /** - * setup up config directory + * setup up config directory. */ protected static void configDirSetup() { @@ -161,7 +165,7 @@ public class HealthCheckFeatureTest { } /** - * cleanup up config directory + * cleanup up config directory. */ protected static void configDirCleanup() { diff --git a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/RestMockHealthCheck.java b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/RestMockHealthCheck.java index 5b3b82b3..8bdad5b6 100644 --- a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/RestMockHealthCheck.java +++ b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/RestMockHealthCheck.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-healthcheck * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ package org.onap.policy.drools.healthcheck; - import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/DmaapManager.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/DmaapManager.java index 278e7fdc..27b81504 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/DmaapManager.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/DmaapManager.java @@ -259,6 +259,8 @@ public class DmaapManager { public static class Factory { /** + * Get topic source. + * * @return the topic sources */ public List<TopicSource> getTopicSources() { @@ -266,6 +268,8 @@ public class DmaapManager { } /** + * Get topic sinks. + * * @return the topic sinks */ public List<TopicSink> getTopicSinks() { diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingFeature.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingFeature.java index ad6a1c56..d45bded7 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingFeature.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingFeature.java @@ -46,7 +46,9 @@ import org.slf4j.LoggerFactory; /** * Controller/session pooling. Multiple hosts may be launched, all servicing the same * controllers/sessions. When this feature is enabled, the requests are divided across the different - * hosts, instead of all running on a single, active host. <p> With each controller, there is an + * hosts, instead of all running on a single, active host. + * + * <p>With each controller, there is an * associated DMaaP topic that is used for internal communication between the different hosts * serving the controller. */ @@ -87,7 +89,7 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF private ThreadLocal<OfferArgs> offerArgs = new ThreadLocal<>(); /** - * + * Constructor. */ public PoolingFeature() { super(); @@ -113,6 +115,8 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF } /** + * Get active latch. + * * @return a latch that will be decremented when a manager enters the active state */ protected CountDownLatch getActiveLatch() { @@ -126,7 +130,7 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF @Override public boolean beforeStart(PolicyEngine engine) { - logger.info("initializing " + PoolingProperties.FEATURE_NAME); + logger.info("initializing {}", PoolingProperties.FEATURE_NAME); featProps = factory.getProperties(PoolingProperties.FEATURE_NAME); // remove any generic pooling topic - always use controller-specific property @@ -138,6 +142,14 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF return false; } + @Override + public boolean beforeStart(PolicyController controller) { + return doManager(controller, mgr -> { + mgr.beforeStart(); + return false; + }); + } + /** * Adds the controller and a new pooling manager to {@link #ctlr2pool}. * @@ -177,14 +189,6 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF } @Override - public boolean beforeStart(PolicyController controller) { - return doManager(controller, mgr -> { - mgr.beforeStart(); - return false; - }); - } - - @Override public boolean afterStart(PolicyController controller) { return doManager(controller, mgr -> { mgr.afterStart(); @@ -307,7 +311,7 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF * Executes a function using the manager associated with the controller. Catches any exceptions * from the function and re-throws it as a runtime exception. * - * @param controller + * @param controller controller * @param func function to be executed * @return {@code true} if the function handled the request, {@code false} otherwise * @throws PoolingFeatureRtException if an error occurs @@ -329,7 +333,7 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF /** * Deletes the manager associated with a controller. * - * @param controller + * @param controller controller * @throws PoolingFeatureRtException if an error occurs */ private void deleteManager(PolicyController controller) { @@ -347,10 +351,11 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF private static interface MgrFunc { /** + * Apply. * - * @param mgr + * @param mgr manager * @return {@code true} if the request was handled by the manager, {@code false} otherwise - * @throws PoolingFeatureException + * @throws PoolingFeatureException feature exception */ public boolean apply(PoolingManagerImpl mgr) throws PoolingFeatureException; } @@ -376,9 +381,10 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF private String event; /** + * Constructor. * - * @param protocol - * @param topic + * @param protocol protocol + * @param topic topic * @param event the actual event data received on the topic */ public OfferArgs(CommInfrastructure protocol, String topic, String event) { @@ -394,6 +400,8 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF public static class Factory { /** + * Get properties. + * * @param featName feature name * @return the properties for the specified feature */ @@ -405,7 +413,7 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF * Makes a pooling manager for a controller. * * @param host name/uuid of this host - * @param controller + * @param controller controller * @param props properties to use to configure the manager * @param activeLatch decremented when the manager goes Active * @return a new pooling manager @@ -418,7 +426,7 @@ public class PoolingFeature implements PolicyEngineFeatureAPI, PolicyControllerF /** * Gets the policy controller associated with a drools controller. * - * @param droolsController + * @param droolsController drools controller * @return the policy controller associated with a drools controller */ public PolicyController getController(DroolsController droolsController) { diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingManager.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingManager.java index c25dc12d..94956d6b 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingManager.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingManager.java @@ -86,7 +86,7 @@ public interface PoolingManager { /** * Handles a {@link Forward} event that was received from the internal topic. * - * @param event + * @param event event */ public void handle(Forward event); @@ -94,7 +94,7 @@ public interface PoolingManager { * Schedules a timer to fire after a delay. * * @param delayMs delay, in milliseconds - * @param task + * @param task task * @return a new scheduled task */ public CancellableScheduledTask schedule(long delayMs, StateTimerTask task); @@ -104,7 +104,7 @@ public interface PoolingManager { * * @param initialDelayMs initial delay, in milliseconds * @param delayMs delay, in milliseconds - * @param task + * @param task task * @return a new scheduled task */ public CancellableScheduledTask scheduleWithFixedDelay(long initialDelayMs, long delayMs, StateTimerTask task); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingManagerImpl.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingManagerImpl.java index 02ba4ec9..17d520ad 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingManagerImpl.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingManagerImpl.java @@ -20,6 +20,7 @@ package org.onap.policy.drools.pooling; +import com.fasterxml.jackson.core.JsonProcessingException; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -29,10 +30,10 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import org.onap.policy.common.utils.properties.SpecProperties; -import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicListener; +import org.onap.policy.common.utils.properties.SpecProperties; +import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.drools.pooling.extractor.ClassExtractors; import org.onap.policy.drools.pooling.message.BucketAssignments; import org.onap.policy.drools.pooling.message.Forward; @@ -50,7 +51,6 @@ import org.onap.policy.drools.protocol.coders.EventProtocolCoder; import org.onap.policy.drools.system.PolicyController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonProcessingException; /** * Implementation of a {@link PoolingManager}. Until bucket assignments have been made, @@ -126,8 +126,8 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Current state. - * <p> - * This uses a finite state machine, wherein the state object contains all of the data + * + * <p>This uses a finite state machine, wherein the state object contains all of the data * relevant to that state. Each state object has a process() method, specific to each * type of {@link Message} subclass. The method returns the next state object, or * {@code null} if the state is to remain the same. @@ -417,10 +417,9 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Handles an event from the internal topic. * - * @param topic2 - * @param event - * @return {@code true} if the event was handled, {@code false} if the controller - * should handle it + * @param commType comm infrastructure + * @param topic2 topic + * @param event event */ @Override public void onTopicEvent(CommInfrastructure commType, String topic2, String event) { @@ -440,14 +439,14 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { * Called by the PolicyController before it offers the event to the DroolsController. * If the controller is locked, then it isn't processing events. However, they still * need to be forwarded, thus in that case, they are decoded and forwarded. - * <p> - * On the other hand, if the controller is not locked, then we just return immediately + * + * <p>On the other hand, if the controller is not locked, then we just return immediately * and let {@link #beforeInsert(Object, String, String, Object) beforeInsert()} handle * it instead, as it already has the decoded message. * - * @param protocol - * @param topic2 - * @param event + * @param protocol protocol + * @param topic2 topic + * @param event event * @return {@code true} if the event was handled by the manager, {@code false} if it * must still be handled by the invoker */ @@ -464,8 +463,8 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Called by the DroolsController before it inserts the event into the rule engine. * - * @param protocol - * @param topic2 + * @param protocol protocol + * @param topic2 topic * @param event original event text, as received from the Bus * @param event2 event, as an object * @return {@code true} if the event was handled by the manager, {@code false} if it @@ -484,9 +483,9 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Handles an event from an external topic. * - * @param protocol - * @param topic2 - * @param event + * @param protocol protocol + * @param topic2 topic + * @param event event * @param reqid request id extracted from the event, or {@code null} if it couldn't be * extracted * @return {@code true} if the event was handled by the manager, {@code false} if it @@ -519,7 +518,7 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Handles an event from an external topic. * - * @param event + * @param event event * @return {@code true} if the event was handled, {@code false} if the invoker should * handle it */ @@ -539,7 +538,7 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Handles a {@link Forward} event, possibly forwarding it again. * - * @param event + * @param event event * @return {@code true} if the event was handled, {@code false} if the invoker should * handle it */ @@ -595,8 +594,8 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Decodes an event from a String into an event Object. * - * @param topic2 - * @param event + * @param topic2 topic + * @param event event * @return the decoded event object, or {@code null} if it can't be decoded */ private Object decodeEvent(String topic2, String event) { @@ -625,10 +624,10 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Makes a {@link Forward}, and validates its contents. * - * @param protocol - * @param topic2 - * @param event - * @param reqid + * @param protocol protocol + * @param topic2 topic + * @param event event + * @param reqid request id * @return a new message, or {@code null} if the message was invalid */ private Forward makeForward(CommInfrastructure protocol, String topic2, String event, String reqid) { @@ -661,7 +660,7 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { /** * Injects an event into the controller. * - * @param event + * @param event event */ private void inject(Forward event) { logger.info("inject event for request {} from topic {}", event.getRequestId(), event.getTopic()); @@ -760,6 +759,7 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { private StateTimerTask task; /** + * Constructor. * * @param task task to execute when this timer runs */ @@ -832,9 +832,9 @@ public class PoolingManagerImpl implements PoolingManager, TopicListener { * @param topic topic on which the event was received * @param event event text to be decoded * @return the decoded event - * @throws IllegalArgumentException - * @throw UnsupportedOperationException - * @throws IllegalStateException + * @throws IllegalArgumentException illegal argument + * @throw UnsupportedOperationException unsupported operation + * @throws IllegalStateException illegal state */ public Object decodeEvent(DroolsController drools, String topic, String event) { return EventProtocolCoder.manager.decode(drools.getGroupId(), drools.getArtifactId(), topic, event); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingProperties.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingProperties.java index 1482366f..795bd29d 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingProperties.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/PoolingProperties.java @@ -129,6 +129,8 @@ public class PoolingProperties extends PropertyConfiguration { private long interHeartbeatMs; /** + * Constructor. + * * @param controllerName the name of the controller * @param props set of properties used to configure this * @throws PropertyException if an error occurs diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/Serializer.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/Serializer.java index 63aefb7a..b37c33b0 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/Serializer.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/Serializer.java @@ -20,11 +20,11 @@ package org.onap.policy.drools.pooling; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.util.Map; import org.onap.policy.drools.pooling.message.Message; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; /** * Serialization helper functions. @@ -38,7 +38,7 @@ public class Serializer { private final ObjectMapper mapper = new ObjectMapper(); /** - * + * Constructor. */ public Serializer() { super(); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/ClassExtractors.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/ClassExtractors.java index 97e96337..a4d1fb1b 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/ClassExtractors.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/ClassExtractors.java @@ -70,6 +70,7 @@ public class ClassExtractors { private final ConcurrentHashMap<String, Extractor> class2extractor = new ConcurrentHashMap<>(); /** + * Constructor. * * @param props properties that specify how the data is to be extracted from * a given class @@ -267,8 +268,8 @@ public class ClassExtractors { * hierarchically, where each name identifies a particular component within * the hierarchy. Supports retrieval from {@link Map} objects, as well as * via getXxx() methods, or by direct field retrieval. - * <p> - * Note: this will <i>not</i> work if POJOs are contained within a Map. + * + * <p>Note: this will <i>not</i> work if POJOs are contained within a Map. */ private class ComponetizedExtractor implements Extractor { @@ -278,11 +279,12 @@ public class ClassExtractors { private final Extractor[] extractors; /** + * Constructor. * * @param clazz the class associated with the object at the root of the * hierarchy * @param names name associated with each component - * @throws ExtractorException + * @throws ExtractorException extractor exception */ public ComponetizedExtractor(Class<?> clazz, String[] names) throws ExtractorException { this.extractors = new Extractor[names.length]; @@ -307,15 +309,12 @@ public class ClassExtractors { * @param comp name of the component to extract * @return a pair containing the extractor and the extracted object's * type - * @throws ExtractorException + * @throws ExtractorException extrator exception */ private Pair<Extractor, Class<?>> buildExtractor(Class<?> clazz, String comp) throws ExtractorException { - Pair<Extractor, Class<?>> pair = null; - - if (pair == null) { - pair = getMethodExtractor(clazz, comp); - } - + + Pair<Extractor, Class<?>> pair = getMethodExtractor(clazz, comp); + if (pair == null) { pair = getFieldExtractor(clazz, comp); } diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/FieldExtractor.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/FieldExtractor.java index d394795d..9389ab22 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/FieldExtractor.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/FieldExtractor.java @@ -37,6 +37,7 @@ public class FieldExtractor implements Extractor { private final Field field; /** + * Constructor. * * @param field field containing the object */ diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/MapExtractor.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/MapExtractor.java index 032ea47e..9c5be5ff 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/MapExtractor.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/MapExtractor.java @@ -37,6 +37,7 @@ public class MapExtractor implements Extractor { private final String key; /** + * Constructor. * * @param key key to the item to extract from the map */ diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/MethodExtractor.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/MethodExtractor.java index 20c4a1a7..3efef5ec 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/MethodExtractor.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/extractor/MethodExtractor.java @@ -38,6 +38,7 @@ public class MethodExtractor implements Extractor { private final Method method; /** + * Constructor. * * @param method method to invoke to extract the contained object */ diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/BucketAssignments.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/BucketAssignments.java index ee871cbd..b5b64693 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/BucketAssignments.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/BucketAssignments.java @@ -20,13 +20,13 @@ package org.onap.policy.drools.pooling.message; +import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import org.onap.policy.drools.pooling.PoolingFeatureException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.annotation.JsonIgnore; /** * Bucket assignments, which is simply an array of host names. @@ -57,13 +57,14 @@ public class BucketAssignments { private String[] hostArray = null; /** - * + * Constructor. */ public BucketAssignments() { super(); } /** + * Constructor. * * @param hostArray maps a bucket number (i.e., array index) to a host. All values * must be non-null @@ -204,12 +205,15 @@ public class BucketAssignments { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } BucketAssignments other = (BucketAssignments) obj; return Arrays.equals(hostArray, other.hostArray); } diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Forward.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Forward.java index fb3d4eb2..d4037e90 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Forward.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Forward.java @@ -20,9 +20,9 @@ package org.onap.policy.drools.pooling.message; +import com.fasterxml.jackson.annotation.JsonIgnore; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.drools.pooling.PoolingFeatureException; -import com.fasterxml.jackson.annotation.JsonIgnore; /** * Message to forward an event to another host. @@ -67,12 +67,13 @@ public class Forward extends Message { } /** + * Constructor. * * @param source host on which the message originated - * @param protocol - * @param topic + * @param protocol protocol + * @param topic topic * @param payload the actual event data received on the topic - * @param requestId + * @param requestId request id */ public Forward(String source, CommInfrastructure protocol, String topic, String payload, String requestId) { super(source); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Heartbeat.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Heartbeat.java index 2a63a5be..50a34138 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Heartbeat.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Heartbeat.java @@ -31,7 +31,7 @@ public class Heartbeat extends Message { private long timestampMs; /** - * + * Constructor. */ public Heartbeat() { super(); @@ -39,6 +39,7 @@ public class Heartbeat extends Message { } /** + * Constructor. * * @param source host on which the message originated * @param timestampMs time, in milliseconds, associated with the message diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Identification.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Identification.java index 5de6b8f9..6bb88363 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Identification.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Identification.java @@ -26,7 +26,7 @@ package org.onap.policy.drools.pooling.message; public class Identification extends MessageWithAssignments { /** - * + * Constructor. */ public Identification() { super(); @@ -34,9 +34,10 @@ public class Identification extends MessageWithAssignments { } /** + * Constructor. * * @param source host on which the message originated - * @param assignments + * @param assignments assignments */ public Identification(String source, BucketAssignments assignments) { super(source, assignments); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Leader.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Leader.java index 0fc48c3c..7464a531 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Leader.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Leader.java @@ -20,8 +20,8 @@ package org.onap.policy.drools.pooling.message; -import org.onap.policy.drools.pooling.PoolingFeatureException; import com.fasterxml.jackson.annotation.JsonIgnore; +import org.onap.policy.drools.pooling.PoolingFeatureException; /** * Indicates that the "source" of this message is now the "lead" host. @@ -29,16 +29,17 @@ import com.fasterxml.jackson.annotation.JsonIgnore; public class Leader extends MessageWithAssignments { /** - * + * Constructor. */ public Leader() { super(); } /** + * Constructor. * * @param source host on which the message originated - * @param assignments + * @param assignments assignments */ public Leader(String source, BucketAssignments assignments) { super(source, assignments); @@ -61,7 +62,7 @@ public class Leader extends MessageWithAssignments { String leader = getSource(); - if(!assignments.hasAssignment(leader)) { + if (!assignments.hasAssignment(leader)) { throw new PoolingFeatureException("leader " + leader + " has no bucket assignments"); } diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Message.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Message.java index e8a4671d..215cdaec 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Message.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Message.java @@ -20,11 +20,11 @@ package org.onap.policy.drools.pooling.message; -import org.onap.policy.drools.pooling.PoolingFeatureException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonSubTypes.Type; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import org.onap.policy.drools.pooling.PoolingFeatureException; /** * Messages sent on the internal topic. @@ -53,13 +53,14 @@ public class Message { private String channel; /** - * + * Constructor. */ public Message() { super(); } /** + * Constructor. * * @param source host on which the message originated */ diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/MessageWithAssignments.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/MessageWithAssignments.java index 9fded815..4a0b8658 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/MessageWithAssignments.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/MessageWithAssignments.java @@ -20,8 +20,8 @@ package org.onap.policy.drools.pooling.message; -import org.onap.policy.drools.pooling.PoolingFeatureException; import com.fasterxml.jackson.annotation.JsonIgnore; +import org.onap.policy.drools.pooling.PoolingFeatureException; /** * A Message that includes bucket assignments. @@ -34,16 +34,17 @@ public class MessageWithAssignments extends Message { private BucketAssignments assignments; /** - * + * Constructor. */ public MessageWithAssignments() { super(); } /** + * Constructor. * * @param source host on which the message originated - * @param assignments + * @param assignments assignements */ public MessageWithAssignments(String source, BucketAssignments assignments) { super(source); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Offline.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Offline.java index 297671ac..487c4f3e 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Offline.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Offline.java @@ -27,7 +27,7 @@ package org.onap.policy.drools.pooling.message; public class Offline extends Message { /** - * + * Constructor. */ public Offline() { super(); @@ -35,6 +35,7 @@ public class Offline extends Message { } /** + * Constructor. * * @param source host on which the message originated */ diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Query.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Query.java index c995a288..8c9898ee 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Query.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/message/Query.java @@ -26,7 +26,7 @@ package org.onap.policy.drools.pooling.message; public class Query extends Message { /** - * + * Constructor. */ public Query() { super(); @@ -34,6 +34,7 @@ public class Query extends Message { } /** + * Constructor. * * @param source host on which the message originated */ diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/ActiveState.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/ActiveState.java index 8f0a902a..58205ddd 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/ActiveState.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/ActiveState.java @@ -66,8 +66,9 @@ public class ActiveState extends ProcessingState { private boolean predHeartbeatSeen = false; /** + * Constructor. * - * @param mgr + * @param mgr pooling manager */ public ActiveState(PoolingManager mgr) { super(mgr, mgr.getAssignments().getLeader()); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/FilterUtils.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/FilterUtils.java index a2da0ea2..069ca656 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/FilterUtils.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/FilterUtils.java @@ -44,7 +44,7 @@ public class FilterUtils { protected static final String CLASS_EQUALS = "Equals"; /** - * + * Constructor. */ private FilterUtils() { super(); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/InactiveState.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/InactiveState.java index f717aa52..579dc16d 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/InactiveState.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/InactiveState.java @@ -35,8 +35,9 @@ public class InactiveState extends State { private static final Logger logger = LoggerFactory.getLogger(InactiveState.class); /** + * Constructor. * - * @param mgr + * @param mgr pooling manager */ public InactiveState(PoolingManager mgr) { super(mgr); diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/ProcessingState.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/ProcessingState.java index e9dc0324..7fed6a15 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/ProcessingState.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/ProcessingState.java @@ -48,8 +48,9 @@ public class ProcessingState extends State { private String leader; /** + * Constructor. * - * @param mgr + * @param mgr pooling manager * @param leader current known leader, which need not be the same as the assignment * leader. Never {@code null} * @throws IllegalArgumentException if an argument is invalid @@ -308,10 +309,10 @@ public class ProcessingState extends State { } // move the bucket from the larger to the smaller - Integer b = larger.remove(); - smaller.add(b); + Integer bucket = larger.remove(); + smaller.add(bucket); - bucket2host[b] = smaller.host; + bucket2host[bucket] = smaller.host; // put the items back, with their new counts assignments.add(larger); @@ -349,8 +350,9 @@ public class ProcessingState extends State { private Queue<Integer> buckets = new LinkedList<>(); /** + * Constructor. * - * @param host + * @param host host */ public HostBucket(String host) { this.host = host; @@ -375,6 +377,7 @@ public class ProcessingState extends State { } /** + * Size. * * @return the number of buckets assigned to this host */ @@ -388,11 +391,11 @@ public class ProcessingState extends State { */ @Override public final int compareTo(HostBucket other) { - int d = buckets.size() - other.buckets.size(); - if (d == 0) { - d = host.compareTo(other.host); + int diff = buckets.size() - other.buckets.size(); + if (diff == 0) { + diff = host.compareTo(other.host); } - return d; + return diff; } @Override diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/QueryState.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/QueryState.java index 1a4da150..ea74f03a 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/QueryState.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/QueryState.java @@ -51,8 +51,9 @@ public class QueryState extends ProcessingState { private boolean sawSelfIdent = false; /** + * Constructor. * - * @param mgr + * @param mgr manager */ public QueryState(PoolingManager mgr) { // this host is the leader, until a better candidate identifies itself diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/StartState.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/StartState.java index 3068cfc9..59d264ec 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/StartState.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/StartState.java @@ -25,6 +25,7 @@ import static org.onap.policy.drools.pooling.state.FilterUtils.MSG_TIMESTAMP; import static org.onap.policy.drools.pooling.state.FilterUtils.makeAnd; import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals; import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr; + import java.util.Map; import org.onap.policy.drools.pooling.PoolingManager; import org.onap.policy.drools.pooling.message.Heartbeat; @@ -47,14 +48,16 @@ public class StartState extends State { private long hbTimestampMs = System.currentTimeMillis(); /** + * Constructor. * - * @param mgr + * @param mgr pooling manager */ public StartState(PoolingManager mgr) { super(mgr); } /** + * Get Heart beat time stamp in milliseconds. * * @return the time stamp inserted into the heart beat message */ diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java index a1be2a7c..edffb3fe 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java @@ -23,6 +23,7 @@ package org.onap.policy.drools.pooling.state; import static org.onap.policy.drools.pooling.state.FilterUtils.MSG_CHANNEL; import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals; import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr; + import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -42,8 +43,8 @@ import org.slf4j.LoggerFactory; /** * A state in the finite state machine. - * <p> - * A state may have several timers associated with it, which must be cancelled whenever + * + * <p>A state may have several timers associated with it, which must be cancelled whenever * the state is changed. Assumes that timers are not continuously added to the same state. */ public abstract class State { @@ -61,8 +62,9 @@ public abstract class State { private final List<CancellableScheduledTask> timers = new LinkedList<>(); /** + * Constructor. * - * @param mgr + * @param mgr pooling manager */ public State(PoolingManager mgr) { this.mgr = mgr; @@ -197,6 +199,28 @@ public abstract class State { } /** + * Processes a message. The default method just returns {@code null}. + * + * @param msg message to be processed + * @return the new state, or {@code null} if the state is unchanged + */ + public State process(Offline msg) { + logger.info("ignored offline message from {} on topic {}", msg.getSource(), getTopic()); + return null; + } + + /** + * Processes a message. The default method just returns {@code null}. + * + * @param msg message to be processed + * @return the new state, or {@code null} if the state is unchanged + */ + public State process(Query msg) { + logger.info("ignored Query message from {} on topic {}", msg.getSource(), getTopic()); + return null; + } + + /** * Determines if a message is valid and did not originate from this host. * * @param msg message to be validated @@ -227,28 +251,6 @@ public abstract class State { } /** - * Processes a message. The default method just returns {@code null}. - * - * @param msg message to be processed - * @return the new state, or {@code null} if the state is unchanged - */ - public State process(Offline msg) { - logger.info("ignored offline message from {} on topic {}", msg.getSource(), getTopic()); - return null; - } - - /** - * Processes a message. The default method just returns {@code null}. - * - * @param msg message to be processed - * @return the new state, or {@code null} if the state is unchanged - */ - public State process(Query msg) { - logger.info("ignored Query message from {} on topic {}", msg.getSource(), getTopic()); - return null; - } - - /** * Publishes a message. * * @param msg message to be published @@ -287,7 +289,7 @@ public abstract class State { /** * Publishes a message on the specified channel. * - * @param channel + * @param channel channel * @param msg message to be published */ protected final void publish(String channel, Forward msg) { @@ -297,7 +299,7 @@ public abstract class State { /** * Publishes a message on the specified channel. * - * @param channel + * @param channel channel * @param msg message to be published */ protected final void publish(String channel, Heartbeat msg) { @@ -307,7 +309,7 @@ public abstract class State { /** * Starts distributing messages using the specified bucket assignments. * - * @param assignments + * @param assignments assignments */ protected final void startDistributing(BucketAssignments assignments) { if (assignments != null) { @@ -318,8 +320,8 @@ public abstract class State { /** * Schedules a timer to fire after a delay. * - * @param delayMs - * @param task + * @param delayMs delay in ms + * @param task task */ protected final void schedule(long delayMs, StateTimerTask task) { timers.add(mgr.schedule(delayMs, task)); @@ -328,9 +330,9 @@ public abstract class State { /** * Schedules a timer to fire repeatedly. * - * @param initialDelayMs - * @param delayMs - * @param task + * @param initialDelayMs initial delay ms + * @param delayMs delay ms + * @param task task */ protected final void scheduleWithFixedDelay(long initialDelayMs, long delayMs, StateTimerTask task) { timers.add(mgr.scheduleWithFixedDelay(initialDelayMs, delayMs, task)); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/DmaapManagerTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/DmaapManagerTest.java index d48dea5b..e554a34a 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/DmaapManagerTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/DmaapManagerTest.java @@ -30,6 +30,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.Arrays; import java.util.LinkedList; import java.util.concurrent.CountDownLatch; @@ -70,6 +71,11 @@ public class DmaapManagerTest { DmaapManager.setFactory(saveFactory); } + /** + * Setup. + * + * @throws Exception throws an exception + */ @Before public void setUp() throws Exception { listener = mock(TopicListener.class); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest.java index 3c3466be..709f1b06 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest.java @@ -27,6 +27,9 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.onap.policy.drools.pooling.PoolingProperties.PREFIX; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.util.Arrays; import java.util.Deque; @@ -51,21 +54,19 @@ import org.junit.BeforeClass; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.common.endpoints.event.comm.FilterableTopicSource; import org.onap.policy.common.endpoints.event.comm.Topic; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicListener; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.common.endpoints.event.comm.TopicSource; +import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.drools.pooling.message.Message; import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.drools.utils.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; /** * End-to-end tests of the pooling feature. Launches one or more "hosts", each one having @@ -118,6 +119,10 @@ public class FeatureTest { */ private Context ctx; + /** + * Setup before class. + * + */ @BeforeClass public static void setUpBeforeClass() { saveFeatureFactory = PoolingFeature.getFactory(); @@ -127,6 +132,9 @@ public class FeatureTest { // note: invoke runSlow() to slow things down } + /** + * Tear down after class. + */ @AfterClass public static void tearDownAfterClass() { PoolingFeature.setFactory(saveFeatureFactory); @@ -134,11 +142,17 @@ public class FeatureTest { DmaapManager.setFactory(saveDmaapFactory); } + /** + * Setup. + */ @Before public void setUp() { ctx = null; } + /** + * Tear down. + */ @After public void tearDown() { if (ctx != null) { @@ -189,15 +203,15 @@ public class FeatureTest { * Invoke this to slow the timers down. */ protected static void runSlow() { - stdReactivateWaitMs = 10000; - stdIdentificationMs = 10000; - stdStartHeartbeatMs = 15000; - stdActiveHeartbeatMs = 12000; - stdInterHeartbeatMs = 5000; - stdOfflinePubWaitMs = 2; - stdPollMs = 2; - stdInterPollMs = 2000; - stdEventWaitSec = 1000; + stdReactivateWaitMs = 10000; + stdIdentificationMs = 10000; + stdStartHeartbeatMs = 15000; + stdActiveHeartbeatMs = 12000; + stdInterHeartbeatMs = 5000; + stdOfflinePubWaitMs = 2; + stdPollMs = 2; + stdInterPollMs = 2000; + stdEventWaitSec = 1000; } /** @@ -232,7 +246,7 @@ public class FeatureTest { /** * Counts the number of decode errors. */ - private final AtomicInteger nDecodeErrors = new AtomicInteger(0); + private final AtomicInteger numDecodeErrors = new AtomicInteger(0); /** * Number of events we're still waiting to receive. @@ -246,14 +260,15 @@ public class FeatureTest { private Host currentHost = null; /** + * Constructor. * * @param nEvents number of events to be processed */ - public Context(int nEvents) { + public Context(int events) { featureFactory = new FeatureFactory(this); managerFactory = new ManagerFactory(this); dmaapFactory = new DmaapFactory(this); - eventCounter = new CountDownLatch(nEvents); + eventCounter = new CountDownLatch(events); PoolingFeature.setFactory(featureFactory); PoolingManagerImpl.setFactory(managerFactory); @@ -298,10 +313,10 @@ public class FeatureTest { * Verifies that all hosts processed at least one message. */ public void checkAllSawAMsg() { - int x = 0; + int msgs = 0; for (Host host : hosts) { - assertTrue("x=" + x, host.messageSeen()); - ++x; + assertTrue("msgs=" + msgs, host.messageSeen()); + ++msgs; } } @@ -309,7 +324,7 @@ public class FeatureTest { * Sets {@link #currentHost} to the specified host, and then invokes the given * function. Resets {@link #currentHost} to {@code null} before returning. * - * @param host + * @param host host * @param func function to invoke */ public void withHost(Host host, VoidFunction func) { @@ -321,7 +336,7 @@ public class FeatureTest { /** * Offers an event to the external topic. * - * @param event + * @param event event */ public void offerExternal(String event) { externalTopic.offer(event); @@ -330,7 +345,7 @@ public class FeatureTest { /** * Adds an internal channel to the set of channels. * - * @param channel + * @param channel channel * @param queue the channel's queue */ public void addInternal(String channel, BlockingQueue<String> queue) { @@ -340,7 +355,7 @@ public class FeatureTest { /** * Offers a message to all internal channels. * - * @param message + * @param message message */ public void offerInternal(String message) { channel2queue.values().forEach(queue -> queue.offer(message)); @@ -349,8 +364,8 @@ public class FeatureTest { /** * Offers amessage to an internal channel. * - * @param channel - * @param message + * @param channel channel + * @param message message */ public void offerInternal(String channel, String message) { BlockingQueue<String> queue = channel2queue.get(channel); @@ -362,7 +377,7 @@ public class FeatureTest { /** * Decodes an event. * - * @param event + * @param event event * @return the decoded event, or {@code null} if it cannot be decoded */ public Object decodeEvent(String event) { @@ -372,15 +387,17 @@ public class FeatureTest { /** * Associates a controller with its drools controller. * - * @param controller - * @param droolsController + * @param controller controller + * @param droolsController drools controller */ public void addController(PolicyController controller, DroolsController droolsController) { drools2policy.put(droolsController, controller); } /** - * @param droolsController + * Get controller. + * + * @param droolsController drools controller * @return the controller associated with a drools controller, or {@code null} if * it has no associated controller */ @@ -389,6 +406,8 @@ public class FeatureTest { } /** + * Constructor. + * * @return queue for the external topic */ public BlockingQueue<String> getExternalTopic() { @@ -396,21 +415,23 @@ public class FeatureTest { } /** + * Get decode errors. * * @return the number of decode errors so far */ public int getDecodeErrors() { - return nDecodeErrors.get(); + return numDecodeErrors.get(); } /** * Increments the count of decode errors. */ public void bumpDecodeErrors() { - nDecodeErrors.incrementAndGet(); + numDecodeErrors.incrementAndGet(); } /** + * Get remaining events. * * @return the number of events that haven't been processed */ @@ -428,10 +449,10 @@ public class FeatureTest { /** * Waits, for a period of time, for all events to be processed. * - * @param time - * @param units + * @param time time + * @param units units * @return {@code true} if all events have been processed, {@code false} otherwise - * @throws InterruptedException + * @throws InterruptedException throws interrupted */ public boolean awaitEvents(long time, TimeUnit units) throws InterruptedException { return eventCounter.await(time, units); @@ -478,8 +499,9 @@ public class FeatureTest { private final DroolsController drools = mock(DroolsController.class); /** + * Constructor. * - * @param context + * @param context context */ public Host(Context context) { this.context = context; @@ -502,6 +524,8 @@ public class FeatureTest { } /** + * Get name. + * * @return the host name */ public String getName() { @@ -543,9 +567,9 @@ public class FeatureTest { /** * Offers an event to the feature, before the policy controller handles it. * - * @param protocol - * @param topic2 - * @param event + * @param protocol protocol + * @param topic2 topic + * @param event event * @return {@code true} if the event was handled, {@code false} otherwise */ public boolean beforeOffer(CommInfrastructure protocol, String topic2, String event) { @@ -555,10 +579,10 @@ public class FeatureTest { /** * Offers an event to the feature, after the policy controller handles it. * - * @param protocol - * @param topic - * @param event - * @param success + * @param protocol protocol + * @param topic topic + * @param event event + * @param success success * @return {@code true} if the event was handled, {@code false} otherwise */ public boolean afterOffer(CommInfrastructure protocol, String topic, String event, boolean success) { @@ -569,7 +593,7 @@ public class FeatureTest { /** * Offers an event to the feature, before the drools controller handles it. * - * @param fact + * @param fact fact * @return {@code true} if the event was handled, {@code false} otherwise */ public boolean beforeInsert(Object fact) { @@ -579,7 +603,7 @@ public class FeatureTest { /** * Offers an event to the feature, after the drools controller handles it. * - * @param fact + * @param fact fact * @param successInsert {@code true} if it was successfully inserted by the drools * controller, {@code false} otherwise * @return {@code true} if the event was handled, {@code false} otherwise @@ -596,6 +620,7 @@ public class FeatureTest { } /** + * Message seen. * * @return {@code true} if a message was seen for this host, {@code false} * otherwise @@ -605,6 +630,8 @@ public class FeatureTest { } /** + * Get internal queue. + * * @return the queue associated with this host's internal topic */ public BlockingQueue<String> getInternalQueue() { @@ -628,10 +655,10 @@ public class FeatureTest { @Override public Void answer(InvocationOnMock args) throws Throwable { - int i = 0; - CommInfrastructure commType = args.getArgument(i++); - String topic = args.getArgument(i++); - String event = args.getArgument(i++); + int index = 0; + CommInfrastructure commType = args.getArgument(index++); + String topic = args.getArgument(index++); + String event = args.getArgument(index++); if (host.beforeOffer(commType, topic, event)) { return null; @@ -676,8 +703,9 @@ public class FeatureTest { private final Serializer serializer = new Serializer(); /** + * Constructor. * - * @param context + * @param context context */ public TopicSinkImpl(Context context) { this.context = context; @@ -732,8 +760,9 @@ public class FeatureTest { private AtomicReference<Pair<CountDownLatch, CountDownLatch>> pair = new AtomicReference<>(null); /** + * Constructor. * - * @param context + * @param context context * @param internal {@code true} if to read from the internal topic, {@code false} * to read from the external topic */ @@ -778,7 +807,8 @@ public class FeatureTest { try { do { processMessages(newPair.first(), listener); - } while (!newPair.first().await(stdInterPollMs, TimeUnit.MILLISECONDS)); + } + while (!newPair.first().await(stdInterPollMs, TimeUnit.MILLISECONDS)); logger.info("topic source thread completed"); @@ -850,8 +880,8 @@ public class FeatureTest { * Polls for messages from the topic and offers them to the listener. * * @param stopped triggered if processing should stop - * @param listener - * @throws InterruptedException + * @param listener listener + * @throws InterruptedException throws interrupted exception */ private void processMessages(CountDownLatch stopped, TopicListener listener) throws InterruptedException { @@ -874,7 +904,7 @@ public class FeatureTest { private static class TopicImpl implements Topic { /** - * + * Constructor. */ public TopicImpl() { super(); @@ -954,8 +984,9 @@ public class FeatureTest { private final Context context; /** + * Constructor. * - * @param context + * @param context context */ public FeatureFactory(Context context) { this.context = context; @@ -1029,8 +1060,9 @@ public class FeatureTest { private final TypeReference<TreeMap<String, String>> typeRef = new TypeReference<TreeMap<String, String>>() {}; /** + * Constructor. * - * @param context + * @param context context */ public ManagerFactory(Context context) { @@ -1065,8 +1097,9 @@ public class FeatureTest { private final Context context; /** + * Constructor. * - * @param context + * @param context context */ public DmaapFactory(Context context) { this.context = context; diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest2.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest2.java index 70bacb1b..050ca612 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest2.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest2.java @@ -70,7 +70,7 @@ import org.slf4j.LoggerFactory; * following are not: <dl> <dt>PolicyEngine, PolicyController, DroolsController</dt> <dd>mocked</dd> * </dl> * - * <p> The following fields must be set before executing this: <ul> <li>UEB_SERVERS</li> + * <p>The following fields must be set before executing this: <ul> <li>UEB_SERVERS</li> * <li>INTERNAL_TOPIC</li> <li>EXTERNAL_TOPIC</li> </ul> */ public class FeatureTest2 { @@ -135,7 +135,10 @@ public class FeatureTest2 { */ private Context ctx; - + /** + * Setup before class. + * + */ @BeforeClass public static void setUpBeforeClass() { saveFeatureFactory = PoolingFeature.getFactory(); @@ -149,6 +152,10 @@ public class FeatureTest2 { internalSink.start(); } + /** + * Tear down after class. + * + */ @AfterClass public static void tearDownAfterClass() { PoolingFeature.setFactory(saveFeatureFactory); @@ -159,11 +166,17 @@ public class FeatureTest2 { internalSink.stop(); } + /** + * Setup. + */ @Before public void setUp() { ctx = null; } + /** + * Tear down. + */ @After public void tearDown() { if (ctx != null) { @@ -275,7 +288,7 @@ public class FeatureTest2 { /** * Counts the number of decode errors. */ - private final AtomicInteger nDecodeErrors = new AtomicInteger(0); + private final AtomicInteger decodeErrors = new AtomicInteger(0); /** * Number of events we're still waiting to receive. @@ -283,13 +296,14 @@ public class FeatureTest2 { private final CountDownLatch eventCounter; /** + * Constructor. * * @param nEvents number of events to be processed */ - public Context(int nEvents) { + public Context(int events) { featureFactory = new FeatureFactory(this); managerFactory = new ManagerFactory(this); - eventCounter = new CountDownLatch(nEvents); + eventCounter = new CountDownLatch(events); PoolingFeature.setFactory(featureFactory); PoolingManagerImpl.setFactory(managerFactory); @@ -333,17 +347,17 @@ public class FeatureTest2 { * Verifies that all hosts processed at least one message. */ public void checkAllSawAMsg() { - int x = 0; + int msgs = 0; for (Host host : hosts) { - assertTrue("x=" + x, host.messageSeen()); - ++x; + assertTrue("msgs=" + msgs, host.messageSeen()); + ++msgs; } } /** * Offers an event to the external topic. * - * @param event + * @param event event */ public void offerExternal(String event) { externalSink.send(event); @@ -352,7 +366,7 @@ public class FeatureTest2 { /** * Decodes an event. * - * @param event + * @param event event * @return the decoded event, or {@code null} if it cannot be decoded */ public Object decodeEvent(String event) { @@ -362,15 +376,17 @@ public class FeatureTest2 { /** * Associates a controller with its drools controller. * - * @param controller - * @param droolsController + * @param controller controller + * @param droolsController drools controller */ public void addController(PolicyController controller, DroolsController droolsController) { drools2policy.put(droolsController, controller); } /** - * @param droolsController + * Get controller. + * + * @param droolsController drools controller * @return the controller associated with a drools controller, or {@code null} if it has no * associated controller */ @@ -379,21 +395,23 @@ public class FeatureTest2 { } /** + * Get decode errors. * * @return the number of decode errors so far */ public int getDecodeErrors() { - return nDecodeErrors.get(); + return decodeErrors.get(); } /** * Increments the count of decode errors. */ public void bumpDecodeErrors() { - nDecodeErrors.incrementAndGet(); + decodeErrors.incrementAndGet(); } /** + * Get remaining events. * * @return the number of events that haven't been processed */ @@ -411,10 +429,10 @@ public class FeatureTest2 { /** * Waits, for a period of time, for all events to be processed. * - * @param time - * @param units + * @param time time + * @param units units * @return {@code true} if all events have been processed, {@code false} otherwise - * @throws InterruptedException + * @throws InterruptedException throws interrupted exception */ public boolean awaitEvents(long time, TimeUnit units) throws InterruptedException { return eventCounter.await(time, units); @@ -424,7 +442,7 @@ public class FeatureTest2 { * Waits, for a period of time, for all hosts to enter the Active state. * * @param timeMs maximum time to wait, in milliseconds - * @throws InterruptedException + * @throws InterruptedException throws interrupted exception */ public void awaitAllActive(long timeMs) throws InterruptedException { long tend = timeMs + System.currentTimeMillis(); @@ -457,8 +475,9 @@ public class FeatureTest2 { private final DroolsController drools = mock(DroolsController.class); /** + * Constructor. * - * @param context + * @param context context */ public Host(Context context) { @@ -485,7 +504,7 @@ public class FeatureTest2 { * @param timeMs time to wait, in milliseconds * @return {@code true} if the host entered the Active state within the given amount of * time, {@code false} otherwise - * @throws InterruptedException + * @throws InterruptedException throws interrupted exception */ public boolean awaitActive(long timeMs) throws InterruptedException { return feature.getActiveLatch().await(timeMs, TimeUnit.MILLISECONDS); @@ -531,9 +550,9 @@ public class FeatureTest2 { /** * Offers an event to the feature, before the policy controller handles it. * - * @param protocol - * @param topic2 - * @param event + * @param protocol protocol + * @param topic2 topic + * @param event event * @return {@code true} if the event was handled, {@code false} otherwise */ public boolean beforeOffer(CommInfrastructure protocol, String topic2, String event) { @@ -543,10 +562,10 @@ public class FeatureTest2 { /** * Offers an event to the feature, after the policy controller handles it. * - * @param protocol - * @param topic - * @param event - * @param success + * @param protocol protocol + * @param topic topic + * @param event event + * @param success success * @return {@code true} if the event was handled, {@code false} otherwise */ public boolean afterOffer(CommInfrastructure protocol, String topic, String event, boolean success) { @@ -557,7 +576,7 @@ public class FeatureTest2 { /** * Offers an event to the feature, before the drools controller handles it. * - * @param fact + * @param fact fact * @return {@code true} if the event was handled, {@code false} otherwise */ public boolean beforeInsert(Object fact) { @@ -567,7 +586,7 @@ public class FeatureTest2 { /** * Offers an event to the feature, after the drools controller handles it. * - * @param fact + * @param fact fact * @param successInsert {@code true} if it was successfully inserted by the drools * controller, {@code false} otherwise * @return {@code true} if the event was handled, {@code false} otherwise @@ -584,6 +603,7 @@ public class FeatureTest2 { } /** + * Message seen. * * @return {@code true} if a message was seen for this host, {@code false} otherwise */ @@ -608,10 +628,10 @@ public class FeatureTest2 { @Override public Void answer(InvocationOnMock args) throws Throwable { - int i = 0; - CommInfrastructure commType = args.getArgument(i++); - String topic = args.getArgument(i++); - String event = args.getArgument(i++); + int index = 0; + CommInfrastructure commType = args.getArgument(index++); + String topic = args.getArgument(index++); + String event = args.getArgument(index++); if (host.beforeOffer(commType, topic, event)) { return null; @@ -649,8 +669,9 @@ public class FeatureTest2 { private final Context context; /** + * Constructor. * - * @param context + * @param context context */ public FeatureFactory(Context context) { this.context = context; @@ -727,8 +748,9 @@ public class FeatureTest2 { private final TypeReference<TreeMap<String, String>> typeRef = new TypeReference<TreeMap<String, String>>() {}; /** + * Constructor. * - * @param context + * @param context context */ public ManagerFactory(Context context) { diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureExceptionTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureExceptionTest.java index 34b604c9..731736a1 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureExceptionTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureExceptionTest.java @@ -20,7 +20,8 @@ package org.onap.policy.drools.pooling; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + import org.junit.Test; import org.onap.policy.common.utils.test.ExceptionsTester; diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureRtExceptionTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureRtExceptionTest.java index cbb24421..fcd23322 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureRtExceptionTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureRtExceptionTest.java @@ -20,7 +20,8 @@ package org.onap.policy.drools.pooling; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + import org.junit.Test; import org.onap.policy.common.utils.test.ExceptionsTester; import org.onap.policy.drools.pooling.PoolingFeatureRtException; diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureTest.java index c57a9f6f..07fc757b 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureTest.java @@ -31,6 +31,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.LinkedList; import java.util.List; import java.util.Properties; @@ -38,8 +39,8 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.drools.pooling.PoolingFeature.Factory; import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyEngine; @@ -95,6 +96,11 @@ public class PoolingFeatureTest { PoolingFeature.setFactory(saveFactory); } + /** + * Setup. + * + * @throws Exception exception + */ @Before public void setUp() throws Exception { props = initProperties(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java index d90bac4b..e6b6e4cd 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java @@ -34,6 +34,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.LinkedList; import java.util.Properties; import java.util.Queue; @@ -46,9 +47,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.ArgumentCaptor; -import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicListener; +import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.drools.pooling.PoolingManagerImpl.Factory; import org.onap.policy.drools.pooling.extractor.ClassExtractors; import org.onap.policy.drools.pooling.message.BucketAssignments; @@ -125,6 +126,11 @@ public class PoolingManagerImplTest { PoolingManagerImpl.setFactory(saveFactory); } + /** + * Setup. + * + * @throws Exception throws exception + */ @Before public void setUp() throws Exception { plainProps = new Properties(); @@ -202,7 +208,7 @@ public class PoolingManagerImplTest { PolicyController ctlr = mock(PolicyController.class); PoolingFeatureRtException ex = expectException(PoolingFeatureRtException.class, - () -> new PoolingManagerImpl(MY_HOST, ctlr, poolProps, active)); + () -> new PoolingManagerImpl(MY_HOST, ctlr, poolProps, active)); assertNotNull(ex.getCause()); assertTrue(ex.getCause() instanceof ClassCastException); } @@ -214,7 +220,7 @@ public class PoolingManagerImplTest { when(factory.makeDmaapManager(any())).thenThrow(ex); PoolingFeatureRtException ex2 = expectException(PoolingFeatureRtException.class, - () -> new PoolingManagerImpl(MY_HOST, controller, poolProps, active)); + () -> new PoolingManagerImpl(MY_HOST, controller, poolProps, active)); assertEquals(ex, ex2.getCause()); } @@ -304,26 +310,26 @@ public class PoolingManagerImplTest { Forward msg = new Forward(mgr.getHost(), CommInfrastructure.UEB, TOPIC2, THE_EVENT, REQUEST_ID); mgr.handle(msg); - verify(dmaap, times(START_PUB+1)).publish(any()); + verify(dmaap, times(START_PUB + 1)).publish(any()); mgr.beforeStop(); verify(dmaap).stopConsumer(mgr); verify(sched).shutdownNow(); - verify(dmaap, times(START_PUB+2)).publish(any()); + verify(dmaap, times(START_PUB + 2)).publish(any()); verify(dmaap).publish(contains("offline")); assertTrue(mgr.getCurrent() instanceof IdleState); // verify that next message is handled locally mgr.handle(msg); - verify(dmaap, times(START_PUB+2)).publish(any()); + verify(dmaap, times(START_PUB + 2)).publish(any()); verify(controller).onTopicEvent(CommInfrastructure.UEB, TOPIC2, THE_EVENT); } @Test public void testBeforeStop_NotRunning() throws Exception { - State st = mgr.getCurrent(); + final State st = mgr.getCurrent(); mgr.beforeStop(); @@ -339,7 +345,7 @@ public class PoolingManagerImplTest { // call beforeStart but not afterStart mgr.beforeStart(); - State st = mgr.getCurrent(); + final State st = mgr.getCurrent(); mgr.beforeStop(); @@ -624,7 +630,7 @@ public class PoolingManagerImplTest { // route the message to this host mgr.startDistributing(makeAssignments(true)); - CountDownLatch latch = catchRecursion(false); + final CountDownLatch latch = catchRecursion(false); Forward msg = new Forward(mgr.getHost(), CommInfrastructure.UEB, TOPIC2, THE_EVENT, REQUEST_ID); mgr.handle(msg); @@ -644,7 +650,7 @@ public class PoolingManagerImplTest { // route the message to this host mgr.startDistributing(makeAssignments(true)); - CountDownLatch latch = catchRecursion(true); + final CountDownLatch latch = catchRecursion(true); Forward msg = new Forward(mgr.getHost(), CommInfrastructure.UEB, TOPIC2, THE_EVENT, REQUEST_ID); mgr.handle(msg); @@ -870,7 +876,7 @@ public class PoolingManagerImplTest { assertTrue(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); - verify(dmaap, times(START_PUB+1)).publish(any()); + verify(dmaap, times(START_PUB + 1)).publish(any()); } @Test @@ -921,7 +927,7 @@ public class PoolingManagerImplTest { // route the message to this host mgr.startDistributing(makeAssignments(true)); - CountDownLatch latch = catchRecursion(true); + final CountDownLatch latch = catchRecursion(true); Forward msg = new Forward(mgr.getHost(), CommInfrastructure.UEB, TOPIC2, THE_EVENT, REQUEST_ID); mgr.handle(msg); @@ -943,7 +949,7 @@ public class PoolingManagerImplTest { // generate RuntimeException when onTopicEvent() is invoked doThrow(new IllegalArgumentException("expected")).when(controller).onTopicEvent(any(), any(), any()); - CountDownLatch latch = catchRecursion(true); + final CountDownLatch latch = catchRecursion(true); Forward msg = new Forward(mgr.getHost(), CommInfrastructure.UEB, TOPIC2, THE_EVENT, REQUEST_ID); mgr.handle(msg); @@ -975,7 +981,7 @@ public class PoolingManagerImplTest { } @Test - public void testHandleInternal_IOEx() throws Exception { + public void testHandleInternal_IoEx() throws Exception { startMgr(); mgr.onTopicEvent(CommInfrastructure.UEB, MY_TOPIC, "invalid message"); @@ -1028,7 +1034,7 @@ public class PoolingManagerImplTest { // route the message to the other host mgr.startDistributing(makeAssignments(false)); assertTrue(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); - verify(dmaap, times(START_PUB+1)).publish(any()); + verify(dmaap, times(START_PUB + 1)).publish(any()); } @Test diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingPropertiesTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingPropertiesTest.java index eac8bfb1..cc716a2a 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingPropertiesTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingPropertiesTest.java @@ -32,6 +32,7 @@ import static org.onap.policy.drools.pooling.PoolingProperties.POOLING_TOPIC; import static org.onap.policy.drools.pooling.PoolingProperties.PREFIX; import static org.onap.policy.drools.pooling.PoolingProperties.REACTIVATE_MS; import static org.onap.policy.drools.pooling.PoolingProperties.START_HEARTBEAT_MS; + import java.util.Properties; import java.util.function.Function; import org.junit.Before; @@ -57,6 +58,11 @@ public class PoolingPropertiesTest { private Properties plain; private PoolingProperties pooling; + /** + * Setup. + * + * @throws Exception throws an exception + */ @Before public void setUp() throws Exception { plain = makeProperties(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/SerializerTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/SerializerTest.java index 4206a836..0b098c13 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/SerializerTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/SerializerTest.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertNotNull; import static org.onap.policy.drools.pooling.state.FilterUtils.makeAnd; import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals; import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr; + import java.util.Map; import java.util.TreeMap; import org.junit.Test; @@ -42,7 +43,7 @@ public class SerializerTest { @Test @SuppressWarnings("unchecked") public void testEncodeFilter() throws Exception { - Serializer ser = new Serializer(); + final Serializer ser = new Serializer(); /* * Ensure raw maps serialize as expected. Use a TreeMap so the field diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ClassExtractorsTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ClassExtractorsTest.java index e6269a9a..c277a00f 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ClassExtractorsTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ClassExtractorsTest.java @@ -23,6 +23,7 @@ package org.onap.policy.drools.pooling.extractor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; + import java.util.Map; import java.util.Properties; import java.util.TreeMap; @@ -44,6 +45,10 @@ public class ClassExtractorsTest { private Properties props; private ClassExtractors map; + /** + * Setup. + * + */ @Before public void setUp() { props = new Properties(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ClassExtractorsTestSupport.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ClassExtractorsTestSupport.java index 98b679d4..df42fe0f 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ClassExtractorsTestSupport.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ClassExtractorsTestSupport.java @@ -28,7 +28,7 @@ public class ClassExtractorsTestSupport { private ClassExtractorsTestSupport2 nested = new ClassExtractorsTestSupport2(); /** - * + * Constructor. */ public ClassExtractorsTestSupport() { super(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ExtractorExceptionTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ExtractorExceptionTest.java index d1458de7..aef0a925 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ExtractorExceptionTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/ExtractorExceptionTest.java @@ -21,6 +21,7 @@ package org.onap.policy.drools.pooling.extractor; import static org.junit.Assert.assertEquals; + import org.junit.Test; import org.onap.policy.common.utils.test.ExceptionsTester; diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/FieldExtractorTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/FieldExtractorTest.java index 9794bffa..7536d007 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/FieldExtractorTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/FieldExtractorTest.java @@ -22,6 +22,7 @@ package org.onap.policy.drools.pooling.extractor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; + import java.lang.reflect.Field; import org.junit.Before; import org.junit.Test; diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/MapExtractorTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/MapExtractorTest.java index 48985bf3..afaa2b65 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/MapExtractorTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/MapExtractorTest.java @@ -20,7 +20,9 @@ package org.onap.policy.drools.pooling.extractor; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + import java.util.HashMap; import java.util.Map; import org.junit.Before; @@ -48,7 +50,7 @@ public class MapExtractorTest { public void testExtract_MissingValue() { Map<String,Object> map = new HashMap<>(); - map.put(KEY+"x", VALUE+"x"); + map.put(KEY + "x", VALUE + "x"); // object is a map, but doesn't have the key assertNull(ext.extract(map)); @@ -58,7 +60,7 @@ public class MapExtractorTest { public void testExtract() { Map<String,Object> map = new HashMap<>(); - map.put(KEY+"x", VALUE+"x"); + map.put(KEY + "x", VALUE + "x"); map.put(KEY, VALUE); // object is a map and contains the key diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/MethodExtractorTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/MethodExtractorTest.java index ae5858e7..41f731fb 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/MethodExtractorTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/extractor/MethodExtractorTest.java @@ -22,6 +22,7 @@ package org.onap.policy.drools.pooling.extractor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; + import java.lang.reflect.Method; import org.junit.Before; import org.junit.Test; diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/BasicMessageTester.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/BasicMessageTester.java index 69d7e67c..2dab38d1 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/BasicMessageTester.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/BasicMessageTester.java @@ -23,9 +23,10 @@ package org.onap.policy.drools.pooling.message; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; + +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; import org.onap.policy.drools.pooling.PoolingFeatureException; -import com.fasterxml.jackson.databind.ObjectMapper; /** * Superclass used to test subclasses of {@link Message}. @@ -49,6 +50,7 @@ public abstract class BasicMessageTester<T extends Message> { private final Class<T> subclazz; /** + * Constructor. * * @param subclazz subclass of {@link Message} being tested */ @@ -60,7 +62,6 @@ public abstract class BasicMessageTester<T extends Message> { * Creates a default Message and verifies that the source and channel are * {@code null}. * - * @return the default Message */ @Test public final void testDefaultConstructor() { @@ -71,8 +72,6 @@ public abstract class BasicMessageTester<T extends Message> { * Tests that the Message has the correct source, and that the channel is * {@code null}. * - * @param msg message to be checked - * @param expectedSource what the source is expected to be */ @Test public final void testConstructorWithArgs() { diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/BucketAssignmentsTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/BucketAssignmentsTest.java index c14e8dba..5788141b 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/BucketAssignmentsTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/BucketAssignmentsTest.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; + import java.util.Arrays; import java.util.SortedSet; import java.util.TreeSet; @@ -41,7 +42,7 @@ public class BucketAssignmentsTest { @Test public void testBucketAssignmentsStringArray() { - String arr[] = {"abc", "def"}; + String[] arr = {"abc", "def"}; BucketAssignments asgn = new BucketAssignments(arr); assertNotNull(asgn.getHostArray()); @@ -51,13 +52,13 @@ public class BucketAssignmentsTest { @Test public void testGetHostArray_testSetHostArray() { - String arr[] = {"abc", "def"}; + String[] arr = {"abc", "def"}; BucketAssignments asgn = new BucketAssignments(arr); assertNotNull(asgn.getHostArray()); assertEquals(arr.toString(), asgn.getHostArray().toString()); - String arr2[] = {"xyz"}; + String[] arr2 = {"xyz"}; asgn.setHostArray(arr2); assertNotNull(asgn.getHostArray()); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/ForwardTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/ForwardTest.java index 2549fa94..c51cafed 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/ForwardTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/ForwardTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; + import org.junit.Test; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/MessageTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/MessageTest.java index 432dcc3c..4e683ff9 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/MessageTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/MessageTest.java @@ -22,6 +22,7 @@ package org.onap.policy.drools.pooling.message; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; + import org.junit.Test; public class MessageTest extends BasicMessageTester<Message> { diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/MessageWithAssignmentsTester.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/MessageWithAssignmentsTester.java index 2b670dcc..e5dfae9b 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/MessageWithAssignmentsTester.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/MessageWithAssignmentsTester.java @@ -22,6 +22,7 @@ package org.onap.policy.drools.pooling.message; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; + import org.junit.Test; /** @@ -31,7 +32,7 @@ import org.junit.Test; */ public abstract class MessageWithAssignmentsTester<T extends MessageWithAssignments> extends BasicMessageTester<T> { // values set by makeValidMessage() - public static final String[] VALID_ARRAY = {VALID_HOST, VALID_HOST+"_xxx"}; + public static final String[] VALID_ARRAY = {VALID_HOST, VALID_HOST + "_xxx"}; public static final BucketAssignments VALID_ASGN = new BucketAssignments(VALID_ARRAY); /** @@ -41,6 +42,7 @@ public abstract class MessageWithAssignmentsTester<T extends MessageWithAssignme private boolean nullAssignments; /** + * Constructor. * * @param subclazz subclass of {@link MessageWithAssignments} being tested */ diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/ActiveStateTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/ActiveStateTest.java index f2701038..9cb835c9 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/ActiveStateTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/ActiveStateTest.java @@ -33,6 +33,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.Arrays; import java.util.Map; import org.junit.Before; @@ -50,6 +51,9 @@ public class ActiveStateTest extends BasicStateTester { private ActiveState state; + /** + * Setup. + */ @Before public void setUp() throws Exception { super.setUp(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/BasicStateTester.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/BasicStateTester.java index 75ca7564..20f49a0e 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/BasicStateTester.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/BasicStateTester.java @@ -27,6 +27,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; + import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -113,6 +114,11 @@ public class BasicStateTester { super(); } + /** + * Setup. + * + * @throws Exception throws exception + */ public void setUp() throws Exception { onceSchedules = new LinkedList<>(); onceTasks = new LinkedList<>(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/FilterUtilsTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/FilterUtilsTest.java index ba517194..f4eb870e 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/FilterUtilsTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/FilterUtilsTest.java @@ -31,6 +31,7 @@ import static org.onap.policy.drools.pooling.state.FilterUtils.JSON_VALUE; import static org.onap.policy.drools.pooling.state.FilterUtils.makeAnd; import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals; import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr; + import java.util.Map; import org.junit.Test; diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/IdleStateTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/IdleStateTest.java index 497dbbb7..cf2c9c7c 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/IdleStateTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/IdleStateTest.java @@ -26,6 +26,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -42,6 +43,9 @@ public class IdleStateTest extends BasicStateTester { private IdleState state; + /** + * Setup. + */ @Before public void setUp() throws Exception { super.setUp(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/InactiveStateTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/InactiveStateTest.java index ae53ce05..ee7fd5e5 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/InactiveStateTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/InactiveStateTest.java @@ -27,6 +27,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -41,6 +42,10 @@ public class InactiveStateTest extends BasicStateTester { private InactiveState state; + /** + * Setup. + * + */ @Before public void setUp() throws Exception { super.setUp(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/ProcessingStateTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/ProcessingStateTest.java index 7ac58439..4f634516 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/ProcessingStateTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/ProcessingStateTest.java @@ -29,6 +29,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.Arrays; import java.util.Map; import org.junit.Before; @@ -45,6 +46,9 @@ public class ProcessingStateTest extends BasicStateTester { private ProcessingState state; private HostBucket hostBucket; + /** + * Setup. + */ @Before public void setUp() throws Exception { super.setUp(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/QueryStateTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/QueryStateTest.java index 7cd37581..5601932e 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/QueryStateTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/QueryStateTest.java @@ -30,6 +30,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -44,6 +45,9 @@ public class QueryStateTest extends BasicStateTester { private QueryState state; + /** + * Setup. + */ @Before public void setUp() throws Exception { super.setUp(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StartStateTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StartStateTest.java index 18f12ff8..faafb8cb 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StartStateTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StartStateTest.java @@ -29,6 +29,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -46,6 +47,9 @@ public class StartStateTest extends BasicStateTester { private StartState state; + /** + * Setup. + */ @Before public void setUp() throws Exception { super.setUp(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StateTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StateTest.java index e3d383d9..42bb35f2 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StateTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StateTest.java @@ -29,6 +29,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -47,6 +48,9 @@ public class StateTest extends BasicStateTester { private State state; + /** + * Setup. + */ @Before public void setUp() throws Exception { super.setUp(); diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsPersistenceProperties.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsPersistenceProperties.java index c3f00359..4700e8ae 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsPersistenceProperties.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsPersistenceProperties.java @@ -21,17 +21,17 @@ package org.onap.policy.drools.persistence; public class DroolsPersistenceProperties { - /* - * feature-session-persistence.properties parameter key values - */ - public static final String DB_DRIVER = "javax.persistence.jdbc.driver"; - public static final String DB_URL = "javax.persistence.jdbc.url"; - public static final String DB_USER = "javax.persistence.jdbc.user"; - public static final String DB_PWD = "javax.persistence.jdbc.password"; - public static final String DB_SESSIONINFO_TIMEOUT = "persistence.sessioninfo.timeout"; - public static final String JTA_OBJECTSTORE_DIR = "persistence.objectstore.dir"; + /* + * feature-session-persistence.properties parameter key values + */ + public static final String DB_DRIVER = "javax.persistence.jdbc.driver"; + public static final String DB_URL = "javax.persistence.jdbc.url"; + public static final String DB_USER = "javax.persistence.jdbc.user"; + public static final String DB_PWD = "javax.persistence.jdbc.password"; + public static final String DB_SESSIONINFO_TIMEOUT = "persistence.sessioninfo.timeout"; + public static final String JTA_OBJECTSTORE_DIR = "persistence.objectstore.dir"; - private DroolsPersistenceProperties() { - super(); - } + private DroolsPersistenceProperties() { + super(); + } } diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSession.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSession.java index a012f3c2..e8b50817 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSession.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSession.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,12 +24,19 @@ import java.util.Date; public interface DroolsSession { - public String getSessionName(); - public void setSessionName(String sessionName); - public long getSessionId(); - public void setSessionId(long sessionId); - public Date getCreatedDate(); - public void setCreatedDate(Date createdDate); - public Date getUpdatedDate(); - public void setUpdatedDate(Date updatedDate); + public String getSessionName(); + + public void setSessionName(String sessionName); + + public long getSessionId(); + + public void setSessionId(long sessionId); + + public Date getCreatedDate(); + + public void setCreatedDate(Date createdDate); + + public Date getUpdatedDate(); + + public void setUpdatedDate(Date updatedDate); } diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSessionConnector.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSessionConnector.java index e2f4ac54..3a8885f8 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSessionConnector.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSessionConnector.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,19 +20,18 @@ package org.onap.policy.drools.persistence; -public interface DroolsSessionConnector -{ - /** - * Gets a session by PDP id and name. - * @param sessName - * @return a session, or {@code null} if it is not found - */ - public DroolsSession get(String sessName); - - /** - * Replaces a session, adding it if it does not exist. - * @param sess session to be replaced - */ - public void replace(DroolsSession sess); +public interface DroolsSessionConnector { + /** + * Gets a session by PDP id and name. + * @param sessName session name + * @return a session, or {@code null} if it is not found + */ + public DroolsSession get(String sessName); + + /** + * Replaces a session, adding it if it does not exist. + * @param sess session to be replaced + */ + public void replace(DroolsSession sess); } diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSessionEntity.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSessionEntity.java index b3616c43..117e6410 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSessionEntity.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/DroolsSessionEntity.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -34,105 +34,101 @@ import javax.persistence.TemporalType; @Entity public class DroolsSessionEntity implements Serializable, DroolsSession { - private static final long serialVersionUID = -5495057038819948709L; - - @Id - @Column(name = "sessionName", nullable = false) - private String sessionName = "-1"; - - @Column(name = "sessionId", nullable = false) - private long sessionId = -1L; - - @Temporal(TemporalType.TIMESTAMP) - @Column(name = "createdDate", nullable = false) - private Date createdDate; - - @Temporal(TemporalType.TIMESTAMP) - @Column(name = "updatedDate", nullable = false) - private Date updatedDate; - - public DroolsSessionEntity() { - - } - - public DroolsSessionEntity(String sessionName, long sessionId) { - this.sessionName = sessionName; - this.sessionId = sessionId; - - } - - @PrePersist - public void prePersist() { - this.createdDate = new Date(); - this.updatedDate = new Date(); - } - - @PreUpdate - public void preUpdate() { - this.updatedDate = new Date(); - } - - @Override - public String getSessionName() { - return sessionName; - } - - @Override - public void setSessionName(String sessionName) { - this.sessionName = sessionName; - } - - @Override - public long getSessionId() { - return sessionId; - } - - @Override - public void setSessionId(long sessionId) { - this.sessionId = sessionId; - } - - @Override - public Date getCreatedDate() { - return createdDate; - } - - @Override - public void setCreatedDate(Date createdDate) { - this.createdDate = createdDate; - } - - @Override - public Date getUpdatedDate() { - return updatedDate; - } - - @Override - public void setUpdatedDate(Date updatedDate) { - this.updatedDate = updatedDate; - } - - @Override - public boolean equals(Object other) { - if (other instanceof DroolsSession) { - DroolsSession p = (DroolsSession) other; - return this.getSessionName().equals(p.getSessionName()); - } else { - return false; - } - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + getSessionName().hashCode(); - return result; - } - - @Override - public String toString() { - return "{name=" + getSessionName() + ", id=" + getSessionId() + "}"; - } - + private static final long serialVersionUID = -5495057038819948709L; + + @Id + @Column(name = "sessionName", nullable = false) + private String sessionName = "-1"; + + @Column(name = "sessionId", nullable = false) + private long sessionId = -1L; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "createdDate", nullable = false) + private Date createdDate; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "updatedDate", nullable = false) + private Date updatedDate; + + public DroolsSessionEntity() {} + + public DroolsSessionEntity(String sessionName, long sessionId) { + this.sessionName = sessionName; + this.sessionId = sessionId; + } + + @PrePersist + public void prePersist() { + this.createdDate = new Date(); + this.updatedDate = new Date(); + } + + @PreUpdate + public void preUpdate() { + this.updatedDate = new Date(); + } + + @Override + public String getSessionName() { + return sessionName; + } + + @Override + public void setSessionName(String sessionName) { + this.sessionName = sessionName; + } + + @Override + public long getSessionId() { + return sessionId; + } + + @Override + public void setSessionId(long sessionId) { + this.sessionId = sessionId; + } + + @Override + public Date getCreatedDate() { + return createdDate; + } + + @Override + public void setCreatedDate(Date createdDate) { + this.createdDate = createdDate; + } + + @Override + public Date getUpdatedDate() { + return updatedDate; + } + + @Override + public void setUpdatedDate(Date updatedDate) { + this.updatedDate = updatedDate; + } + + @Override + public boolean equals(Object other) { + if (other instanceof DroolsSession) { + DroolsSession session = (DroolsSession) other; + return this.getSessionName().equals(session.getSessionName()); + } else { + return false; + } + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getSessionName().hashCode(); + return result; + } + + @Override + public String toString() { + return "{name=" + getSessionName() + ", id=" + getSessionId() + "}"; + } } diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java index 47ed221e..3d276519 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -32,109 +32,99 @@ import javax.transaction.UserTransaction; import org.onap.policy.common.utils.jpa.EntityMgrCloser; /** - * Wrapper for an <i>EntityManager</i> that creates a JTA transaction that is - * auto-rolled back when closed. + * Wrapper for an <i>EntityManager</i> that creates a JTA transaction that is auto-rolled back when + * closed. */ public class EntityMgrTrans extends EntityMgrCloser { - /** - * Transaction to be rolled back. - */ - private static UserTransaction userTrans = com.arjuna.ats.jta.UserTransaction.userTransaction(); - - /** - * - * @param em - * entity for which a transaction is to be begun - */ - public EntityMgrTrans(EntityManager em) { - super(em); - - try { - userTrans.begin(); - em.joinTransaction(); - - } catch (RuntimeException |NotSupportedException | SystemException e) { - em.close(); - throw new EntityMgrException(e); - } - } - - /** - * Gets the user transaction. For use by junit tests. - * - * @return the user transaction - */ - protected static UserTransaction getUserTrans() { - return userTrans; - } - - /** - * Sets the user transaction. For use by junit tests. - * - * @param userTrans - * the new user transaction - */ - protected static void setUserTrans(UserTransaction userTrans) { - EntityMgrTrans.userTrans = userTrans; - } - - /** - * Commits the transaction. - */ - public void commit() { - try { - userTrans.commit(); - - } catch (SecurityException | IllegalStateException | RollbackException | HeuristicMixedException - | HeuristicRollbackException | SystemException e) { - - throw new EntityMgrException(e); - } - } - - /** - * Rolls back the transaction. - */ - public void rollback() { - try { - userTrans.rollback(); - - } catch (IllegalStateException | SecurityException | SystemException e) { - throw new EntityMgrException(e); - } - } - - @Override - public void close() { - try { - if (userTrans.getStatus() == Status.STATUS_ACTIVE) { - userTrans.rollback(); - } - - } catch (IllegalStateException | SecurityException | SystemException e) { - throw new EntityMgrException(e); - - } finally { - super.close(); - } - } - - /** - * Runtime exceptions generated by this class. Wraps exceptions generated by - * delegated operations, particularly when they are not, themselves, Runtime - * exceptions. - */ - public static class EntityMgrException extends RuntimeException { - private static final long serialVersionUID = 1L; - - /** - * - * @param e - * exception to be wrapped - */ - public EntityMgrException(Exception e) { - super(e); - } - } + /** Transaction to be rolled back. */ + private static UserTransaction userTrans = com.arjuna.ats.jta.UserTransaction.userTransaction(); + + /** + * Constructor. + * + * @param em entity for which a transaction is to be begun */ + public EntityMgrTrans(EntityManager em) { + super(em); + + try { + userTrans.begin(); + em.joinTransaction(); + + } catch (RuntimeException | NotSupportedException | SystemException e) { + em.close(); + throw new EntityMgrException(e); + } + } + + /** + * Gets the user transaction. For use by junit tests. + * + * @return the user transaction + */ + protected static UserTransaction getUserTrans() { + return userTrans; + } + + /** + * Sets the user transaction. For use by junit tests. + * + * @param userTrans the new user transaction + */ + protected static void setUserTrans(UserTransaction userTrans) { + EntityMgrTrans.userTrans = userTrans; + } + + /** Commits the transaction. */ + public void commit() { + try { + userTrans.commit(); + + } catch (SecurityException + | IllegalStateException + | RollbackException + | HeuristicMixedException + | HeuristicRollbackException + | SystemException e) { + + throw new EntityMgrException(e); + } + } + + /** Rolls back the transaction. */ + public void rollback() { + try { + userTrans.rollback(); + + } catch (IllegalStateException | SecurityException | SystemException e) { + throw new EntityMgrException(e); + } + } + + @Override + public void close() { + try { + if (userTrans.getStatus() == Status.STATUS_ACTIVE) { + userTrans.rollback(); + } + + } catch (IllegalStateException | SecurityException | SystemException e) { + throw new EntityMgrException(e); + + } finally { + super.close(); + } + } + + /** + * Runtime exceptions generated by this class. Wraps exceptions generated by delegated operations, + * particularly when they are not, themselves, Runtime exceptions. + */ + public static class EntityMgrException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public EntityMgrException(Exception ex) { + super(ex); + } + } } diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/JpaDroolsSessionConnector.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/JpaDroolsSessionConnector.java index cd76ae8d..381a896e 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/JpaDroolsSessionConnector.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/JpaDroolsSessionConnector.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -28,89 +28,84 @@ import org.slf4j.LoggerFactory; public class JpaDroolsSessionConnector implements DroolsSessionConnector { - private static Logger logger = LoggerFactory.getLogger(JpaDroolsSessionConnector.class); + private static Logger logger = LoggerFactory.getLogger(JpaDroolsSessionConnector.class); - private final EntityManagerFactory emf; + private final EntityManagerFactory emf; - public JpaDroolsSessionConnector(EntityManagerFactory emf) { - this.emf = emf; - } + public JpaDroolsSessionConnector(EntityManagerFactory emf) { + this.emf = emf; + } - @Override - public DroolsSession get(String sessName) { + @Override + public DroolsSession get(String sessName) { - EntityManager em = emf.createEntityManager(); - DroolsSessionEntity s = null; + EntityManager em = emf.createEntityManager(); + DroolsSessionEntity entity = null; - try (EntityMgrTrans trans = new EntityMgrTrans(em)) { + try (EntityMgrTrans trans = new EntityMgrTrans(em)) { - s = em.find(DroolsSessionEntity.class, sessName); - if (s != null) { - em.refresh(s); - } + entity = em.find(DroolsSessionEntity.class, sessName); + if (entity != null) { + em.refresh(entity); + } - trans.commit(); - } + trans.commit(); + } - return s; - } + return entity; + } - @Override - public void replace(DroolsSession sess) { - String sessName = sess.getSessionName(); + @Override + public void replace(DroolsSession sess) { + String sessName = sess.getSessionName(); - logger.info("replace: Entering and manually updating session name= {}", sessName); + logger.info("replace: Entering and manually updating session name= {}", sessName); - EntityManager em = emf.createEntityManager(); + EntityManager em = emf.createEntityManager(); - try (EntityMgrTrans trans = new EntityMgrTrans(em)) { + try (EntityMgrTrans trans = new EntityMgrTrans(em)) { - if (!update(em, sess)) { - add(em, sess); - } + if (!update(em, sess)) { + add(em, sess); + } - trans.commit(); - } + trans.commit(); + } - logger.info("replace: Exiting"); - } + logger.info("replace: Exiting"); + } - /** - * Adds a session to the persistent store. - * - * @param em - * entity manager - * @param sess - * session to be added - */ - private void add(EntityManager em, DroolsSession sess) { - logger.info("add: Inserting session id={}", sess.getSessionId()); + /** + * Adds a session to the persistent store. + * + * @param em entity manager + * @param sess session to be added + */ + private void add(EntityManager em, DroolsSession sess) { + logger.info("add: Inserting session id={}", sess.getSessionId()); - DroolsSessionEntity ent = new DroolsSessionEntity(sess.getSessionName(), sess.getSessionId()); + DroolsSessionEntity ent = new DroolsSessionEntity(sess.getSessionName(), sess.getSessionId()); - em.persist(ent); - } + em.persist(ent); + } - /** - * Updates a session, if it exists within the persistent store. - * - * @param em - * entity manager - * @param sess - * session data to be persisted - * @return {@code true} if a record was updated, {@code false} if it was not - * found - */ - private boolean update(EntityManager em, DroolsSession sess) { + /** + * Updates a session, if it exists within the persistent store. + * + * @param em entity manager + * @param sess session data to be persisted + * @return {@code true} if a record was updated, {@code false} if it was not found + */ + private boolean update(EntityManager em, DroolsSession sess) { - DroolsSessionEntity s = em.find(DroolsSessionEntity.class, sess.getSessionName()); - if (s == null) { - return false; - } + DroolsSessionEntity s = em.find(DroolsSessionEntity.class, sess.getSessionName()); + if (s == null) { + return false; + } - logger.info("update: Updating session id to {}", sess.getSessionId()); - s.setSessionId(sess.getSessionId()); + logger.info("update: Updating session id to {}", sess.getSessionId()); + s.setSessionId(sess.getSessionId()); - return true; - } + return true; + } } diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java index 129be8a7..7b17de6c 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -54,971 +54,898 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * If this feature is supported, there is a single instance of it. It adds - * persistence to Drools sessions. In addition, if an active-standby feature - * exists, then that is used to determine the active and last-active PDP. If it - * does not exist, then the current host name is used as the PDP id. + * If this feature is supported, there is a single instance of it. It adds persistence to Drools + * sessions. In addition, if an active-standby feature exists, then that is used to determine the + * active and last-active PDP. If it does not exist, then the current host name is used as the PDP + * id. * - * The bulk of the code here was once in other classes, such as - * 'PolicyContainer' and 'Main'. It was moved here as part of making this a - * separate optional feature. + * <p>The bulk of the code here was once in other classes, such as 'PolicyContainer' and 'Main'. It + * was moved here as part of making this a separate optional feature. */ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngineFeatureAPI { - private static final Logger logger = LoggerFactory.getLogger(PersistenceFeature.class); - - /** - * Standard factory used to get various items. - */ - private static Factory stdFactory = new Factory(); - - /** - * Factory used to get various items. - */ - private Factory fact = stdFactory; - - /** - * KieService factory. - */ - private KieServices kieSvcFact; - - /** - * Persistence properties. - */ - private Properties persistProps; - - /** - * Whether or not the SessionInfo records should be cleaned out. - */ - private boolean sessInfoCleaned; - - /** - * SessionInfo timeout, in milli-seconds, as read from - * {@link #persistProps}. - */ - private long sessionInfoTimeoutMs; - - /** - * Object used to serialize cleanup of sessioninfo table. - */ - private Object cleanupLock = new Object(); - - /** - * Sets the factory to be used during junit testing. - * - * @param fact - * factory to be used - */ - protected void setFactory(Factory fact) { - this.fact = fact; - } - - /** - * Lookup the adjunct for this feature that is associated with the specified - * PolicyContainer. If not found, create one. - * - * @param policyContainer - * the container whose adjunct we are looking up, and possibly - * creating - * @return the associated 'ContainerAdjunct' instance, which may be new - */ - private ContainerAdjunct getContainerAdjunct(PolicyContainer policyContainer) { - - Object rval = policyContainer.getAdjunct(this); - - if (rval == null || !(rval instanceof ContainerAdjunct)) { - // adjunct does not exist, or has the wrong type (should never - // happen) - rval = new ContainerAdjunct(policyContainer); - policyContainer.setAdjunct(this, rval); - } - - return (ContainerAdjunct) rval; - } - - /** - * {@inheritDoc} - */ - @Override - public int getSequenceNumber() { - return 1; - } - - /** - * {@inheritDoc} - */ - @Override - public void globalInit(String[] args, String configDir) { - - kieSvcFact = fact.getKieServices(); - - try { - persistProps = fact.loadProperties(configDir + "/feature-session-persistence.properties"); - - } catch (IOException e1) { - logger.error("initializePersistence: ", e1); - } - - sessionInfoTimeoutMs = getPersistenceTimeout(); - } - - /** - * Creates a persistent KieSession, loading it from the persistent store, or - * creating one, if it does not exist yet. - */ - @Override - public KieSession activatePolicySession(PolicyContainer policyContainer, String name, String kieBaseName) { - - if (isPersistenceEnabled(policyContainer, name)) { - cleanUpSessionInfo(); - - return getContainerAdjunct(policyContainer).newPersistentKieSession(name, kieBaseName); - } - - return null; - } - - /** - * {@inheritDoc} - */ - @Override - public PolicySession.ThreadModel selectThreadModel(PolicySession session) { - - PolicyContainer policyContainer = session.getPolicyContainer(); - if (isPersistenceEnabled(policyContainer, session.getName())) { - return new PersistentThreadModel(session, getProperties(policyContainer)); - } - return null; - } - - /** - * {@inheritDoc} - */ - @Override - public void disposeKieSession(PolicySession policySession) { - - ContainerAdjunct contAdj = (ContainerAdjunct) policySession.getPolicyContainer().getAdjunct(this); - if (contAdj != null) { - contAdj.disposeKieSession(policySession.getName()); - } - } - - /** - * {@inheritDoc} - */ - @Override - public void destroyKieSession(PolicySession policySession) { - - ContainerAdjunct contAdj = (ContainerAdjunct) policySession.getPolicyContainer().getAdjunct(this); - if (contAdj != null) { - contAdj.destroyKieSession(policySession.getName()); - } - } - - /** - * {@inheritDoc} - */ - @Override - public boolean afterStart(PolicyEngine engine) { - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean beforeStart(PolicyEngine engine) { - synchronized (cleanupLock) { - sessInfoCleaned = false; - } - - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean beforeActivate(PolicyEngine engine) { - synchronized (cleanupLock) { - sessInfoCleaned = false; - } - - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean afterActivate(PolicyEngine engine) { - return false; - } - - /* ============================================================ */ - - /** - * Gets the persistence timeout value for sessioninfo records. - * - * @return the timeout value, in milli-seconds, or {@code -1} if it is - * unspecified or invalid - */ - private long getPersistenceTimeout() { - String timeoutString = null; - - try { - timeoutString = persistProps.getProperty(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT); - - if (timeoutString != null) { - // timeout parameter is specified - return Long.valueOf(timeoutString) * 1000; - } - - } catch (NumberFormatException e) { - logger.error("Invalid value for Drools persistence property persistence.sessioninfo.timeout: {}", - timeoutString, e); - } - - return -1; - } - - /* ============================================================ */ - - /** - * Each instance of this class is a logical extension of a 'PolicyContainer' - * instance. Its reference is stored in the 'adjuncts' table within the - * 'PolicyContainer', and will be garbage-collected with the container. - */ - protected class ContainerAdjunct { - /** - * 'PolicyContainer' instance that this adjunct is extending. - */ - private PolicyContainer policyContainer; - - /** - * Maps a KIE session name to its data source. - */ - private Map<String, DsEmf> name2ds = new HashMap<>(); - - /** - * Constructor - initialize a new 'ContainerAdjunct' - * - * @param policyContainer - * the 'PolicyContainer' instance this adjunct is extending - */ - private ContainerAdjunct(PolicyContainer policyContainer) { - this.policyContainer = policyContainer; - } - - /** - * Create a new persistent KieSession. If there is already a - * corresponding entry in the database, it is used to initialize the - * KieSession. If not, a completely new session is created. - * - * @param name - * the name of the KieSession (which is also the name of the - * associated PolicySession) - * @param kieBaseName - * the name of the 'KieBase' instance containing this session - * @return a new KieSession with persistence enabled - */ - private KieSession newPersistentKieSession(String name, String kieBaseName) { - - configureSysProps(); - - BasicDataSource ds = fact.makeDataSource(getDataSourceProperties()); - DsEmf dsemf = new DsEmf(ds); - - try { - EntityManagerFactory emf = dsemf.emf; - DroolsSessionConnector conn = fact.makeJpaConnector(emf); - - long desiredSessionId = getSessionId(conn, name); - - logger.info("\n\nThis controller is primary... coming up with session {} \n\n", desiredSessionId); - - // session does not exist -- attempt to create one - logger.info("getPolicySession:session does not exist -- attempt to create one with name {}", name); - - Environment env = kieSvcFact.newEnvironment(); - - configureKieEnv(env, emf); - - KieSessionConfiguration kConf = kieSvcFact.newKieSessionConfiguration(); - - KieSession kieSession = (desiredSessionId >= 0 - ? loadKieSession(kieBaseName, desiredSessionId, env, kConf) : null); - - if (kieSession == null) { - // loadKieSession() returned null or desiredSessionId < 0 - logger.info("LOADING We cannot load session {}. Going to create a new one", desiredSessionId); - - kieSession = newKieSession(kieBaseName, env); - } - - replaceSession(conn, name, kieSession); - - name2ds.put(name, dsemf); - - return kieSession; - - } catch (RuntimeException e) { - dsemf.close(); - throw e; - } - } - - /** - * Loads an existing KieSession from the persistent store. - * - * @param kieBaseName - * the name of the 'KieBase' instance containing this session - * @param desiredSessionId - * id of the desired KieSession - * @param env - * Kie Environment for the session - * @param kConf - * Kie Configuration for the session - * @return the persistent session, or {@code null} if it could not be - * loaded - */ - private KieSession loadKieSession(String kieBaseName, long desiredSessionId, Environment env, - KieSessionConfiguration kConf) { - try { - KieSession kieSession = kieSvcFact.getStoreServices().loadKieSession(desiredSessionId, - policyContainer.getKieContainer().getKieBase(kieBaseName), kConf, env); - - logger.info("LOADING Loaded session {}", desiredSessionId); - - return kieSession; - - } catch (Exception e) { - logger.error("loadKieSession error: ", e); - return null; - } - } - - /** - * Creates a new, persistent KieSession. - * - * @param kieBaseName - * the name of the 'KieBase' instance containing this session - * @param env - * Kie Environment for the session - * @return a new, persistent session - */ - private KieSession newKieSession(String kieBaseName, Environment env) { - KieSession kieSession = kieSvcFact.getStoreServices() - .newKieSession(policyContainer.getKieContainer().getKieBase(kieBaseName), null, env); - - logger.info("LOADING CREATED {}", kieSession.getIdentifier()); - - return kieSession; - } - - /** - * Closes the data source associated with a session. - * - * @param name - * name of the session being destroyed - */ - private void destroyKieSession(String name) { - closeDataSource(name); - } - - /** - * Closes the data source associated with a session. - * - * @param name - * name of the session being disposed of - */ - private void disposeKieSession(String name) { - closeDataSource(name); - } - - /** - * Closes the data source associated with a session. - * - * @param name - * name of the session whose data source is to be closed - */ - private void closeDataSource(String name) { - DsEmf ds = name2ds.remove(name); - if (ds != null) { - ds.close(); - } - } - - /** - * Configures java system properties for JPA/JTA. - */ - private void configureSysProps() { - System.setProperty("com.arjuna.ats.arjuna.coordinator.defaultTimeout", "60"); - System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", - persistProps.getProperty(DroolsPersistenceProperties.JTA_OBJECTSTORE_DIR)); - System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", - persistProps.getProperty(DroolsPersistenceProperties.JTA_OBJECTSTORE_DIR)); - } - - /** - * Configures a Kie Environment - * - * @param env - * environment to be configured - * @param emf - * entity manager factory - */ - private void configureKieEnv(Environment env, EntityManagerFactory emf) { - env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf); - env.set(EnvironmentName.TRANSACTION, fact.getUserTrans()); - env.set(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY, fact.getTransSyncReg()); - env.set(EnvironmentName.TRANSACTION_MANAGER, fact.getTransMgr()); - } - - /** - * Gets a session's ID from the persistent store. - * - * @param conn - * persistence connector - * @param sessnm - * name of the session - * @return the session's id, or {@code -1} if the session is not found - */ - private long getSessionId(DroolsSessionConnector conn, String sessnm) { - DroolsSession sess = conn.get(sessnm); - return sess != null ? sess.getSessionId() : -1; - } - - /** - * Replaces a session within the persistent store, if it exists. Adds it - * otherwise. - * - * @param conn - * persistence connector - * @param sessnm - * name of session to be updated - * @param kieSession - * new session information - */ - private void replaceSession(DroolsSessionConnector conn, String sessnm, KieSession kieSession) { - - DroolsSessionEntity sess = new DroolsSessionEntity(); - - sess.setSessionName(sessnm); - sess.setSessionId(kieSession.getIdentifier()); - - conn.replace(sess); - } - } - - /* ============================================================ */ - - /** - * Gets the data source properties. - * - * @return the data source properties - */ - private Properties getDataSourceProperties() { - Properties props = new Properties(); - props.put("driverClassName", persistProps.getProperty(DroolsPersistenceProperties.DB_DRIVER)); - props.put("url", persistProps.getProperty(DroolsPersistenceProperties.DB_URL)); - props.put("username", persistProps.getProperty(DroolsPersistenceProperties.DB_USER)); - props.put("password", persistProps.getProperty(DroolsPersistenceProperties.DB_PWD)); - props.put("maxActive", "3"); - props.put("maxIdle", "1"); - props.put("maxWait", "120000"); - props.put("whenExhaustedAction", "2"); - props.put("testOnBorrow", "false"); - props.put("poolPreparedStatements", "true"); - - return props; - } - - /** - * Removes "old" Drools 'sessioninfo' records, so they aren't used to - * restore data to Drools sessions. This also has the useful side-effect of - * removing abandoned records as well. - */ - private void cleanUpSessionInfo() { - - synchronized (cleanupLock) { - - if (sessInfoCleaned) { - logger.info("Clean up of sessioninfo table: already done"); - return; - } - - if (sessionInfoTimeoutMs < 0) { - logger.info("Clean up of sessioninfo table: no timeout specified"); - return; - } - - // now do the record deletion - try (BasicDataSource ds = fact.makeDataSource(getDataSourceProperties()); - Connection connection = ds.getConnection(); - PreparedStatement statement = connection.prepareStatement( - "DELETE FROM sessioninfo WHERE timestampdiff(second,lastmodificationdate,now()) > ?")) { - - connection.setAutoCommit(true); - - statement.setLong(1, sessionInfoTimeoutMs / 1000); - - int count = statement.executeUpdate(); - logger.info("Cleaning up sessioninfo table -- {} records removed", count); - - } catch (SQLException e) { - logger.error("Clean up of sessioninfo table failed", e); - } - - // TODO: delete DroolsSessionEntity where sessionId not in - // (sessinfo.xxx) - - sessInfoCleaned = true; - } - } - - /** - * Determine whether persistence is enabled for a specific container - * - * @param container - * container to be checked - * @param sessionName - * name of the session to be checked - * @return {@code true} if persistence is enabled for this container, and - * {@code false} if not - */ - private boolean isPersistenceEnabled(PolicyContainer container, String sessionName) { - Properties properties = getProperties(container); - boolean rval = false; - - if (properties != null) { - // fetch the 'type' property - String type = getProperty(properties, sessionName, "type"); - rval = "auto".equals(type) || "native".equals(type); - } - - return rval; - } - - /** - * Determine the controller properties associated with the policy container. - * - * @param container - * container whose properties are to be retrieved - * @return the container's properties, or {@code null} if not found - */ - private Properties getProperties(PolicyContainer container) { - try { - return fact.getPolicyController(container).getProperties(); - } catch (IllegalArgumentException e) { - logger.error("getProperties exception: ", e); - return null; - } - } - - /** - * Fetch the persistence property associated with a session. The name may - * have the form: - * <ul> - * <li>persistence.SESSION-NAME.PROPERTY</li> - * <li>persistence.PROPERTY</li> - * </ul> - * - * @param properties - * properties from which the value is to be retrieved - * @param sessionName - * session name of interest - * @param property - * property name of interest - * @return the property value, or {@code null} if not found - */ - private String getProperty(Properties properties, String sessionName, String property) { - String value = properties.getProperty("persistence." + sessionName + "." + property); - if (value == null) { - value = properties.getProperty("persistence." + property); - } - - return value; - } - - /* ============================================================ */ - - /** - * This 'ThreadModel' variant periodically calls - * 'KieSession.fireAllRules()', because the 'fireUntilHalt' method isn't - * compatible with persistence. - */ - public class PersistentThreadModel implements Runnable, PolicySession.ThreadModel { - - /** - * Session associated with this persistent thread. - */ - private final PolicySession session; - - /** - * The session thread. - */ - private final Thread thread; - - /** - * Used to indicate that processing should stop. - */ - private final CountDownLatch stopped = new CountDownLatch(1); - - /** - * Minimum time, in milli-seconds, that the thread should sleep before - * firing rules again. - */ - long minSleepTime = 100; - - /** - * Maximum time, in milli-seconds, that the thread should sleep before - * firing rules again. This is a "half" time, so that we can multiply it - * by two without overflowing the word size. - */ - long halfMaxSleepTime = 5000L / 2L; - - /** - * Constructor - initialize variables and create thread - * - * @param session - * the 'PolicySession' instance - * @param properties - * may contain additional session properties - */ - public PersistentThreadModel(PolicySession session, Properties properties) { - this.session = session; - this.thread = new Thread(this, getThreadName()); - - if (properties == null) { - return; - } - - // extract 'minSleepTime' and/or 'maxSleepTime' - String name = session.getName(); - - // fetch 'minSleepTime' value, and update if defined - String sleepTimeString = getProperty(properties, name, "minSleepTime"); - if (sleepTimeString != null) { - try { - minSleepTime = Math.max(1, Integer.valueOf(sleepTimeString)); - } catch (Exception e) { - logger.error(sleepTimeString + ": Illegal value for 'minSleepTime'", e); - } - } - - // fetch 'maxSleepTime' value, and update if defined - long maxSleepTime = 2 * halfMaxSleepTime; - sleepTimeString = getProperty(properties, name, "maxSleepTime"); - if (sleepTimeString != null) { - try { - maxSleepTime = Math.max(1, Integer.valueOf(sleepTimeString)); - } catch (Exception e) { - logger.error(sleepTimeString + ": Illegal value for 'maxSleepTime'", e); - } - } - - // swap values if needed - if (minSleepTime > maxSleepTime) { - logger.error("minSleepTime(" + minSleepTime + ") is greater than maxSleepTime(" + maxSleepTime - + ") -- swapping"); - long tmp = minSleepTime; - minSleepTime = maxSleepTime; - maxSleepTime = tmp; - } - - halfMaxSleepTime = Math.max(1, maxSleepTime / 2); - } - - /** - * @return the String to use as the thread name - */ - private String getThreadName() { - return "Session " + session.getFullName() + " (persistent)"; - } - - /***************************/ - /* 'ThreadModel' interface */ - /***************************/ - - /** - * {@inheritDoc} - */ - @Override - public void start() { - thread.start(); - } - - /** - * {@inheritDoc} - */ - @Override - public void stop() { - // tell the thread to stop - stopped.countDown(); - - // wait up to 10 seconds for the thread to stop - try { - thread.join(10000); - - } catch (InterruptedException e) { - logger.error("stopThread exception: ", e); - Thread.currentThread().interrupt(); - } - - // verify that it's done - if (thread.isAlive()) { - logger.error("stopThread: still running"); - } - } - - /** - * {@inheritDoc} - */ - @Override - public void updated() { - // the container artifact has been updated -- adjust the thread name - thread.setName(getThreadName()); - } - - /************************/ - /* 'Runnable' interface */ - /************************/ - - /** - * {@inheritDoc} - */ - @Override - public void run() { - logger.info("PersistentThreadModel running"); - - // set thread local variable - session.setPolicySession(); - - KieSession kieSession = session.getKieSession(); - long sleepTime = 2 * halfMaxSleepTime; - - // We want to continue, despite any exceptions that occur - // while rules are fired. - - boolean cont = true; - while(cont) { - - try { - if (kieSession.fireAllRules() > 0) { - // some rules fired -- reduce poll delay - sleepTime = Math.max(minSleepTime, sleepTime / 2); - } else { - // no rules fired -- increase poll delay - sleepTime = 2 * Math.min(halfMaxSleepTime, sleepTime); - } - - } catch (Exception | LinkageError e) { - logger.error("Exception during kieSession.fireAllRules", e); - } - - - try { - if (stopped.await(sleepTime, TimeUnit.MILLISECONDS)) { - cont = false; - } - - } catch (InterruptedException e) { - logger.error("startThread exception: ", e); - Thread.currentThread().interrupt(); - cont = false; - } - } - - logger.info("PersistentThreadModel completed"); - } - } - - /* ============================================================ */ - - /** - * DataSource-EntityManagerFactory pair. - */ - private class DsEmf { - private BasicDataSource bds; - private EntityManagerFactory emf; - - /** - * Makes an entity manager factory for the given data source. - * - * @param bds - * pooled data source - */ - public DsEmf(BasicDataSource bds) { - try { - Map<String, Object> props = new HashMap<>(); - props.put(org.hibernate.cfg.Environment.JPA_JTA_DATASOURCE, bds); - - this.bds = bds; - this.emf = fact.makeEntMgrFact(props); - - } catch (RuntimeException e) { - closeDataSource(); - throw e; - } - } - - /** - * Closes the entity manager factory and the data source. - */ - public void close() { - try { - emf.close(); - - } catch (RuntimeException e) { - closeDataSource(); - throw e; - } - - closeDataSource(); - } - - /** - * Closes the data source only. - */ - private void closeDataSource() { - try { - bds.close(); - - } catch (SQLException e) { - throw new PersistenceFeatureException(e); - } - - } - } - - private static class SingletonRegistry { - private static final TransactionSynchronizationRegistry transreg = new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple(); - - private SingletonRegistry() { - super(); - } - } - - /** - * Factory for various items. Methods can be overridden for junit testing. - */ - protected static class Factory { - - /** - * Gets the transaction manager. - * - * @return the transaction manager - */ - public TransactionManager getTransMgr() { - return com.arjuna.ats.jta.TransactionManager.transactionManager(); - } - - /** - * Gets the user transaction. - * - * @return the user transaction - */ - public UserTransaction getUserTrans() { - return com.arjuna.ats.jta.UserTransaction.userTransaction(); - } - - /** - * Gets the transaction synchronization registry. - * - * @return the transaction synchronization registry - */ - public TransactionSynchronizationRegistry getTransSyncReg() { - return SingletonRegistry.transreg; - } - - /** - * Gets the KIE services. - * - * @return the KIE services - */ - public KieServices getKieServices() { - return KieServices.Factory.get(); - } - - /** - * Loads properties from a file. - * - * @param filenm - * name of the file to load - * @return properties, as loaded from the file - * @throws IOException - * if an error occurs reading from the file - */ - public Properties loadProperties(String filenm) throws IOException { - return PropertyUtil.getProperties(filenm); - } - - /** - * Makes a Data Source. - * - * @param dsProps - * data source properties - * @return a new data source - */ - public BasicDataSource makeDataSource(Properties dsProps) { - try { - return BasicDataSourceFactory.createDataSource(dsProps); - - } catch (Exception e) { - throw new PersistenceFeatureException(e); - } - } - - /** - * Makes a new JPA connector for drools sessions. - * - * @param emf - * entity manager factory - * @return a new JPA connector for drools sessions - */ - public DroolsSessionConnector makeJpaConnector(EntityManagerFactory emf) { - return new JpaDroolsSessionConnector(emf); - } - - /** - * Makes a new entity manager factory. - * - * @param props - * properties with which the factory should be configured - * @return a new entity manager factory - */ - public EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { - return Persistence.createEntityManagerFactory("onapsessionsPU", props); - } - - /** - * Gets the policy controller associated with a given policy container. - * - * @param container - * container whose controller is to be retrieved - * @return the container's controller - */ - public PolicyController getPolicyController(PolicyContainer container) { - return PolicyController.factory.get(container.getGroupId(), container.getArtifactId()); - } - } - - /** - * Runtime exceptions generated by this class. Wraps exceptions generated by - * delegated operations, particularly when they are not, themselves, Runtime - * exceptions. - */ - public static class PersistenceFeatureException extends RuntimeException { - private static final long serialVersionUID = 1L; - - /** - * - * @param e - * exception to be wrapped - */ - public PersistenceFeatureException(Exception e) { - super(e); - } - } + private static final Logger logger = LoggerFactory.getLogger(PersistenceFeature.class); + + /** Standard factory used to get various items. */ + private static Factory stdFactory = new Factory(); + + /** Factory used to get various items. */ + private Factory fact = stdFactory; + + /** KieService factory. */ + private KieServices kieSvcFact; + + /** Persistence properties. */ + private Properties persistProps; + + /** Whether or not the SessionInfo records should be cleaned out. */ + private boolean sessInfoCleaned; + + /** SessionInfo timeout, in milli-seconds, as read from + * {@link #persistProps}. */ + private long sessionInfoTimeoutMs; + + /** Object used to serialize cleanup of sessioninfo table. */ + private Object cleanupLock = new Object(); + + /** + * Sets the factory to be used during junit testing. + * + * @param fact factory to be used + */ + protected void setFactory(Factory fact) { + this.fact = fact; + } + + /** + * Lookup the adjunct for this feature that is associated with the specified PolicyContainer. If + * not found, create one. + * + * @param policyContainer the container whose adjunct we are looking up, and possibly creating + * @return the associated 'ContainerAdjunct' instance, which may be new + */ + private ContainerAdjunct getContainerAdjunct(PolicyContainer policyContainer) { + + Object rval = policyContainer.getAdjunct(this); + + if (rval == null || !(rval instanceof ContainerAdjunct)) { + // adjunct does not exist, or has the wrong type (should never + // happen) + rval = new ContainerAdjunct(policyContainer); + policyContainer.setAdjunct(this, rval); + } + + return (ContainerAdjunct) rval; + } + + /** + * {@inheritDoc} */ + @Override + public int getSequenceNumber() { + return 1; + } + + /** + * {@inheritDoc} */ + @Override + public void globalInit(String[] args, String configDir) { + + kieSvcFact = fact.getKieServices(); + + try { + persistProps = fact.loadProperties(configDir + "/feature-session-persistence.properties"); + + } catch (IOException e1) { + logger.error("initializePersistence: ", e1); + } + + sessionInfoTimeoutMs = getPersistenceTimeout(); + } + + /** + * Creates a persistent KieSession, loading it from the persistent store, or creating one, if it + * does not exist yet. + */ + @Override + public KieSession activatePolicySession( + PolicyContainer policyContainer, String name, String kieBaseName) { + + if (isPersistenceEnabled(policyContainer, name)) { + cleanUpSessionInfo(); + + return getContainerAdjunct(policyContainer).newPersistentKieSession(name, kieBaseName); + } + + return null; + } + + /** + * {@inheritDoc} */ + @Override + public PolicySession.ThreadModel selectThreadModel(PolicySession session) { + + PolicyContainer policyContainer = session.getPolicyContainer(); + if (isPersistenceEnabled(policyContainer, session.getName())) { + return new PersistentThreadModel(session, getProperties(policyContainer)); + } + return null; + } + + /** + * {@inheritDoc} */ + @Override + public void disposeKieSession(PolicySession policySession) { + + ContainerAdjunct contAdj = + (ContainerAdjunct) policySession.getPolicyContainer().getAdjunct(this); + if (contAdj != null) { + contAdj.disposeKieSession(policySession.getName()); + } + } + + /** + * {@inheritDoc} */ + @Override + public void destroyKieSession(PolicySession policySession) { + + ContainerAdjunct contAdj = + (ContainerAdjunct) policySession.getPolicyContainer().getAdjunct(this); + if (contAdj != null) { + contAdj.destroyKieSession(policySession.getName()); + } + } + + /** + * {@inheritDoc} */ + @Override + public boolean afterStart(PolicyEngine engine) { + return false; + } + + /** + * {@inheritDoc} */ + @Override + public boolean beforeStart(PolicyEngine engine) { + synchronized (cleanupLock) { + sessInfoCleaned = false; + } + + return false; + } + + /** + * {@inheritDoc} */ + @Override + public boolean beforeActivate(PolicyEngine engine) { + synchronized (cleanupLock) { + sessInfoCleaned = false; + } + + return false; + } + + /** + * {@inheritDoc} */ + @Override + public boolean afterActivate(PolicyEngine engine) { + return false; + } + + /* ============================================================ */ + + /** + * Gets the persistence timeout value for sessioninfo records. + * + * @return the timeout value, in milli-seconds, or {@code -1} if it is unspecified or invalid + */ + private long getPersistenceTimeout() { + String timeoutString = null; + + try { + timeoutString = persistProps.getProperty(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT); + + if (timeoutString != null) { + // timeout parameter is specified + return Long.valueOf(timeoutString) * 1000; + } + + } catch (NumberFormatException e) { + logger.error( + "Invalid value for Drools persistence property persistence.sessioninfo.timeout: {}", + timeoutString, + e); + } + + return -1; + } + + /* ============================================================ */ + + /** + * Each instance of this class is a logical extension of a 'PolicyContainer' instance. Its + * reference is stored in the 'adjuncts' table within the 'PolicyContainer', and will be + * garbage-collected with the container. + */ + protected class ContainerAdjunct { + /** 'PolicyContainer' instance that this adjunct is extending. */ + private PolicyContainer policyContainer; + + /** Maps a KIE session name to its data source. */ + private Map<String, DsEmf> name2ds = new HashMap<>(); + + /** + * Constructor - initialize a new 'ContainerAdjunct'. + * + * @param policyContainer the 'PolicyContainer' instance this adjunct is extending + */ + private ContainerAdjunct(PolicyContainer policyContainer) { + this.policyContainer = policyContainer; + } + + /** + * Create a new persistent KieSession. If there is already a corresponding entry in the + * database, it is used to initialize the KieSession. If not, a completely new session is + * created. + * + * @param name the name of the KieSession (which is also the name of the associated + * PolicySession) + * @param kieBaseName the name of the 'KieBase' instance containing this session + * @return a new KieSession with persistence enabled + */ + private KieSession newPersistentKieSession(String name, String kieBaseName) { + + configureSysProps(); + + BasicDataSource ds = fact.makeDataSource(getDataSourceProperties()); + DsEmf dsemf = new DsEmf(ds); + + try { + EntityManagerFactory emf = dsemf.emf; + DroolsSessionConnector conn = fact.makeJpaConnector(emf); + + long desiredSessionId = getSessionId(conn, name); + + logger.info( + "\n\nThis controller is primary... coming up with session {} \n\n", desiredSessionId); + + // session does not exist -- attempt to create one + logger.info( + "getPolicySession:session does not exist -- attempt to create one with name {}", name); + + Environment env = kieSvcFact.newEnvironment(); + + configureKieEnv(env, emf); + + KieSessionConfiguration kieConf = kieSvcFact.newKieSessionConfiguration(); + + KieSession kieSession = + (desiredSessionId >= 0 + ? loadKieSession(kieBaseName, desiredSessionId, env, kieConf) + : null); + + if (kieSession == null) { + // loadKieSession() returned null or desiredSessionId < 0 + logger.info( + "LOADING We cannot load session {}. Going to create a new one", desiredSessionId); + + kieSession = newKieSession(kieBaseName, env); + } + + replaceSession(conn, name, kieSession); + + name2ds.put(name, dsemf); + + return kieSession; + + } catch (RuntimeException e) { + dsemf.close(); + throw e; + } + } + + /** + * Loads an existing KieSession from the persistent store. + * + * @param kieBaseName the name of the 'KieBase' instance containing this session + * @param desiredSessionId id of the desired KieSession + * @param env Kie Environment for the session + * @param kConf Kie Configuration for the session + * @return the persistent session, or {@code null} if it could not be loaded + */ + private KieSession loadKieSession( + String kieBaseName, long desiredSessionId, Environment env, KieSessionConfiguration kieConf) { + try { + KieSession kieSession = + kieSvcFact + .getStoreServices() + .loadKieSession( + desiredSessionId, + policyContainer.getKieContainer().getKieBase(kieBaseName), + kieConf, + env); + + logger.info("LOADING Loaded session {}", desiredSessionId); + + return kieSession; + + } catch (Exception e) { + logger.error("loadKieSession error: ", e); + return null; + } + } + + /** + * Creates a new, persistent KieSession. + * + * @param kieBaseName the name of the 'KieBase' instance containing this session + * @param env Kie Environment for the session + * @return a new, persistent session + */ + private KieSession newKieSession(String kieBaseName, Environment env) { + KieSession kieSession = + kieSvcFact + .getStoreServices() + .newKieSession(policyContainer.getKieContainer().getKieBase(kieBaseName), null, env); + + logger.info("LOADING CREATED {}", kieSession.getIdentifier()); + + return kieSession; + } + + /** + * Closes the data source associated with a session. + * + * @param name name of the session being destroyed + */ + private void destroyKieSession(String name) { + closeDataSource(name); + } + + /** + * Closes the data source associated with a session. + * + * @param name name of the session being disposed of + */ + private void disposeKieSession(String name) { + closeDataSource(name); + } + + /** + * Closes the data source associated with a session. + * + * @param name name of the session whose data source is to be closed + */ + private void closeDataSource(String name) { + DsEmf ds = name2ds.remove(name); + if (ds != null) { + ds.close(); + } + } + + /** Configures java system properties for JPA/JTA. */ + private void configureSysProps() { + System.setProperty("com.arjuna.ats.arjuna.coordinator.defaultTimeout", "60"); + System.setProperty( + "com.arjuna.ats.arjuna.objectstore.objectStoreDir", + persistProps.getProperty(DroolsPersistenceProperties.JTA_OBJECTSTORE_DIR)); + System.setProperty( + "ObjectStoreEnvironmentBean.objectStoreDir", + persistProps.getProperty(DroolsPersistenceProperties.JTA_OBJECTSTORE_DIR)); + } + + /** + * Configures a Kie Environment. + * + * @param env environment to be configured + * @param emf entity manager factory + */ + private void configureKieEnv(Environment env, EntityManagerFactory emf) { + env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf); + env.set(EnvironmentName.TRANSACTION, fact.getUserTrans()); + env.set(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY, fact.getTransSyncReg()); + env.set(EnvironmentName.TRANSACTION_MANAGER, fact.getTransMgr()); + } + + /** + * Gets a session's ID from the persistent store. + * + * @param conn persistence connector + * @param sessnm name of the session + * @return the session's id, or {@code -1} if the session is not found + */ + private long getSessionId(DroolsSessionConnector conn, String sessnm) { + DroolsSession sess = conn.get(sessnm); + return sess != null ? sess.getSessionId() : -1; + } + + /** + * Replaces a session within the persistent store, if it exists. Adds it otherwise. + * + * @param conn persistence connector + * @param sessnm name of session to be updated + * @param kieSession new session information + */ + private void replaceSession(DroolsSessionConnector conn, String sessnm, KieSession kieSession) { + + DroolsSessionEntity sess = new DroolsSessionEntity(); + + sess.setSessionName(sessnm); + sess.setSessionId(kieSession.getIdentifier()); + + conn.replace(sess); + } + } + + /* ============================================================ */ + + /** + * Gets the data source properties. + * + * @return the data source properties + */ + private Properties getDataSourceProperties() { + Properties props = new Properties(); + props.put("driverClassName", persistProps.getProperty(DroolsPersistenceProperties.DB_DRIVER)); + props.put("url", persistProps.getProperty(DroolsPersistenceProperties.DB_URL)); + props.put("username", persistProps.getProperty(DroolsPersistenceProperties.DB_USER)); + props.put("password", persistProps.getProperty(DroolsPersistenceProperties.DB_PWD)); + props.put("maxActive", "3"); + props.put("maxIdle", "1"); + props.put("maxWait", "120000"); + props.put("whenExhaustedAction", "2"); + props.put("testOnBorrow", "false"); + props.put("poolPreparedStatements", "true"); + + return props; + } + + /** + * Removes "old" Drools 'sessioninfo' records, so they aren't used to restore data to Drools + * sessions. This also has the useful side-effect of removing abandoned records as well. + */ + private void cleanUpSessionInfo() { + + synchronized (cleanupLock) { + if (sessInfoCleaned) { + logger.info("Clean up of sessioninfo table: already done"); + return; + } + + if (sessionInfoTimeoutMs < 0) { + logger.info("Clean up of sessioninfo table: no timeout specified"); + return; + } + + // now do the record deletion + try (BasicDataSource ds = fact.makeDataSource(getDataSourceProperties()); + Connection connection = ds.getConnection(); + PreparedStatement statement = + connection.prepareStatement( + "DELETE FROM sessioninfo WHERE timestampdiff(second,lastmodificationdate,now()) > ?")) { + + connection.setAutoCommit(true); + + statement.setLong(1, sessionInfoTimeoutMs / 1000); + + int count = statement.executeUpdate(); + logger.info("Cleaning up sessioninfo table -- {} records removed", count); + + } catch (SQLException e) { + logger.error("Clean up of sessioninfo table failed", e); + } + + // TODO: delete DroolsSessionEntity where sessionId not in + // (sessinfo.xxx) + + sessInfoCleaned = true; + } + } + + /** + * Determine whether persistence is enabled for a specific container. + * + * @param container container to be checked + * @param sessionName name of the session to be checked + * @return {@code true} if persistence is enabled for this container, and {@code false} if not + */ + private boolean isPersistenceEnabled(PolicyContainer container, String sessionName) { + Properties properties = getProperties(container); + boolean rval = false; + + if (properties != null) { + // fetch the 'type' property + String type = getProperty(properties, sessionName, "type"); + rval = "auto".equals(type) || "native".equals(type); + } + + return rval; + } + + /** + * Determine the controller properties associated with the policy container. + * + * @param container container whose properties are to be retrieved + * @return the container's properties, or {@code null} if not found + */ + private Properties getProperties(PolicyContainer container) { + try { + return fact.getPolicyController(container).getProperties(); + } catch (IllegalArgumentException e) { + logger.error("getProperties exception: ", e); + return null; + } + } + + /** + * Fetch the persistence property associated with a session. The name may have the form: + * + * <ul> + * <li>persistence.SESSION-NAME.PROPERTY + * <li>persistence.PROPERTY + * </ul> + * + * @param properties properties from which the value is to be retrieved + * @param sessionName session name of interest + * @param property property name of interest + * @return the property value, or {@code null} if not found + */ + private String getProperty(Properties properties, String sessionName, String property) { + String value = properties.getProperty("persistence." + sessionName + "." + property); + if (value == null) { + value = properties.getProperty("persistence." + property); + } + + return value; + } + + /* ============================================================ */ + + /** + * This 'ThreadModel' variant periodically calls 'KieSession.fireAllRules()', because the + * 'fireUntilHalt' method isn't compatible with persistence. + */ + public class PersistentThreadModel implements Runnable, PolicySession.ThreadModel { + + /** Session associated with this persistent thread. */ + private final PolicySession session; + + /** The session thread. */ + private final Thread thread; + + /** Used to indicate that processing should stop. */ + private final CountDownLatch stopped = new CountDownLatch(1); + + /** Minimum time, in milli-seconds, that the thread should sleep before firing rules again. */ + long minSleepTime = 100; + + /** + * Maximum time, in milli-seconds, that the thread should sleep before firing rules again. This + * is a "half" time, so that we can multiply it by two without overflowing the word size. + */ + long halfMaxSleepTime = 5000L / 2L; + + /** + * Constructor - initialize variables and create thread. + * + * @param session the 'PolicySession' instance + * @param properties may contain additional session properties + */ + public PersistentThreadModel(PolicySession session, Properties properties) { + this.session = session; + this.thread = new Thread(this, getThreadName()); + + if (properties == null) { + return; + } + + // extract 'minSleepTime' and/or 'maxSleepTime' + String name = session.getName(); + + // fetch 'minSleepTime' value, and update if defined + String sleepTimeString = getProperty(properties, name, "minSleepTime"); + if (sleepTimeString != null) { + try { + minSleepTime = Math.max(1, Integer.valueOf(sleepTimeString)); + } catch (Exception e) { + logger.error(sleepTimeString + ": Illegal value for 'minSleepTime'", e); + } + } + + // fetch 'maxSleepTime' value, and update if defined + long maxSleepTime = 2 * halfMaxSleepTime; + sleepTimeString = getProperty(properties, name, "maxSleepTime"); + if (sleepTimeString != null) { + try { + maxSleepTime = Math.max(1, Integer.valueOf(sleepTimeString)); + } catch (Exception e) { + logger.error(sleepTimeString + ": Illegal value for 'maxSleepTime'", e); + } + } + + // swap values if needed + if (minSleepTime > maxSleepTime) { + logger.error( + "minSleepTime(" + + minSleepTime + + ") is greater than maxSleepTime(" + + maxSleepTime + + ") -- swapping"); + long tmp = minSleepTime; + minSleepTime = maxSleepTime; + maxSleepTime = tmp; + } + + halfMaxSleepTime = Math.max(1, maxSleepTime / 2); + } + + /** + * Get thread name. + * + * @return the String to use as the thread name */ + private String getThreadName() { + return "Session " + session.getFullName() + " (persistent)"; + } + + /** ************************ */ + /* 'ThreadModel' interface */ + /** ************************ */ + + /** + * {@inheritDoc} */ + @Override + public void start() { + thread.start(); + } + + /** + * {@inheritDoc} */ + @Override + public void stop() { + // tell the thread to stop + stopped.countDown(); + + // wait up to 10 seconds for the thread to stop + try { + thread.join(10000); + + } catch (InterruptedException e) { + logger.error("stopThread exception: ", e); + Thread.currentThread().interrupt(); + } + + // verify that it's done + if (thread.isAlive()) { + logger.error("stopThread: still running"); + } + } + + /** + * {@inheritDoc} */ + @Override + public void updated() { + // the container artifact has been updated -- adjust the thread name + thread.setName(getThreadName()); + } + + /** ********************* */ + /* 'Runnable' interface */ + /** ********************* */ + + /** + * {@inheritDoc} */ + @Override + public void run() { + logger.info("PersistentThreadModel running"); + + // set thread local variable + session.setPolicySession(); + + KieSession kieSession = session.getKieSession(); + long sleepTime = 2 * halfMaxSleepTime; + + // We want to continue, despite any exceptions that occur + // while rules are fired. + + boolean cont = true; + while (cont) { + + try { + if (kieSession.fireAllRules() > 0) { + // some rules fired -- reduce poll delay + sleepTime = Math.max(minSleepTime, sleepTime / 2); + } else { + // no rules fired -- increase poll delay + sleepTime = 2 * Math.min(halfMaxSleepTime, sleepTime); + } + + } catch (Exception | LinkageError e) { + logger.error("Exception during kieSession.fireAllRules", e); + } + + try { + if (stopped.await(sleepTime, TimeUnit.MILLISECONDS)) { + cont = false; + } + + } catch (InterruptedException e) { + logger.error("startThread exception: ", e); + Thread.currentThread().interrupt(); + cont = false; + } + } + + logger.info("PersistentThreadModel completed"); + } + } + + /* ============================================================ */ + + /** DataSource-EntityManagerFactory pair. */ + private class DsEmf { + private BasicDataSource bds; + private EntityManagerFactory emf; + + /** + * Makes an entity manager factory for the given data source. + * + * @param bds pooled data source + */ + public DsEmf(BasicDataSource bds) { + try { + Map<String, Object> props = new HashMap<>(); + props.put(org.hibernate.cfg.Environment.JPA_JTA_DATASOURCE, bds); + + this.bds = bds; + this.emf = fact.makeEntMgrFact(props); + + } catch (RuntimeException e) { + closeDataSource(); + throw e; + } + } + + /** Closes the entity manager factory and the data source. */ + public void close() { + try { + emf.close(); + + } catch (RuntimeException e) { + closeDataSource(); + throw e; + } + + closeDataSource(); + } + + /** Closes the data source only. */ + private void closeDataSource() { + try { + bds.close(); + + } catch (SQLException e) { + throw new PersistenceFeatureException(e); + } + } + } + + private static class SingletonRegistry { + private static final TransactionSynchronizationRegistry transreg = + new com.arjuna.ats.internal.jta.transaction.arjunacore + .TransactionSynchronizationRegistryImple(); + + private SingletonRegistry() { + super(); + } + } + + /** Factory for various items. Methods can be overridden for junit testing. */ + protected static class Factory { + + /** + * Gets the transaction manager. + * + * @return the transaction manager + */ + public TransactionManager getTransMgr() { + return com.arjuna.ats.jta.TransactionManager.transactionManager(); + } + + /** + * Gets the user transaction. + * + * @return the user transaction + */ + public UserTransaction getUserTrans() { + return com.arjuna.ats.jta.UserTransaction.userTransaction(); + } + + /** + * Gets the transaction synchronization registry. + * + * @return the transaction synchronization registry + */ + public TransactionSynchronizationRegistry getTransSyncReg() { + return SingletonRegistry.transreg; + } + + /** + * Gets the KIE services. + * + * @return the KIE services + */ + public KieServices getKieServices() { + return KieServices.Factory.get(); + } + + /** + * Loads properties from a file. + * + * @param filenm name of the file to load + * @return properties, as loaded from the file + * @throws IOException if an error occurs reading from the file + */ + public Properties loadProperties(String filenm) throws IOException { + return PropertyUtil.getProperties(filenm); + } + + /** + * Makes a Data Source. + * + * @param dsProps data source properties + * @return a new data source + */ + public BasicDataSource makeDataSource(Properties dsProps) { + try { + return BasicDataSourceFactory.createDataSource(dsProps); + + } catch (Exception e) { + throw new PersistenceFeatureException(e); + } + } + + /** + * Makes a new JPA connector for drools sessions. + * + * @param emf entity manager factory + * @return a new JPA connector for drools sessions + */ + public DroolsSessionConnector makeJpaConnector(EntityManagerFactory emf) { + return new JpaDroolsSessionConnector(emf); + } + + /** + * Makes a new entity manager factory. + * + * @param props properties with which the factory should be configured + * @return a new entity manager factory + */ + public EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { + return Persistence.createEntityManagerFactory("onapsessionsPU", props); + } + + /** + * Gets the policy controller associated with a given policy container. + * + * @param container container whose controller is to be retrieved + * @return the container's controller + */ + public PolicyController getPolicyController(PolicyContainer container) { + return PolicyController.factory.get(container.getGroupId(), container.getArtifactId()); + } + } + + /** + * Runtime exceptions generated by this class. Wraps exceptions generated by delegated operations, + * particularly when they are not, themselves, Runtime exceptions. + */ + public static class PersistenceFeatureException extends RuntimeException { + private static final long serialVersionUID = 1L; + + /** + * Constructor. + * */ + public PersistenceFeatureException(Exception ex) { + super(ex); + } + } } diff --git a/feature-session-persistence/src/main/resources/META-INF/persistence.xml b/feature-session-persistence/src/main/resources/META-INF/persistence.xml index 6b8345c3..661803a5 100644 --- a/feature-session-persistence/src/main/resources/META-INF/persistence.xml +++ b/feature-session-persistence/src/main/resources/META-INF/persistence.xml @@ -3,7 +3,7 @@ ============LICENSE_START======================================================= policy-core ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,37 +20,54 @@ --> <persistence version="2.1" - xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> - <persistence-unit name="onapsessionsPU" transaction-type="JTA"> - <!-- Used for drools session data access --> - <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> - <class>org.onap.policy.drools.persistence.DroolsSessionEntity</class> - <class>org.drools.persistence.info.SessionInfo</class> - <class>org.drools.persistence.info.WorkItemInfo</class> - <properties> - <property name="hibernate.dialect" value="org.hibernate.dialect.MariaDBDialect" /> - <property name="hibernate.max_fetch_depth" value="3" /> - <property name="hibernate.hbm2ddl.auto" value="update" /> - <property name="hibernate.show_sql" value="false" /> - <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" /> - <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" /> - </properties> - </persistence-unit> - - <persistence-unit name="schemaDroolsPU" transaction-type="RESOURCE_LOCAL"> - <!-- Limited use for generating the DB and schema files for drools DB - uses eclipselink for convenience --> - <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> - <class>org.onap.policy.drools.persistence.DroolsSessionEntity</class> - <class>org.drools.persistence.info.SessionInfo</class> - <class>org.drools.persistence.info.WorkItemInfo</class> - <properties> - <property name="hibernate.dialect" value="org.hibernate.dialect.MariaDBDialect" /> - <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/> - <property name="javax.persistence.schema-generation.scripts.create-target" value="sql/generatedCreateDrools.ddl"/> - <property name="javax.persistence.schema-generation.scripts.drop-target" value="sql/generatedDropDrools.ddl"/> + <persistence-unit name="onapsessionsPU" + transaction-type="JTA"> + <!-- Used for drools session data access --> + <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> + <class>org.onap.policy.drools.persistence.DroolsSessionEntity</class> + <class>org.drools.persistence.info.SessionInfo</class> + <class>org.drools.persistence.info.WorkItemInfo</class> + <properties> + <property name="hibernate.dialect" + value="org.hibernate.dialect.MariaDBDialect" /> + <property name="hibernate.max_fetch_depth" + value="3" /> + <property name="hibernate.hbm2ddl.auto" + value="update" /> + <property name="hibernate.show_sql" value="false" /> + <property + name="hibernate.transaction.factory_class" + value="org.hibernate.transaction.JTATransactionFactory" /> + <property + name="hibernate.transaction.manager_lookup_class" + value="org.hibernate.transaction.JBossTransactionManagerLookup" /> </properties> - </persistence-unit> - + </persistence-unit> + + <persistence-unit name="schemaDroolsPU" + transaction-type="RESOURCE_LOCAL"> + <!-- Limited use for generating the DB and schema files for drools DB - uses eclipselink for convenience --> + <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> + <class>org.onap.policy.drools.persistence.DroolsSessionEntity</class> + <class>org.drools.persistence.info.SessionInfo</class> + <class>org.drools.persistence.info.WorkItemInfo</class> + <properties> + <property name="hibernate.dialect" + value="org.hibernate.dialect.MariaDBDialect" /> + <property + name="javax.persistence.schema-generation.scripts.action" + value="drop-and-create" /> + <property + name="javax.persistence.schema-generation.scripts.create-target" + value="sql/generatedCreateDrools.ddl" /> + <property + name="javax.persistence.schema-generation.scripts.drop-target" + value="sql/generatedDropDrools.ddl" /> + </properties> + </persistence-unit> + </persistence> diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/DroolsSessionEntityTest.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/DroolsSessionEntityTest.java index 7624d043..b2dcfb52 100644 --- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/DroolsSessionEntityTest.java +++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/DroolsSessionEntityTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -31,173 +31,162 @@ import org.onap.policy.drools.persistence.DroolsSessionEntity; public class DroolsSessionEntityTest { - @Test - public void testHashCode() { - DroolsSessionEntity e = makeEnt("mynameA", 1); + @Test + public void testHashCode() { + DroolsSessionEntity entity = makeEnt("mynameA", 1); - DroolsSessionEntity e2 = makeEnt("mynameA", 2); + DroolsSessionEntity e2 = makeEnt("mynameA", 2); - // session id is not part of hash code - assertTrue(e.hashCode() == e2.hashCode()); + // session id is not part of hash code + assertTrue(entity.hashCode() == e2.hashCode()); - // diff sess name - e2 = makeEnt("mynameB", 1); - assertTrue(e.hashCode() != e2.hashCode()); - } + // diff sess name + e2 = makeEnt("mynameB", 1); + assertTrue(entity.hashCode() != e2.hashCode()); + } - /** - * Ensures that hashCode() functions as expected when the getXxx methods are - * overridden. - */ - @Test - public void testHashCode_Subclass() { - DroolsSessionEntity e = makeEnt2("mynameA", 1); + /** Ensures that hashCode() functions as expected when the getXxx methods are overridden. */ + @Test + public void testHashCode_Subclass() { + DroolsSessionEntity entity = makeEnt2("mynameA", 1); - DroolsSessionEntity e2 = makeEnt("mynameA", 2); + DroolsSessionEntity e2 = makeEnt("mynameA", 2); - // session id is not part of hash code - assertTrue(e.hashCode() == e2.hashCode()); + // session id is not part of hash code + assertTrue(entity.hashCode() == e2.hashCode()); - // diff sess name - e2 = makeEnt("mynameB", 1); - assertTrue(e.hashCode() != e2.hashCode()); - } + // diff sess name + e2 = makeEnt("mynameB", 1); + assertTrue(entity.hashCode() != e2.hashCode()); + } - @Test - public void testGetSessionName_testSetSessionName() { - DroolsSessionEntity e = makeEnt("mynameZ", 1); + @Test + public void testGetSessionName_testSetSessionName() { + DroolsSessionEntity entity = makeEnt("mynameZ", 1); - assertEquals("mynameZ", e.getSessionName()); + assertEquals("mynameZ", entity.getSessionName()); - e.setSessionName("another"); - assertEquals("another", e.getSessionName()); + entity.setSessionName("another"); + assertEquals("another", entity.getSessionName()); - // others unchanged - assertEquals(1, e.getSessionId()); - } + // others unchanged + assertEquals(1, entity.getSessionId()); + } - @Test - public void testGetSessionId_testSetSessionId() { - DroolsSessionEntity e = makeEnt("mynameA", 1); + @Test + public void testGetSessionId_testSetSessionId() { + DroolsSessionEntity entity = makeEnt("mynameA", 1); - assertEquals(1, e.getSessionId()); - - e.setSessionId(20); - assertEquals(20, e.getSessionId()); - - // others unchanged - assertEquals("mynameA", e.getSessionName()); - } + assertEquals(1, entity.getSessionId()); - @Test - public void testGetCreatedDate_testSetCreatedDate_testGetUpdatedDate_testSetUpdatedDate() { - DroolsSessionEntity e = new DroolsSessionEntity(); + entity.setSessionId(20); + assertEquals(20, entity.getSessionId()); - Date crtdt = new Date(System.currentTimeMillis() - 100); - e.setCreatedDate(crtdt); - - Date updt = new Date(System.currentTimeMillis() - 200); - e.setUpdatedDate(updt); - - assertEquals(crtdt, e.getCreatedDate()); - assertEquals(updt, e.getUpdatedDate()); - } - - @Test - public void testEqualsObject() { - DroolsSessionEntity e = makeEnt("mynameA", 1); - - // reflexive - assertTrue(e.equals(e)); - - DroolsSessionEntity e2 = makeEnt("mynameA", 2); - - // session id is not part of hash code - assertTrue(e.equals(e2)); - assertTrue(e.equals(e2)); - - // diff sess name - e2 = makeEnt("mynameB", 1); - assertFalse(e.equals(e2)); - assertFalse(e.equals(e2)); - } - - /** - * Ensures that equals() functions as expected when the getXxx methods are - * overridden. - */ - @Test - public void testEqualsObject_Subclass() { - DroolsSessionEntity e = makeEnt2("mynameA", 1); - - // reflexive - assertTrue(e.equals(e)); - - DroolsSessionEntity e2 = makeEnt("mynameA", 2); - - // session id is not part of hash code - assertTrue(e.equals(e2)); - assertTrue(e.equals(e2)); - - // diff sess name - e2 = makeEnt("mynameB", 1); - assertFalse(e.equals(e2)); - assertFalse(e.equals(e2)); - } - - @Test - public void testToString() { - DroolsSessionEntity e = makeEnt("mynameA", 23); - - assertEquals("{name=mynameA, id=23}", e.toString()); - } - - /** - * Makes a session Entity. The parameters are stored into the Entity object - * via the setXxx methods. - * - * @param sessnm - * session name - * @param sessid - * session id - * @return a new session Entity - */ - private DroolsSessionEntity makeEnt(String sessnm, long sessid) { - - DroolsSessionEntity e = new DroolsSessionEntity(); - - e.setSessionName(sessnm); - e.setSessionId(sessid); - - return e; - } - - /** - * Makes a session Entity that overrides the getXxx methods. The parameters - * that are provided are returned by the overridden methods, but they are - * <i>not</i> stored into the Entity object via the setXxx methods. - * - * @param sessnm - * session name - * @param sessid - * session id - * @return a new session Entity - */ - @SuppressWarnings("serial") - private DroolsSessionEntity makeEnt2(String sessnm, long sessid) { - - return new DroolsSessionEntity() { - - @Override - public String getSessionName() { - return sessnm; - } - - @Override - public long getSessionId() { - return sessid; - } - }; - } + // others unchanged + assertEquals("mynameA", entity.getSessionName()); + } + @Test + public void testGetCreatedDate_testSetCreatedDate_testGetUpdatedDate_testSetUpdatedDate() { + DroolsSessionEntity entity = new DroolsSessionEntity(); + + Date crtdt = new Date(System.currentTimeMillis() - 100); + entity.setCreatedDate(crtdt); + + Date updt = new Date(System.currentTimeMillis() - 200); + entity.setUpdatedDate(updt); + + assertEquals(crtdt, entity.getCreatedDate()); + assertEquals(updt, entity.getUpdatedDate()); + } + + @Test + public void testEqualsObject() { + DroolsSessionEntity entity = makeEnt("mynameA", 1); + + // reflexive + assertTrue(entity.equals(entity)); + + DroolsSessionEntity e2 = makeEnt("mynameA", 2); + + // session id is not part of hash code + assertTrue(entity.equals(e2)); + assertTrue(entity.equals(e2)); + + // diff sess name + e2 = makeEnt("mynameB", 1); + assertFalse(entity.equals(e2)); + assertFalse(entity.equals(e2)); + } + + /** Ensures that equals() functions as expected when the getXxx methods are overridden. */ + @Test + public void testEqualsObject_Subclass() { + DroolsSessionEntity entity = makeEnt2("mynameA", 1); + + // reflexive + assertTrue(entity.equals(entity)); + + DroolsSessionEntity e2 = makeEnt("mynameA", 2); + + // session id is not part of hash code + assertTrue(entity.equals(e2)); + assertTrue(entity.equals(e2)); + + // diff sess name + e2 = makeEnt("mynameB", 1); + assertFalse(entity.equals(e2)); + assertFalse(entity.equals(e2)); + } + + @Test + public void testToString() { + DroolsSessionEntity entity = makeEnt("mynameA", 23); + + assertEquals("{name=mynameA, id=23}", entity.toString()); + } + + /** + * Makes a session Entity. The parameters are stored into the Entity object via the setXxx + * methods. + * + * @param sessnm session name + * @param sessid session id + * @return a new session Entity + */ + private DroolsSessionEntity makeEnt(String sessnm, long sessid) { + + DroolsSessionEntity entity = new DroolsSessionEntity(); + + entity.setSessionName(sessnm); + entity.setSessionId(sessid); + + return entity; + } + + /** + * Makes a session Entity that overrides the getXxx methods. The parameters that are provided are + * returned by the overridden methods, but they are <i>not</i> stored into the Entity object via + * the setXxx methods. + * + * @param sessnm session name + * @param sessid session id + * @return a new session Entity + */ + @SuppressWarnings("serial") + private DroolsSessionEntity makeEnt2(String sessnm, long sessid) { + + return new DroolsSessionEntity() { + + @Override + public String getSessionName() { + return sessnm; + } + + @Override + public long getSessionId() { + return sessid; + } + }; + } } diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java index 9c9a30b3..aba1d80e 100644 --- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java +++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,13 +20,14 @@ package org.onap.policy.drools.persistence; +import static org.junit.Assert.assertEquals; + +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.doThrow; import javax.persistence.EntityManager; import javax.transaction.HeuristicMixedException; @@ -47,399 +48,402 @@ import org.slf4j.LoggerFactory; public class EntityMgrTransTest { - private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class); - - private static UserTransaction savetrans; - - private UserTransaction trans; - private EntityManager mgr; - - @BeforeClass - public static void setUpBeforeClass() { - System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); - System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); - - savetrans = EntityMgrTrans.getUserTrans(); - } - - @AfterClass - public static void tearDownAfterClass() { - EntityMgrTrans.setUserTrans(savetrans); - } - - @Before - public void setUp() throws Exception { - trans = mock(UserTransaction.class); - mgr = mock(EntityManager.class); - - EntityMgrTrans.setUserTrans(trans); - } - - /** - * Verifies that the constructor starts a transaction, but does not do - * anything extra before being closed. - * - * @throws Exception - */ - @Test - public void testEntityMgrTrans() throws Exception { - - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - - EntityMgrTrans t = new EntityMgrTrans(mgr); - - // verify that transaction was started - verify(trans).begin(); - - // verify not closed, committed, or rolled back yet - verify(trans, never()).commit(); - verify(trans, never()).rollback(); - verify(mgr, never()).close(); - - t.close(); - } - - @Test(expected = EntityMgrException.class) - public void testEntityMgrTrans_RtEx() throws Exception { - - doThrow(new IllegalArgumentException("expected exception")).when(trans).begin(); - - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - - } - } - - @Test(expected = EntityMgrException.class) - public void testEntityMgrTrans_NotSuppEx() throws Exception { - - doThrow(new NotSupportedException("expected exception")).when(trans).begin(); - - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - - } - } - - @Test(expected = EntityMgrException.class) - public void testEntityMgrTrans_SysEx() throws Exception { - - doThrow(new SystemException("expected exception")).when(trans).begin(); - - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - - } - } - - /** - * Verifies that the transaction is rolled back and the manager is closed - * when and a transaction is active. - */ - @Test - public void testClose_Active() throws Exception { - EntityMgrTrans t = new EntityMgrTrans(mgr); - - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - - t.close(); - - // closed and rolled back, but not committed - verify(trans, never()).commit(); - verify(trans).rollback(); - verify(mgr).close(); - } - - /** - * Verifies that the manager is closed, but that the transaction is - * <i>not</i> rolled back when and no transaction is active. - */ - @Test - public void testClose_Inactive() throws Exception { - EntityMgrTrans t = new EntityMgrTrans(mgr); - - when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION); - - t.close(); - - // closed, but not committed or rolled back - verify(mgr).close(); - verify(trans, never()).commit(); - verify(trans, never()).rollback(); - } - - @Test(expected = EntityMgrException.class) - public void testClose_IllStateEx() throws Exception { - - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new IllegalStateException("expected exception")).when(trans).rollback(); - - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - - } - } - - @Test(expected = EntityMgrException.class) - public void testClose_SecEx() throws Exception { - - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new SecurityException("expected exception")).when(trans).rollback(); - - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - - } - } - - @Test(expected = EntityMgrException.class) - public void testClose_SysEx() throws Exception { - - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new SystemException("expected exception")).when(trans).rollback(); - - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - - } - } - - /** - * Verifies that the manager is closed and the transaction rolled back when - * "try" block exits normally and a transaction is active. - */ - @Test - public void testClose_TryWithoutExcept_Active() throws Exception { - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - - } - - // closed and rolled back, but not committed - verify(trans, never()).commit(); - verify(trans).rollback(); - verify(mgr).close(); - } + private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class); - /** - * Verifies that the manager is closed, but that the transaction is - * <i>not</i> rolled back when "try" block exits normally and no transaction - * is active. - */ - @Test - public void testClose_TryWithoutExcept_Inactive() throws Exception { + private static UserTransaction savetrans; - when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION); + private UserTransaction trans; + private EntityManager mgr; - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + /** + * Setup before the class. + * + */ + @BeforeClass + public static void setUpBeforeClass() { + System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); + System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); + + savetrans = EntityMgrTrans.getUserTrans(); + } - } + @AfterClass + public static void tearDownAfterClass() { + EntityMgrTrans.setUserTrans(savetrans); + } - // closed, but not rolled back or committed - verify(trans, never()).commit(); - verify(trans, never()).rollback(); - verify(mgr).close(); - } + /** + * Setup. + * + * @throws Exception exception + */ + @Before + public void setUp() throws Exception { + trans = mock(UserTransaction.class); + mgr = mock(EntityManager.class); - /** - * Verifies that the manager is closed and the transaction rolled back when - * "try" block throws an exception and a transaction is active. - */ - @Test - public void testClose_TryWithExcept_Active() throws Exception { + EntityMgrTrans.setUserTrans(trans); + } - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + /** + * Verifies that the constructor starts a transaction, but does not do anything extra before being + * closed. + * + * @throws Exception exception + */ + @Test + public void testEntityMgrTrans() throws Exception { - try { - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - throw new SystemException("expected exception"); - } + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - } catch (Exception e) { - logger.trace("expected exception", e); - } + final EntityMgrTrans newTrans = new EntityMgrTrans(mgr); - // closed and rolled back, but not committed - verify(trans, never()).commit(); - verify(trans).rollback(); - verify(mgr).close(); - } + // verify that transaction was started + verify(trans).begin(); - /** - * Verifies that the manager is closed, but that the transaction is - * <i>not</i> rolled back when "try" block throws an exception and no - * transaction is active. - */ - @Test - public void testClose_TryWithExcept_Inactive() throws Exception { + // verify not closed, committed, or rolled back yet + verify(trans, never()).commit(); + verify(trans, never()).rollback(); + verify(mgr, never()).close(); - when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION); + newTrans.close(); + } - try { - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - throw new SystemException("expected exception"); - } + @Test(expected = EntityMgrException.class) + public void testEntityMgrTrans_RtEx() throws Exception { - } catch (Exception e) { - logger.trace("expected exception", e); - } + doThrow(new IllegalArgumentException("expected exception")).when(trans).begin(); - // closed, but not rolled back or committed - verify(trans, never()).commit(); - verify(trans, never()).rollback(); - verify(mgr).close(); - } - - /** - * Verifies that commit() only commits, and that the subsequent close() does - * not re-commit. - */ - @Test - public void testCommit() throws Exception { - EntityMgrTrans t = new EntityMgrTrans(mgr); - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - - t.commit(); - - when(trans.getStatus()).thenReturn(Status.STATUS_COMMITTED); - - // committed, but not closed or rolled back - verify(trans).commit(); - verify(trans, never()).rollback(); - verify(mgr, never()).close(); - - // closed, but not re-committed - t.close(); + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + // Empty + } + } + + @Test(expected = EntityMgrException.class) + public void testEntityMgrTrans_NotSuppEx() throws Exception { - verify(trans, times(1)).commit(); - verify(mgr).close(); - } - - @Test(expected = EntityMgrException.class) - public void testCommit_SecEx() throws Exception { - - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new SecurityException("expected exception")).when(trans).commit(); + doThrow(new NotSupportedException("expected exception")).when(trans).begin(); + + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + // Empty + } + } + + @Test(expected = EntityMgrException.class) + public void testEntityMgrTrans_SysEx() throws Exception { + + doThrow(new SystemException("expected exception")).when(trans).begin(); + + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + // Empty + } + } + + /** + * Verifies that the transaction is rolled back and the manager is closed when and a transaction + * is active. + */ + @Test + public void testClose_Active() throws Exception { + EntityMgrTrans newTrans = new EntityMgrTrans(mgr); + + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + + newTrans.close(); + + // closed and rolled back, but not committed + verify(trans, never()).commit(); + verify(trans).rollback(); + verify(mgr).close(); + } + + /** + * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled back when + * and no transaction is active. + */ + @Test + public void testClose_Inactive() throws Exception { + EntityMgrTrans newTrans = new EntityMgrTrans(mgr); + + when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION); + + newTrans.close(); + + // closed, but not committed or rolled back + verify(mgr).close(); + verify(trans, never()).commit(); + verify(trans, never()).rollback(); + } + + @Test(expected = EntityMgrException.class) + public void testClose_IllStateEx() throws Exception { + + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new IllegalStateException("expected exception")).when(trans).rollback(); + + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + // Empty + } + } + + @Test(expected = EntityMgrException.class) + public void testClose_SecEx() throws Exception { + + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new SecurityException("expected exception")).when(trans).rollback(); + + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + // Empty + } + } + + @Test(expected = EntityMgrException.class) + public void testClose_SysEx() throws Exception { + + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new SystemException("expected exception")).when(trans).rollback(); + + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + // Empty + } + } + + /** + * Verifies that the manager is closed and the transaction rolled back when "try" block exits + * normally and a transaction is active. + */ + @Test + public void testClose_TryWithoutExcept_Active() throws Exception { + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + // Empty + } + + // closed and rolled back, but not committed + verify(trans, never()).commit(); + verify(trans).rollback(); + verify(mgr).close(); + } + + /** + * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled back when + * "try" block exits normally and no transaction is active. + */ + @Test + public void testClose_TryWithoutExcept_Inactive() throws Exception { + + when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION); + + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + // Empty + } + + // closed, but not rolled back or committed + verify(trans, never()).commit(); + verify(trans, never()).rollback(); + verify(mgr).close(); + } + + /** + * Verifies that the manager is closed and the transaction rolled back when "try" block throws an + * exception and a transaction is active. + */ + @Test + public void testClose_TryWithExcept_Active() throws Exception { + + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + + try { + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + throw new SystemException("expected exception"); + } + + } catch (Exception e) { + logger.trace("expected exception", e); + } + + // closed and rolled back, but not committed + verify(trans, never()).commit(); + verify(trans).rollback(); + verify(mgr).close(); + } + + /** + * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled back when + * "try" block throws an exception and no transaction is active. + */ + @Test + public void testClose_TryWithExcept_Inactive() throws Exception { + + when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION); + + try { + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + throw new SystemException("expected exception"); + } + + } catch (Exception e) { + logger.trace("expected exception", e); + } + + // closed, but not rolled back or committed + verify(trans, never()).commit(); + verify(trans, never()).rollback(); + verify(mgr).close(); + } + + /** Verifies that commit() only commits, and that the subsequent close() does not re-commit. */ + @Test + public void testCommit() throws Exception { + EntityMgrTrans newTrans = new EntityMgrTrans(mgr); + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + + newTrans.commit(); + + when(trans.getStatus()).thenReturn(Status.STATUS_COMMITTED); + + // committed, but not closed or rolled back + verify(trans).commit(); + verify(trans, never()).rollback(); + verify(mgr, never()).close(); + + // closed, but not re-committed + newTrans.close(); + + verify(trans, times(1)).commit(); + verify(mgr).close(); + } + + @Test(expected = EntityMgrException.class) + public void testCommit_SecEx() throws Exception { + + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new SecurityException("expected exception")).when(trans).commit(); + + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.commit(); + } + } - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.commit(); - } - } + @Test(expected = EntityMgrException.class) + public void testCommit_IllStateEx() throws Exception { - @Test(expected = EntityMgrException.class) - public void testCommit_IllStateEx() throws Exception { + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new IllegalStateException("expected exception")).when(trans).commit(); - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new IllegalStateException("expected exception")).when(trans).commit(); + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.commit(); + } + } - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.commit(); - } - } - - @Test(expected = EntityMgrException.class) - public void testCommit_RbEx() throws Exception { + @Test(expected = EntityMgrException.class) + public void testCommit_RbEx() throws Exception { - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new RollbackException("expected exception")).when(trans).commit(); - - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.commit(); - } - } + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new RollbackException("expected exception")).when(trans).commit(); - @Test(expected = EntityMgrException.class) - public void testCommit_HmEx() throws Exception { + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.commit(); + } + } - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new HeuristicMixedException("expected exception")).when(trans).commit(); + @Test(expected = EntityMgrException.class) + public void testCommit_HmEx() throws Exception { - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.commit(); - } - } + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new HeuristicMixedException("expected exception")).when(trans).commit(); - @Test(expected = EntityMgrException.class) - public void testCommit_HrbEx() throws Exception { + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.commit(); + } + } - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new HeuristicRollbackException("expected exception")).when(trans).commit(); + @Test(expected = EntityMgrException.class) + public void testCommit_HrbEx() throws Exception { - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.commit(); - } - } + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new HeuristicRollbackException("expected exception")).when(trans).commit(); - @Test(expected = EntityMgrException.class) - public void testCommit_SysEx() throws Exception { + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.commit(); + } + } - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new SystemException("expected exception")).when(trans).commit(); + @Test(expected = EntityMgrException.class) + public void testCommit_SysEx() throws Exception { - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.commit(); - } - } + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new SystemException("expected exception")).when(trans).commit(); - /** - * Verifies that rollback() only rolls back, and that the subsequent close() - * does not re-roll back. - */ - @Test - public void testRollback() throws Exception { - EntityMgrTrans t = new EntityMgrTrans(mgr); - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.commit(); + } + } - t.rollback(); + /** + * Verifies that rollback() only rolls back, and that the subsequent close() does not re-roll + * back. + */ + @Test + public void testRollback() throws Exception { + EntityMgrTrans newTrans = new EntityMgrTrans(mgr); + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - when(trans.getStatus()).thenReturn(Status.STATUS_ROLLEDBACK); + newTrans.rollback(); - // rolled back, but not closed or committed - verify(trans, never()).commit(); - verify(trans).rollback(); - verify(mgr, never()).close(); + when(trans.getStatus()).thenReturn(Status.STATUS_ROLLEDBACK); - // closed, but not re-rolled back - t.close(); + // rolled back, but not closed or committed + verify(trans, never()).commit(); + verify(trans).rollback(); + verify(mgr, never()).close(); - verify(trans, times(1)).rollback(); - verify(mgr).close(); - } + // closed, but not re-rolled back + newTrans.close(); - @Test(expected = EntityMgrException.class) - public void testRollback_IllStateEx() throws Exception { + verify(trans, times(1)).rollback(); + verify(mgr).close(); + } - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new IllegalStateException("expected exception")).when(trans).rollback(); + @Test(expected = EntityMgrException.class) + public void testRollback_IllStateEx() throws Exception { - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.rollback(); - } - } + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new IllegalStateException("expected exception")).when(trans).rollback(); - @Test(expected = EntityMgrException.class) - public void testRollback_SecEx() throws Exception { + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.rollback(); + } + } - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new SecurityException("expected exception")).when(trans).rollback(); + @Test(expected = EntityMgrException.class) + public void testRollback_SecEx() throws Exception { - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.rollback(); - } - } + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new SecurityException("expected exception")).when(trans).rollback(); - @Test(expected = EntityMgrException.class) - public void testRollback_SysEx() throws Exception { + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.rollback(); + } + } - when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); - doThrow(new SystemException("expected exception")).when(trans).rollback(); + @Test(expected = EntityMgrException.class) + public void testRollback_SysEx() throws Exception { - try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { - t.rollback(); - } - } + when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE); + doThrow(new SystemException("expected exception")).when(trans).rollback(); - @Test - public void testEntityMgrException() { - SecurityException secex = new SecurityException("expected exception"); - EntityMgrException ex = new EntityMgrException(secex); + try (EntityMgrTrans t = new EntityMgrTrans(mgr)) { + t.rollback(); + } + } - assertEquals(secex, ex.getCause()); + @Test + public void testEntityMgrException() { + SecurityException secex = new SecurityException("expected exception"); + EntityMgrException ex = new EntityMgrException(secex); - } + assertEquals(secex, ex.getCause()); + } } diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/GenSchema.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/GenSchema.java index a0af2e6c..38e3ec23 100644 --- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/GenSchema.java +++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/GenSchema.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -29,41 +29,38 @@ import javax.persistence.Persistence; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Generates the schema DDL files. - */ +/** Generates the schema DDL files. */ public class GenSchema { - private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class); + private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class); - private EntityManagerFactory emf; + private EntityManagerFactory emf; - /** - * Opens the EMF, which generates the schema, as a side-effect. - * - * @throws Exception - */ - private GenSchema() throws Exception { - Map<String, Object> propMap = new HashMap<>(); + /** + * Opens the EMF, which generates the schema, as a side-effect. + * + * @throws Exception exception + */ + private GenSchema() throws Exception { + Map<String, Object> propMap = new HashMap<>(); - propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver"); - propMap.put("javax.persistence.jdbc.url", "jdbc:h2:mem:JpaDroolsSessionConnectorTest"); + propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver"); + propMap.put("javax.persistence.jdbc.url", "jdbc:h2:mem:JpaDroolsSessionConnectorTest"); - emf = Persistence.createEntityManagerFactory("schemaDroolsPU", propMap); + emf = Persistence.createEntityManagerFactory("schemaDroolsPU", propMap); - emf.close(); - } + emf.close(); + } - /** - * This is is provided as a utility for producing a basic ddl schema file in - * the sql directory. - */ - public static void main(String[] args) { - try { - new GenSchema(); + /** + * This is is provided as a utility for producing a basic ddl schema file in the sql directory. + */ + public static void main(String[] args) { + try { + new GenSchema(); - } catch (Exception e) { - logger.error("failed to generate schema", e); - } - } + } catch (Exception e) { + logger.error("failed to generate schema", e); + } + } } diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/JpaDroolsSessionConnectorTest.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/JpaDroolsSessionConnectorTest.java index dd601dd3..def8f2ec 100644 --- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/JpaDroolsSessionConnectorTest.java +++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/JpaDroolsSessionConnectorTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -45,155 +45,158 @@ import org.onap.policy.drools.persistence.JpaDroolsSessionConnector; public class JpaDroolsSessionConnectorTest { - private EntityManagerFactory emf; - private JpaDroolsSessionConnector conn; + private EntityManagerFactory emf; + private JpaDroolsSessionConnector conn; - @BeforeClass - public static void setUpBeforeClass() { - System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); - System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); - } + @BeforeClass + public static void setUpBeforeClass() { + System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); + System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); + } - @Before - public void setUp() throws Exception { - Map<String, Object> propMap = new HashMap<>(); + /** + * Setup. + * + * @throws Exception exception + */ + @Before + public void setUp() throws Exception { + Map<String, Object> propMap = new HashMap<>(); - propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver"); - propMap.put("javax.persistence.jdbc.url", "jdbc:h2:mem:JpaDroolsSessionConnectorTest"); + propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver"); + propMap.put("javax.persistence.jdbc.url", "jdbc:h2:mem:JpaDroolsSessionConnectorTest"); - emf = Persistence.createEntityManagerFactory("junitDroolsSessionEntityPU", propMap); + emf = Persistence.createEntityManagerFactory("junitDroolsSessionEntityPU", propMap); - conn = new JpaDroolsSessionConnector(emf); - } + conn = new JpaDroolsSessionConnector(emf); + } - @After - public void tearDown() { - // this will cause the memory db to be dropped - emf.close(); - } + @After + public void tearDown() { + // this will cause the memory db to be dropped + emf.close(); + } - @Test - public void testGet() { - /* - * Load up the DB with some data. - */ + @Test + public void testGet() { + /* + * Load up the DB with some data. + */ - addSession("nameA", 10); - addSession("nameY", 20); + addSession("nameA", 10); + addSession("nameY", 20); - /* - * Now test the functionality. - */ + /* + * Now test the functionality. + */ - // not found - assertNull(conn.get("unknown")); + // not found + assertNull(conn.get("unknown")); - assertEquals("{name=nameA, id=10}", conn.get("nameA").toString()); + assertEquals("{name=nameA, id=10}", conn.get("nameA").toString()); - assertEquals("{name=nameY, id=20}", conn.get("nameY").toString()); - } + assertEquals("{name=nameY, id=20}", conn.get("nameY").toString()); + } - @Test(expected = IllegalArgumentException.class) - public void testGet_NewEx() { - EntityManagerFactory emf = mock(EntityManagerFactory.class); - EntityManager em = mock(EntityManager.class); + @Test(expected = IllegalArgumentException.class) + public void testGet_NewEx() { + EntityManagerFactory emf = mock(EntityManagerFactory.class); + EntityManager em = mock(EntityManager.class); - when(emf.createEntityManager()).thenReturn(em); - when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception")); + when(emf.createEntityManager()).thenReturn(em); + when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception")); - conn = new JpaDroolsSessionConnector(emf); - conn.get("xyz"); - } + conn = new JpaDroolsSessionConnector(emf); + conn.get("xyz"); + } - @Test(expected = IllegalArgumentException.class) - public void testGet_FindEx() { - EntityManagerFactory emf = mock(EntityManagerFactory.class); - EntityManager em = mock(EntityManager.class); - EntityTransaction tr = mock(EntityTransaction.class); + @Test(expected = IllegalArgumentException.class) + public void testGet_FindEx() { + EntityManagerFactory emf = mock(EntityManagerFactory.class); + EntityManager em = mock(EntityManager.class); + EntityTransaction tr = mock(EntityTransaction.class); - when(emf.createEntityManager()).thenReturn(em); - when(em.getTransaction()).thenReturn(tr); - when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception")); + when(emf.createEntityManager()).thenReturn(em); + when(em.getTransaction()).thenReturn(tr); + when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception")); - new JpaDroolsSessionConnector(emf).get("xyz"); - } + new JpaDroolsSessionConnector(emf).get("xyz"); + } - @Test(expected = IllegalArgumentException.class) - public void testGet_FindEx_CloseEx() { - EntityManagerFactory emf = mock(EntityManagerFactory.class); - EntityManager em = mock(EntityManager.class); - EntityTransaction tr = mock(EntityTransaction.class); + @Test(expected = IllegalArgumentException.class) + public void testGet_FindEx_CloseEx() { + EntityManagerFactory emf = mock(EntityManagerFactory.class); + EntityManager em = mock(EntityManager.class); + EntityTransaction tr = mock(EntityTransaction.class); - when(emf.createEntityManager()).thenReturn(em); - when(em.getTransaction()).thenReturn(tr); - when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception")); - doThrow(new IllegalArgumentException("expected exception #2")).when(em).close(); + when(emf.createEntityManager()).thenReturn(em); + when(em.getTransaction()).thenReturn(tr); + when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception")); + doThrow(new IllegalArgumentException("expected exception #2")).when(em).close(); - new JpaDroolsSessionConnector(emf).get("xyz"); - } + new JpaDroolsSessionConnector(emf).get("xyz"); + } - @Test - public void testReplace_Existing() { - addSession("nameA", 10); + @Test + public void testReplace_Existing() { + addSession("nameA", 10); - DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30); + DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30); - conn.replace(sess); + conn.replace(sess); - // id should be changed - assertEquals(sess.toString(), conn.get("nameA").toString()); - } + // id should be changed + assertEquals(sess.toString(), conn.get("nameA").toString()); + } - @Test - public void testReplace_New() { - DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30); + @Test + public void testReplace_New() { + DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30); - conn.replace(sess); + conn.replace(sess); - assertEquals(sess.toString(), conn.get("nameA").toString()); - } + assertEquals(sess.toString(), conn.get("nameA").toString()); + } - @Test - public void testAdd() { - DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30); + @Test + public void testAdd() { + DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30); - conn.replace(sess); + conn.replace(sess); - assertEquals(sess.toString(), conn.get("nameA").toString()); - } + assertEquals(sess.toString(), conn.get("nameA").toString()); + } - @Test - public void testUpdate() { - addSession("nameA", 10); + @Test + public void testUpdate() { + addSession("nameA", 10); - DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30); + DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30); - conn.replace(sess); + conn.replace(sess); - // id should be changed - assertEquals("{name=nameA, id=30}", conn.get("nameA").toString()); - } + // id should be changed + assertEquals("{name=nameA, id=30}", conn.get("nameA").toString()); + } - /** - * Adds a session to the DB. - * - * @param sessnm - * session name - * @param sessid - * session id - */ - private void addSession(String sessnm, int sessid) { - EntityManager em = emf.createEntityManager(); + /** + * Adds a session to the DB. + * + * @param sessnm session name + * @param sessid session id + */ + private void addSession(String sessnm, int sessid) { + EntityManager em = emf.createEntityManager(); - try (EntityMgrTrans trans = new EntityMgrTrans(em)) { - DroolsSessionEntity ent = new DroolsSessionEntity(); + try (EntityMgrTrans trans = new EntityMgrTrans(em)) { + DroolsSessionEntity ent = new DroolsSessionEntity(); - ent.setSessionName(sessnm); - ent.setSessionId(sessid); + ent.setSessionName(sessnm); + ent.setSessionId(sessid); - em.persist(ent); + em.persist(ent); - trans.commit(); - } - } + trans.commit(); + } + } } diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java index a7c33aba..27ac2cc1 100644 --- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java +++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -29,12 +29,12 @@ import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyLong; import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.doThrow; import java.io.FileNotFoundException; import java.io.FileReader; @@ -83,1335 +83,1332 @@ import org.slf4j.LoggerFactory; public class PersistenceFeatureTest { - private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class); - - private static final String JDBC_DRIVER = "fake.driver"; - private static final String JDBC_URL = "fake.url"; - private static final String JDBC_USER = "fake.user"; - private static final String JDBC_PASSWD = "fake.password"; - private static final String JTA_OSDIR = "target"; - private static final String SRC_TEST_RESOURCES = "src/test/resources"; - - private static Properties stdprops; - - private JpaDroolsSessionConnector jpa; - private DroolsSession sess; - private KieSession kiesess; - private BasicDataSource bds; - private EntityManagerFactory emf; - private Connection conn; - private Properties props; - private KieServices kiesvc; - private Environment kieenv; - private KieSessionConfiguration kiecfg; - private KieBase kiebase; - private KieStoreServices kiestore; - private KieContainer kiecont; - private TransactionManager transmgr; - private UserTransaction usertrans; - private TransactionSynchronizationRegistry transreg; - private PolicyController polctlr; - private PolicyContainer polcont; - private PolicySession polsess; - private PersistenceFeature.Factory fact; - - private PersistenceFeature feat; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - stdprops = new Properties(); - - stdprops.put(DroolsPersistenceProperties.DB_DRIVER, JDBC_DRIVER); - stdprops.put(DroolsPersistenceProperties.DB_URL, JDBC_URL); - stdprops.put(DroolsPersistenceProperties.DB_USER, JDBC_USER); - stdprops.put(DroolsPersistenceProperties.DB_PWD, JDBC_PASSWD); - stdprops.put(DroolsPersistenceProperties.JTA_OBJECTSTORE_DIR, JTA_OSDIR); - stdprops.put(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "50"); - - System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); - System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); - } - - @Before - public void setUp() throws Exception { - jpa = mock(JpaDroolsSessionConnector.class); - sess = mock(DroolsSession.class); - bds = mock(BasicDataSource.class); - emf = mock(EntityManagerFactory.class); - kiesess = mock(KieSession.class); - conn = null; - props = new Properties(); - kiesvc = mock(KieServices.class); - kieenv = mock(Environment.class); - kiecfg = mock(KieSessionConfiguration.class); - kiebase = mock(KieBase.class); - kiestore = mock(KieStoreServices.class); - kiecont = mock(KieContainer.class); - transmgr = mock(TransactionManager.class); - usertrans = mock(UserTransaction.class); - transreg = mock(TransactionSynchronizationRegistry.class); - polcont = mock(PolicyContainer.class); - polctlr = mock(PolicyController.class); - polsess = mock(PolicySession.class); - fact = mock(PersistenceFeature.Factory.class); - - feat = new PersistenceFeature(); - feat.setFactory(fact); - - props.putAll(stdprops); - - System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); - System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); - - when(fact.getKieServices()).thenReturn(kiesvc); - when(fact.getTransMgr()).thenReturn(transmgr); - when(fact.getUserTrans()).thenReturn(usertrans); - when(fact.getTransSyncReg()).thenReturn(transreg); - when(fact.loadProperties(anyString())).thenReturn(props); - - when(kiesvc.newEnvironment()).thenReturn(kieenv); - when(kiesvc.getStoreServices()).thenReturn(kiestore); - when(kiesvc.newKieSessionConfiguration()).thenReturn(kiecfg); - - when(polcont.getKieContainer()).thenReturn(kiecont); - - when(polsess.getPolicyContainer()).thenReturn(polcont); - - when(kiecont.getKieBase(anyString())).thenReturn(kiebase); - } - - @After - public void tearDown() { - // this will cause the in-memory test DB to be dropped - if (conn != null) { - try { - conn.close(); - } catch (SQLException e) { - logger.warn("failed to close connection", e); - } - } - - if (emf != null) { - try { - emf.close(); - } catch (IllegalArgumentException e) { - logger.trace("ignored exception", e); - } - } - } - - @Test - public void testGetContainerAdjunct_New() throws Exception { - - feat.globalInit(null, SRC_TEST_RESOURCES); + private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class); + + private static final String JDBC_DRIVER = "fake.driver"; + private static final String JDBC_URL = "fake.url"; + private static final String JDBC_USER = "fake.user"; + private static final String JDBC_PASSWD = "fake.password"; + private static final String JTA_OSDIR = "target"; + private static final String SRC_TEST_RESOURCES = "src/test/resources"; + + private static Properties stdprops; + + private JpaDroolsSessionConnector jpa; + private DroolsSession sess; + private KieSession kiesess; + private BasicDataSource bds; + private EntityManagerFactory emf; + private Connection conn; + private Properties props; + private KieServices kiesvc; + private Environment kieenv; + private KieSessionConfiguration kiecfg; + private KieBase kiebase; + private KieStoreServices kiestore; + private KieContainer kiecont; + private TransactionManager transmgr; + private UserTransaction usertrans; + private TransactionSynchronizationRegistry transreg; + private PolicyController polctlr; + private PolicyContainer polcont; + private PolicySession polsess; + private PersistenceFeature.Factory fact; + + private PersistenceFeature feat; + + /** + * Setup before class. + * + * @throws Exception exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + stdprops = new Properties(); + + stdprops.put(DroolsPersistenceProperties.DB_DRIVER, JDBC_DRIVER); + stdprops.put(DroolsPersistenceProperties.DB_URL, JDBC_URL); + stdprops.put(DroolsPersistenceProperties.DB_USER, JDBC_USER); + stdprops.put(DroolsPersistenceProperties.DB_PWD, JDBC_PASSWD); + stdprops.put(DroolsPersistenceProperties.JTA_OBJECTSTORE_DIR, JTA_OSDIR); + stdprops.put(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "50"); + + System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); + System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); + } + + /** + * Setup. + * + * @throws Exception exception + */ + @Before + public void setUp() throws Exception { + jpa = mock(JpaDroolsSessionConnector.class); + sess = mock(DroolsSession.class); + bds = mock(BasicDataSource.class); + emf = mock(EntityManagerFactory.class); + kiesess = mock(KieSession.class); + conn = null; + props = new Properties(); + kiesvc = mock(KieServices.class); + kieenv = mock(Environment.class); + kiecfg = mock(KieSessionConfiguration.class); + kiebase = mock(KieBase.class); + kiestore = mock(KieStoreServices.class); + kiecont = mock(KieContainer.class); + transmgr = mock(TransactionManager.class); + usertrans = mock(UserTransaction.class); + transreg = mock(TransactionSynchronizationRegistry.class); + polcont = mock(PolicyContainer.class); + polctlr = mock(PolicyController.class); + polsess = mock(PolicySession.class); + fact = mock(PersistenceFeature.Factory.class); + + feat = new PersistenceFeature(); + feat.setFactory(fact); + + props.putAll(stdprops); + + System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); + System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); + + when(fact.getKieServices()).thenReturn(kiesvc); + when(fact.getTransMgr()).thenReturn(transmgr); + when(fact.getUserTrans()).thenReturn(usertrans); + when(fact.getTransSyncReg()).thenReturn(transreg); + when(fact.loadProperties(anyString())).thenReturn(props); + + when(kiesvc.newEnvironment()).thenReturn(kieenv); + when(kiesvc.getStoreServices()).thenReturn(kiestore); + when(kiesvc.newKieSessionConfiguration()).thenReturn(kiecfg); + + when(polcont.getKieContainer()).thenReturn(kiecont); + + when(polsess.getPolicyContainer()).thenReturn(polcont); + + when(kiecont.getKieBase(anyString())).thenReturn(kiebase); + } + + /** + * Tear down. + */ + @After + public void tearDown() { + // this will cause the in-memory test DB to be dropped + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + logger.warn("failed to close connection", e); + } + } + + if (emf != null) { + try { + emf.close(); + } catch (IllegalArgumentException e) { + logger.trace("ignored exception", e); + } + } + } + + @Test + public void testGetContainerAdjunct_New() throws Exception { + + feat.globalInit(null, SRC_TEST_RESOURCES); + + mockDbConn(5); + setUpKie("myname", 999L, true); + + // force getContainerAdjunct() to be invoked + feat.activatePolicySession(polcont, "myname", "mybase"); + + ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); + + verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); + + assertNotNull(adjcap.getValue()); + } + + @Test + public void testGetContainerAdjunct_Existing() throws Exception { + + feat.globalInit(null, SRC_TEST_RESOURCES); + + mockDbConn(5); + setUpKie("myname", 999L, true); + + // force getContainerAdjunct() to be invoked + feat.activatePolicySession(polcont, "myname", "mybase"); + + ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); + + verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); + + // return adjunct on next call + when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); + + // force getContainerAdjunct() to be invoked again + setUpKie("myname2", 999L, true); + feat.activatePolicySession(polcont, "myname2", "mybase"); + + // ensure it isn't invoked again + verify(polcont, times(1)).setAdjunct(any(), any()); + } + + @Test + public void testGetContainerAdjunct_WrongType() throws Exception { + + feat.globalInit(null, SRC_TEST_RESOURCES); + + mockDbConn(5); + setUpKie("myname", 999L, true); + + // return false adjunct on next call + when(polcont.getAdjunct(any())).thenReturn("not-a-real-adjunct"); + + // force getContainerAdjunct() to be invoked + setUpKie("myname2", 999L, true); + feat.activatePolicySession(polcont, "myname2", "mybase"); + + ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); + + verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); + + assertNotNull(adjcap.getValue()); + } + + @Test + public void testGetSequenceNumber() { + assertEquals(1, feat.getSequenceNumber()); + } + + @Test + public void testGlobalInit() throws Exception { + + feat.globalInit(null, SRC_TEST_RESOURCES); + + // verify that various factory methods were invoked + verify(fact).getKieServices(); + verify(fact).loadProperties("src/test/resources/feature-session-persistence.properties"); + } + + @Test(expected = NullPointerException.class) + public void testGlobalInitIoEx() throws Exception { + + when(fact.loadProperties(anyString())).thenThrow(new IOException("expected exception")); + + feat.globalInit(null, SRC_TEST_RESOURCES); + } + + @Test + public void testActivatePolicySession() throws Exception { + final PreparedStatement ps = mockDbConn(5); + setUpKie("myname", 999L, true); + + feat.globalInit(null, SRC_TEST_RESOURCES); + feat.beforeActivate(null); + + KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); + + verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); + verify(kiestore, never()).newKieSession(any(), any(), any()); + + assertEquals(session, kiesess); + + verify(ps).executeUpdate(); + + verify(kieenv, times(4)).set(anyString(), any()); + + verify(jpa).get("myname"); + verify(jpa).replace(any()); + } + + @Test + public void testActivatePolicySession_NoPersistence() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - mockDbConn(5); - setUpKie("myname", 999L, true); + final PreparedStatement ps = mockDbConn(5); + setUpKie("myname", 999L, true); - // force getContainerAdjunct() to be invoked - feat.activatePolicySession(polcont, "myname", "mybase"); + props.remove("persistence.type"); - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + feat.beforeStart(null); - verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); + assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); - assertNotNull(adjcap.getValue()); - } + verify(ps, never()).executeUpdate(); + verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any()); + verify(kiestore, never()).newKieSession(any(), any(), any()); + } - @Test - public void testGetContainerAdjunct_Existing() throws Exception { + /** Verifies that a new KIE session is created when there is no existing session entity. */ + @Test + public void testActivatePolicySession_New() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("noName", 999L, true); - mockDbConn(5); - setUpKie("myname", 999L, true); + KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); - // force getContainerAdjunct() to be invoked - feat.activatePolicySession(polcont, "myname", "mybase"); + verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any()); + verify(kiestore).newKieSession(any(), any(), any()); - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + assertEquals(session, kiesess); - verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); + verify(kieenv, times(4)).set(anyString(), any()); - // return adjunct on next call - when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); + verify(jpa).get("myname"); + verify(jpa).replace(any()); + } - // force getContainerAdjunct() to be invoked again - setUpKie("myname2", 999L, true); - feat.activatePolicySession(polcont, "myname2", "mybase"); + /** + * Verifies that a new KIE session is created when there KIE fails to load an existing session. + */ + @Test + public void testActivatePolicySession_LoadFailed() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - // ensure it isn't invoked again - verify(polcont, times(1)).setAdjunct(any(), any()); - } + mockDbConn(5); + setUpKie("myname", 999L, false); - @Test - public void testGetContainerAdjunct_WrongType() throws Exception { + KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); - feat.globalInit(null, SRC_TEST_RESOURCES); + verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); + verify(kiestore).newKieSession(any(), any(), any()); - mockDbConn(5); - setUpKie("myname", 999L, true); + assertEquals(session, kiesess); - // return false adjunct on next call - when(polcont.getAdjunct(any())).thenReturn("not-a-real-adjunct"); + verify(kieenv, times(4)).set(anyString(), any()); - // force getContainerAdjunct() to be invoked - setUpKie("myname2", 999L, true); - feat.activatePolicySession(polcont, "myname2", "mybase"); + verify(jpa).get("myname"); - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + ArgumentCaptor<DroolsSession> drools = ArgumentCaptor.forClass(DroolsSession.class); + verify(jpa).replace(drools.capture()); - verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); + assertEquals("myname", drools.getValue().getSessionName()); + assertEquals(100L, drools.getValue().getSessionId()); + } - assertNotNull(adjcap.getValue()); - } + @Test + public void testLoadDataSource() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testGetSequenceNumber() { - assertEquals(1, feat.getSequenceNumber()); - } + mockDbConn(5); + setUpKie("myname", 999L, false); - @Test - public void testGlobalInit() throws Exception { + feat.activatePolicySession(polcont, "myname", "mybase"); - feat.globalInit(null, SRC_TEST_RESOURCES); + verify(fact).makeEntMgrFact(any()); + } - // verify that various factory methods were invoked - verify(fact).getKieServices(); - verify(fact).loadProperties("src/test/resources/feature-session-persistence.properties"); - } + @Test + public void testConfigureSysProps() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test(expected = NullPointerException.class) - public void testGlobalInit_IOEx() throws Exception { - - when(fact.loadProperties(anyString())).thenThrow(new IOException("expected exception")); + mockDbConn(5); + setUpKie("myname", 999L, false); - feat.globalInit(null, SRC_TEST_RESOURCES); - } + feat.activatePolicySession(polcont, "myname", "mybase"); - @Test - public void testActivatePolicySession() throws Exception { - PreparedStatement ps = mockDbConn(5); - setUpKie("myname", 999L, true); + assertEquals("60", System.getProperty("com.arjuna.ats.arjuna.coordinator.defaultTimeout")); + assertEquals(JTA_OSDIR, System.getProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir")); + assertEquals(JTA_OSDIR, System.getProperty("ObjectStoreEnvironmentBean.objectStoreDir")); + } - feat.globalInit(null, SRC_TEST_RESOURCES); - feat.beforeActivate(null); + @Test + public void testConfigureKieEnv() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - KieSession s = feat.activatePolicySession(polcont, "myname", "mybase"); + mockDbConn(5); + setUpKie("myname", 999L, false); - verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); - verify(kiestore, never()).newKieSession(any(), any(), any()); + feat.activatePolicySession(polcont, "myname", "mybase"); - assertEquals(s, kiesess); + verify(kieenv, times(4)).set(any(), any()); - verify(ps).executeUpdate(); + verify(kieenv).set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf); + verify(kieenv).set(EnvironmentName.TRANSACTION, usertrans); + verify(kieenv).set(EnvironmentName.TRANSACTION_MANAGER, transmgr); + verify(kieenv).set(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY, transreg); - verify(kieenv, times(4)).set(anyString(), any()); + verify(bds, times(1)).close(); + } - verify(jpa).get("myname"); - verify(jpa).replace(any()); - } + @Test + public void testConfigureKieEnv_RtEx() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testActivatePolicySession_NoPersistence() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, false); - PreparedStatement ps = mockDbConn(5); - setUpKie("myname", 999L, true); + when(fact.getUserTrans()).thenThrow(new IllegalArgumentException("expected exception")); - props.remove("persistence.type"); + try { + feat.activatePolicySession(polcont, "myname", "mybase"); + fail("missing exception"); - feat.beforeStart(null); + } catch (IllegalArgumentException ex) { + logger.trace("expected exception", ex); + } - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + verify(bds, times(2)).close(); + } - verify(ps, never()).executeUpdate(); - verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any()); - verify(kiestore, never()).newKieSession(any(), any(), any()); - } + @Test + public void testLoadKieSession() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - /** - * Verifies that a new KIE session is created when there is no existing - * session entity. - */ - @Test - public void testActivatePolicySession_New() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, true); - mockDbConn(5); - setUpKie("noName", 999L, true); + KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); - KieSession s = feat.activatePolicySession(polcont, "myname", "mybase"); + verify(kiestore).loadKieSession(999L, kiebase, kiecfg, kieenv); + verify(kiestore, never()).newKieSession(any(), any(), any()); - verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any()); - verify(kiestore).newKieSession(any(), any(), any()); + assertEquals(session, kiesess); + } - assertEquals(s, kiesess); + /* + * Verifies that loadKieSession() returns null (thus causing newKieSession() + * to be called) when an Exception occurs. + */ + @Test + public void testLoadKieSession_Ex() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - verify(kieenv, times(4)).set(anyString(), any()); + mockDbConn(5); + setUpKie("myname", 999L, false); - verify(jpa).get("myname"); - verify(jpa).replace(any()); - } + when(kiestore.loadKieSession(anyLong(), any(), any(), any())) + .thenThrow(new IllegalArgumentException("expected exception")); - /** - * Verifies that a new KIE session is created when there KIE fails to load - * an existing session. - */ - @Test - public void testActivatePolicySession_LoadFailed() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); - mockDbConn(5); - setUpKie("myname", 999L, false); + verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); + verify(kiestore).newKieSession(any(), any(), any()); - KieSession s = feat.activatePolicySession(polcont, "myname", "mybase"); + assertEquals(session, kiesess); + } - verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); - verify(kiestore).newKieSession(any(), any(), any()); + @Test + public void testNewKieSession() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - assertEquals(s, kiesess); + mockDbConn(5); + setUpKie("myname", 999L, false); - verify(kieenv, times(4)).set(anyString(), any()); + KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); - verify(jpa).get("myname"); + verify(kiestore).newKieSession(kiebase, null, kieenv); - ArgumentCaptor<DroolsSession> d = ArgumentCaptor.forClass(DroolsSession.class); - verify(jpa).replace(d.capture()); + assertEquals(session, kiesess); + } - assertEquals("myname", d.getValue().getSessionName()); - assertEquals(100L, d.getValue().getSessionId()); - } + @Test + public void testLoadDataSource_DiffSession() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testLoadDataSource() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, false); + feat.activatePolicySession(polcont, "myname", "mybase"); - mockDbConn(5); - setUpKie("myname", 999L, false); + ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - feat.activatePolicySession(polcont, "myname", "mybase"); + verify(polcont).setAdjunct(any(), adjcap.capture()); - verify(fact).makeEntMgrFact(any()); - } + when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); - @Test - public void testConfigureSysProps() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + setUpKie("myname2", 999L, false); - mockDbConn(5); - setUpKie("myname", 999L, false); + // invoke it again + feat.activatePolicySession(polcont, "myname2", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); + verify(fact, times(2)).makeEntMgrFact(any()); + } - assertEquals("60", System.getProperty("com.arjuna.ats.arjuna.coordinator.defaultTimeout")); - assertEquals(JTA_OSDIR, System.getProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir")); - assertEquals(JTA_OSDIR, System.getProperty("ObjectStoreEnvironmentBean.objectStoreDir")); - } + @Test + public void testSelectThreadModel_Persistent() throws Exception { + setUpKie("myname", 999L, true); - @Test - public void testConfigureKieEnv() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + ThreadModel model = feat.selectThreadModel(polsess); + assertNotNull(model); + assertTrue(model instanceof PersistentThreadModel); + } - mockDbConn(5); - setUpKie("myname", 999L, false); + @Test + public void testSelectThreadModel_NotPersistent() throws Exception { + when(fact.getPolicyController(any())).thenReturn(polctlr); + assertNull(feat.selectThreadModel(polsess)); + } - feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testSelectThreadModel_Start__Run_Update_Stop() throws Exception { + setUpKie("myname", 999L, true); - verify(kieenv, times(4)).set(any(), any()); + ThreadModel model = feat.selectThreadModel(polsess); + assertNotNull(model); + assertTrue(model instanceof PersistentThreadModel); - verify(kieenv).set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf); - verify(kieenv).set(EnvironmentName.TRANSACTION, usertrans); - verify(kieenv).set(EnvironmentName.TRANSACTION_MANAGER, transmgr); - verify(kieenv).set(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY, transreg); - - verify(bds, times(1)).close(); - } + when(polsess.getKieSession()).thenReturn(kiesess); - @Test - public void testConfigureKieEnv_RtEx() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + model.start(); + new CountDownLatch(1).await(10, TimeUnit.MILLISECONDS); + model.updated(); + model.stop(); + } - mockDbConn(5); - setUpKie("myname", 999L, false); - - when(fact.getUserTrans()).thenThrow(new IllegalArgumentException("expected exception")); + @Test + public void testDisposeKieSession() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); - - } catch(IllegalArgumentException ex) { - logger.trace("expected exception", ex); - } + final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - verify(bds, times(2)).close(); - } + mockDbConn(5); + setUpKie("myname", 999L, false); - @Test - public void testLoadKieSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + feat.activatePolicySession(polcont, "myname", "mybase"); - mockDbConn(5); - setUpKie("myname", 999L, true); + verify(emf, never()).close(); + verify(polcont).setAdjunct(any(), adjcap.capture()); - KieSession s = feat.activatePolicySession(polcont, "myname", "mybase"); + when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); - verify(kiestore).loadKieSession(999L, kiebase, kiecfg, kieenv); - verify(kiestore, never()).newKieSession(any(), any(), any()); + feat.disposeKieSession(polsess); - assertEquals(s, kiesess); - } + // call twice to ensure it isn't re-closed + feat.disposeKieSession(polsess); - /* - * Verifies that loadKieSession() returns null (thus causing newKieSession() - * to be called) when an Exception occurs. - */ - @Test - public void testLoadKieSession_Ex() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + verify(emf, times(1)).close(); + } - mockDbConn(5); - setUpKie("myname", 999L, false); + @Test + public void testDisposeKieSession_NoAdjunct() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - when(kiestore.loadKieSession(anyLong(), any(), any(), any())) - .thenThrow(new IllegalArgumentException("expected exception")); + feat.disposeKieSession(polsess); + } - KieSession s = feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testDisposeKieSession_NoPersistence() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); - verify(kiestore).newKieSession(any(), any(), any()); + final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - assertEquals(s, kiesess); - } + mockDbConn(5); + setUpKie("myname", 999L, false); - @Test - public void testNewKieSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + feat.activatePolicySession(polcont, "myname", "mybase"); - mockDbConn(5); - setUpKie("myname", 999L, false); + verify(emf, never()).close(); + verify(polcont).setAdjunct(any(), adjcap.capture()); - KieSession s = feat.activatePolicySession(polcont, "myname", "mybase"); + when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); - verify(kiestore).newKieSession(kiebase, null, kieenv); + // specify a session that was never loaded + when(polsess.getName()).thenReturn("anotherName"); - assertEquals(s, kiesess); - } + feat.disposeKieSession(polsess); - @Test - public void testLoadDataSource_DiffSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + verify(emf, never()).close(); + } - mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testDestroyKieSession() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - verify(polcont).setAdjunct(any(), adjcap.capture()); + mockDbConn(5); + setUpKie("myname", 999L, false); - when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); + feat.activatePolicySession(polcont, "myname", "mybase"); - setUpKie("myname2", 999L, false); + verify(emf, never()).close(); + verify(polcont).setAdjunct(any(), adjcap.capture()); - // invoke it again - feat.activatePolicySession(polcont, "myname2", "mybase"); + when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); - verify(fact, times(2)).makeEntMgrFact(any()); - } - - @Test - public void testSelectThreadModel_Persistent() throws Exception { - setUpKie("myname", 999L, true); - - ThreadModel m = feat.selectThreadModel(polsess); - assertNotNull(m); - assertTrue(m instanceof PersistentThreadModel); - - } - - @Test - public void testSelectThreadModel_NotPersistent() throws Exception { - when(fact.getPolicyController(any())).thenReturn(polctlr); - assertNull(feat.selectThreadModel(polsess)); - - } - - @Test - public void testSelectThreadModel_Start__Run_Update_Stop() throws Exception { - setUpKie("myname", 999L, true); - - ThreadModel m = feat.selectThreadModel(polsess); - assertNotNull(m); - assertTrue(m instanceof PersistentThreadModel); - - when(polsess.getKieSession()).thenReturn(kiesess); - - m.start(); - new CountDownLatch(1).await(10, TimeUnit.MILLISECONDS); - m.updated(); - m.stop(); - } + feat.destroyKieSession(polsess); - @Test - public void testDisposeKieSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + // call twice to ensure it isn't re-closed + feat.destroyKieSession(polsess); - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + verify(emf, times(1)).close(); + } - mockDbConn(5); - setUpKie("myname", 999L, false); + @Test + public void testDestroyKieSession_NoAdjunct() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.destroyKieSession(polsess); + } - verify(emf, never()).close(); - verify(polcont).setAdjunct(any(), adjcap.capture()); + @Test + public void testDestroyKieSession_NoPersistence() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); + final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - feat.disposeKieSession(polsess); + mockDbConn(5); + setUpKie("myname", 999L, false); - // call twice to ensure it isn't re-closed - feat.disposeKieSession(polsess); + feat.activatePolicySession(polcont, "myname", "mybase"); - verify(emf, times(1)).close(); - } + verify(emf, never()).close(); + verify(polcont).setAdjunct(any(), adjcap.capture()); - @Test - public void testDisposeKieSession_NoAdjunct() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); - feat.disposeKieSession(polsess); - } + // specify a session that was never loaded + when(polsess.getName()).thenReturn("anotherName"); - @Test - public void testDisposeKieSession_NoPersistence() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + feat.destroyKieSession(polsess); - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + verify(emf, never()).close(); + } - mockDbConn(5); - setUpKie("myname", 999L, false); + @Test + public void testAfterStart() { + assertFalse(feat.afterStart(null)); + } - feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testBeforeStart() { + assertFalse(feat.beforeStart(null)); + } - verify(emf, never()).close(); - verify(polcont).setAdjunct(any(), adjcap.capture()); + @Test + public void testBeforeShutdown() { + assertFalse(feat.beforeShutdown(null)); + } - when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); + @Test + public void testAfterShutdown() { + assertFalse(feat.afterShutdown(null)); + } - // specify a session that was never loaded - when(polsess.getName()).thenReturn("anotherName"); + @Test + public void testBeforeConfigure() { + assertFalse(feat.beforeConfigure(null, null)); + } - feat.disposeKieSession(polsess); + @Test + public void testAfterConfigure() { + assertFalse(feat.afterConfigure(null)); + } - verify(emf, never()).close(); - } + @Test + public void testBeforeActivate() { + assertFalse(feat.beforeActivate(null)); + } - @Test - public void testDestroyKieSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + @Test + public void testAfterActivate() { + assertFalse(feat.afterActivate(null)); + } - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + @Test + public void testBeforeDeactivate() { + assertFalse(feat.beforeDeactivate(null)); + } - mockDbConn(5); - setUpKie("myname", 999L, false); + @Test + public void testAfterDeactivate() { + assertFalse(feat.afterDeactivate(null)); + } - feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testBeforeStop() { + assertFalse(feat.beforeStop(null)); + } - verify(emf, never()).close(); - verify(polcont).setAdjunct(any(), adjcap.capture()); + @Test + public void testAfterStop() { + assertFalse(feat.afterStop(null)); + } - when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); + @Test + public void testBeforeLock() { + assertFalse(feat.beforeLock(null)); + } - feat.destroyKieSession(polsess); + @Test + public void testAfterLock() { + assertFalse(feat.afterLock(null)); + } - // call twice to ensure it isn't re-closed - feat.destroyKieSession(polsess); + @Test + public void testBeforeUnlock() { + assertFalse(feat.beforeUnlock(null)); + } - verify(emf, times(1)).close(); - } + @Test + public void testAfterUnlock() { + assertFalse(feat.afterUnlock(null)); + } - @Test - public void testDestroyKieSession_NoAdjunct() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + @Test + public void testGetPersistenceTimeout_Valid() throws Exception { + final PreparedStatement statement = mockDbConn(5); - feat.destroyKieSession(polsess); - } + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testDestroyKieSession_NoPersistence() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + setUpKie("myname", 999L, true); - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + feat.activatePolicySession(polcont, "myname", "mybase"); - mockDbConn(5); - setUpKie("myname", 999L, false); + verify(statement).executeUpdate(); + } - feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testGetPersistenceTimeout_Missing() throws Exception { - verify(emf, never()).close(); - verify(polcont).setAdjunct(any(), adjcap.capture()); + props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT); - when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); + final PreparedStatement statement = mockDbConn(0); - // specify a session that was never loaded - when(polsess.getName()).thenReturn("anotherName"); + feat.globalInit(null, SRC_TEST_RESOURCES); - feat.destroyKieSession(polsess); + setUpKie("myname", 999L, true); - verify(emf, never()).close(); - } + feat.activatePolicySession(polcont, "myname", "mybase"); - @Test - public void testAfterStart() { - assertFalse(feat.afterStart(null)); - } + verify(statement, never()).executeUpdate(); + } - @Test - public void testBeforeStart() { - assertFalse(feat.beforeStart(null)); - } + @Test + public void testGetPersistenceTimeout_Invalid() throws Exception { + props.setProperty(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "abc"); + final PreparedStatement s = mockDbConn(0); - @Test - public void testBeforeShutdown() { - assertFalse(feat.beforeShutdown(null)); - } + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testAfterShutdown() { - assertFalse(feat.afterShutdown(null)); - } + setUpKie("myname", 999L, true); - @Test - public void testBeforeConfigure() { - assertFalse(feat.beforeConfigure(null, null)); - } + feat.activatePolicySession(polcont, "myname", "mybase"); - @Test - public void testAfterConfigure() { - assertFalse(feat.afterConfigure(null)); - } + verify(s, never()).executeUpdate(); + } - @Test - public void testBeforeActivate() { - assertFalse(feat.beforeActivate(null)); - } + @Test + public void testCleanUpSessionInfo() throws Exception { + setUpKie("myname", 999L, true); - @Test - public void testAfterActivate() { - assertFalse(feat.afterActivate(null)); - } + // use a real DB so we can verify that the "delete" works correctly + fact = new PartialFactory(); + feat.setFactory(fact); - @Test - public void testBeforeDeactivate() { - assertFalse(feat.beforeDeactivate(null)); - } + makeSessionInfoTbl(20000); - @Test - public void testAfterDeactivate() { - assertFalse(feat.afterDeactivate(null)); - } + // create mock entity manager for use by JPA connector + EntityManager em = mock(EntityManager.class); + when(emf.createEntityManager()).thenReturn(em); - @Test - public void testBeforeStop() { - assertFalse(feat.beforeStop(null)); - } + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testAfterStop() { - assertFalse(feat.afterStop(null)); - } + feat.beforeStart(null); + feat.activatePolicySession(polcont, "myname", "mybase"); - @Test - public void testBeforeLock() { - assertFalse(feat.beforeLock(null)); - } + assertEquals("[1, 4, 5]", getSessions().toString()); + } - @Test - public void testAfterLock() { - assertFalse(feat.afterLock(null)); - } + @Test + public void testCleanUpSessionInfo_WithBeforeStart() throws Exception { + final PreparedStatement statement = mockDbConn(0); - @Test - public void testBeforeUnlock() { - assertFalse(feat.beforeUnlock(null)); - } + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testAfterUnlock() { - assertFalse(feat.afterUnlock(null)); - } + setUpKie("myname", 999L, true); - @Test - public void testGetPersistenceTimeout_Valid() throws Exception { - PreparedStatement s = mockDbConn(5); + // reset + feat.beforeStart(null); - feat.globalInit(null, SRC_TEST_RESOURCES); + feat.activatePolicySession(polcont, "myname", "mybase"); + verify(statement, times(1)).executeUpdate(); - setUpKie("myname", 999L, true); + // should not clean-up again + feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, "myname", "mybase"); + verify(statement, times(1)).executeUpdate(); - feat.activatePolicySession(polcont, "myname", "mybase"); + // reset + feat.beforeStart(null); - verify(s).executeUpdate(); - } + feat.activatePolicySession(polcont, "myname", "mybase"); + verify(statement, times(2)).executeUpdate(); - @Test - public void testGetPersistenceTimeout_Missing() throws Exception { + // should not clean-up again + feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, "myname", "mybase"); + verify(statement, times(2)).executeUpdate(); + } - props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT); + @Test + public void testCleanUpSessionInfo_WithBeforeActivate() throws Exception { + final PreparedStatement statement = mockDbConn(0); - PreparedStatement s = mockDbConn(0); + feat.globalInit(null, SRC_TEST_RESOURCES); - feat.globalInit(null, SRC_TEST_RESOURCES); + setUpKie("myname", 999L, true); - setUpKie("myname", 999L, true); + // reset + feat.beforeActivate(null); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, "myname", "mybase"); + verify(statement, times(1)).executeUpdate(); - verify(s, never()).executeUpdate(); - } + // should not clean-up again + feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, "myname", "mybase"); + verify(statement, times(1)).executeUpdate(); - @Test - public void testGetPersistenceTimeout_Invalid() throws Exception { - props.setProperty(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "abc"); - PreparedStatement s = mockDbConn(0); + // reset + feat.beforeActivate(null); - feat.globalInit(null, SRC_TEST_RESOURCES); + feat.activatePolicySession(polcont, "myname", "mybase"); + verify(statement, times(2)).executeUpdate(); - setUpKie("myname", 999L, true); + // should not clean-up again + feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, "myname", "mybase"); + verify(statement, times(2)).executeUpdate(); + } - feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testCleanUpSessionInfo_NoTimeout() throws Exception { - verify(s, never()).executeUpdate(); - } + props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT); - @Test - public void testCleanUpSessionInfo() throws Exception { - setUpKie("myname", 999L, true); + final PreparedStatement statement = mockDbConn(0); - // use a real DB so we can verify that the "delete" works correctly - fact = new PartialFactory(); - feat.setFactory(fact); + feat.globalInit(null, SRC_TEST_RESOURCES); - makeSessionInfoTbl(20000); + setUpKie("myname", 999L, true); - // create mock entity manager for use by JPA connector - EntityManager em = mock(EntityManager.class); - when(emf.createEntityManager()).thenReturn(em); + feat.activatePolicySession(polcont, "myname", "mybase"); - feat.globalInit(null, SRC_TEST_RESOURCES); + verify(statement, never()).executeUpdate(); + } - feat.beforeStart(null); - feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testCleanUpSessionInfo_NoUrl() throws Exception { + final PreparedStatement statement = mockDbConn(0); - assertEquals("[1, 4, 5]", getSessions().toString()); - } + props.remove(DroolsPersistenceProperties.DB_URL); - @Test - public void testCleanUpSessionInfo_WithBeforeStart() throws Exception { - PreparedStatement s = mockDbConn(0); + feat.globalInit(null, SRC_TEST_RESOURCES); - feat.globalInit(null, SRC_TEST_RESOURCES); + setUpKie("myname", 999L, true); - setUpKie("myname", 999L, true); + try { + feat.activatePolicySession(polcont, "myname", "mybase"); + fail("missing exception"); + } catch (RuntimeException e) { + logger.trace("expected exception", e); + } - // reset - feat.beforeStart(null); + verify(statement, never()).executeUpdate(); + } - feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, times(1)).executeUpdate(); + @Test + public void testCleanUpSessionInfo_NoUser() throws Exception { + final PreparedStatement statement = mockDbConn(0); - // should not clean-up again - feat.activatePolicySession(polcont, "myname", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, times(1)).executeUpdate(); + props.remove(DroolsPersistenceProperties.DB_USER); - // reset - feat.beforeStart(null); + feat.globalInit(null, SRC_TEST_RESOURCES); - feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, times(2)).executeUpdate(); + setUpKie("myname", 999L, true); - // should not clean-up again - feat.activatePolicySession(polcont, "myname", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, times(2)).executeUpdate(); - } + try { + feat.activatePolicySession(polcont, "myname", "mybase"); + fail("missing exception"); + } catch (RuntimeException e) { + logger.trace("expected exception", e); + } - @Test - public void testCleanUpSessionInfo_WithBeforeActivate() throws Exception { - PreparedStatement s = mockDbConn(0); + verify(statement, never()).executeUpdate(); + } - feat.globalInit(null, SRC_TEST_RESOURCES); + @Test + public void testCleanUpSessionInfo_NoPassword() throws Exception { + final PreparedStatement statement = mockDbConn(0); - setUpKie("myname", 999L, true); + props.remove(DroolsPersistenceProperties.DB_PWD); - // reset - feat.beforeActivate(null); + feat.globalInit(null, SRC_TEST_RESOURCES); - feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, times(1)).executeUpdate(); + setUpKie("myname", 999L, true); - // should not clean-up again - feat.activatePolicySession(polcont, "myname", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, times(1)).executeUpdate(); + try { + feat.activatePolicySession(polcont, "myname", "mybase"); + fail("missing exception"); + } catch (RuntimeException e) { + logger.trace("expected exception", e); + } - // reset - feat.beforeActivate(null); + verify(statement, never()).executeUpdate(); + } - feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, times(2)).executeUpdate(); + @Test + public void testCleanUpSessionInfo_SqlEx() throws Exception { + final PreparedStatement statement = mockDbConn(-1); - // should not clean-up again - feat.activatePolicySession(polcont, "myname", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, times(2)).executeUpdate(); - } + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testCleanUpSessionInfo_NoTimeout() throws Exception { + setUpKie("myname", 999L, true); - props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT); + feat.activatePolicySession(polcont, "myname", "mybase"); - PreparedStatement s = mockDbConn(0); + verify(statement).executeUpdate(); + } - feat.globalInit(null, SRC_TEST_RESOURCES); + @Test + public void testGetDroolsSessionConnector() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - setUpKie("myname", 999L, true); + mockDbConn(5); + setUpKie("myname", 999L, true); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, "myname", "mybase"); - verify(s, never()).executeUpdate(); - } + verify(fact).makeJpaConnector(emf); + } - @Test - public void testCleanUpSessionInfo_NoUrl() throws Exception { - PreparedStatement s = mockDbConn(0); + @Test + public void testReplaceSession() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - props.remove(DroolsPersistenceProperties.DB_URL); + final ArgumentCaptor<DroolsSession> sesscap = ArgumentCaptor.forClass(DroolsSession.class); - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, true); - setUpKie("myname", 999L, true); + feat.activatePolicySession(polcont, "myname", "mybase"); - try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); - } catch (RuntimeException e) { - logger.trace("expected exception", e); - } + verify(jpa).replace(sesscap.capture()); - verify(s, never()).executeUpdate(); - } + assertEquals("myname", sesscap.getValue().getSessionName()); + assertEquals(999L, sesscap.getValue().getSessionId()); + } - @Test - public void testCleanUpSessionInfo_NoUser() throws Exception { - PreparedStatement s = mockDbConn(0); + @Test + public void testIsPersistenceEnabled_Auto() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - props.remove(DroolsPersistenceProperties.DB_USER); + mockDbConn(5); + setUpKie("myname", 999L, true); - feat.globalInit(null, SRC_TEST_RESOURCES); + props.setProperty("persistence.type", "auto"); - setUpKie("myname", 999L, true); + assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); - } catch (RuntimeException e) { - logger.trace("expected exception", e); - } + @Test + public void testIsPersistenceEnabled_Native() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - verify(s, never()).executeUpdate(); - } + mockDbConn(5); + setUpKie("myname", 999L, true); - @Test - public void testCleanUpSessionInfo_NoPassword() throws Exception { - PreparedStatement s = mockDbConn(0); + props.setProperty("persistence.type", "native"); - props.remove(DroolsPersistenceProperties.DB_PWD); + assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - feat.globalInit(null, SRC_TEST_RESOURCES); + @Test + public void testIsPersistenceEnabled_None() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - setUpKie("myname", 999L, true); + mockDbConn(5); + setUpKie("myname", 999L, true); - try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); - } catch (RuntimeException e) { - logger.trace("expected exception", e); - } + props.remove("persistence.type"); - verify(s, never()).executeUpdate(); - } + assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - @Test - public void testCleanUpSessionInfo_SqlEx() throws Exception { - PreparedStatement s = mockDbConn(-1); + @Test + public void testGetProperties_Ex() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, true); - setUpKie("myname", 999L, true); + when(fact.getPolicyController(polcont)) + .thenThrow(new IllegalArgumentException("expected exception")); - feat.activatePolicySession(polcont, "myname", "mybase"); + assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - verify(s).executeUpdate(); - } + @Test + public void testGetProperty_Specific() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testGetDroolsSessionConnector() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, true); - mockDbConn(5); - setUpKie("myname", 999L, true); + props.remove("persistence.type"); + props.setProperty("persistence.myname.type", "auto"); - feat.activatePolicySession(polcont, "myname", "mybase"); + assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - verify(fact).makeJpaConnector(emf); - } + @Test + public void testGetProperty_Specific_None() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testReplaceSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, true); - ArgumentCaptor<DroolsSession> sesscap = ArgumentCaptor.forClass(DroolsSession.class); + props.remove("persistence.type"); + props.setProperty("persistence.xxx.type", "auto"); - mockDbConn(5); - setUpKie("myname", 999L, true); + assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - feat.activatePolicySession(polcont, "myname", "mybase"); + @Test + public void testGetProperty_Both_SpecificOn() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - verify(jpa).replace(sesscap.capture()); + mockDbConn(5); + setUpKie("myname", 999L, true); - assertEquals("myname", sesscap.getValue().getSessionName()); - assertEquals(999L, sesscap.getValue().getSessionId()); - } + props.setProperty("persistence.type", "other"); + props.setProperty("persistence.myname.type", "auto"); - @Test - public void testIsPersistenceEnabled_Auto() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - mockDbConn(5); - setUpKie("myname", 999L, true); + @Test + public void testGetProperty_Both_SpecificDisabledOff() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - props.setProperty("persistence.type", "auto"); + mockDbConn(5); + setUpKie("myname", 999L, true); - assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + props.setProperty("persistence.type", "auto"); + props.setProperty("persistence.myname.type", "other"); - @Test - public void testIsPersistenceEnabled_Native() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - mockDbConn(5); - setUpKie("myname", 999L, true); + @Test + public void testGetProperty_None() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - props.setProperty("persistence.type", "native"); + mockDbConn(5); + setUpKie("myname", 999L, true); - assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + props.remove("persistence.type"); - @Test - public void testIsPersistenceEnabled_None() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + } - mockDbConn(5); - setUpKie("myname", 999L, true); + @Test + public void testPersistenceFeatureException() { + SecurityException secex = new SecurityException("expected exception"); + PersistenceFeatureException ex = new PersistenceFeatureException(secex); - props.remove("persistence.type"); + assertEquals(secex, ex.getCause()); + } - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + @Test + public void testDsEmf_RtEx() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testGetProperties_Ex() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, false); - mockDbConn(5); - setUpKie("myname", 999L, true); + when(fact.makeEntMgrFact(any())).thenThrow(new IllegalArgumentException("expected exception")); - when(fact.getPolicyController(polcont)).thenThrow(new IllegalArgumentException("expected exception")); + try { + feat.activatePolicySession(polcont, "myname", "mybase"); + fail("missing exception"); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + } catch (IllegalArgumentException ex) { + logger.trace("expected exception", ex); + } - @Test - public void testGetProperty_Specific() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + verify(bds, times(2)).close(); + } - mockDbConn(5); - setUpKie("myname", 999L, true); + @Test + public void testDsEmf_Close_RtEx() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - props.remove("persistence.type"); - props.setProperty("persistence.myname.type", "auto"); + mockDbConn(5); + setUpKie("myname", 999L, false); - assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + feat.activatePolicySession(polcont, "myname", "mybase"); - @Test - public void testGetProperty_Specific_None() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - mockDbConn(5); - setUpKie("myname", 999L, true); + verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); - props.remove("persistence.type"); - props.setProperty("persistence.xxx.type", "auto"); + // return adjunct on next call + when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + try { + doThrow(new IllegalArgumentException("expected exception")).when(emf).close(); - @Test - public void testGetProperty_Both_SpecificOn() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + feat.destroyKieSession(polsess); + fail("missing exception"); - mockDbConn(5); - setUpKie("myname", 999L, true); + } catch (IllegalArgumentException ex) { + logger.trace("expected exception", ex); + } - props.setProperty("persistence.type", "other"); - props.setProperty("persistence.myname.type", "auto"); + verify(bds, times(2)).close(); + } - assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + @Test + public void testDsEmf_CloseDataSource_RtEx() throws Exception { + feat.globalInit(null, SRC_TEST_RESOURCES); - @Test - public void testGetProperty_Both_SpecificDisabledOff() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + mockDbConn(5); + setUpKie("myname", 999L, false); - mockDbConn(5); - setUpKie("myname", 999L, true); + feat.activatePolicySession(polcont, "myname", "mybase"); - props.setProperty("persistence.type", "auto"); - props.setProperty("persistence.myname.type", "other"); + ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); - @Test - public void testGetProperty_None() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + // return adjunct on next call + when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); - mockDbConn(5); - setUpKie("myname", 999L, true); + try { + doThrow(new SQLException("expected exception")).when(bds).close(); - props.remove("persistence.type"); + feat.destroyKieSession(polsess); + fail("missing exception"); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); - } + } catch (PersistenceFeatureException ex) { + logger.trace("expected exception", ex); + } + } - @Test - public void testPersistenceFeatureException() { - SecurityException secex = new SecurityException("expected exception"); - PersistenceFeatureException ex = new PersistenceFeatureException(secex); + /** + * Gets an ordered list of ids of the current SessionInfo records. + * + * @return ordered list of SessInfo IDs + * @throws SQLException sql exception + * @throws IOException io exception + */ + private List<Integer> getSessions() throws SQLException, IOException { + attachDb(); - assertEquals(secex, ex.getCause()); + ArrayList<Integer> lst = new ArrayList<>(5); - } + try (PreparedStatement stmt = conn.prepareStatement("SELECT id from sessioninfo order by id"); + ResultSet rs = stmt.executeQuery()) { - @Test - public void testDsEmf_RtEx() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + while (rs.next()) { + lst.add(rs.getInt(1)); + } + } - mockDbConn(5); - setUpKie("myname", 999L, false); - - when(fact.makeEntMgrFact(any())).thenThrow(new IllegalArgumentException("expected exception")); + return lst; + } - try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); - - } catch(IllegalArgumentException ex) { - logger.trace("expected exception", ex); - } + /** + * Sets up for doing invoking the newKieSession() method. + * + * @param sessnm name to which JPA should respond with a session + * @param sessid session id to be returned by the session + * @param loadOk {@code true} if loadKieSession() should return a value, {@code false} to return + * null + * @throws Exception exception + */ + private void setUpKie(String sessnm, long sessid, boolean loadOk) throws Exception { - verify(bds, times(2)).close(); - } + when(fact.makeJpaConnector(emf)).thenReturn(jpa); + when(fact.makeEntMgrFact(any())).thenReturn(emf); + when(fact.getPolicyController(polcont)).thenReturn(polctlr); - @Test - public void testDsEmf_Close_RtEx() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); + props.setProperty("persistence.type", "auto"); - mockDbConn(5); - setUpKie("myname", 999L, false); - - feat.activatePolicySession(polcont, "myname", "mybase"); + when(polctlr.getProperties()).thenReturn(props); - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); + when(jpa.get(sessnm)).thenReturn(sess); - verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); + when(sess.getSessionId()).thenReturn(sessid); - // return adjunct on next call - when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); + when(polsess.getPolicyContainer()).thenReturn(polcont); + when(polsess.getName()).thenReturn(sessnm); - try { - doThrow(new IllegalArgumentException("expected exception")).when(emf).close(); - - feat.destroyKieSession(polsess); - fail("missing exception"); - - } catch(IllegalArgumentException ex) { - logger.trace("expected exception", ex); - } + if (loadOk) { + when(kiesess.getIdentifier()).thenReturn(sessid); + when(kiestore.loadKieSession(anyLong(), any(), any(), any())).thenReturn(kiesess); - verify(bds, times(2)).close(); - } - - @Test - public void testDsEmf_CloseDataSource_RtEx() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - - mockDbConn(5); - setUpKie("myname", 999L, false); - - feat.activatePolicySession(polcont, "myname", "mybase"); - - ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor - .forClass(PersistenceFeature.ContainerAdjunct.class); - - verify(polcont, times(1)).setAdjunct(any(), adjcap.capture()); - - // return adjunct on next call - when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); - - try { - doThrow(new SQLException("expected exception")).when(bds).close(); - - feat.destroyKieSession(polsess); - fail("missing exception"); - - } catch(PersistenceFeatureException ex) { - logger.trace("expected exception", ex); - } - } - - /** - * Gets an ordered list of ids of the current SessionInfo records. - * - * @return ordered list of SessInfo IDs - * @throws SQLException - * @throws IOException - */ - private List<Integer> getSessions() throws SQLException, IOException { - attachDb(); - - ArrayList<Integer> lst = new ArrayList<>(5); - - try (PreparedStatement stmt = conn.prepareStatement("SELECT id from sessioninfo order by id"); - ResultSet rs = stmt.executeQuery()) { - - while (rs.next()) { - lst.add(rs.getInt(1)); - } - } - - return lst; - } - - /** - * Sets up for doing invoking the newKieSession() method. - * - * @param sessnm - * name to which JPA should respond with a session - * @param sessid - * session id to be returned by the session - * @param loadOk - * {@code true} if loadKieSession() should return a value, - * {@code false} to return null - * @throws Exception - */ - private void setUpKie(String sessnm, long sessid, boolean loadOk) throws Exception { - - when(fact.makeJpaConnector(emf)).thenReturn(jpa); - when(fact.makeEntMgrFact(any())).thenReturn(emf); - when(fact.getPolicyController(polcont)).thenReturn(polctlr); - - props.setProperty("persistence.type", "auto"); - - when(polctlr.getProperties()).thenReturn(props); - - when(jpa.get(sessnm)).thenReturn(sess); - - when(sess.getSessionId()).thenReturn(sessid); - - when(polsess.getPolicyContainer()).thenReturn(polcont); - when(polsess.getName()).thenReturn(sessnm); - - if (loadOk) { - when(kiesess.getIdentifier()).thenReturn(sessid); - when(kiestore.loadKieSession(anyLong(), any(), any(), any())).thenReturn(kiesess); - - } else { - // use an alternate id for the new session - when(kiesess.getIdentifier()).thenReturn(100L); - when(kiestore.loadKieSession(anyLong(), any(), any(), any())).thenReturn(null); - } - - when(kiestore.newKieSession(any(), any(), any())).thenReturn(kiesess); - } - - /** - * Creates the SessionInfo DB table and populates it with some data. - * - * @param expMs - * number of milli-seconds for expired sessioninfo records - * @throws SQLException - * @throws IOException - */ - private void makeSessionInfoTbl(int expMs) throws SQLException, IOException { - - attachDb(); - - try (PreparedStatement stmt = conn - .prepareStatement("CREATE TABLE sessioninfo(id int, lastmodificationdate timestamp)")) { - - stmt.executeUpdate(); - } - - try (PreparedStatement stmt = conn - .prepareStatement("INSERT into sessioninfo(id, lastmodificationdate) values(?, ?)")) { - - Timestamp ts; - - // current data - ts = new Timestamp(System.currentTimeMillis()); - stmt.setTimestamp(2, ts); - - stmt.setInt(1, 1); - stmt.executeUpdate(); - - stmt.setInt(1, 4); - stmt.executeUpdate(); - - stmt.setInt(1, 5); - stmt.executeUpdate(); - - // expired data - ts = new Timestamp(System.currentTimeMillis() - expMs); - stmt.setTimestamp(2, ts); - - stmt.setInt(1, 2); - stmt.executeUpdate(); - - stmt.setInt(1, 3); - stmt.executeUpdate(); - } - } - - /** - * Attaches {@link #conn} to the DB, if it isn't already attached. - * - * @throws SQLException - * @throws IOException - * if the property file cannot be read - */ - private void attachDb() throws SQLException, IOException { - if (conn == null) { - Properties p = loadDbProps(); - - conn = DriverManager.getConnection(p.getProperty(DroolsPersistenceProperties.DB_URL), - p.getProperty(DroolsPersistenceProperties.DB_USER), - p.getProperty(DroolsPersistenceProperties.DB_PWD)); - conn.setAutoCommit(true); - } - } - - /** - * Loads the DB properties from the file, - * <i>feature-session-persistence.properties</i>. - * - * @return the properties that were loaded - * @throws IOException - * if the property file cannot be read - * @throws FileNotFoundException - * if the property file does not exist - */ - private Properties loadDbProps() throws IOException, FileNotFoundException { - - Properties p = new Properties(); - - try (FileReader rdr = new FileReader("src/test/resources/feature-session-persistence.properties")) { - p.load(rdr); - } - - return p; - } - - /** - * Create a mock DB connection and statement. - * - * @param retval - * value to be returned when the statement is executed, or - * negative to throw an exception - * @return the statement that will be returned by the connection - * @throws SQLException - */ - private PreparedStatement mockDbConn(int retval) throws SQLException { - Connection c = mock(Connection.class); - PreparedStatement s = mock(PreparedStatement.class); - - when(bds.getConnection()).thenReturn(c); - when(fact.makeDataSource(any())).thenReturn(bds); - when(c.prepareStatement(anyString())).thenReturn(s); - - if (retval < 0) { - // should throw an exception - when(s.executeUpdate()).thenThrow(new SQLException("expected exception")); - - } else { - // should return the value - when(s.executeUpdate()).thenReturn(retval); - } - - return s; - } - - /** - * A partial factory, which exports a few of the real methods, but overrides - * the rest. - */ - private class PartialFactory extends PersistenceFeature.Factory { - - @Override - public TransactionManager getTransMgr() { - return transmgr; - } - - @Override - public UserTransaction getUserTrans() { - return usertrans; - } - - @Override - public TransactionSynchronizationRegistry getTransSyncReg() { - return transreg; - } - - @Override - public KieServices getKieServices() { - return kiesvc; - } - - @Override - public EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { - return emf; - } - - @Override - public PolicyController getPolicyController(PolicyContainer container) { - return polctlr; - } - - } + } else { + // use an alternate id for the new session + when(kiesess.getIdentifier()).thenReturn(100L); + when(kiestore.loadKieSession(anyLong(), any(), any(), any())).thenReturn(null); + } + + when(kiestore.newKieSession(any(), any(), any())).thenReturn(kiesess); + } + + /** + * Creates the SessionInfo DB table and populates it with some data. + * + * @param expMs number of milli-seconds for expired sessioninfo records + * @throws SQLException exception + * @throws IOException exception + */ + private void makeSessionInfoTbl(int expMs) throws SQLException, IOException { + + attachDb(); + + try (PreparedStatement stmt = + conn.prepareStatement("CREATE TABLE sessioninfo(id int, lastmodificationdate timestamp)")) { + + stmt.executeUpdate(); + } + + try (PreparedStatement stmt = + conn.prepareStatement("INSERT into sessioninfo(id, lastmodificationdate) values(?, ?)")) { + + Timestamp ts; + + // current data + ts = new Timestamp(System.currentTimeMillis()); + stmt.setTimestamp(2, ts); + + stmt.setInt(1, 1); + stmt.executeUpdate(); + + stmt.setInt(1, 4); + stmt.executeUpdate(); + + stmt.setInt(1, 5); + stmt.executeUpdate(); + + // expired data + ts = new Timestamp(System.currentTimeMillis() - expMs); + stmt.setTimestamp(2, ts); + + stmt.setInt(1, 2); + stmt.executeUpdate(); + + stmt.setInt(1, 3); + stmt.executeUpdate(); + } + } + + /** + * Attaches {@link #conn} to the DB, if it isn't already attached. + * + * @throws SQLException sql exception + * @throws IOException if the property file cannot be read + */ + private void attachDb() throws SQLException, IOException { + if (conn == null) { + Properties props = loadDbProps(); + + conn = + DriverManager.getConnection( + props.getProperty(DroolsPersistenceProperties.DB_URL), + props.getProperty(DroolsPersistenceProperties.DB_USER), + props.getProperty(DroolsPersistenceProperties.DB_PWD)); + conn.setAutoCommit(true); + } + } + + /** + * Loads the DB properties from the file, <i>feature-session-persistence.properties</i>. + * + * @return the properties that were loaded + * @throws IOException if the property file cannot be read + * @throws FileNotFoundException if the property file does not exist + */ + private Properties loadDbProps() throws IOException, FileNotFoundException { + + Properties props = new Properties(); + + try (FileReader rdr = + new FileReader("src/test/resources/feature-session-persistence.properties")) { + props.load(rdr); + } + + return props; + } + + /** + * Create a mock DB connection and statement. + * + * @param retval value to be returned when the statement is executed, or negative to throw an + * exception + * @return the statement that will be returned by the connection + * @throws SQLException sql exception + */ + private PreparedStatement mockDbConn(int retval) throws SQLException { + Connection connection = mock(Connection.class); + PreparedStatement statement = mock(PreparedStatement.class); + + when(bds.getConnection()).thenReturn(connection); + when(fact.makeDataSource(any())).thenReturn(bds); + when(connection.prepareStatement(anyString())).thenReturn(statement); + + if (retval < 0) { + // should throw an exception + when(statement.executeUpdate()).thenThrow(new SQLException("expected exception")); + + } else { + // should return the value + when(statement.executeUpdate()).thenReturn(retval); + } + + return statement; + } + + /** A partial factory, which exports a few of the real methods, but overrides the rest. */ + private class PartialFactory extends PersistenceFeature.Factory { + + @Override + public TransactionManager getTransMgr() { + return transmgr; + } + + @Override + public UserTransaction getUserTrans() { + return usertrans; + } + + @Override + public TransactionSynchronizationRegistry getTransSyncReg() { + return transreg; + } + + @Override + public KieServices getKieServices() { + return kiesvc; + } + + @Override + public EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { + return emf; + } + + @Override + public PolicyController getPolicyController(PolicyContainer container) { + return polctlr; + } + } } diff --git a/feature-session-persistence/src/test/resources/META-INF/persistence.xml b/feature-session-persistence/src/test/resources/META-INF/persistence.xml index 5cc1badc..0fc695d3 100644 --- a/feature-session-persistence/src/test/resources/META-INF/persistence.xml +++ b/feature-session-persistence/src/test/resources/META-INF/persistence.xml @@ -3,7 +3,7 @@ ============LICENSE_START======================================================= feature-session-persistence ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,15 +20,19 @@ --> <persistence version="2.1" - xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> - <persistence-unit name="junitDroolsSessionEntityPU" transaction-type="JTA"> - <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> - <class>org.onap.policy.drools.persistence.DroolsSessionEntity</class> - <properties> - <property name="javax.persistence.schema-generation.database.action" value="create"/> + <persistence-unit name="junitDroolsSessionEntityPU" + transaction-type="JTA"> + <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> + <class>org.onap.policy.drools.persistence.DroolsSessionEntity</class> + <properties> + <property + name="javax.persistence.schema-generation.database.action" + value="create" /> </properties> - </persistence-unit> + </persistence-unit> </persistence> diff --git a/feature-session-persistence/src/test/resources/logback-test.xml b/feature-session-persistence/src/test/resources/logback-test.xml index 6e919eb9..7f151173 100644 --- a/feature-session-persistence/src/test/resources/logback-test.xml +++ b/feature-session-persistence/src/test/resources/logback-test.xml @@ -2,7 +2,7 @@ ============LICENSE_START======================================================= feature-session-persistence ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -22,25 +22,28 @@ <configuration> - <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> - <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> - <Pattern> - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n - </Pattern> - </encoder> - </appender> - <appender name="FILE" class="ch.qos.logback.core.FileAppender"> - <file>logs/debug.log</file> - <encoder> - <Pattern> - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n - </Pattern> - </encoder> - </appender> - - <root level="debug"> - <appender-ref ref="STDOUT" /> - <appender-ref ref="FILE" /> - </root> + <appender name="STDOUT" + class="ch.qos.logback.core.ConsoleAppender"> + <encoder + class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> + <Pattern> + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n + </Pattern> + </encoder> + </appender> + <appender name="FILE" + class="ch.qos.logback.core.FileAppender"> + <file>logs/debug.log</file> + <encoder> + <Pattern> + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n + </Pattern> + </encoder> + </appender> + + <root level="debug"> + <appender-ref ref="STDOUT" /> + <appender-ref ref="FILE" /> + </root> </configuration> diff --git a/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java b/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java index 44700890..5e7861d2 100644 --- a/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java +++ b/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java @@ -22,11 +22,10 @@ package org.onap.policy.drools.simulators; import java.io.IOException; import java.util.Map; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import java.util.function.Function; -import java.util.concurrent.BlockingQueue; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; @@ -44,96 +43,111 @@ import org.slf4j.LoggerFactory; @Path("/events") public class DMaaPSimulatorJaxRs { - private static final String NO_DATA_MSG = "No Data"; - private static final Map<String, BlockingQueue<String>> queues = new ConcurrentHashMap<>(); - private static final Logger logger = LoggerFactory.getLogger(DMaaPSimulatorJaxRs.class); - private static int responseCode = 200; + private static final String NO_DATA_MSG = "No Data"; + private static final Map<String, BlockingQueue<String>> queues = new ConcurrentHashMap<>(); + private static final Logger logger = LoggerFactory.getLogger(DMaaPSimulatorJaxRs.class); + private static int responseCode = 200; + + /** + * Get consumer ID. + * + * @param timeout timeout value + * @param topicName the dmaap topic + * @param httpResponse http response object + * @return topic or error message + */ + @GET + @Path("/{topicName}/{consumeGroup}/{consumerId}") + public String subscribe(@DefaultValue("0") @QueryParam("timeout") int timeout, @PathParam("topicName") + String topicName, @Context final HttpServletResponse httpResponse) { + int currentRespCode = responseCode; + httpResponse.setStatus(currentRespCode); + try { + httpResponse.flushBuffer(); + } catch (IOException e) { + logger.error("flushBuffer threw: ", e); + return "Got an error"; + } - @GET - @Path("/{topicName}/{consumeGroup}/{consumerId}") - public String subscribe(@DefaultValue("0") @QueryParam("timeout") int timeout, @PathParam("topicName") String topicName, - @Context final HttpServletResponse httpResponse) { - int currentRespCode = responseCode; - httpResponse.setStatus(currentRespCode); - try { - httpResponse.flushBuffer(); - } catch (IOException e) { - logger.error("flushBuffer threw: ", e); - return "Got an error"; - } + if (currentRespCode < 200 || currentRespCode >= 300) { + return "You got response code: " + currentRespCode; + } - if (currentRespCode < 200 || currentRespCode >= 300) - { - return "You got response code: " + currentRespCode; - } + if (queues.containsKey(topicName)) { + return getNextMessageFromQueue(timeout, topicName); + } + else if (timeout > 0) { + return waitForNextMessageFromQueue(timeout, topicName); + } + return "No topic"; + } - if (queues.containsKey(topicName)) { - return getNextMessageFromQueue(timeout, topicName); - } - else if (timeout > 0) { - return waitForNextMessageFromQueue(timeout, topicName); - } - return "No topic"; - } + private String getNextMessageFromQueue(final int timeout, final String topicName) { + BlockingQueue<String> queue = queues.get(topicName); + String response = NO_DATA_MSG; + try { + response = queue.poll(timeout, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + logger.debug("error in DMaaP simulator", e); + Thread.currentThread().interrupt(); + } + if (response == null) { + response = NO_DATA_MSG; + } + return response; + } - private String getNextMessageFromQueue(final int timeout, final String topicName) { - BlockingQueue<String> queue = queues.get(topicName); - String response = NO_DATA_MSG; - try { - response = queue.poll(timeout, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - logger.debug("error in DMaaP simulator", e); - Thread.currentThread().interrupt(); - } - if (response == null) { - response = NO_DATA_MSG; - } - return response; - } + private String waitForNextMessageFromQueue(int timeout, String topicName) { + try { + Thread.sleep(timeout); + if (queues.containsKey(topicName)) { + BlockingQueue<String> queue = queues.get(topicName); + String response = queue.poll(); + if (response == null) { + response = NO_DATA_MSG; + } + return response; + } + } catch (InterruptedException e) { + logger.debug("error in DMaaP simulator", e); + Thread.currentThread().interrupt(); + } + return "No topic"; + } - private String waitForNextMessageFromQueue(int timeout, String topicName) { - try { - Thread.sleep(timeout); - if (queues.containsKey(topicName)) { - BlockingQueue<String> queue = queues.get(topicName); - String response = queue.poll(); - if (response == null) { - response = NO_DATA_MSG; - } - return response; - } - } catch (InterruptedException e) { - logger.debug("error in DMaaP simulator", e); - Thread.currentThread().interrupt(); - } - return "No topic"; - } + /** + * Post to a topic. + * + * @param topicName name of the topic + * @param body message + * @return empty string + */ + @POST + @Path("/{topicName}") + @Consumes(MediaType.TEXT_PLAIN) + public String publish(@PathParam("topicName") String topicName, String body) { + BlockingQueue<String> queue = queues.computeIfAbsent(topicName, entry -> new LinkedBlockingQueue<>()); - @POST - @Path("/{topicName}") - @Consumes(MediaType.TEXT_PLAIN) - public String publish(@PathParam("topicName") String topicName, String body) { - BlockingQueue<String> queue = queues.computeIfAbsent(topicName, entry -> new LinkedBlockingQueue<>()); - - if (!queue.offer(body)) { - logger.warn("error on topic {}, failed to place body {} on queue", topicName, body); - } + if (!queue.offer(body)) { + logger.warn("error on topic {}, failed to place body {} on queue", topicName, body); + } - return ""; - } + return ""; + } - @POST - @Path("/setStatus") - public String setStatus(@QueryParam("statusCode") int statusCode) { - setResponseCode(statusCode); - return "Status code set"; - } + @POST + @Path("/setStatus") + public String setStatus(@QueryParam("statusCode") int statusCode) { + setResponseCode(statusCode); + return "Status code set"; + } - /** - * Static method to set static response code, synchronized for multiple possible uses - * @param incomingResponseCode - */ - private static synchronized void setResponseCode(final int incomingResponseCode) { - responseCode = incomingResponseCode; - } + /** + * Static method to set static response code, synchronized for multiple possible uses. + * + * @param incomingResponseCode the response code to set + */ + private static synchronized void setResponseCode(final int incomingResponseCode) { + responseCode = incomingResponseCode; + } } diff --git a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java index 1a19284b..2a849525 100644 --- a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java +++ b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java @@ -44,6 +44,9 @@ public class DMaaPSimulatorTest { private static final int DMAAPSIM_SERVER_PORT = 6670; + /** + * Setup the simulator. + */ @BeforeClass public static void setUpSimulator() { LoggerUtil.setLevel("ROOT", "INFO"); @@ -71,8 +74,8 @@ public class DMaaPSimulatorTest { int timeout = 1000; Pair<Integer, String> response = dmaapGet("myTopicNoData", timeout); assertNotNull(response); - assertNotNull(response.a); - assertEquals("No topic", response.b); + assertNotNull(response.first); + assertEquals("No topic", response.second); } @Test @@ -81,13 +84,13 @@ public class DMaaPSimulatorTest { String testData = "This is some test data"; Pair<Integer, String> response = dmaapPost(myTopic, testData); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapGet(myTopic, 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals(testData, response.b); + assertNotNull(response.first); + assertEquals(testData, response.second); } @Test @@ -96,118 +99,118 @@ public class DMaaPSimulatorTest { String myTopic = "myTopicMultiPost"; Pair<Integer, String> response = dmaapPost(myTopic, data[0]); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapPost(myTopic, data[1]); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapPost(myTopic, data[2]); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapGet(myTopic, 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals(data[0], response.b); + assertNotNull(response.first); + assertEquals(data[0], response.second); response = dmaapGet(myTopic, 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals(data[1], response.b); + assertNotNull(response.first); + assertEquals(data[1], response.second); response = dmaapGet(myTopic, 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals(data[2], response.b); + assertNotNull(response.first); + assertEquals(data[2], response.second); } @Test public void testMultiTopic() { String[][] data = {{"Topic one message one", "Topic one message two"}, - {"Topic two message one", "Topic two message two"}}; + {"Topic two message one", "Topic two message two"}}; String[] topics = {"topic1", "topic2"}; Pair<Integer, String> response = dmaapPost(topics[0], data[0][0]); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapGet(topics[0], 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals(data[0][0], response.b); + assertNotNull(response.first); + assertEquals(data[0][0], response.second); response = dmaapGet(topics[1], 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals("No topic", response.b); + assertNotNull(response.first); + assertEquals("No topic", response.second); response = dmaapPost(topics[1], data[1][0]); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapPost(topics[1], data[1][1]); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapPost(topics[0], data[0][1]); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapGet(topics[1], 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals(data[1][0], response.b); + assertNotNull(response.first); + assertEquals(data[1][0], response.second); response = dmaapGet(topics[0], 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals(data[0][1], response.b); + assertNotNull(response.first); + assertEquals(data[0][1], response.second); response = dmaapGet(topics[1], 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals(data[1][1], response.b); + assertNotNull(response.first); + assertEquals(data[1][1], response.second); response = dmaapGet(topics[0], 1000); assertNotNull(response); - assertNotNull(response.a); - assertEquals("No Data", response.b); + assertNotNull(response.first); + assertEquals("No Data", response.second); } @Test public void testResponseCode() { Pair<Integer, String> response = dmaapPost("myTopic", "myTopicData"); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = setStatus(503); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapGet("myTopic", 500); assertNotNull(response); - assertEquals(503, response.a.intValue()); - assertEquals("You got response code: 503", response.b); + assertEquals(503, response.first.intValue()); + assertEquals("You got response code: 503", response.second); response = setStatus(202); assertNotNull(response); - assertNotNull(response.a); - assertNotNull(response.b); + assertNotNull(response.first); + assertNotNull(response.second); response = dmaapGet("myTopic", 500); assertNotNull(response); - assertEquals(202, response.a.intValue()); - assertEquals("myTopicData", response.b); + assertEquals(202, response.first.intValue()); + assertEquals("myTopicData", response.second); } private static Pair<Integer, String> dmaapGet(String topic, int timeout) { @@ -345,12 +348,12 @@ public class DMaaPSimulatorTest { } private static class Pair<A, B> { - public final A a; - public final B b; + public final A first; + public final B second; - public Pair(A a, B b) { - this.a = a; - this.b = b; + public Pair(A first, B second) { + this.first = first; + this.second = second; } } } diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java index 33f672c5..108ee6b0 100644 --- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java +++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-state-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -31,212 +31,203 @@ import java.util.UUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * This class audits the database +/** This class audits the database. */ -public class DbAudit extends DroolsPDPIntegrityMonitor.AuditBase -{ - // get an instance of logger - private static Logger logger = LoggerFactory.getLogger(DbAudit.class); - // single global instance of this audit object - private static final DbAudit instance = new DbAudit(); - - // This indicates if 'CREATE TABLE IF NOT EXISTS Audit ...' should be - // invoked -- doing this avoids the need to create the table in advance. - private static boolean createTableNeeded = true; - - private static boolean isJunit = false; - - /** - * Constructor - set the name to 'Database' - */ - private DbAudit() - { - super("Database"); - } - - private static synchronized void setCreateTableNeeded(boolean b) { - DbAudit.createTableNeeded = b; - } - - public static synchronized void setIsJunit(boolean b) { - DbAudit.isJunit = b; - } - - public static boolean isJunit(){ - return DbAudit.isJunit; - } - - - /** - * @return the single 'DbAudit' instance - */ - public static DroolsPDPIntegrityMonitor.AuditBase getInstance() - { - return instance; - } - - /** - * Invoke the audit - * - * @param properties properties to be passed to the audit - */ - @Override - public void invoke(Properties properties) - { - logger.debug("Running 'DbAudit.invoke'"); - boolean doCreate = createTableNeeded && !isJunit; - - if(!isActive()){ - logger.info("DbAudit.invoke: exiting because isActive = false"); - return; - } - - // fetch DB properties from properties file -- they are already known - // to exist, because they were verified by the 'IntegrityMonitor' - // constructor - String url = properties.getProperty(StateManagementProperties.DB_URL); - String user = properties.getProperty(StateManagementProperties.DB_USER); - String password = properties.getProperty(StateManagementProperties.DB_PWD); - - // operation phase currently running -- used to construct an error - // message, if needed - String phase = null; - - // create connection to DB - phase = "creating connection"; - logger.debug("DbAudit: Creating connection to {}", url); - try (Connection connection = DriverManager.getConnection(url, user, password)) - { - - // create audit table, if needed - if (doCreate) - { - phase = "create table"; - createTable(connection); - } - - // insert an entry into the table - phase = "insert entry"; - String key = UUID.randomUUID().toString(); - insertEntry(connection, key); - - phase = "fetch entry"; - findEntry(connection, key); - - phase = "delete entry"; - deleteEntry(connection, key); - } - catch (Exception e) - { - String message = "DbAudit: Exception during audit, phase = " + phase; - logger.error(message, e); - setResponse(message); - } - } - - /** - * Determines if the DbAudit is active, based on properties. Defaults to - * {@code true}, if not found in the properties. - * @return {@code true} if DbAudit is active, {@code false} otherwise - */ - private boolean isActive() { - String dbAuditIsActive = StateManagementProperties.getProperty("db.audit.is.active"); - logger.debug("DbAudit.invoke: dbAuditIsActive = {}", dbAuditIsActive); - - if (dbAuditIsActive != null) { - try { - return Boolean.parseBoolean(dbAuditIsActive.trim()); - } catch (NumberFormatException e) { - logger.warn("DbAudit.invoke: Ignoring invalid property: db.audit.is.active = {}", dbAuditIsActive); - } - } - - return true; - } - - /** - * Creates the table. - * @param connection - * @throws SQLException - */ - private void createTable(Connection connection) throws SQLException { - logger.info("DbAudit: Creating 'Audit' table, if needed"); - try (PreparedStatement statement = connection.prepareStatement - ("CREATE TABLE IF NOT EXISTS Audit (\n" - + " name varchar(64) DEFAULT NULL,\n" - + " UNIQUE KEY name (name)\n" - + ") DEFAULT CHARSET=latin1;")) { - statement.execute(); - DbAudit.setCreateTableNeeded(false); - } - } - - /** - * Inserts an entry. - * @param connection - * @param key - * @throws SQLException - */ - private void insertEntry(Connection connection, String key) throws SQLException { - try (PreparedStatement statement = connection.prepareStatement - ("INSERT INTO Audit (name) VALUES (?)")) { - statement.setString(1, key); - statement.executeUpdate(); - } - } - - /** - * Finds an entry. - * @param connection - * @param key - * @throws SQLException - */ - private void findEntry(Connection connection, String key) throws SQLException { - try (PreparedStatement statement = connection.prepareStatement - ("SELECT name FROM Audit WHERE name = ?")) { - statement.setString(1, key); - getEntry(statement, key); - } - } - - /** - * Executes the query to determine if the entry exists. Sets the response - * if it fails. - * @param statement - * @param key - * @throws SQLException - */ - private void getEntry(PreparedStatement statement, String key) throws SQLException { - try (ResultSet rs = statement.executeQuery()) { - if (rs.first()) - { - // found entry - if(logger.isDebugEnabled()){ - logger.debug("DbAudit: Found key {}", rs.getString(1)); - } - } - else - { - logger.error - ("DbAudit: can't find newly-created entry with key {}", key); - setResponse("Can't find newly-created entry"); - } - } - } - - /** - * Deletes an entry. - * @param connection - * @param key - * @throws SQLException - */ - private void deleteEntry(Connection connection, String key) throws SQLException { - try (PreparedStatement statement = connection.prepareStatement - ("DELETE FROM Audit WHERE name = ?")) { - statement.setString(1, key); - statement.executeUpdate(); - } - } - +public class DbAudit extends DroolsPDPIntegrityMonitor.AuditBase { + // get an instance of logger + private static Logger logger = LoggerFactory.getLogger(DbAudit.class); + // single global instance of this audit object + private static final DbAudit instance = new DbAudit(); + + // This indicates if 'CREATE TABLE IF NOT EXISTS Audit ...' should be + // invoked -- doing this avoids the need to create the table in advance. + private static boolean createTableNeeded = true; + + private static boolean isJunit = false; + + /** Constructor - set the name to 'Database'. */ + private DbAudit() { + super("Database"); + } + + private static synchronized void setCreateTableNeeded(boolean isNeeded) { + DbAudit.createTableNeeded = isNeeded; + } + + public static synchronized void setIsJunit(boolean isJUnit) { + DbAudit.isJunit = isJUnit; + } + + public static boolean isJunit() { + return DbAudit.isJunit; + } + + /** + * Get the instance. + * + * @return the single 'DbAudit' instance. */ + public static DroolsPDPIntegrityMonitor.AuditBase getInstance() { + return instance; + } + + /** + * Invoke the audit. + * + * @param properties properties to be passed to the audit + */ + @Override + public void invoke(Properties properties) { + logger.debug("Running 'DbAudit.invoke'"); + boolean doCreate = createTableNeeded && !isJunit; + + if (!isActive()) { + logger.info("DbAudit.invoke: exiting because isActive = false"); + return; + } + + // fetch DB properties from properties file -- they are already known + // to exist, because they were verified by the 'IntegrityMonitor' + // constructor + String url = properties.getProperty(StateManagementProperties.DB_URL); + String user = properties.getProperty(StateManagementProperties.DB_USER); + String password = properties.getProperty(StateManagementProperties.DB_PWD); + + // operation phase currently running -- used to construct an error + // message, if needed + String phase = null; + + // create connection to DB + phase = "creating connection"; + logger.debug("DbAudit: Creating connection to {}", url); + try (Connection connection = DriverManager.getConnection(url, user, password)) { + + // create audit table, if needed + if (doCreate) { + phase = "create table"; + createTable(connection); + } + + // insert an entry into the table + phase = "insert entry"; + String key = UUID.randomUUID().toString(); + insertEntry(connection, key); + + phase = "fetch entry"; + findEntry(connection, key); + + phase = "delete entry"; + deleteEntry(connection, key); + } catch (Exception e) { + String message = "DbAudit: Exception during audit, phase = " + phase; + logger.error(message, e); + setResponse(message); + } + } + + /** + * Determines if the DbAudit is active, based on properties. Defaults to {@code true}, if not + * found in the properties. + * + * @return {@code true} if DbAudit is active, {@code false} otherwise + */ + private boolean isActive() { + String dbAuditIsActive = StateManagementProperties.getProperty("db.audit.is.active"); + logger.debug("DbAudit.invoke: dbAuditIsActive = {}", dbAuditIsActive); + + if (dbAuditIsActive != null) { + try { + return Boolean.parseBoolean(dbAuditIsActive.trim()); + } catch (NumberFormatException e) { + logger.warn( + "DbAudit.invoke: Ignoring invalid property: db.audit.is.active = {}", dbAuditIsActive); + } + } + + return true; + } + + /** + * Creates the table. + * + * @param connection connection + * @throws SQLException exception + */ + private void createTable(Connection connection) throws SQLException { + logger.info("DbAudit: Creating 'Audit' table, if needed"); + try (PreparedStatement statement = + connection.prepareStatement( + "CREATE TABLE IF NOT EXISTS Audit (\n" + + " name varchar(64) DEFAULT NULL,\n" + + " UNIQUE KEY name (name)\n" + + ") DEFAULT CHARSET=latin1;")) { + statement.execute(); + DbAudit.setCreateTableNeeded(false); + } + } + + /** + * Inserts an entry. + * + * @param connection connection + * @param key key + * @throws SQLException exception + */ + private void insertEntry(Connection connection, String key) throws SQLException { + try (PreparedStatement statement = + connection.prepareStatement("INSERT INTO Audit (name) VALUES (?)")) { + statement.setString(1, key); + statement.executeUpdate(); + } + } + + /** + * Finds an entry. + * + * @param connection connection + * @param key key + * @throws SQLException exception + */ + private void findEntry(Connection connection, String key) throws SQLException { + try (PreparedStatement statement = + connection.prepareStatement("SELECT name FROM Audit WHERE name = ?")) { + statement.setString(1, key); + getEntry(statement, key); + } + } + + /** + * Executes the query to determine if the entry exists. Sets the response if it fails. + * + * @param statement statement + * @param key key + * @throws SQLException exception + */ + private void getEntry(PreparedStatement statement, String key) throws SQLException { + try (ResultSet rs = statement.executeQuery()) { + if (rs.first()) { + // found entry + if (logger.isDebugEnabled()) { + logger.debug("DbAudit: Found key {}", rs.getString(1)); + } + } else { + logger.error("DbAudit: can't find newly-created entry with key {}", key); + setResponse("Can't find newly-created entry"); + } + } + } + + /** + * Deletes an entry. + * + * @param connection connection + * @param key key + * @throws SQLException exception + */ + private void deleteEntry(Connection connection, String key) throws SQLException { + try (PreparedStatement statement = + connection.prepareStatement("DELETE FROM Audit WHERE name = ?")) { + statement.setString(1, key); + statement.executeUpdate(); + } + } } diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java index a7606eb2..3cb99f23 100644 --- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java +++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-state-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,7 +54,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { private static final String PROPERTIES_NAME = "feature-state-management.properties"; /** - * Constructor - pass arguments to superclass, but remember properties + * Constructor - pass arguments to superclass, but remember properties. * * @param resourceName unique name of this Integrity Monitor * @param url the JMX URL of the MBean server @@ -81,10 +81,10 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { /** * Static initialization -- create Drools Integrity Monitor, and an HTTP server to handle REST - * 'test' requests + * 'test' requests. * - * @throws StateManagementPropertiesException - * @throws IntegrityMonitorException + * @throws StateManagementPropertiesException exception + * @throws IntegrityMonitorException exception */ public static DroolsPDPIntegrityMonitor init(String configDir) throws IntegrityMonitorException { @@ -125,9 +125,9 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { checkPropError(stateManagementProperties, StateManagementProperties.DB_USER); checkPropError(stateManagementProperties, StateManagementProperties.DB_PWD); - String testHost = stateManagementProperties.getProperty(StateManagementProperties.TEST_HOST); - String testPort = stateManagementProperties.getProperty(StateManagementProperties.TEST_PORT); - String resourceName = stateManagementProperties.getProperty(StateManagementProperties.RESOURCE_NAME); + final String testHost = stateManagementProperties.getProperty(StateManagementProperties.TEST_HOST); + final String testPort = stateManagementProperties.getProperty(StateManagementProperties.TEST_PORT); + final String resourceName = stateManagementProperties.getProperty(StateManagementProperties.RESOURCE_NAME); subsystemTestProperties = stateManagementProperties; @@ -148,8 +148,8 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { * * @param resourceName unique name of this Integrity Monitor * @param properties properties used to configure the Integrity Monitor - * @return - * @throws IntegrityMonitorException + * @return monitor object + * @throws IntegrityMonitorException exception */ private static DroolsPDPIntegrityMonitor makeMonitor(String resourceName, Properties properties) throws IntegrityMonitorException { @@ -168,7 +168,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { * @param testHost host name * @param testPort port * @param properties properties used to configure the rest server - * @throws IntegrityMonitorException + * @throws IntegrityMonitorException exception */ private static void makeRestServer(String testHost, String testPort, Properties properties) throws IntegrityMonitorException { @@ -190,7 +190,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { * * @param configDir directory containing the property file * @return the properties - * @throws IntegrityMonitorException + * @throws IntegrityMonitorException exception */ private static Properties getProperties(String configDir) throws IntegrityMonitorException { try { @@ -206,7 +206,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { * * @param props set of properties * @param name name of the property to check - * @throws IntegrityMonitorException + * @throws IntegrityMonitorException exception */ private static void checkPropError(Properties props, String name) throws IntegrityMonitorException { String val = props.getProperty(name); @@ -260,7 +260,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { } /** - * Run tests (audits) unique to Drools PDP VM (Database + Repository) + * Run tests (audits) unique to Drools PDP VM (Database + Repository). */ @Override public void subsystemTest() throws IntegrityMonitorException { @@ -308,7 +308,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { /* ============================================================ */ /** - * This is the base class for audits invoked in 'subsystemTest' + * This is the base class for audits invoked in 'subsystemTest'. */ public abstract static class AuditBase { // name of the audit @@ -318,7 +318,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { protected String response; /** - * Constructor - initialize the name, and clear the initial response + * Constructor - initialize the name, and clear the initial response. * * @param name name of the audit */ @@ -328,6 +328,8 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { } /** + * Get the name. + * * @return the name of this audit */ public String getName() { @@ -335,6 +337,8 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { } /** + * Get the response. + * * @return the response String (non-null indicates the error message) */ public String getResponse() { @@ -342,7 +346,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { } /** - * Set the response string to the specified value + * Set the response string to the specified value. * * @param value the new value of the response string (null = no errors) */ @@ -351,7 +355,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { } /** - * Abstract method to invoke the audit + * Abstract method to invoke the audit. * * @param persistenceProperties Used for DB access * @throws Exception passed in by the audit @@ -416,6 +420,12 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor { } } + /** + * Returns the instance. + * + * @return DroolsPDPIntegrityMonitor object + * @throws IntegrityMonitorException exception + */ public static DroolsPDPIntegrityMonitor getInstance() throws IntegrityMonitorException { if (logger.isDebugEnabled()) { logger.debug("getInstance() called"); diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java index b6491e7a..49e4577c 100644 --- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java +++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-state-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,11 @@ package org.onap.policy.drools.statemanagement; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response; @@ -27,84 +32,82 @@ import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; @Api(value = "test") - @Path("/") +@Path("/") public class IntegrityMonitorRestManager { - private static Logger logger = LoggerFactory.getLogger(IntegrityMonitorRestManager.class); - private DroolsPDPIntegrityMonitor im; - - /** - * Test interface for Integrity Monitor - * - * @return Exception message if exception, otherwise empty - */ - @ApiOperation( - value = "Test endpoint for integrity monitor", - notes = "The TEST command is used to request data from a subcomponent " - + "instance that can be used to determine its operational state. " - + "A 200/success response status code should be returned if the " - + "subcomponent instance is functioning properly and able to respond to requests.", - response = String.class) - @ApiResponses(value = { - @ApiResponse( - code = 200, - message = "Integrity monitor sanity check passed"), - @ApiResponse( - code = 500, - message = "Integrity monitor sanity check encountered an exception. This can indicate operational state disabled or administrative state locked") - }) - @GET - @Path("test") - public Response test() { - if(logger.isDebugEnabled()){ - logger.debug("integrity monitor /test accessed"); - } - // The responses are stored within the audit objects, so we need to - // invoke the audits and get responses before we handle another - // request. - synchronized (IntegrityMonitorRestManager.class) { - // will include messages associated with subsystem failures - StringBuilder body = new StringBuilder(); + private static Logger logger = LoggerFactory.getLogger(IntegrityMonitorRestManager.class); + private DroolsPDPIntegrityMonitor im; + + /** + * Test interface for Integrity Monitor. + * + * @return Exception message if exception, otherwise empty + */ + @ApiOperation( + value = "Test endpoint for integrity monitor", + notes = "The TEST command is used to request data from a subcomponent " + + "instance that can be used to determine its operational state. " + + "A 200/success response status code should be returned if the " + + "subcomponent instance is functioning properly and able to respond to requests.", + response = String.class) + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "Integrity monitor sanity check passed"), + @ApiResponse( + code = 500, + message = "Integrity monitor sanity check encountered an exception. " + + "This can indicate operational state disabled or administrative state locked") + }) + @GET + @Path("test") + public Response test() { + if (logger.isDebugEnabled()) { + logger.debug("integrity monitor /test accessed"); + } + // The responses are stored within the audit objects, so we need to + // invoke the audits and get responses before we handle another + // request. + synchronized (IntegrityMonitorRestManager.class) { + // will include messages associated with subsystem failures + StringBuilder body = new StringBuilder(); - // 200=SUCCESS, 500=failure - int responseValue = 200; + // 200=SUCCESS, 500=failure + int responseValue = 200; - if (im == null) { - try { - im = DroolsPDPIntegrityMonitor.getInstance(); - } catch (Exception e) { - logger.error("IntegrityMonitorRestManager: test() interface caught an exception", e); - body.append("\nException: " + e + "\n"); - responseValue = 500; - } - } + if (im == null) { + try { + im = DroolsPDPIntegrityMonitor.getInstance(); + } catch (Exception e) { + logger.error("IntegrityMonitorRestManager: test() interface caught an exception", e); + body.append("\nException: " + e + "\n"); + responseValue = 500; + } + } - if (im != null) { - try { - // call 'IntegrityMonitor.evaluateSanity()' - im.evaluateSanity(); - } catch (Exception e) { - // this exception isn't coming from one of the audits, - // because those are caught in 'subsystemTest()' - logger.error("DroolsPDPIntegrityMonitor.evaluateSanity()", e); + if (im != null) { + try { + // call 'IntegrityMonitor.evaluateSanity()' + im.evaluateSanity(); + } catch (Exception e) { + // this exception isn't coming from one of the audits, + // because those are caught in 'subsystemTest()' + logger.error("DroolsPDPIntegrityMonitor.evaluateSanity()", e); - // include exception in HTTP response - body.append("\nException: " + e + "\n"); - responseValue = 500; - } - } + // include exception in HTTP response + body.append("\nException: " + e + "\n"); + responseValue = 500; + } + } - // send response, including the contents of 'body' - // (which is empty if everything is successful) - if (responseValue == 200) - return Response.status(Response.Status.OK).build(); - else - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(body.toString()).build(); - } - } + // send response, including the contents of 'body' + // (which is empty if everything is successful) + if (responseValue == 200) { + return Response.status(Response.Status.OK).build(); + } else { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(body.toString()).build(); + } + } + } } diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java index 6ddf0c78..92ec2ac0 100644 --- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java +++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-state-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,529 +37,456 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * This class audits the Maven repository + * This class audits the Maven repository. */ -public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase -{ - private static final long DEFAULT_TIMEOUT = 60; // timeout in 60 seconds - - // get an instance of logger - private static Logger logger = LoggerFactory.getLogger(RepositoryAudit.class); - // single global instance of this audit object - private static RepositoryAudit instance = new RepositoryAudit(); - - /** - * Constructor - set the name to 'Repository' - */ - private RepositoryAudit() - { - super("Repository"); - } - - /** - * @return the single 'RepositoryAudit' instance - */ - public static DroolsPDPIntegrityMonitor.AuditBase getInstance() - { - return instance; - } - - /** - * Invoke the audit - * - * @param properties properties to be passed to the audit - */ - @Override - public void invoke(Properties properties) - throws IOException, InterruptedException - { - if(logger.isDebugEnabled()){ - logger.debug("Running 'RepositoryAudit.invoke'"); - } - - boolean isActive = true; - boolean ignoreErrors = true; // ignore errors by default - String repoAuditIsActive = StateManagementProperties.getProperty("repository.audit.is.active"); - String repoAuditIgnoreErrors = - StateManagementProperties.getProperty("repository.audit.ignore.errors"); - logger.debug("RepositoryAudit.invoke: repoAuditIsActive = {}" - + ", repoAuditIgnoreErrors = {}",repoAuditIsActive, repoAuditIgnoreErrors); - - if (repoAuditIsActive != null) { - try { - isActive = Boolean.parseBoolean(repoAuditIsActive.trim()); - } catch (NumberFormatException e) { - logger.warn("RepositoryAudit.invoke: Ignoring invalid property: repository.audit.is.active = {}", repoAuditIsActive); - } - } - - if(!isActive){ - logger.info("RepositoryAudit.invoke: exiting because isActive = {}", isActive); - return; - } - - if (repoAuditIgnoreErrors != null) - { - try - { - ignoreErrors = Boolean.parseBoolean(repoAuditIgnoreErrors.trim()); - } - catch (NumberFormatException e) - { - ignoreErrors = true; - logger.warn("RepositoryAudit.invoke: Ignoring invalid property: repository.audit.ignore.errors = {}", repoAuditIgnoreErrors); - } - }else{ - ignoreErrors = true; - } - - // Fetch repository information from 'IntegrityMonitorProperties' - String repositoryId = - StateManagementProperties.getProperty("repository.audit.id"); - String repositoryUrl = - StateManagementProperties.getProperty("repository.audit.url"); - String repositoryUsername = - StateManagementProperties.getProperty("repository.audit.username"); - String repositoryPassword = - StateManagementProperties.getProperty("repository.audit.password"); - boolean upload = - repositoryId != null && repositoryUrl != null - && repositoryUsername != null && repositoryPassword != null; - - // used to incrementally construct response as problems occur - // (empty = no problems) - StringBuilder response = new StringBuilder(); - - long timeoutInSeconds = DEFAULT_TIMEOUT; - String timeoutString = - StateManagementProperties.getProperty("repository.audit.timeout"); - if (timeoutString != null && !timeoutString.isEmpty()) - { - try - { - timeoutInSeconds = Long.valueOf(timeoutString); - } - catch (NumberFormatException e) - { - logger.error - ("RepositoryAudit: Invalid 'repository.audit.timeout' value: '{}'", timeoutString, e); - if (!ignoreErrors) - { - response.append("Invalid 'repository.audit.timeout' value: '") - .append(timeoutString).append("'\n"); - setResponse(response.toString()); - } - } - } - - // artifacts to be downloaded - LinkedList<Artifact> artifacts = new LinkedList<>(); - - /* - * 1) create temporary directory - */ - Path dir = Files.createTempDirectory("auditRepo"); - logger.info("RepositoryAudit: temporary directory = {}", dir); - - // nested 'pom.xml' file and 'repo' directory - Path pom = dir.resolve("pom.xml"); - Path repo = dir.resolve("repo"); - - /* - * 2) Create test file, and upload to repository - * (only if repository information is specified) - */ - String groupId = null; - String artifactId = null; - String version = null; - if (upload) - { - groupId = "org.onap.policy.audit"; - artifactId = "repository-audit"; - version = "0." + System.currentTimeMillis(); - - if (repositoryUrl.toLowerCase().contains("snapshot")) - { - // use SNAPSHOT version - version += "-SNAPSHOT"; - } - - // create text file to write - FileOutputStream fos = - new FileOutputStream(dir.resolve("repository-audit.txt").toFile()); - try - { - fos.write(version.getBytes()); - } - finally - { - fos.close(); - } - - // try to install file in repository - if (runProcess - (timeoutInSeconds, dir.toFile(), null, - "mvn", "deploy:deploy-file", - "-DrepositoryId=" + repositoryId, - "-Durl=" + repositoryUrl, - "-Dfile=repository-audit.txt", - "-DgroupId=" + groupId, - "-DartifactId=" + artifactId, - "-Dversion=" + version, - "-Dpackaging=txt", - "-DgeneratePom=false") != 0) - { - logger.error - ("RepositoryAudit: 'mvn deploy:deploy-file' failed"); - if (!ignoreErrors) - { - response.append("'mvn deploy:deploy-file' failed\n"); - setResponse(response.toString()); - } - } - else - { - logger.info - ("RepositoryAudit: 'mvn deploy:deploy-file succeeded"); - - // we also want to include this new artifact in the download - // test (steps 3 and 4) - artifacts.add(new Artifact(groupId, artifactId, version, "txt")); - } - } - - /* - * 3) create 'pom.xml' file in temporary directory - */ - artifacts.add(new Artifact("org.apache.maven/maven-embedder/3.2.2")); - - StringBuilder sb = new StringBuilder(); - sb.append - ("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" - + " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" - + "\n" - + " <modelVersion>4.0.0</modelVersion>\n" - + " <groupId>empty</groupId>\n" - + " <artifactId>empty</artifactId>\n" - + " <version>1.0-SNAPSHOT</version>\n" - + " <packaging>pom</packaging>\n" - + "\n" - + " <build>\n" - + " <plugins>\n" - + " <plugin>\n" - + " <groupId>org.apache.maven.plugins</groupId>\n" - + " <artifactId>maven-dependency-plugin</artifactId>\n" - + " <version>2.10</version>\n" - + " <executions>\n" - + " <execution>\n" - + " <id>copy</id>\n" - + " <goals>\n" - + " <goal>copy</goal>\n" - + " </goals>\n" - + " <configuration>\n" - + " <localRepositoryDirectory>") - .append(repo) - .append("</localRepositoryDirectory>\n") - .append(" <artifactItems>\n"); - for (Artifact artifact : artifacts) - { - // each artifact results in an 'artifactItem' element - sb.append - (" <artifactItem>\n" - + " <groupId>") - .append(artifact.groupId) - .append - ("</groupId>\n" - + " <artifactId>") - .append(artifact.artifactId) - .append - ("</artifactId>\n" - + " <version>") - .append(artifact.version) - .append - ("</version>\n" - + " <type>") - .append(artifact.type) - .append - ("</type>\n" - + " </artifactItem>\n"); - } - sb.append - (" </artifactItems>\n" - + " </configuration>\n" - + " </execution>\n" - + " </executions>\n" - + " </plugin>\n" - + " </plugins>\n" - + " </build>\n" - + "</project>\n"); - FileOutputStream fos = new FileOutputStream(pom.toFile()); - try - { - fos.write(sb.toString().getBytes()); - } - finally - { - fos.close(); - } - - /* - * 4) Invoke external 'mvn' process to do the downloads - */ - - // output file = ${dir}/out (this supports step '4a') - File output = dir.resolve("out").toFile(); - - // invoke process, and wait for response - int rval = runProcess - (timeoutInSeconds, dir.toFile(), output, "mvn", "compile"); - logger.info("RepositoryAudit: 'mvn' return value = {}", rval); - if (rval != 0) - { - logger.error - ("RepositoryAudit: 'mvn compile' invocation failed"); - if (!ignoreErrors) - { - response.append("'mvn compile' invocation failed\n"); - setResponse(response.toString()); - } - } - - /* - * 4a) Check attempted and successful downloads from output file - * Note: at present, this step just generates log messages, - * but doesn't do any verification. - */ - if (rval == 0 && output != null) - { - // place output in 'fileContents' (replacing the Return characters - // with Newline) - byte[] outputData = new byte[(int)output.length()]; - String fileContents; - try (FileInputStream fis = new FileInputStream(output)) { - // - // Ideally this should be in a loop or even better use - // Java 8 nio functionality. - // - int bytesRead = fis.read(outputData); - logger.info("fileContents read {} bytes", bytesRead); - fileContents = new String(outputData).replace('\r','\n'); - } - - // generate log messages from 'Downloading' and 'Downloaded' - // messages within the 'mvn' output - int index = 0; - while ((index = fileContents.indexOf("\nDown", index)) > 0) - { - index += 5; - if (fileContents.regionMatches(index, "loading: ", 0, 9)) - { - index += 9; - int endIndex = fileContents.indexOf('\n', index); - logger.info - ("RepositoryAudit: Attempted download: '{}'", fileContents.substring(index, endIndex)); - index = endIndex; - } - else if (fileContents.regionMatches(index, "loaded: ", 0, 8)) - { - index += 8; - int endIndex = fileContents.indexOf(' ', index); - logger.info - ("RepositoryAudit: Successful download: '{}'",fileContents.substring(index, endIndex)); - index = endIndex; - } - } - } - - /* - * 5) Check the contents of the directory to make sure the downloads - * were successful - */ - for (Artifact artifact : artifacts) - { - if (repo.resolve(artifact.groupId.replace('.','/')) - .resolve(artifact.artifactId) - .resolve(artifact.version) - .resolve(artifact.artifactId + "-" + artifact.version + "." - + artifact.type).toFile().exists()) - { - // artifact exists, as expected - logger.info("RepositoryAudit: {} : exists", artifact.toString()); - } - else - { - // Audit ERROR: artifact download failed for some reason - logger.error("RepositoryAudit: {}: does not exist", artifact.toString()); - if (!ignoreErrors) - { - response.append("Failed to download artifact: ") - .append(artifact).append('\n'); - setResponse(response.toString()); - } - } - } - - /* - * 6) Use 'curl' to delete the uploaded test file - * (only if repository information is specified) - */ - if (upload) - { - if (runProcess - (timeoutInSeconds, dir.toFile(), null, - "curl", - "--request", "DELETE", - "--user", repositoryUsername + ":" + repositoryPassword, - repositoryUrl + "/" + groupId.replace('.', '/') + "/" + - artifactId + "/" + version) - != 0) - { - logger.error - ("RepositoryAudit: delete of uploaded artifact failed"); - if (!ignoreErrors) - { - response.append("delete of uploaded artifact failed\n"); - setResponse(response.toString()); - } - } - else - { - logger.info - ("RepositoryAudit: delete of uploaded artifact succeeded"); - artifacts.add(new Artifact(groupId, artifactId, version, "txt")); - } - } - - /* - * 7) Remove the temporary directory - */ - Files.walkFileTree(dir, new RecursivelyDeleteDirectory()); - } - - /** - * Run a process, and wait for the response - * - * @param timeoutInSeconds the number of seconds to wait for the - * process to terminate - * @param directory the execution directory of the process - * (null = current directory) - * @param stdout the file to contain the standard output - * (null = discard standard output) - * @param command command and arguments - * @return the return value of the process - * @throws IOException, InterruptedException - */ - static int runProcess(long timeoutInSeconds, - File directory, File stdout, String... command) - throws IOException, InterruptedException - { - ProcessBuilder pb = new ProcessBuilder(command); - if (directory != null) - { - pb.directory(directory); - } - if (stdout != null) - { - pb.redirectOutput(stdout); - } - - Process process = pb.start(); - if (process.waitFor(timeoutInSeconds, TimeUnit.SECONDS)) - { - // process terminated before the timeout - return process.exitValue(); - } - - // process timed out -- kill it, and return -1 - process.destroyForcibly(); - return -1; - } - - /** - * This class is used to recursively delete a directory and all of its - * contents. - */ - private final class RecursivelyDeleteDirectory extends SimpleFileVisitor<Path> { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) - { - file.toFile().delete(); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path file, IOException e) - throws IOException - { - if (e == null) - { - file.toFile().delete(); - return FileVisitResult.CONTINUE; - } - else - { - throw e; - } - } - } - -/* ============================================================ */ - - /** - * An instance of this class exists for each artifact that we are trying - * to download. - */ - static class Artifact - { - String groupId; - String artifactId; - String version; - String type; - - /** - * Constructor - populate the 'Artifact' instance - * - * @param groupId groupId of artifact - * @param artifactId artifactId of artifact - * @param version version of artifact - * @param type type of the artifact (e.g. "jar") - */ - Artifact(String groupId, String artifactId, String version, String type) - { - this.groupId = groupId; - this.artifactId = artifactId; - this.version = version; - this.type = type; - } - - /** - * Constructor - populate an 'Artifact' instance - * - * @param artifact a string of the form: - * "<groupId>/<artifactId>/<version>[/<type>]" - * @throws IllegalArgumentException if 'artifact' has the incorrect format - */ - Artifact(String artifact) - { - String[] segments = artifact.split("/"); - if (segments.length != 4 && segments.length != 3) - { - throw new IllegalArgumentException("groupId/artifactId/version/type"); - } - groupId = segments[0]; - artifactId = segments[1]; - version = segments[2]; - type = segments.length == 4 ? segments[3] : "jar"; - } - - /** - * @return the artifact id in the form: - * "<groupId>/<artifactId>/<version>/<type>" - */ - @Override - public String toString() - { - return groupId + "/" + artifactId + "/" + version + "/" + type; - } - } +public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase { + private static final long DEFAULT_TIMEOUT = 60; //timeout in 60 seconds + + // get an instance of logger + private static Logger logger = LoggerFactory.getLogger(RepositoryAudit.class); + // single global instance of this audit object + private static RepositoryAudit instance = new RepositoryAudit(); + + /** + * Constructor - set the name to 'Repository'. + */ + private RepositoryAudit() { + super("Repository"); + } + + /** + * Get the integrity monitor instance. + * + * @return the single 'RepositoryAudit' instance + */ + public static DroolsPDPIntegrityMonitor.AuditBase getInstance() { + return instance; + } + + /** + * Invoke the audit. + * + * @param properties properties to be passed to the audit + */ + @Override + public void invoke(Properties properties) + throws IOException, InterruptedException { + if (logger.isDebugEnabled()) { + logger.debug("Running 'RepositoryAudit.invoke'"); + } + + boolean isActive = true; + // ignore errors by default + boolean ignoreErrors = true; + String repoAuditIsActive = StateManagementProperties.getProperty("repository.audit.is.active"); + String repoAuditIgnoreErrors = + StateManagementProperties.getProperty("repository.audit.ignore.errors"); + logger.debug("RepositoryAudit.invoke: repoAuditIsActive = {}" + + ", repoAuditIgnoreErrors = {}",repoAuditIsActive, repoAuditIgnoreErrors); + + if (repoAuditIsActive != null) { + try { + isActive = Boolean.parseBoolean(repoAuditIsActive.trim()); + } catch (NumberFormatException e) { + logger.warn("RepositoryAudit.invoke: Ignoring invalid property: repository.audit.is.active = {}", + repoAuditIsActive); + } + } + + if (!isActive) { + logger.info("RepositoryAudit.invoke: exiting because isActive = {}", isActive); + return; + } + + if (repoAuditIgnoreErrors != null) { + try { + ignoreErrors = Boolean.parseBoolean(repoAuditIgnoreErrors.trim()); + } catch (NumberFormatException e) { + ignoreErrors = true; + logger.warn("RepositoryAudit.invoke: Ignoring invalid property: repository.audit.ignore.errors = {}", + repoAuditIgnoreErrors); + } + } else { + ignoreErrors = true; + } + + // Fetch repository information from 'IntegrityMonitorProperties' + String repositoryId = + StateManagementProperties.getProperty("repository.audit.id"); + String repositoryUrl = + StateManagementProperties.getProperty("repository.audit.url"); + String repositoryUsername = + StateManagementProperties.getProperty("repository.audit.username"); + String repositoryPassword = + StateManagementProperties.getProperty("repository.audit.password"); + boolean upload = + repositoryId != null && repositoryUrl != null + && repositoryUsername != null && repositoryPassword != null; + + // used to incrementally construct response as problems occur + // (empty = no problems) + StringBuilder response = new StringBuilder(); + + long timeoutInSeconds = DEFAULT_TIMEOUT; + String timeoutString = + StateManagementProperties.getProperty("repository.audit.timeout"); + if (timeoutString != null && !timeoutString.isEmpty()) { + try { + timeoutInSeconds = Long.valueOf(timeoutString); + } catch (NumberFormatException e) { + logger.error("RepositoryAudit: Invalid 'repository.audit.timeout' value: '{}'", + timeoutString, e); + if (!ignoreErrors) { + response.append("Invalid 'repository.audit.timeout' value: '") + .append(timeoutString).append("'\n"); + setResponse(response.toString()); + } + } + } + + // artifacts to be downloaded + LinkedList<Artifact> artifacts = new LinkedList<>(); + + /* + * 1) create temporary directory + */ + Path dir = Files.createTempDirectory("auditRepo"); + logger.info("RepositoryAudit: temporary directory = {}", dir); + + // nested 'pom.xml' file and 'repo' directory + final Path pom = dir.resolve("pom.xml"); + final Path repo = dir.resolve("repo"); + + /* + * 2) Create test file, and upload to repository + * (only if repository information is specified) + */ + String groupId = null; + String artifactId = null; + String version = null; + if (upload) { + groupId = "org.onap.policy.audit"; + artifactId = "repository-audit"; + version = "0." + System.currentTimeMillis(); + + if (repositoryUrl.toLowerCase().contains("snapshot")) { + // use SNAPSHOT version + version += "-SNAPSHOT"; + } + + // create text file to write + try (FileOutputStream fos = + new FileOutputStream(dir.resolve("repository-audit.txt").toFile())) { + fos.write(version.getBytes()); + } + + // try to install file in repository + if (runProcess(timeoutInSeconds, dir.toFile(), null, + "mvn", "deploy:deploy-file", + "-DrepositoryId=" + repositoryId, + "-Durl=" + repositoryUrl, + "-Dfile=repository-audit.txt", + "-DgroupId=" + groupId, + "-DartifactId=" + artifactId, + "-Dversion=" + version, + "-Dpackaging=txt", + "-DgeneratePom=false") != 0) { + logger.error("RepositoryAudit: 'mvn deploy:deploy-file' failed"); + if (!ignoreErrors) { + response.append("'mvn deploy:deploy-file' failed\n"); + setResponse(response.toString()); + } + } + else { + logger.info("RepositoryAudit: 'mvn deploy:deploy-file succeeded"); + + // we also want to include this new artifact in the download + // test (steps 3 and 4) + artifacts.add(new Artifact(groupId, artifactId, version, "txt")); + } + } + + /* + * 3) create 'pom.xml' file in temporary directory + */ + artifacts.add(new Artifact("org.apache.maven/maven-embedder/3.2.2")); + + StringBuilder sb = new StringBuilder(); + sb.append("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + + " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" + + "\n" + + " <modelVersion>4.0.0</modelVersion>\n" + + " <groupId>empty</groupId>\n" + + " <artifactId>empty</artifactId>\n" + + " <version>1.0-SNAPSHOT</version>\n" + + " <packaging>pom</packaging>\n" + + "\n" + + " <build>\n" + + " <plugins>\n" + + " <plugin>\n" + + " <groupId>org.apache.maven.plugins</groupId>\n" + + " <artifactId>maven-dependency-plugin</artifactId>\n" + + " <version>2.10</version>\n" + + " <executions>\n" + + " <execution>\n" + + " <id>copy</id>\n" + + " <goals>\n" + + " <goal>copy</goal>\n" + + " </goals>\n" + + " <configuration>\n" + + " <localRepositoryDirectory>") + .append(repo) + .append("</localRepositoryDirectory>\n") + .append(" <artifactItems>\n"); + + for (Artifact artifact : artifacts) { + // each artifact results in an 'artifactItem' element + sb.append(" <artifactItem>\n" + + " <groupId>") + .append(artifact.groupId) + .append("</groupId>\n" + + " <artifactId>") + .append(artifact.artifactId) + .append("</artifactId>\n" + + " <version>") + .append(artifact.version) + .append("</version>\n" + + " <type>") + .append(artifact.type) + .append("</type>\n" + + " </artifactItem>\n"); + } + sb.append(" </artifactItems>\n" + + " </configuration>\n" + + " </execution>\n" + + " </executions>\n" + + " </plugin>\n" + + " </plugins>\n" + + " </build>\n" + + "</project>\n"); + + try (FileOutputStream fos = new FileOutputStream(pom.toFile())) { + fos.write(sb.toString().getBytes()); + } + + /* + * 4) Invoke external 'mvn' process to do the downloads + */ + + // output file = ${dir}/out (this supports step '4a') + File output = dir.resolve("out").toFile(); + + // invoke process, and wait for response + int rval = runProcess(timeoutInSeconds, dir.toFile(), output, "mvn", "compile"); + logger.info("RepositoryAudit: 'mvn' return value = {}", rval); + if (rval != 0) { + logger.error("RepositoryAudit: 'mvn compile' invocation failed"); + if (!ignoreErrors) { + response.append("'mvn compile' invocation failed\n"); + setResponse(response.toString()); + } + } + + /* + * 4a) Check attempted and successful downloads from output file + * Note: at present, this step just generates log messages, + * but doesn't do any verification. + */ + if (rval == 0 && output != null) { + // place output in 'fileContents' (replacing the Return characters + // with Newline) + byte[] outputData = new byte[(int)output.length()]; + String fileContents; + try (FileInputStream fis = new FileInputStream(output)) { + // + // Ideally this should be in a loop or even better use + // Java 8 nio functionality. + // + int bytesRead = fis.read(outputData); + logger.info("fileContents read {} bytes", bytesRead); + fileContents = new String(outputData).replace('\r','\n'); + } + + // generate log messages from 'Downloading' and 'Downloaded' + // messages within the 'mvn' output + int index = 0; + while ((index = fileContents.indexOf("\nDown", index)) > 0) { + index += 5; + if (fileContents.regionMatches(index, "loading: ", 0, 9)) { + index += 9; + int endIndex = fileContents.indexOf('\n', index); + logger.info("RepositoryAudit: Attempted download: '{}'", + fileContents.substring(index, endIndex)); + index = endIndex; + } else if (fileContents.regionMatches(index, "loaded: ", 0, 8)) { + index += 8; + int endIndex = fileContents.indexOf(' ', index); + logger.info("RepositoryAudit: Successful download: '{}'",fileContents.substring(index, endIndex)); + index = endIndex; + } + } + } + + /* + * 5) Check the contents of the directory to make sure the downloads + * were successful + */ + for (Artifact artifact : artifacts) { + if (repo.resolve(artifact.groupId.replace('.','/')) + .resolve(artifact.artifactId) + .resolve(artifact.version) + .resolve(artifact.artifactId + "-" + artifact.version + "." + + artifact.type).toFile().exists()) { + // artifact exists, as expected + logger.info("RepositoryAudit: {} : exists", artifact.toString()); + } else { + // Audit ERROR: artifact download failed for some reason + logger.error("RepositoryAudit: {}: does not exist", artifact.toString()); + if (!ignoreErrors) { + response.append("Failed to download artifact: ") + .append(artifact).append('\n'); + setResponse(response.toString()); + } + } + } + + /* + * 6) Use 'curl' to delete the uploaded test file + * (only if repository information is specified) + */ + if (upload) { + if (runProcess(timeoutInSeconds, dir.toFile(), null, + "curl", + "--request", "DELETE", + "--user", repositoryUsername + ":" + repositoryPassword, + repositoryUrl + "/" + groupId.replace('.', '/') + "/" + + artifactId + "/" + version) + != 0) { + logger.error("RepositoryAudit: delete of uploaded artifact failed"); + if (!ignoreErrors) { + response.append("delete of uploaded artifact failed\n"); + setResponse(response.toString()); + } + } else { + logger.info("RepositoryAudit: delete of uploaded artifact succeeded"); + artifacts.add(new Artifact(groupId, artifactId, version, "txt")); + } + } + + /* + * 7) Remove the temporary directory + */ + Files.walkFileTree(dir, new RecursivelyDeleteDirectory()); + } + + /** + * Run a process, and wait for the response. + * + * @param timeoutInSeconds the number of seconds to wait for the process to terminate + * @param directory the execution directory of the process (null = current directory) + * @param stdout the file to contain the standard output (null = discard standard output) + * @param command command and arguments + * @return the return value of the process + * @throws IOException InterruptedException + */ + static int runProcess(long timeoutInSeconds, + File directory, File stdout, String... command) + throws IOException, InterruptedException { + ProcessBuilder pb = new ProcessBuilder(command); + if (directory != null) { + pb.directory(directory); + } + if (stdout != null) { + pb.redirectOutput(stdout); + } + + Process process = pb.start(); + if (process.waitFor(timeoutInSeconds, TimeUnit.SECONDS)) { + // process terminated before the timeout + return process.exitValue(); + } + + // process timed out -- kill it, and return -1 + process.destroyForcibly(); + return -1; + } + + /** + * This class is used to recursively delete a directory and all of its + * contents. + */ + private final class RecursivelyDeleteDirectory extends SimpleFileVisitor<Path> { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + file.toFile().delete(); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path file, IOException ex) + throws IOException { + if (ex == null) { + file.toFile().delete(); + return FileVisitResult.CONTINUE; + } else { + throw ex; + } + } + } + + /* ============================================================ */ + + /** + * An instance of this class exists for each artifact that we are trying + * to download. + */ + static class Artifact { + String groupId; + String artifactId; + String version; + String type; + + /** + * Constructor - populate the 'Artifact' instance. + * + * @param groupId groupId of artifact + * @param artifactId artifactId of artifact + * @param version version of artifact + * @param type type of the artifact (e.g. "jar") + */ + Artifact(String groupId, String artifactId, String version, String type) { + this.groupId = groupId; + this.artifactId = artifactId; + this.version = version; + this.type = type; + } + + /** + * Constructor - populate an 'Artifact' instance. + * + * @param artifact a string of the form: + * {@code"<groupId>/<artifactId>/<version>[/<type>]"} + * @throws IllegalArgumentException if 'artifact' has the incorrect format + */ + Artifact(String artifact) { + String[] segments = artifact.split("/"); + if (segments.length != 4 && segments.length != 3) { + throw new IllegalArgumentException("groupId/artifactId/version/type"); + } + groupId = segments[0]; + artifactId = segments[1]; + version = segments[2]; + type = segments.length == 4 ? segments[3] : "jar"; + } + + /** + * Returns string representation. + * + * @return the artifact id in the form: {@code"<groupId>/<artifactId>/<version>/<type>"} + */ + @Override + public String toString() { + return groupId + "/" + artifactId + "/" + version + "/" + type; + } + } } diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java index cb1700e4..d7f9d108 100644 --- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java +++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-state-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,227 +38,218 @@ import org.slf4j.LoggerFactory; * active/standby state management and IntegrityMonitor. For now, they are * all treated as a single feature, but it would be nice to separate them. * - * The bulk of the code here was once in other classes, such as + * <p>The bulk of the code here was once in other classes, such as * 'PolicyContainer' and 'Main'. It was moved here as part of making this * a separate optional feature. */ public class StateManagementFeature implements StateManagementFeatureAPI, - PolicySessionFeatureAPI, PolicyEngineFeatureAPI -{ - // get an instance of logger - private static final Logger logger = - LoggerFactory.getLogger(StateManagementFeature.class); - - private DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = null; - private StateManagement stateManagement = null; - - /**************************/ - /* 'FeatureAPI' interface */ - /**************************/ - - public StateManagementFeature(){ - logger.debug("StateManagementFeature() constructor"); - } - - @Override - public void globalInit(String[] args, String configDir) - { - // Initialization code associated with 'PolicyContainer' - logger.debug("StateManagementFeature.globalInit({}) entry", configDir); - - try - { - droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.init(configDir); - } - catch (Exception e) - { - logger.debug("DroolsPDPIntegrityMonitor initialization exception: ", e); - logger.error("DroolsPDPIntegrityMonitor.init()", e); - } - - initializeProperties(configDir); - - //At this point the DroolsPDPIntegrityMonitor instance must exist. Let's check it. - try { - droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance(); - stateManagement = droolsPdpIntegrityMonitor.getStateManager(); - - if (stateManagement == null) { - logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!"); - } - else { - logger.debug("StateManagementFeature.globalInit(): " - + "stateManagement.getAdminState(): {}", stateManagement.getAdminState()); - } - } catch (Exception e1) { - logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor" - + " initialization failed with exception:", e1); - logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed " - + "to get DroolsPDPIntegrityMonitor instance:", e1); - } - } - - /** - * {@inheritDoc} - */ - @Override - public void addObserver(Observer stateChangeObserver) { - logger.debug("StateManagementFeature.addObserver() entry\n" - + "StateManagementFeature.addObserver(): " - + "stateManagement.getAdminState(): {}", stateManagement.getAdminState()); - - stateManagement.addObserver(stateChangeObserver); - - logger.debug("StateManagementFeature.addObserver() exit"); - } - - /** - * {@inheritDoc} - */ - @Override - public String getAdminState() { - return stateManagement.getAdminState(); - } - - /** - * {@inheritDoc} - */ - @Override - public String getOpState() { - return stateManagement.getOpState(); - } - - /** - * {@inheritDoc} - */ - @Override - public String getAvailStatus() { - return stateManagement.getAvailStatus(); - } - - /** - * {@inheritDoc} - */ - @Override - public String getStandbyStatus() { - return stateManagement.getStandbyStatus(); - } - - /** - * {@inheritDoc} - */ - @Override - public String getStandbyStatus(String resourceName) { - return stateManagement.getStandbyStatus(resourceName); - } - - /** - * {@inheritDoc} - */ - @Override - public void disableFailed(String resourceName) throws Exception { - stateManagement.disableFailed(resourceName); - - } - - /** - * {@inheritDoc} - */ - @Override - public void disableFailed() throws Exception { - stateManagement.disableFailed(); - } - - /** - * {@inheritDoc} - */ - @Override - public void promote() throws Exception { - stateManagement.promote(); - } - - /** - * {@inheritDoc} - */ - @Override - public void demote() throws Exception { - stateManagement.demote(); - } - - /** - * {@inheritDoc} - */ - @Override - public String getResourceName() { - return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME); - } - - /** - * {@inheritDoc} - * @return - */ - @Override - public boolean lock(){ - try{ - stateManagement.lock(); - }catch(Exception e){ - logger.error("StateManagementFeature.lock() failed with exception: {}", e); - return false; - } - return true; - } - - /** - * {@inheritDoc} - * @throws Exception - */ - @Override - public boolean unlock(){ - try{ - stateManagement.unlock(); - }catch(Exception e){ - logger.error("StateManagementFeature.unlock() failed with exception: {}", e); - return false; - } - return true; - } - - /** - * {@inheritDoc} - * @throws Exception - */ - @Override - public boolean isLocked(){ - return StateManagement.LOCKED.equals(stateManagement.getAdminState()); - } - - @Override - public int getSequenceNumber() { - return SEQ_NUM; - } - - /** - * Read in the properties and initialize the StateManagementProperties. - */ - private static void initializeProperties(String configDir) - { - //Get the state management properties - try { - Properties pIm = - PropertyUtil.getProperties(configDir + "/feature-state-management.properties"); - StateManagementProperties.initProperties(pIm); - logger.info("initializeProperties: resourceName= {}", StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME)); - } catch (IOException e1) { - logger.error("initializeProperties", e1); - } - } - - @Override - public void allSeemsWell(String key, Boolean asw, String msg) - throws AllSeemsWellException { - - droolsPdpIntegrityMonitor.allSeemsWell(key, asw, msg); - - } + PolicySessionFeatureAPI, PolicyEngineFeatureAPI { + // get an instance of logger + private static final Logger logger = + LoggerFactory.getLogger(StateManagementFeature.class); + + private DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = null; + private StateManagement stateManagement = null; + + public StateManagementFeature() { + logger.debug("StateManagementFeature() constructor"); + } + + @Override + public void globalInit(String[] args, String configDir) { + // Initialization code associated with 'PolicyContainer' + logger.debug("StateManagementFeature.globalInit({}) entry", configDir); + + try { + droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.init(configDir); + } catch (Exception e) { + logger.debug("DroolsPDPIntegrityMonitor initialization exception: ", e); + logger.error("DroolsPDPIntegrityMonitor.init()", e); + } + + initializeProperties(configDir); + + //At this point the DroolsPDPIntegrityMonitor instance must exist. Let's check it. + try { + droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance(); + stateManagement = droolsPdpIntegrityMonitor.getStateManager(); + + if (stateManagement == null) { + logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!"); + } + else { + logger.debug("StateManagementFeature.globalInit(): " + + "stateManagement.getAdminState(): {}", stateManagement.getAdminState()); + } + } catch (Exception e1) { + logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor" + + " initialization failed with exception:", e1); + logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed " + + "to get DroolsPDPIntegrityMonitor instance:", e1); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void addObserver(Observer stateChangeObserver) { + logger.debug("StateManagementFeature.addObserver() entry\n" + + "StateManagementFeature.addObserver(): " + + "stateManagement.getAdminState(): {}", stateManagement.getAdminState()); + + stateManagement.addObserver(stateChangeObserver); + + logger.debug("StateManagementFeature.addObserver() exit"); + } + + /** + * {@inheritDoc} + */ + @Override + public String getAdminState() { + return stateManagement.getAdminState(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getOpState() { + return stateManagement.getOpState(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getAvailStatus() { + return stateManagement.getAvailStatus(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getStandbyStatus() { + return stateManagement.getStandbyStatus(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getStandbyStatus(String resourceName) { + return stateManagement.getStandbyStatus(resourceName); + } + + /** + * {@inheritDoc} + */ + @Override + public void disableFailed(String resourceName) throws Exception { + stateManagement.disableFailed(resourceName); + + } + + /** + * {@inheritDoc} + */ + @Override + public void disableFailed() throws Exception { + stateManagement.disableFailed(); + } + + /** + * {@inheritDoc} + */ + @Override + public void promote() throws Exception { + stateManagement.promote(); + } + + /** + * {@inheritDoc} + */ + @Override + public void demote() throws Exception { + stateManagement.demote(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getResourceName() { + return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME); + } + + /** + * {@inheritDoc} + * @return + */ + @Override + public boolean lock() { + try { + stateManagement.lock(); + } catch (Exception e) { + logger.error("StateManagementFeature.lock() failed with exception: {}", e); + return false; + } + return true; + } + + /** + * {@inheritDoc} + * @throws Exception exception + */ + @Override + public boolean unlock() { + try { + stateManagement.unlock(); + } catch (Exception e) { + logger.error("StateManagementFeature.unlock() failed with exception: {}", e); + return false; + } + return true; + } + + /** + * {@inheritDoc} + * @throws Exception exception + */ + @Override + public boolean isLocked() { + return StateManagement.LOCKED.equals(stateManagement.getAdminState()); + } + + @Override + public int getSequenceNumber() { + return SEQ_NUM; + } + + /** + * Read in the properties and initialize the StateManagementProperties. + */ + private static void initializeProperties(String configDir) { + //Get the state management properties + try { + Properties props = + PropertyUtil.getProperties(configDir + "/feature-state-management.properties"); + StateManagementProperties.initProperties(props); + logger.info("initializeProperties: resourceName= {}", + StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME)); + } catch (IOException e1) { + logger.error("initializeProperties", e1); + } + } + + @Override + public void allSeemsWell(String key, Boolean asw, String msg) + throws AllSeemsWellException { + + droolsPdpIntegrityMonitor.allSeemsWell(key, asw, msg); + + } } diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementProperties.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementProperties.java index 38356226..471745d4 100644 --- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementProperties.java +++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementProperties.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-state-management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,15 +65,16 @@ public class StateManagementProperties { private StateManagementProperties() {} - /* - * Initialize the parameter values from the feature-state-management.properties file values + /** + * Initialize the parameter values from the feature-state-management.properties file values. * - * This is designed so that the Properties object is obtained from the + * <p>This is designed so that the Properties object is obtained from the * feature-state-management.properties file and then is passed to this method to initialize the * value of the parameters. This allows the flexibility of JUnit tests using * getProperties(filename) to get the properties while runtime methods can use * getPropertiesFromClassPath(filename). * + * @param prop properties */ public static void initProperties(Properties prop) { logger.info("StateManagementProperties.initProperties(Properties): entry"); diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementPropertiesException.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementPropertiesException.java index 59802ebd..a88bcf25 100644 --- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementPropertiesException.java +++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementPropertiesException.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Feature-State-Management * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,22 +20,23 @@ package org.onap.policy.drools.statemanagement; -public class StateManagementPropertiesException extends Exception{ - private static final long serialVersionUID = 1L; - - public StateManagementPropertiesException() { - super(); - } - - public StateManagementPropertiesException(String message) { - super(message); - } +public class StateManagementPropertiesException extends Exception { + private static final long serialVersionUID = 1L; - public StateManagementPropertiesException(Throwable cause) { - super(cause); - } - public StateManagementPropertiesException(String message, Throwable cause) { - super(message, cause); - } + public StateManagementPropertiesException() { + super(); + } + + public StateManagementPropertiesException(String message) { + super(message); + } + + public StateManagementPropertiesException(Throwable cause) { + super(cause); + } + + public StateManagementPropertiesException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/feature-state-management/src/test/java/org/onap/policy/drools/statemanagement/test/Audit.java b/feature-state-management/src/test/java/org/onap/policy/drools/statemanagement/test/Audit.java index b33171bb..943293c9 100644 --- a/feature-state-management/src/test/java/org/onap/policy/drools/statemanagement/test/Audit.java +++ b/feature-state-management/src/test/java/org/onap/policy/drools/statemanagement/test/Audit.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,16 +28,16 @@ import javax.persistence.Id; import javax.persistence.Table; @Entity -@Table(name="Audit") +@Table(name = "Audit") public class Audit implements Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - @Id - @Column(name="name", length=64, unique=true) - private String name; - - public Audit() { - //default constructor - } + @Id + @Column(name = "name", length = 64, unique = true) + private String name; + + public Audit() { + //default constructor + } } diff --git a/feature-state-management/src/test/java/org/onap/policy/drools/statemanagement/test/StateManagementTest.java b/feature-state-management/src/test/java/org/onap/policy/drools/statemanagement/test/StateManagementTest.java index b364ef83..5c69284f 100644 --- a/feature-state-management/src/test/java/org/onap/policy/drools/statemanagement/test/StateManagementTest.java +++ b/feature-state-management/src/test/java/org/onap/policy/drools/statemanagement/test/StateManagementTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-persistence * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,215 +49,216 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class StateManagementTest { - - // get an instance of logger - private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class); - - StateManagementFeatureAPI stateManagementFeature; - - /* - * All you need to do here is create an instance of StateManagementFeature class. Then, - * check it initial state and the state after diableFailed() and promote() - */ - - @BeforeClass - public static void setUpClass() throws Exception { - - logger.info("setUpClass: Entering"); - - String userDir = System.getProperty("user.dir"); - logger.debug("setUpClass: userDir=" + userDir); - System.setProperty("com.sun.management.jmxremote.port", "9980"); - System.setProperty("com.sun.management.jmxremote.authenticate","false"); - - initializeDb(); - - logger.info("setUpClass: Exiting"); - } - - @AfterClass - public static void tearDownClass() throws Exception { - - } - - @Before - public void setUp() throws Exception { - - } - - @After - public void tearDown() throws Exception { - - } - - /* - * Verifies that StateManagementFeature starts and runs successfully. - */ - - //@Ignore - @Test - public void testStateManagementOperation() throws Exception { - - logger.debug("\n\ntestStateManagementOperation: Entering\n\n"); - - logger.debug("testStateManagementOperation: Reading StateManagementProperties"); - - String configDir = "src/test/resources"; - - DbAudit.setIsJunit(true); - - Properties fsmProperties = new Properties(); - fsmProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); - String thisPdpId = fsmProperties - .getProperty(StateManagementProperties.NODE_NAME); - - StateManagementFeatureAPI stateManagementFeature = null; - for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) - { - ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); - stateManagementFeature = feature; - logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): " + stateManagementFeature.getResourceName()); - break; - } - if(stateManagementFeature == null){ - String msg = "testStateManagementOperation failed to initialize. " - + "Unable to get instance of StateManagementFeatureAPI " - + "with resourceID: " + thisPdpId; - logger.error(msg); - logger.debug(msg); - } - - String admin = stateManagementFeature.getAdminState(); - String oper = stateManagementFeature.getOpState(); - String avail = stateManagementFeature.getAvailStatus(); - String standby = stateManagementFeature.getStandbyStatus(); - - logger.debug("admin = {}", admin); - logger.debug("oper = {}", oper); - logger.debug("avail = {}", avail); - logger.debug("standby = {}", standby); - - assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED)); - assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED)); - - try{ - stateManagementFeature.disableFailed(); - }catch(Exception e){ - logger.error(e.getMessage()); - assertTrue(e.getMessage(), false); - } - - admin = stateManagementFeature.getAdminState(); - oper = stateManagementFeature.getOpState(); - avail = stateManagementFeature.getAvailStatus(); - standby = stateManagementFeature.getStandbyStatus(); - - logger.debug("after disableFailed()"); - logger.debug("admin = {}", admin); - logger.debug("oper = {}", oper); - logger.debug("avail = {}", avail); - logger.debug("standby = {}", standby); - - assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED)); - assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED)); - - - try{ - stateManagementFeature.promote(); - }catch(Exception e){ - logger.debug(e.getMessage()); - } - - admin = stateManagementFeature.getAdminState(); - oper = stateManagementFeature.getOpState(); - avail = stateManagementFeature.getAvailStatus(); - standby = stateManagementFeature.getStandbyStatus(); - - logger.debug("after promote()"); - logger.debug("admin = {}", admin); - logger.debug("oper = {}", oper); - logger.debug("avail = {}", avail); - logger.debug("standby = {}", standby); - - assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY)); - - /**************Repository Audit Test**************/ - logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n"); - try{ - StateManagementProperties.initProperties(fsmProperties); - RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance(); - repositoryAudit.invoke(fsmProperties); - - //Should not throw an IOException in Linux Foundation env - assertTrue(true); - }catch(IOException e){ - //Note: this catch is here because in a local environment mvn will not run in - //in the temp directory - logger.debug("testSubsytemTest RepositoryAudit IOException", e); - }catch(InterruptedException e){ - assertTrue(false); - logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e); - } - - /*****************Db Audit Test***************/ - logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n"); - - try{ - DbAudit dbAudit = (DbAudit) DbAudit.getInstance(); - dbAudit.invoke(fsmProperties); - - assertTrue(true); - }catch(Exception e){ - assertTrue(false); - logger.debug("testSubsytemTest DbAudit exception", e); - } - - /*************IntegrityMonitorRestManager Test*************/ - logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n"); - IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager(); - - Response response = integrityMonitorRestManager.test(); - logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString()); - - assertTrue(response.toString().contains("status=500")); - - //All done - logger.debug("\n\ntestStateManagementOperation: Exiting\n\n"); - } - + + // get an instance of logger + private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class); + + StateManagementFeatureAPI stateManagementFeature; + + /** + * Setup the class. + * All you need to do here is create an instance of StateManagementFeature class. Then, + * check it initial state and the state after diableFailed() and promote() + * + * @throws Exception exception + */ + @BeforeClass + public static void setUpClass() throws Exception { + + logger.info("setUpClass: Entering"); + + String userDir = System.getProperty("user.dir"); + logger.debug("setUpClass: userDir=" + userDir); + System.setProperty("com.sun.management.jmxremote.port", "9980"); + System.setProperty("com.sun.management.jmxremote.authenticate","false"); + + initializeDb(); + + logger.info("setUpClass: Exiting"); + } + + @AfterClass + public static void tearDownClass() throws Exception { + + } + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + /* + * Verifies that StateManagementFeature starts and runs successfully. + */ + + //@Ignore + @Test + public void testStateManagementOperation() throws Exception { + + logger.debug("\n\ntestStateManagementOperation: Entering\n\n"); + + logger.debug("testStateManagementOperation: Reading StateManagementProperties"); + + String configDir = "src/test/resources"; + + DbAudit.setIsJunit(true); + + Properties fsmProperties = new Properties(); + fsmProperties.load(new FileInputStream(new File( + configDir + "/feature-state-management.properties"))); + String thisPdpId = fsmProperties + .getProperty(StateManagementProperties.NODE_NAME); + + StateManagementFeatureAPI stateManagementFeature = null; + for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) { + ((PolicySessionFeatureAPI) feature).globalInit(null, configDir); + stateManagementFeature = feature; + logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): " + + stateManagementFeature.getResourceName()); + break; + } + if (stateManagementFeature == null) { + String msg = "testStateManagementOperation failed to initialize. " + + "Unable to get instance of StateManagementFeatureAPI " + + "with resourceID: " + thisPdpId; + logger.error(msg); + logger.debug(msg); + } + + String admin = stateManagementFeature.getAdminState(); + String oper = stateManagementFeature.getOpState(); + String avail = stateManagementFeature.getAvailStatus(); + String standby = stateManagementFeature.getStandbyStatus(); + + logger.debug("admin = {}", admin); + logger.debug("oper = {}", oper); + logger.debug("avail = {}", avail); + logger.debug("standby = {}", standby); + + assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED)); + assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED)); + + try { + stateManagementFeature.disableFailed(); + } catch (Exception e) { + logger.error(e.getMessage()); + assertTrue(e.getMessage(), false); + } + + admin = stateManagementFeature.getAdminState(); + oper = stateManagementFeature.getOpState(); + avail = stateManagementFeature.getAvailStatus(); + standby = stateManagementFeature.getStandbyStatus(); + + logger.debug("after disableFailed()"); + logger.debug("admin = {}", admin); + logger.debug("oper = {}", oper); + logger.debug("avail = {}", avail); + logger.debug("standby = {}", standby); + + assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED)); + assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED)); + + + try { + stateManagementFeature.promote(); + } catch (Exception e) { + logger.debug(e.getMessage()); + } + + admin = stateManagementFeature.getAdminState(); + oper = stateManagementFeature.getOpState(); + avail = stateManagementFeature.getAvailStatus(); + standby = stateManagementFeature.getStandbyStatus(); + + logger.debug("after promote()"); + logger.debug("admin = {}", admin); + logger.debug("oper = {}", oper); + logger.debug("avail = {}", avail); + logger.debug("standby = {}", standby); + + assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY)); + + /**************Repository Audit Test. **************/ + logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n"); + try { + StateManagementProperties.initProperties(fsmProperties); + RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance(); + repositoryAudit.invoke(fsmProperties); + + //Should not throw an IOException in Linux Foundation env + assertTrue(true); + } catch (IOException e) { + //Note: this catch is here because in a local environment mvn will not run in + //in the temp directory + logger.debug("testSubsytemTest RepositoryAudit IOException", e); + } catch (InterruptedException e) { + assertTrue(false); + logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e); + } + + /*****************Db Audit Test. ***************/ + logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n"); + + try { + DbAudit dbAudit = (DbAudit) DbAudit.getInstance(); + dbAudit.invoke(fsmProperties); + + assertTrue(true); + } catch (Exception e) { + assertTrue(false); + logger.debug("testSubsytemTest DbAudit exception", e); + } + + /*************IntegrityMonitorRestManager Test. *************/ + logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n"); + IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager(); + + Response response = integrityMonitorRestManager.test(); + logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString()); + + assertTrue(response.toString().contains("status=500")); + + //All done + logger.debug("\n\ntestStateManagementOperation: Exiting\n\n"); + } + + /** * This method initializes and cleans the DB so that PDP-D will be able to * store fresh records in the DB. */ - - public static void initializeDb(){ - - logger.debug("initializeDb: Entering"); - - Properties cleanProperties = new Properties(); - cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver"); - cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:file:./sql/statemanagement"); - cleanProperties.put(StateManagementProperties.DB_USER, "sa"); - cleanProperties.put(StateManagementProperties.DB_PWD, ""); - - EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties); - - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // Clean up the DB - em.createQuery("Delete from StateManagementEntity").executeUpdate(); - em.createQuery("Delete from ForwardProgressEntity").executeUpdate(); - em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate(); - - // commit transaction - et.commit(); - em.close(); - - logger.debug("initializeDb: Exiting"); - } + public static void initializeDb() { + + logger.debug("initializeDb: Entering"); + + Properties cleanProperties = new Properties(); + cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver"); + cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:file:./sql/statemanagement"); + cleanProperties.put(StateManagementProperties.DB_USER, "sa"); + cleanProperties.put(StateManagementProperties.DB_PWD, ""); + + EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties); + + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // Clean up the DB + em.createQuery("Delete from StateManagementEntity").executeUpdate(); + em.createQuery("Delete from ForwardProgressEntity").executeUpdate(); + em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate(); + + // commit transaction + et.commit(); + em.close(); + + logger.debug("initializeDb: Exiting"); + } } diff --git a/feature-state-management/src/test/resources/META-INF/persistence.xml b/feature-state-management/src/test/resources/META-INF/persistence.xml index 0214b7f5..24dabe1d 100644 --- a/feature-state-management/src/test/resources/META-INF/persistence.xml +++ b/feature-state-management/src/test/resources/META-INF/persistence.xml @@ -3,7 +3,7 @@ ============LICENSE_START======================================================= feature-state-management ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,21 +20,31 @@ --> <persistence version="2.1" - xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> - <persistence-unit name="junitPU" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - <class>org.onap.policy.common.im.jpa.StateManagementEntity</class> - <class>org.onap.policy.common.im.jpa.ForwardProgressEntity</class> - <class>org.onap.policy.common.im.jpa.ResourceRegistrationEntity</class> - <class>org.onap.policy.drools.statemanagement.test.Audit</class> - <properties> - <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> - <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/> - <property name="javax.persistence.schema-generation.scripts.create-target" value="./sql/generatedCreateStateManagement.ddl"/> - <property name="javax.persistence.schema-generation.scripts.drop-target" value="./sql/generatedDropStateManagement.ddl"/> + <persistence-unit name="junitPU" + transaction-type="RESOURCE_LOCAL"> + <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + <class>org.onap.policy.common.im.jpa.StateManagementEntity</class> + <class>org.onap.policy.common.im.jpa.ForwardProgressEntity</class> + <class>org.onap.policy.common.im.jpa.ResourceRegistrationEntity</class> + <class>org.onap.policy.drools.statemanagement.test.Audit</class> + <properties> + <property + name="javax.persistence.schema-generation.database.action" + value="drop-and-create" /> + <property + name="javax.persistence.schema-generation.scripts.action" + value="drop-and-create" /> + <property + name="javax.persistence.schema-generation.scripts.create-target" + value="./sql/generatedCreateStateManagement.ddl" /> + <property + name="javax.persistence.schema-generation.scripts.drop-target" + value="./sql/generatedDropStateManagement.ddl" /> </properties> - </persistence-unit> + </persistence-unit> </persistence> diff --git a/feature-state-management/src/test/resources/logback-test.xml b/feature-state-management/src/test/resources/logback-test.xml index 58cabf98..99cdaf9a 100644 --- a/feature-state-management/src/test/resources/logback-test.xml +++ b/feature-state-management/src/test/resources/logback-test.xml @@ -2,7 +2,7 @@ ============LICENSE_START======================================================= feature-state-management ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -22,26 +22,29 @@ <configuration> - <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> - <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> - <Pattern> - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n - </Pattern> - </encoder> - </appender> - <appender name="FILE" class="ch.qos.logback.core.FileAppender"> - <file>logs/debug.log</file> - <encoder> - <Pattern> - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n - </Pattern> - </encoder> - </appender> - - <root level="debug"> - <appender-ref ref="STDOUT" /> - <appender-ref ref="FILE" /> - </root> + <appender name="STDOUT" + class="ch.qos.logback.core.ConsoleAppender"> + <encoder + class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> + <Pattern> + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n + </Pattern> + </encoder> + </appender> + <appender name="FILE" + class="ch.qos.logback.core.FileAppender"> + <file>logs/debug.log</file> + <encoder> + <Pattern> + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n + </Pattern> + </encoder> + </appender> + + <root level="debug"> + <appender-ref ref="STDOUT" /> + <appender-ref ref="FILE" /> + </root> </configuration> diff --git a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java index 5e209faa..3adecddc 100644 --- a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java +++ b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-test-transaction * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.drools.testtransaction; import java.util.EventObject; @@ -29,203 +30,219 @@ import org.onap.policy.drools.system.PolicyController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * TestTransaction interface - * - */ +/** TestTransaction interface. */ public interface TestTransaction { - public static final String TT_FPC = "TT.FPC"; - public static final String TT_COUNTER = "$ttc"; - public static final String TT_UUID = "43868e59-d1f3-43c2-bd6f-86f89a61eea5"; - public static long DEFAULT_TT_TASK_SLEEP = 20000; - - public static final TestTransaction manager = new TTImpl(); - - /** - * register a controller for monitoring test transactions - * - * @param controller policy controller - */ - public void register(PolicyController controller); - - /** - * unregisters a controller for monitoring test transactions - * - * @param controller policy controller - */ - public void unregister(PolicyController controller); + public static final String TT_FPC = "TT.FPC"; + public static final String TT_COUNTER = "$ttc"; + public static final String TT_UUID = "43868e59-d1f3-43c2-bd6f-86f89a61eea5"; + public static long DEFAULT_TT_TASK_SLEEP = 20000; + + public static final TestTransaction manager = new TTImpl(); + + /** + * register a controller for monitoring test transactions. + * + * @param controller policy controller + */ + public void register(PolicyController controller); + + /** + * unregisters a controller for monitoring test transactions. + * + * @param controller policy controller + */ + public void unregister(PolicyController controller); } - /** * Implementation of TestTransaction interface. Controls the registering/unregistering of * PolicyController objects and the management of their related TTControllerTask threads. */ class TTImpl implements TestTransaction { - protected final Map<String, TTControllerTask> controllers = new HashMap<>(); + protected final Map<String, TTControllerTask> controllers = new HashMap<>(); - @Override - public synchronized void register(PolicyController controller) { - if (this.controllers.containsValue(controller)) { - final TTControllerTask controllerTask = this.controllers.get(controller.getName()); - if (controllerTask.isAlive()) - return; + @Override + public synchronized void register(PolicyController controller) { + if (this.controllers.containsValue(controller)) { + final TTControllerTask controllerTask = this.controllers.get(controller.getName()); + if (controllerTask.isAlive()) { + return; + } - // continue : unregister, register operation - } + // continue : unregister, register operation + } - final TTControllerTask controllerTask = new TTControllerTask(controller); - this.controllers.put(controller.getName(), controllerTask); - } + final TTControllerTask controllerTask = new TTControllerTask(controller); + this.controllers.put(controller.getName(), controllerTask); + } - @Override - public synchronized void unregister(PolicyController controller) { - if (!this.controllers.containsValue(controller)) - return; + @Override + public synchronized void unregister(PolicyController controller) { + if (!this.controllers.containsValue(controller)) { + return; + } - final TTControllerTask controllerTask = this.controllers.get(controller.getName()); - controllerTask.stop(); + final TTControllerTask controllerTask = this.controllers.get(controller.getName()); + controllerTask.stop(); - this.controllers.remove(controller.getName()); - } + this.controllers.remove(controller.getName()); + } } - /** * TTControllerTask implements the Runnabale interface Carries out the injection of an event into a * drools session and subsequent query of a counter to ensure that forward progress is occuring. - * */ class TTControllerTask implements Runnable { - // get an instance of logger - private static final Logger logger = LoggerFactory.getLogger(TTControllerTask.class); - - protected final PolicyController controller; + // get an instance of logger + private static final Logger logger = LoggerFactory.getLogger(TTControllerTask.class); - protected volatile boolean alive = true; - protected final Thread thread = new Thread(this); + protected final PolicyController controller; - public TTControllerTask(PolicyController controller) { - this.controller = controller; - this.thread.setName("tt-controller-task-" + controller.getName()); - this.thread.start(); - } + protected volatile boolean alive = true; + protected final Thread thread = new Thread(this); - public PolicyController getController() { - return this.controller; - } + public TTControllerTask(PolicyController controller) { + this.controller = controller; + this.thread.setName("tt-controller-task-" + controller.getName()); + this.thread.start(); + } - public synchronized boolean isAlive() { - return this.alive; - } + public PolicyController getController() { + return this.controller; + } - public synchronized void stop() { - this.alive = false; - this.thread.interrupt(); - try { - this.thread.join(1000); - } catch (final InterruptedException e) { - logger.error("TestTransaction thread threw", e); - this.thread.interrupt(); + public synchronized boolean isAlive() { + return this.alive; } - } - public Thread getThread() { - return this.thread; - } + public synchronized void stop() { + this.alive = false; + this.thread.interrupt(); + try { + this.thread.join(1000); + } catch (final InterruptedException e) { + logger.error("TestTransaction thread threw", e); + this.thread.interrupt(); + } + } - @Override - public void run() { - try { - final List<String> sessions = this.controller.getDrools().getSessionNames(); + public Thread getThread() { + return this.thread; + } - if (!(this.controller.getDrools().isBrained())) { - this.alive = false; - logger.error(this + ": unknown drools controller"); - return; - } - - final DroolsController drools = this.controller.getDrools(); - - final HashMap<String, Long> fpcs = new HashMap<>(); - for (final String session : sessions) { - fpcs.put(session, -1L); - } - - while (this.controller.isAlive() && !this.controller.isLocked() && drools.isBrained() - && this.alive) { - - injectTxIntoSessions(sessions, fpcs, drools); - - if (!this.alive) - return; - - if (!Thread.currentThread().isInterrupted()) - Thread.sleep(TestTransaction.DEFAULT_TT_TASK_SLEEP); - } - } catch (final InterruptedException e) { - logger.info("{}: stopping ...", this, e); - Thread.currentThread().interrupt(); - } catch (final IllegalArgumentException e) { - logger.error("{}: controller {} has not been enabled for testing: ", this, - this.controller.getName(), e.getMessage(), e); - } catch (final Exception e) { - logger.error("Controller: {} is not testable - TestTransaction caught exception: {} ", - this.controller.getName(), e.getMessage()); - logger.error("TestTransaction thread threw", e); - } finally { - logger.info("Exiting: {}", this); - this.alive = false; + @Override + public void run() { + try { + final List<String> sessions = this.controller.getDrools().getSessionNames(); + + if (!(this.controller.getDrools().isBrained())) { + this.alive = false; + logger.error(this + ": unknown drools controller"); + return; + } + + final DroolsController drools = this.controller.getDrools(); + + final HashMap<String, Long> fpcs = new HashMap<>(); + for (final String session : sessions) { + fpcs.put(session, -1L); + } + + while (this.controller.isAlive() + && !this.controller.isLocked() + && drools.isBrained() + && this.alive) { + + injectTxIntoSessions(sessions, fpcs, drools); + + if (!this.alive) { + return; + } + + if (!Thread.currentThread().isInterrupted()) { + Thread.sleep(TestTransaction.DEFAULT_TT_TASK_SLEEP); + } + } + } catch (final InterruptedException e) { + logger.info("{}: stopping ...", this, e); + Thread.currentThread().interrupt(); + } catch (final IllegalArgumentException e) { + logger.error( + "{}: controller {} has not been enabled for testing: ", + this, + this.controller.getName(), + e.getMessage(), + e); + } catch (final Exception e) { + logger.error( + "Controller: {} is not testable - TestTransaction caught exception: {} ", + this.controller.getName(), + e.getMessage()); + logger.error("TestTransaction thread threw", e); + } finally { + logger.info("Exiting: {}", this); + this.alive = false; + } } - } - - private void injectTxIntoSessions(List<String> sessions, HashMap<String, Long> fpcs, DroolsController drools) { - - for (final String session : sessions) { - final List<Object> facts = this.controller.getDrools().factQuery(session, - TestTransaction.TT_FPC, TestTransaction.TT_COUNTER, false); - if (facts == null || facts.size() != 1) { - /* - * unexpected something wrong here, can't expect to recover note this exception is - * caught right below at the exit of run() - */ - logger.error( - "Controller: {}, with rules artifact: (group) {}, (artifact) {}, (version) {} - FPC query failed after EventObject insertion! ", - this.controller.getName(), this.controller.getDrools().getGroupId(), - this.controller.getDrools().getArtifactId(), - this.controller.getDrools().getVersion()); - break; + + private void injectTxIntoSessions( + List<String> sessions, HashMap<String, Long> fpcs, DroolsController drools) { + + for (final String session : sessions) { + final List<Object> facts = + this.controller + .getDrools() + .factQuery(session, TestTransaction.TT_FPC, TestTransaction.TT_COUNTER, false); + if (facts == null || facts.size() != 1) { + /* + * unexpected something wrong here, can't expect to recover note this exception is + * caught right below at the exit of run() + */ + logger.error( + "Controller: {}, with rules artifact: (group) {}, (artifact) {}, (version) {} " + + "- FPC query failed after EventObject insertion! ", + this.controller.getName(), + this.controller.getDrools().getGroupId(), + this.controller.getDrools().getArtifactId(), + this.controller.getDrools().getVersion()); + break; + } + logger.debug("Facts: {}", facts); + + final long fpc = (Long) facts.get(0); + if (fpc != fpcs.get(session)) { + logger.info( + "Controller: {} , session {} - Forward progress successful: {} -> {}", + this.controller.getName(), + session, + fpcs.get(session), + fpc); + } else { + logger.error( + "Controller: {}, session {} - Forward progress failure: {}", + this.controller.getName(), + session, + fpc); + } + fpcs.put(session, fpc); + drools.getContainer().insert(session, new EventObject(TestTransaction.TT_UUID)); } - logger.debug("Facts: {}", facts); - - final long fpc = (Long) facts.get(0); - if (fpc != fpcs.get(session)) - logger.info("Controller: {} , session {} - Forward progress successful: {} -> {}", - this.controller.getName(), session, fpcs.get(session), fpc); - else - logger.error("Controller: {}, session {} - Forward progress failure: {}", - this.controller.getName(), session, fpc); - - fpcs.put(session, fpc); - drools.getContainer().insert(session, new EventObject(TestTransaction.TT_UUID)); - } - - } - - - @Override - public String toString() { - final StringBuilder builder = new StringBuilder(); - builder.append("TTControllerTask [controller="); - builder.append(this.controller); - builder.append(", alive="); - builder.append(this.alive); - builder.append(", thread="); - builder.append(this.thread.getName()); - builder.append("]"); - return builder.toString(); - } + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("TTControllerTask [controller="); + builder.append(this.controller); + builder.append(", alive="); + builder.append(this.alive); + builder.append(", thread="); + builder.append(this.thread.getName()); + builder.append("]"); + return builder.toString(); + } } diff --git a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransactionFeature.java b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransactionFeature.java index e78668ab..956304b9 100644 --- a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransactionFeature.java +++ b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransactionFeature.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * feature-test-transaction * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -24,55 +24,52 @@ import org.onap.policy.drools.features.PolicyControllerFeatureAPI; import org.onap.policy.drools.system.PolicyController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** - * TestTransactionFeature implements the PolicyControllerFeatureAPI. - * TestTransactionFeature is the interface to the TestTransaction feature logic. - * + * TestTransactionFeature implements the PolicyControllerFeatureAPI. TestTransactionFeature is the + * interface to the TestTransaction feature logic. */ public class TestTransactionFeature implements PolicyControllerFeatureAPI { - // get an instance of logger - private static final Logger logger = LoggerFactory.getLogger(TestTransactionFeature.class); - + // get an instance of logger + private static final Logger logger = LoggerFactory.getLogger(TestTransactionFeature.class); + @Override - public boolean afterStart(PolicyController controller){ - + public boolean afterStart(PolicyController controller) { + logger.info("TEST_TRANSACTION FEATURE LOADED"); - - if (controller.isAlive() && - !controller.isLocked() && - controller.getDrools().isBrained()) - TestTransaction.manager.register(controller); - + + if (controller.isAlive() && !controller.isLocked() && controller.getDrools().isBrained()) { + TestTransaction.manager.register(controller); + } return false; } - + @Override public boolean afterLock(PolicyController controller) { logger.info("controller {} locked", controller.getName()); - + TestTransaction.manager.unregister(controller); return false; } - + @Override public boolean afterUnlock(PolicyController controller) { logger.info("controller {} unlocked", controller.getName()); - - if (controller.isAlive() && - !controller.isLocked() && - controller.getDrools().isBrained()) - TestTransaction.manager.register(controller); - + + if (controller.isAlive() && !controller.isLocked() && controller.getDrools().isBrained()) { + TestTransaction.manager.register(controller); + } + return false; } @Override public boolean beforeStop(PolicyController controller) { logger.info("controller {} stopping", controller.getName()); - + TestTransaction.manager.unregister(controller); - + return false; } diff --git a/feature-test-transaction/src/test/java/org/onap/policy/drools/testtransaction/TestTransactionTest.java b/feature-test-transaction/src/test/java/org/onap/policy/drools/testtransaction/TestTransactionTest.java index fd373a1d..09be93f9 100644 --- a/feature-test-transaction/src/test/java/org/onap/policy/drools/testtransaction/TestTransactionTest.java +++ b/feature-test-transaction/src/test/java/org/onap/policy/drools/testtransaction/TestTransactionTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-test-transaction * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,101 +40,93 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestTransactionTest { - /** - * Test JUnit Controller Name - */ - public static final String TEST_CONTROLLER_NAME = "unnamed"; - /** - * Controller Configuration File - */ - public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties"; - - /** - * Controller Configuration Backup File - */ - public static final String TEST_CONTROLLER_FILE_BAK = - TEST_CONTROLLER_NAME + "-controller.properties.bak"; - - - /** - * logger - */ - private static Logger logger = LoggerFactory.getLogger(TestTransactionTest.class); - - - - @BeforeClass - public static void startUp() throws IOException { - logger.info("enter"); - - cleanUpWorkingDir(); - - /* ensure presence of config directory */ - SystemPersistence.manager.setConfigurationDir(null); - } - - @Test - public void registerUnregisterTest() throws InterruptedException { - final Properties controllerProperties = new Properties(); - controllerProperties.put(DroolsProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME); - final PolicyController controller = - PolicyEngine.manager.createPolicyController(TEST_CONTROLLER_NAME, controllerProperties); - assertNotNull(PolicyController.factory.get(TEST_CONTROLLER_NAME)); - logger.info(controller.toString()); - - Thread ttThread = null; - - TestTransaction.manager.register(controller); - assertNotNull(TestTransaction.manager); - - /* - * Unregistering the controller should terminate its TestTransaction thread if it hasn't already - * been terminated + /** Test JUnit Controller Name. */ + public static final String TEST_CONTROLLER_NAME = "unnamed"; + /** Controller Configuration File. */ + public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties"; + + /** Controller Configuration Backup File. */ + public static final String TEST_CONTROLLER_FILE_BAK = + TEST_CONTROLLER_NAME + "-controller.properties.bak"; + + /** logger. */ + private static Logger logger = LoggerFactory.getLogger(TestTransactionTest.class); + + /** + * Start up. + * + * @throws IOException exception */ - TestTransaction.manager.unregister(controller); - - ttThread = this.getThread("tt-controller-task-" + TEST_CONTROLLER_NAME); - assertEquals(null, ttThread); - - - } + @BeforeClass + public static void startUp() throws IOException { + logger.info("enter"); - /* - * Returns thread object based on String name - */ - public Thread getThread(String threadName) throws InterruptedException { - // give a chance to the transaction thread to be spawned/destroyed - Thread.sleep(5000L); - - final Set<Thread> threadSet = Thread.getAllStackTraces().keySet(); - for (final Thread thread : threadSet) { - if (thread.getName().equals(threadName)) { - return thread; - } + cleanUpWorkingDir(); + /* ensure presence of config directory */ + SystemPersistence.manager.setConfigurationDir(null); } - return null; - } - - /** - * clean up working directory - */ - protected static void cleanUpWorkingDir() { - final Path testControllerPath = Paths - .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE); - try { - Files.deleteIfExists(testControllerPath); - } catch (final Exception e) { - logger.info("Problem cleaning {}", testControllerPath, e); + + @Test + public void registerUnregisterTest() throws InterruptedException { + final Properties controllerProperties = new Properties(); + controllerProperties.put(DroolsProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME); + final PolicyController controller = + PolicyEngine.manager.createPolicyController(TEST_CONTROLLER_NAME, controllerProperties); + assertNotNull(PolicyController.factory.get(TEST_CONTROLLER_NAME)); + logger.info(controller.toString()); + + TestTransaction.manager.register(controller); + assertNotNull(TestTransaction.manager); + + /* + * Unregistering the controller should terminate its TestTransaction thread if it hasn't already + * been terminated + */ + TestTransaction.manager.unregister(controller); + + Thread ttThread = this.getThread("tt-controller-task-" + TEST_CONTROLLER_NAME); + assertEquals(null, ttThread); } - final Path testControllerBakPath = Paths - .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK); - try { - Files.deleteIfExists(testControllerBakPath); - } catch (final Exception e) { - logger.info("Problem cleaning {}", testControllerBakPath, e); + /** + * Returns thread object based on String name. + * + * @param threadName thread name + * @return the thread + * @throws InterruptedException exception + */ + public Thread getThread(String threadName) throws InterruptedException { + // give a chance to the transaction thread to be spawned/destroyed + Thread.sleep(5000L); + + final Set<Thread> threadSet = Thread.getAllStackTraces().keySet(); + for (final Thread thread : threadSet) { + if (thread.getName().equals(threadName)) { + return thread; + } + } + return null; } - } + /** clean up working directory. */ + protected static void cleanUpWorkingDir() { + final Path testControllerPath = + Paths.get( + SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE); + try { + Files.deleteIfExists(testControllerPath); + } catch (final Exception e) { + logger.info("Problem cleaning {}", testControllerPath, e); + } + + final Path testControllerBakPath = + Paths.get( + SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK); + try { + Files.deleteIfExists(testControllerBakPath); + } catch (final Exception e) { + logger.info("Problem cleaning {}", testControllerBakPath, e); + } + } } |