2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.drools.controller.test;
23 import static org.junit.Assert.assertTrue;
26 import java.io.FileInputStream;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.Properties;
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.EntityTransaction;
34 import javax.persistence.Persistence;
36 import org.apache.commons.lang3.time.DateUtils;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.openecomp.policy.common.im.AdministrativeStateException;
44 import org.openecomp.policy.common.im.IntegrityMonitor;
45 import org.openecomp.policy.common.im.StandbyStatusException;
46 import org.openecomp.policy.common.im.StateManagement;
47 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
48 import org.openecomp.policy.common.logging.flexlogger.Logger;
49 import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
50 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
51 import org.openecomp.policy.drools.core.PolicyContainer;
52 import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
53 import org.openecomp.policy.drools.persistence.DroolsPdp;
54 import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
55 import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
56 import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
57 import org.openecomp.policy.drools.persistence.DroolsPdpsElectionHandler;
58 import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
59 import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
60 import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
61 import org.openecomp.policy.drools.system.Main;
64 * All JUnits are designed to run in the local development environment
65 * where they have write privileges and can execute time-sensitive
68 * These tests can be run as JUnits, but there is some issue with running them
69 * as part of a "mvn install" build. Also, they take a very long time to run
70 * due to many real time breaks. Consequently, they are marked as @Ignore and
71 * only run from the desktop.
74 public class StandbyStateManagementTest {
75 private static Logger logger = FlexLogger.getLogger(StandbyStateManagementTest.class);
77 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every ten seconds, starting
78 * at ten seconds after the minute boundary (e.g. 13:05:10). So, an 80 second sleep should be
79 * sufficient to ensure that we wait for the DesignationWaiter to do its job, before
80 * checking the results.
82 long sleepTime = 80000;
85 * DroolsPdpsElectionHandler runs every ten seconds, so a 15 second sleep should be
86 * plenty to ensure it has time to re-promote this PDP.
88 long electionWaitSleepTime = 15000;
91 * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
93 long interruptRecoveryTime = 5000;
95 private static EntityManagerFactory emfx;
96 private static EntityManagerFactory emfd;
97 private static EntityManager emx;
98 private static EntityManager emd;
99 private static EntityTransaction et;
102 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
105 public static void setUpClass() throws Exception {
107 String userDir = System.getProperty("user.dir");
108 logger.debug("setUpClass: userDir=" + userDir);
109 System.setProperty("com.sun.management.jmxremote.port", "9980");
110 System.setProperty("com.sun.management.jmxremote.authenticate","false");
112 // Make sure path to config directory is set correctly in PolicyContainer.main
113 // Also make sure we ignore HTTP server failures resulting from port conflicts.
114 PolicyContainer.isUnitTesting = true;
119 public static void tearDownClass() throws Exception {
123 public void setUp() throws Exception {
124 //Create teh data access for xaml db
125 Properties xacmlPersistenceProperties = new Properties();
126 xacmlPersistenceProperties.load(new FileInputStream(new File(
127 "src/test/server/config/xacmlPersistence.properties")));
129 emfx = Persistence.createEntityManagerFactory("junitXacmlPU", xacmlPersistenceProperties);
131 // Create an entity manager to use the DB
132 emx = emfx.createEntityManager();
134 //Create the data access for drools db
135 Properties droolsPersistenceProperties = new Properties();
136 droolsPersistenceProperties.load(new FileInputStream(new File(
137 "src/test/server/config/droolsPersistence.properties")));
139 emfd = Persistence.createEntityManagerFactory("junitDroolsPU", droolsPersistenceProperties);
141 // Create an entity manager to use the DB
142 emd = emfd.createEntityManager();
146 public void tearDown() throws Exception {
150 public void cleanXacmlDb(){
151 et = emx.getTransaction();
154 // Make sure we leave the DB clean
155 emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
156 emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
157 emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
158 emx.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate();
163 public void cleanDroolsDb(){
164 et = emd.getTransaction();
167 // Make sure we leave the DB clean
168 emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
169 emd.createQuery("DELETE FROM DroolsSessionEntity").executeUpdate();
170 emd.createQuery("DELETE FROM SessionInfo").executeUpdate();
171 emd.createQuery("DELETE FROM WorkItemInfo").executeUpdate();
172 emd.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate();
178 * These JUnit tests must be run in an eclipse environment by right-clicking
179 * StandbyStateManagementTest and selecting "Run As" -> "JUnit Test". If you
180 * run them as part of mvn install, they will fail with an error "JUnit The
181 * forked VM terminated without saying properly goodbye"
185 public void runAllTests() throws Exception {
191 //testSanitizeDesignatedList();
192 //testComputeMostRecentPrimary();
193 //testComputeDesignatedPdp();
194 testPMStandbyStateChangeNotifier();
197 private void testPMStandbyStateChangeNotifier() throws Exception {
198 logger.debug("\n\ntestPMStandbyStateChangeNotifier: Entering\n\n");
201 logger.debug("testPMStandbyStateChangeNotifier: Reading IntegrityMonitorProperties");
203 Properties integrityMonitorProperties = new Properties();
204 integrityMonitorProperties.load(new FileInputStream(new File(
205 "src/test/server/config/IntegrityMonitor.properties")));
206 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
208 logger.debug("testPMStandbyStateChangeNotifier: Reading droolsPersistenceProperties");
209 Properties droolsPersistenceProperties = new Properties();
210 droolsPersistenceProperties.load(new FileInputStream(new File(
211 "src/test/server/config/droolsPersistence.properties")));
212 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
214 logger.debug("testPMStandbyStateChangeNotifier: Reading xacmlPersistenceProperties");
216 Properties xacmlPersistenceProperties = new Properties();
217 xacmlPersistenceProperties.load(new FileInputStream(new File(
218 "src/test/server/config/xacmlPersistence.properties")));
219 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
221 logger.debug("testPMStandbyStateChangeNotifier: Creating emfXacml");
222 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
223 "junitXacmlPU", xacmlPersistenceProperties);
225 //Now get the StateManagement instance so we can register our observer
226 StateManagement sm = new StateManagement(emfXacml, "pdp1");
228 //Create an instance of the Observer
229 PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
231 //Register the PMStandbyStateChangeNotifier Observer
232 sm.addObserver(pmNotifier);
234 //At this point the standbystatus = 'null'
236 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
239 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
241 //Adding standbystatus=hotstandby
243 System.out.println(pmNotifier.getPreviousStandbyStatus());
244 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
246 //Now making standbystatus=coldstandby
248 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
250 //standbystatus = hotstandby
252 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
254 //standbystatus = providingservice
256 //The previousStandbyStatus is not updated until after the delay activation expires
257 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
259 //Sleep long enough for the delayActivationTimer to run
261 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
263 //standbystatus = providingservice
265 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
267 //standbystatus = coldstandby
269 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
271 //standbystatus = hotstandby
273 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
275 //standbystatus = hotstandby
277 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
284 public void testSanitizeDesignatedList() throws Exception {
286 logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
289 * Get a DroolsPdpsConnector
291 logger.debug("testSanitizeDesignatedList: Reading droolsPersistenceProperties");
292 Properties droolsPersistenceProperties = new Properties();
293 droolsPersistenceProperties.load(new FileInputStream(new File(
294 "src/test/server/config/droolsPersistence.properties")));
295 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
297 logger.debug("testSanitizeDesignatedList: Creating emfDrools");
298 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
299 "junitDroolsPU", droolsPersistenceProperties);
301 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
304 * Create 4 pdpd all not designated
306 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
307 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
308 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
309 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
311 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
312 listOfDesignated.add(pdp1);
313 listOfDesignated.add(pdp2);
314 listOfDesignated.add(pdp3);
315 listOfDesignated.add(pdp4);
317 DroolsPDPIntegrityMonitor droolsPDPIntegrityMonitor;
319 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.init("src/test/server/config");
321 //If it already exists, just get it
322 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
324 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1, droolsPDPIntegrityMonitor);
326 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
328 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = " + listOfDesignated.size() + "\n\n");
330 assertTrue(listOfDesignated.size()==4);
333 * Now make 2 designated
335 pdp1.setDesignated(true);
336 pdp2.setDesignated(true);
338 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
340 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = " + listOfDesignated.size() + "\n\n");
342 assertTrue(listOfDesignated.size()==2);
343 assertTrue(listOfDesignated.contains(pdp1));
344 assertTrue(listOfDesignated.contains(pdp2));
347 * Now all are designated. But, we have to add back the previously non-designated nodes
349 pdp3.setDesignated(true);
350 pdp4.setDesignated(true);
351 listOfDesignated.add(pdp3);
352 listOfDesignated.add(pdp4);
354 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
356 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = " + listOfDesignated.size() + "\n\n");
358 assertTrue(listOfDesignated.size()==4);
364 public void testComputeMostRecentPrimary() throws Exception {
366 logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
369 * Get a DroolsPdpsConnector
371 logger.debug("testComputeMostRecentPrimary: Reading droolsPersistenceProperties");
372 Properties droolsPersistenceProperties = new Properties();
373 droolsPersistenceProperties.load(new FileInputStream(new File(
374 "src/test/server/config/droolsPersistence.properties")));
375 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
377 logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
378 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
379 "junitDroolsPU", droolsPersistenceProperties);
381 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
384 * Create 4 pdpd all not designated
386 long designatedDateMS = new Date().getTime();
387 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
388 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
390 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
392 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
394 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
395 pdp3.setDesignatedDate(new Date(designatedDateMS - 1));
397 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
399 pdp4.setDesignatedDate(new Date(designatedDateMS));
401 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
402 listOfAllPdps.add(pdp1);
403 listOfAllPdps.add(pdp2);
404 listOfAllPdps.add(pdp3);
405 listOfAllPdps.add(pdp4);
408 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
409 listOfDesignated.add(pdp1);
410 listOfDesignated.add(pdp2);
411 listOfDesignated.add(pdp3);
412 listOfDesignated.add(pdp4);
416 * Because the way we sanitize the listOfDesignated, it will always contain all hot standby
417 * or all designated members.
419 DroolsPDPIntegrityMonitor droolsPDPIntegrityMonitor;
421 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.init("src/test/server/config");
423 //If it already exists, just get it
424 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
426 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1, droolsPDPIntegrityMonitor);
428 DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
430 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
433 * If all of the pdps are included in the listOfDesignated and none are designated, it will choose
434 * the one which has the most recent designated date.
436 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
439 * Now let's designate all of those on the listOfDesignated. It will choose the first one designated
441 pdp1.setDesignated(true);
442 pdp2.setDesignated(true);
443 pdp3.setDesignated(true);
444 pdp4.setDesignated(true);
446 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
448 logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
451 * If all of the pdps are included in the listOfDesignated and all are designated, it will choose
452 * the one which was designated first
454 assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
457 * Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
458 * look for the most recently designated pdp which is not currently designated.
460 pdp3.setDesignated(false);
461 pdp4.setDesignated(false);
463 listOfDesignated.remove(pdp3);
464 listOfDesignated.remove(pdp4);
466 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
468 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
470 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
474 * Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
475 * look for the most recently designated pdp regardless of whether it is currently marked as designated.
477 pdp1.setDesignated(false);
478 pdp2.setDesignated(false);
480 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
482 logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
484 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
487 * If we have only one pdp on in the listOfDesignated, the most recently designated pdp will be chosen, regardless
488 * of its designation status
490 listOfDesignated.remove(pdp1);
492 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
494 logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
496 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
499 * Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
501 listOfDesignated.remove(pdp2);
503 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
505 logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
507 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
513 public void testComputeDesignatedPdp() throws Exception{
515 logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
518 * Get a DroolsPdpsConnector
520 logger.debug("testComputeDesignatedPdp: Reading droolsPersistenceProperties");
521 Properties droolsPersistenceProperties = new Properties();
522 droolsPersistenceProperties.load(new FileInputStream(new File(
523 "src/test/server/config/droolsPersistence.properties")));
524 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
526 logger.debug("testComputeDesignatedPdp: Creating emfDrools");
527 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
528 "junitDroolsPU", droolsPersistenceProperties);
530 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
533 * Create 4 pdpd all not designated. Two on site1. Two on site2
535 long designatedDateMS = new Date().getTime();
536 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
537 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
538 pdp1.setSiteName("site1");
540 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
541 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
542 pdp2.setSiteName("site1");
545 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
546 pdp3.setDesignatedDate(new Date(designatedDateMS - 4));
547 pdp3.setSiteName("site2");
549 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
551 pdp4.setDesignatedDate(new Date(designatedDateMS));
552 pdp4.setSiteName("site2");
554 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
555 listOfAllPdps.add(pdp1);
556 listOfAllPdps.add(pdp2);
557 listOfAllPdps.add(pdp3);
558 listOfAllPdps.add(pdp4);
561 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
564 * We will first test an empty listOfDesignated. As we know from the previous JUnit,
565 * the pdp with the most designated date will be chosen for mostRecentPrimary
568 DroolsPDPIntegrityMonitor droolsPDPIntegrityMonitor;
570 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.init("src/test/server/config");
572 //If it already exists, just get it
573 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
575 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1, droolsPDPIntegrityMonitor);
577 DroolsPdp mostRecentPrimary = pdp4;
579 DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
582 * The designatedPdp should be null
584 assertTrue(designatedPdp==null);
587 * Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
590 listOfDesignated.add(pdp2);
592 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
595 * Now the designatedPdp should be the one and only selection in the listOfDesignated
597 assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
600 * Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
602 listOfDesignated.add(pdp1);
604 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
607 * The designatedPdp should now be the one with the lowest lexiographic score - pdp1
609 assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
612 * Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
614 listOfDesignated.remove(pdp1);
615 listOfDesignated.add(pdp3);
617 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
620 * The designatedPdp should now be the one on the same site as the mostRecentPrimary
622 assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
628 public void testColdStandby() throws Exception {
630 logger.debug("\n\ntestColdStandby: Entering\n\n");
634 logger.debug("testColdStandby: Reading IntegrityMonitorProperties");
635 Properties integrityMonitorProperties = new Properties();
636 integrityMonitorProperties.load(new FileInputStream(new File(
637 "src/test/server/config/IntegrityMonitor.properties")));
638 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
639 String thisPdpId = IntegrityMonitorProperties
640 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
642 logger.debug("testColdStandby: Reading xacmlPersistenceProperties");
643 Properties xacmlPersistenceProperties = new Properties();
644 xacmlPersistenceProperties.load(new FileInputStream(new File(
645 "src/test/server/config/xacmlPersistence.properties")));
646 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
648 logger.debug("testColdStandby: Creating emfXacml");
649 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
650 "junitXacmlPU", xacmlPersistenceProperties);
652 logger.debug("testColdStandby: Reading droolsPersistenceProperties");
653 Properties droolsPersistenceProperties = new Properties();
654 droolsPersistenceProperties.load(new FileInputStream(new File(
655 "src/test/server/config/droolsPersistence.properties")));
656 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
658 logger.debug("testColdStandby: Creating emfDrools");
659 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
660 "junitDroolsPU", droolsPersistenceProperties);
662 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
664 logger.debug("testColdStandby: Cleaning up tables");
665 conn.deleteAllSessions();
666 conn.deleteAllPdps();
668 logger.debug("testColdStandby: Inserting PDP=" + thisPdpId + " as designated");
669 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
671 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
672 logger.debug("testColdStandby: After insertion, DESIGNATED="
673 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
674 assertTrue(droolsPdpEntity.isDesignated() == true);
677 * When the Standby Status changes (from providingservice) to hotstandby
678 * or coldstandby,the Active/Standby selection algorithm must stand down
679 * if thePDP-D is currently the lead/active node and allow another PDP-D
682 * It must also call lock on all engines in the engine management.
684 * Yes, this is kludgy, but we have a chicken and egg problem here: we
685 * need a StateManagement object to invoke the
686 * deleteAllStateManagementEntities method.
688 logger.debug("testColdStandby: Instantiating stateManagement object");
689 StateManagement sm = new StateManagement(emfXacml, "dummy");
690 sm.deleteAllStateManagementEntities();
691 sm = new StateManagement(emfXacml, thisPdpId);
692 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
693 sm.addObserver(pmStandbyStateChangeNotifier);
695 // Artificially putting a PDP into service is really a two step process, 1)
696 // inserting it as designated and 2) promoting it so that its standbyStatus
697 // is providing service.
699 logger.debug("testColdStandby: Running policy-management.Main class");
700 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
701 policyManagementRunner.start();
703 logger.debug("testColdStandby: Runner started; Sleeping "
704 + interruptRecoveryTime + "ms before promoting PDP="
706 Thread.sleep(interruptRecoveryTime);
708 logger.debug("testColdStandby: Promoting PDP=" + thisPdpId);
711 String standbyStatus = sm.getStandbyStatus(thisPdpId);
712 logger.debug("testColdStandby: Before locking, PDP=" + thisPdpId + " has standbyStatus="
715 logger.debug("testColdStandby: Locking sm");
718 Thread.sleep(interruptRecoveryTime);
720 * Verify that the PDP is no longer designated.
722 droolsPdpEntity = conn.getPdp(thisPdpId);
723 logger.debug("testColdStandby: After lock sm.lock() invoked, DESIGNATED="
724 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
725 assertTrue(droolsPdpEntity.isDesignated() == false);
727 logger.debug("testColdStandby: Stopping policyManagementRunner");
728 //policyManagementRunner.stopRunner();
730 logger.debug("\n\ntestColdStandby: Exiting\n\n");
731 Thread.sleep(interruptRecoveryTime);
736 * Tests hot standby when there is only one PDP.
740 public void testHotStandby1() throws Exception {
742 logger.debug("\n\ntestHotStandby1: Entering\n\n");
746 logger.debug("testHotStandby1: Reading IntegrityMonitorProperties");
747 Properties integrityMonitorProperties = new Properties();
748 integrityMonitorProperties.load(new FileInputStream(new File(
749 "src/test/server/config/IntegrityMonitor.properties")));
750 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
751 String thisPdpId = IntegrityMonitorProperties
752 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
754 logger.debug("testHotStandby1: Reading xacmlPersistenceProperties");
755 Properties xacmlPersistenceProperties = new Properties();
756 xacmlPersistenceProperties.load(new FileInputStream(new File(
757 "src/test/server/config/xacmlPersistence.properties")));
758 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
760 logger.debug("testHotStandby1: Creating emfXacml");
761 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
762 "junitXacmlPU", xacmlPersistenceProperties);
764 logger.debug("testHotStandby1: Reading droolsPersistenceProperties");
765 Properties droolsPersistenceProperties = new Properties();
766 droolsPersistenceProperties.load(new FileInputStream(new File(
767 "src/test/server/config/droolsPersistence.properties")));
768 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
770 logger.debug("testHotStandby1: Creating emfDrools");
771 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
772 "junitDroolsPU", droolsPersistenceProperties);
774 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
776 logger.debug("testHotStandby1: Cleaning up tables");
777 conn.deleteAllSessions();
778 conn.deleteAllPdps();
781 * Insert this PDP as not designated. Initial standby state will be
782 * either null or cold standby. Demoting should transit state to
785 logger.debug("testHotStandby1: Inserting PDP=" + thisPdpId + " as not designated");
786 Date yesterday = DateUtils.addDays(new Date(), -1);
787 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
789 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
790 logger.debug("testHotStandby1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
791 + droolsPdpEntity.isDesignated());
792 assertTrue(droolsPdpEntity.isDesignated() == false);
794 logger.debug("testHotStandby1: Instantiating stateManagement object");
795 StateManagement sm = new StateManagement(emfXacml, "dummy");
796 sm.deleteAllStateManagementEntities();
797 sm = new StateManagement(emfXacml, thisPdpId);
798 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
799 sm.addObserver(pmStandbyStateChangeNotifier);
801 logger.debug("testHotStandby1: Demoting PDP=" + thisPdpId);
802 // demoting should cause state to transit to hotstandby
805 logger.debug("testHotStandby1: Running policy-management.Main class");
806 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
807 policyManagementRunner.start();
809 logger.debug("testHotStandby1: Sleeping "
811 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
812 Thread.sleep(sleepTime);
815 * Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
817 droolsPdpEntity = conn.getPdp(thisPdpId);
818 logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED="
819 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
820 assertTrue(droolsPdpEntity.isDesignated() == true);
821 String standbyStatus = sm.getStandbyStatus(thisPdpId);
822 logger.debug("testHotStandby1: After demotion, PDP=" + thisPdpId + " has standbyStatus="
824 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
826 logger.debug("testHotStandby1: Stopping policyManagementRunner");
827 //policyManagementRunner.stopRunner();
829 logger.debug("\n\ntestHotStandby1: Exiting\n\n");
830 Thread.sleep(interruptRecoveryTime);
835 * Tests hot standby when two PDPs are involved.
839 public void testHotStandby2() throws Exception {
841 logger.info("\n\ntestHotStandby2: Entering\n\n");
845 logger.info("testHotStandby2: Reading IntegrityMonitorProperties");
846 Properties integrityMonitorProperties = new Properties();
847 integrityMonitorProperties.load(new FileInputStream(new File(
848 "src/test/server/config/IntegrityMonitor.properties")));
849 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
850 String thisPdpId = IntegrityMonitorProperties
851 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
853 logger.info("testHotStandby2: Reading xacmlPersistenceProperties");
854 Properties xacmlPersistenceProperties = new Properties();
855 xacmlPersistenceProperties.load(new FileInputStream(new File(
856 "src/test/server/config/xacmlPersistence.properties")));
857 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
859 logger.info("testHotStandby2: Creating emfXacml");
860 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
861 "junitXacmlPU", xacmlPersistenceProperties);
863 logger.info("testHotStandby2: Reading droolsPersistenceProperties");
864 Properties droolsPersistenceProperties = new Properties();
865 droolsPersistenceProperties.load(new FileInputStream(new File(
866 "src/test/server/config/droolsPersistence.properties")));
867 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
869 logger.info("testHotStandby2: Creating emfDrools");
870 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
871 "junitDroolsPU", droolsPersistenceProperties);
873 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
875 logger.info("testHotStandby2: Cleaning up tables");
876 conn.deleteAllSessions();
877 conn.deleteAllPdps();
880 * Insert a PDP that's designated but not current.
882 String activePdpId = "pdp2";
883 logger.info("testHotStandby2: Inserting PDP=" + activePdpId + " as stale, designated PDP");
884 Date yesterday = DateUtils.addDays(new Date(), -1);
885 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
887 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
888 logger.info("testHotStandby2: After insertion, PDP=" + activePdpId + ", which is not current, has DESIGNATED="
889 + droolsPdpEntity.isDesignated());
890 assertTrue(droolsPdpEntity.isDesignated() == true);
893 * Promote the designated PDP.
895 * We have a chicken and egg problem here: we need a StateManagement
896 * object to invoke the deleteAllStateManagementEntities method.
898 logger.info("testHotStandby2: Promoting PDP=" + activePdpId);
899 StateManagement sm = new StateManagement(emfXacml, "dummy");
900 sm.deleteAllStateManagementEntities();
901 sm = new StateManagement(emfXacml, activePdpId);//pdp2
902 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
903 sm.addObserver(pmStandbyStateChangeNotifier);
905 // Artificially putting a PDP into service is really a two step process, 1)
906 // inserting it as designated and 2) promoting it so that its standbyStatus
907 // is providing service.
910 * Insert this PDP as not designated. Initial standby state will be
911 * either null or cold standby. Demoting should transit state to
914 logger.info("testHotStandby2: Inserting PDP=" + thisPdpId + " as not designated");
915 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
917 droolsPdpEntity = conn.getPdp(thisPdpId);
918 logger.info("testHotStandby2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
919 + droolsPdpEntity.isDesignated());
920 assertTrue(droolsPdpEntity.isDesignated() == false);
922 logger.info("testHotStandby2: Demoting PDP=" + thisPdpId);//pdp1
923 StateManagement sm2 = new StateManagement(emfXacml, thisPdpId);
924 sm2.addObserver(pmStandbyStateChangeNotifier);
926 logger.info("testHotStandby2: Running policy-management.Main class");
927 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner(); //pdp1
928 policyManagementRunner.start();
930 logger.info("testHotStandby2: Runner started; Sleeping "
931 + interruptRecoveryTime + "ms before promoting/demoting");
932 Thread.sleep(interruptRecoveryTime);
934 logger.info("testHotStandby2: Runner started; promoting PDP=" + activePdpId);//pdpd2xs
935 //at this point, the newly created pdp will have set the state to disabled/failed/cold standby
936 //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
937 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
938 sm.enableNotFailed();//pdp1
940 String standbyStatus = sm.getStandbyStatus(activePdpId);
941 logger.info("testHotStandby2: After promoting, PDP=" + activePdpId + " has standbyStatus="
944 // demoting PDP should ensure that state transits to hotstandby
945 logger.info("testHotStandby2: Runner started; demoting PDP=" + thisPdpId);
947 standbyStatus = sm.getStandbyStatus(thisPdpId);
948 logger.info("testHotStandby2: After demoting, PDP=" + thisPdpId + " has standbyStatus="
951 logger.info("testHotStandby2: Sleeping "
953 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
954 Thread.sleep(sleepTime);
957 * Verify that this PDP, demoted to HOT_STANDBY, is now
958 * re-designated and providing service.
960 droolsPdpEntity = conn.getPdp(thisPdpId);
961 logger.info("testHotStandby2: After demoting PDP=" + activePdpId
962 + ", DESIGNATED=" + droolsPdpEntity.isDesignated()
963 + " for PDP=" + thisPdpId);
964 assertTrue(droolsPdpEntity.isDesignated() == true);
965 standbyStatus = sm2.getStandbyStatus(thisPdpId);
966 logger.info("testHotStandby2: After demoting PDP=" + activePdpId
967 + ", PDP=" + thisPdpId + " has standbyStatus=" + standbyStatus);
968 assertTrue(standbyStatus != null
969 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
971 logger.info("testHotStandby2: Stopping policyManagementRunner");
972 //policyManagementRunner.stopRunner();
974 logger.info("\n\ntestHotStandby2: Exiting\n\n");
975 Thread.sleep(interruptRecoveryTime);
980 * 1) Inserts and designates this PDP, then verifies that startTransaction
983 * 2) Demotes PDP, and verifies that because there is only one PDP, it will
984 * be immediately re-promoted, thus allowing startTransaction to be
987 * 3) Locks PDP and verifies that startTransaction results in
988 * AdministrativeStateException.
990 * 4) Unlocks PDP and verifies that startTransaction results in
991 * StandbyStatusException.
993 * 5) Promotes PDP and verifies that startTransaction is once again
998 public void testLocking1() throws Exception {
999 logger.debug("testLocking1: Entry");
1003 logger.debug("testLocking1: Reading IntegrityMonitorProperties");
1004 Properties integrityMonitorProperties = new Properties();
1005 integrityMonitorProperties.load(new FileInputStream(new File(
1006 "src/test/server/config/IntegrityMonitor.properties")));
1007 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
1008 String thisPdpId = IntegrityMonitorProperties
1009 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
1011 logger.debug("testLocking1: Reading xacmlPersistenceProperties");
1012 Properties xacmlPersistenceProperties = new Properties();
1013 xacmlPersistenceProperties.load(new FileInputStream(new File(
1014 "src/test/server/config/xacmlPersistence.properties")));
1015 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
1017 logger.debug("testLocking1: Creating emfXacml");
1018 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1019 "junitXacmlPU", xacmlPersistenceProperties);
1021 logger.debug("testLocking1: Reading droolsPersistenceProperties");
1022 Properties droolsPersistenceProperties = new Properties();
1023 droolsPersistenceProperties.load(new FileInputStream(new File(
1024 "src/test/server/config/droolsPersistence.properties")));
1025 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
1027 logger.debug("testLocking1: Creating emfDrools");
1028 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1029 "junitDroolsPU", droolsPersistenceProperties);
1031 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1033 logger.debug("testLocking1: Cleaning up tables");
1034 conn.deleteAllSessions();
1035 conn.deleteAllPdps();
1038 * Insert this PDP as designated. Initial standby state will be
1039 * either null or cold standby.
1041 logger.debug("testLocking1: Inserting PDP=" + thisPdpId + " as designated");
1042 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
1043 conn.insertPdp(pdp);
1044 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1045 logger.debug("testLocking1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
1046 + droolsPdpEntity.isDesignated());
1047 assertTrue(droolsPdpEntity.isDesignated() == true);
1049 logger.debug("testLocking1: Instantiating stateManagement object");
1050 StateManagement sm = new StateManagement(emfXacml, "dummy");
1051 sm.deleteAllStateManagementEntities();
1052 sm = new StateManagement(emfXacml, thisPdpId);
1053 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
1054 sm.addObserver(pmStandbyStateChangeNotifier);
1056 logger.debug("testLocking1: Running policy-management.Main class, designated="
1057 + conn.getPdp(thisPdpId).isDesignated());
1058 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
1059 policyManagementRunner.start();
1061 logger.debug("testLocking1: Runner started; Sleeping "
1062 + interruptRecoveryTime + "ms before promoting PDP="
1064 Thread.sleep(interruptRecoveryTime);
1066 logger.debug("testLocking1: Promoting PDP=" + thisPdpId);
1069 logger.debug("testLocking1: Sleeping "
1071 + "ms, to allow time for policy-management.Main class to come up, designated="
1072 + conn.getPdp(thisPdpId).isDesignated());
1073 Thread.sleep(sleepTime);
1075 logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP="
1078 + conn.getPdp(thisPdpId).isDesignated());
1079 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
1082 droolsPdpIntegrityMonitor.startTransaction();
1083 droolsPdpIntegrityMonitor.endTransaction();
1084 logger.debug("testLocking1: As expected, transaction successful");
1085 } catch (AdministrativeStateException e) {
1086 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1088 } catch (StandbyStatusException e) {
1089 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1091 } catch (Exception e) {
1092 logger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1096 // demoting should cause state to transit to hotstandby, followed by re-promotion,
1097 // since there is only one PDP.
1098 logger.debug("testLocking1: demoting PDP=" + thisPdpId);
1099 sm = droolsPdpIntegrityMonitor.getStateManager();
1102 logger.debug("testLocking1: sleeping" + electionWaitSleepTime
1103 + " to allow election handler to re-promote PDP=" + thisPdpId);
1104 Thread.sleep(electionWaitSleepTime);
1106 logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP="
1109 + conn.getPdp(thisPdpId).isDesignated());
1111 droolsPdpIntegrityMonitor.startTransaction();
1112 droolsPdpIntegrityMonitor.endTransaction();
1113 logger.debug("testLocking1: As expected, transaction successful");
1114 } catch (AdministrativeStateException e) {
1115 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1117 } catch (StandbyStatusException e) {
1118 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1120 } catch (Exception e) {
1121 logger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1125 // locking should cause state to transit to cold standby
1126 logger.debug("testLocking1: locking PDP=" + thisPdpId);
1129 // Just to avoid any race conditions, sleep a little after locking
1130 logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
1133 logger.debug("testLocking1: Invoking startTransaction on locked PDP="
1136 + conn.getPdp(thisPdpId).isDesignated());
1138 droolsPdpIntegrityMonitor.startTransaction();
1139 logger.error("testLocking1: startTransaction unexpectedly successful");
1141 } catch (AdministrativeStateException e) {
1142 logger.debug("testLocking1: As expected, caught AdministrativeStateException, message=" + e.getMessage());
1143 } catch (StandbyStatusException e) {
1144 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1146 } catch (Exception e) {
1147 logger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1150 droolsPdpIntegrityMonitor.endTransaction();
1153 // unlocking should cause state to transit to hot standby and then providing service
1154 logger.debug("testLocking1: unlocking PDP=" + thisPdpId);
1157 // Just to avoid any race conditions, sleep a little after locking
1158 logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1159 Thread.sleep(electionWaitSleepTime);
1161 logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1164 + conn.getPdp(thisPdpId).isDesignated());
1166 droolsPdpIntegrityMonitor.startTransaction();
1167 logger.error("testLocking1: startTransaction successful as expected");
1168 } catch (AdministrativeStateException e) {
1169 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1171 } catch (StandbyStatusException e) {
1172 logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1174 } catch (Exception e) {
1175 logger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1178 droolsPdpIntegrityMonitor.endTransaction();
1181 // demoting should cause state to transit to providing service
1182 logger.debug("testLocking1: demoting PDP=" + thisPdpId);
1185 // Just to avoid any race conditions, sleep a little after promoting
1186 logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
1189 logger.debug("testLocking1: Invoking startTransaction on demoted PDP="
1192 + conn.getPdp(thisPdpId).isDesignated());
1194 droolsPdpIntegrityMonitor.startTransaction();
1195 droolsPdpIntegrityMonitor.endTransaction();
1196 logger.debug("testLocking1: Unexpectedly, transaction successful");
1198 } catch (AdministrativeStateException e) {
1199 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1201 } catch (StandbyStatusException e) {
1202 logger.error("testLocking1: As expected caught StandbyStatusException, message=" + e.getMessage());
1203 } catch (Exception e) {
1204 logger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1208 logger.debug("testLocking1: Stopping policyManagementRunner");
1209 //policyManagementRunner.stopRunner();
1211 logger.debug("\n\ntestLocking1: Exiting\n\n");
1212 Thread.sleep(interruptRecoveryTime);
1217 * 1) Inserts and designates this PDP, then verifies that startTransaction
1220 * 2) Inserts another PDP in hotstandby.
1222 * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1223 * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1227 public void testLocking2() throws Exception {
1229 logger.debug("\n\ntestLocking2: Entering\n\n");
1233 logger.debug("testLocking2: Reading IntegrityMonitorProperties");
1234 Properties integrityMonitorProperties = new Properties();
1235 integrityMonitorProperties.load(new FileInputStream(new File(
1236 "src/test/server/config/IntegrityMonitor.properties")));
1237 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
1238 String thisPdpId = IntegrityMonitorProperties
1239 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
1241 logger.debug("testLocking2: Reading xacmlPersistenceProperties");
1242 Properties xacmlPersistenceProperties = new Properties();
1243 xacmlPersistenceProperties.load(new FileInputStream(new File(
1244 "src/test/server/config/xacmlPersistence.properties")));
1245 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
1247 logger.debug("testLocking2: Creating emfXacml");
1248 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1249 "junitXacmlPU", xacmlPersistenceProperties);
1251 logger.debug("testLocking2: Reading droolsPersistenceProperties");
1252 Properties droolsPersistenceProperties = new Properties();
1253 droolsPersistenceProperties.load(new FileInputStream(new File(
1254 "src/test/server/config/droolsPersistence.properties")));
1255 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
1257 logger.debug("testLocking2: Creating emfDrools");
1258 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1259 "junitDroolsPU", droolsPersistenceProperties);
1261 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1263 logger.debug("testLocking2: Cleaning up tables");
1264 conn.deleteAllSessions();
1265 conn.deleteAllPdps();
1268 * Insert this PDP as designated. Initial standby state will be
1269 * either null or cold standby. Demoting should transit state to
1272 logger.debug("testLocking2: Inserting PDP=" + thisPdpId + " as designated");
1273 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
1274 conn.insertPdp(pdp);
1275 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1276 logger.debug("testLocking2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
1277 + droolsPdpEntity.isDesignated());
1278 assertTrue(droolsPdpEntity.isDesignated() == true);
1280 logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP=" + thisPdpId);
1281 StateManagement sm = new StateManagement(emfXacml, "dummy");
1282 sm.deleteAllStateManagementEntities();
1283 sm = new StateManagement(emfXacml, thisPdpId);
1284 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
1285 sm.addObserver(pmStandbyStateChangeNotifier);
1288 * Insert another PDP as not designated. Initial standby state will be
1289 * either null or cold standby. Demoting should transit state to
1292 String standbyPdpId = "pdp2";
1293 logger.debug("testLocking2: Inserting PDP=" + standbyPdpId + " as not designated");
1294 Date yesterday = DateUtils.addDays(new Date(), -1);
1295 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1296 conn.insertPdp(pdp);
1297 droolsPdpEntity = conn.getPdp(standbyPdpId);
1298 logger.debug("testLocking2: After insertion, PDP=" + standbyPdpId + " has DESIGNATED="
1299 + droolsPdpEntity.isDesignated());
1300 assertTrue(droolsPdpEntity.isDesignated() == false);
1302 logger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
1303 StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
1304 sm2.addObserver(pmStandbyStateChangeNotifier);
1306 logger.debug("testLocking2: Running policy-management.Main class");
1307 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
1308 policyManagementRunner.start();
1310 logger.debug("testLocking2: Runner started; Sleeping "
1311 + interruptRecoveryTime + "ms before promoting/demoting");
1312 Thread.sleep(interruptRecoveryTime);
1314 logger.debug("testLocking2: Promoting PDP=" + thisPdpId);
1317 // demoting PDP should ensure that state transits to hotstandby
1318 logger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
1321 logger.debug("testLocking2: Sleeping "
1323 + "ms, to allow time for policy-management.Main class to come up");
1324 Thread.sleep(sleepTime);
1326 logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP="
1329 + conn.getPdp(thisPdpId).isDesignated());
1330 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
1333 droolsPdpIntegrityMonitor.startTransaction();
1334 droolsPdpIntegrityMonitor.endTransaction();
1335 logger.debug("testLocking2: As expected, transaction successful");
1336 } catch (AdministrativeStateException e) {
1337 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1339 } catch (StandbyStatusException e) {
1340 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1342 } catch (Exception e) {
1343 logger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
1347 // demoting should cause state to transit to hotstandby followed by re-promotion.
1348 logger.debug("testLocking2: demoting PDP=" + thisPdpId);
1349 sm = droolsPdpIntegrityMonitor.getStateManager();
1352 logger.debug("testLocking2: sleeping" + electionWaitSleepTime
1353 + " to allow election handler to re-promote PDP=" + thisPdpId);
1354 Thread.sleep(electionWaitSleepTime);
1356 logger.debug("testLocking2: Waking up and invoking startTransaction on re-promoted PDP="
1357 + thisPdpId + ", designated="
1358 + conn.getPdp(thisPdpId).isDesignated());
1360 droolsPdpIntegrityMonitor.startTransaction();
1361 droolsPdpIntegrityMonitor.endTransaction();
1362 logger.debug("testLocking2: As expected, transaction successful");
1363 } catch (AdministrativeStateException e) {
1364 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1366 } catch (StandbyStatusException e) {
1367 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1369 } catch (Exception e) {
1370 logger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
1374 logger.debug("testLocking2: Verifying designated status for PDP="
1376 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
1377 assertTrue(standbyPdpDesignated == false);
1379 logger.debug("testLocking2: Stopping policyManagementRunner");
1380 //policyManagementRunner.stopRunner();
1382 logger.debug("\n\ntestLocking2: Exiting\n\n");
1383 Thread.sleep(interruptRecoveryTime);
1388 private class PolicyManagementRunner extends Thread {
1391 logger.info("PolicyManagementRunner.run: Entering");
1392 String args[] = { "src/main/server/config" };
1395 } catch (Exception e) {
1397 .info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
1401 logger.info("PolicyManagementRunner.run: Exiting");