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.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Date;
32 import java.util.Properties;
34 import javax.persistence.EntityManager;
35 import javax.persistence.EntityManagerFactory;
36 import javax.persistence.EntityTransaction;
37 import javax.persistence.Persistence;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
46 import org.openecomp.policy.common.im.AdministrativeStateException;
47 import org.openecomp.policy.common.im.IntegrityMonitor;
48 import org.openecomp.policy.common.im.StandbyStatusException;
49 import org.openecomp.policy.common.im.StateManagement;
50 import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
51 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
52 import org.openecomp.policy.drools.core.PolicyContainer;
53 import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
54 import org.openecomp.policy.drools.persistence.DroolsPdp;
55 import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
56 import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
57 import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
58 import org.openecomp.policy.drools.persistence.DroolsPdpsElectionHandler;
59 import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
60 import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
61 import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
62 import org.openecomp.policy.drools.system.Main;
63 import org.openecomp.policy.drools.system.PolicyEngine;
64 import org.apache.commons.lang3.time.DateUtils;
67 * All JUnits are designed to run in the local development environment
68 * where they have write privileges and can execute time-sensitive
71 * These tests can be run as JUnits, but there is some issue with running them
72 * as part of a "mvn install" build. Also, they take a very long time to run
73 * due to many real time breaks. Consequently, they are marked as @Ignore and
74 * only run from the desktop.
77 public class StandbyStateManagementTest {
80 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every ten seconds, starting
81 * at ten seconds after the minute boundary (e.g. 13:05:10). So, an 80 second sleep should be
82 * sufficient to ensure that we wait for the DesignationWaiter to do its job, before
83 * checking the results.
85 long sleepTime = 80000;
88 * DroolsPdpsElectionHandler runs every ten seconds, so a 15 second sleep should be
89 * plenty to ensure it has time to re-promote this PDP.
91 long electionWaitSleepTime = 15000;
94 * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
96 long interruptRecoveryTime = 5000;
98 private static EntityManagerFactory emfx;
99 private static EntityManagerFactory emfd;
100 private static EntityManager emx;
101 private static EntityManager emd;
102 private static EntityTransaction et;
105 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
108 public static void setUpClass() throws Exception {
110 String userDir = System.getProperty("user.dir");
111 PolicyLogger.debug("setUpClass: userDir=" + userDir);
112 System.setProperty("com.sun.management.jmxremote.port", "9980");
113 System.setProperty("com.sun.management.jmxremote.authenticate","false");
115 // Make sure path to config directory is set correctly in PolicyContainer.main
116 // Also make sure we ignore HTTP server failures resulting from port conflicts.
117 PolicyContainer.isUnitTesting = true;
122 public static void tearDownClass() throws Exception {
126 public void setUp() throws Exception {
127 //Create teh data access for xaml db
128 Properties xacmlPersistenceProperties = new Properties();
129 xacmlPersistenceProperties.load(new FileInputStream(new File(
130 "src/test/server/config/xacmlPersistence.properties")));
132 emfx = Persistence.createEntityManagerFactory("junitXacmlPU", xacmlPersistenceProperties);
134 // Create an entity manager to use the DB
135 emx = emfx.createEntityManager();
137 //Create the data access for drools db
138 Properties droolsPersistenceProperties = new Properties();
139 droolsPersistenceProperties.load(new FileInputStream(new File(
140 "src/test/server/config/droolsPersistence.properties")));
142 emfd = Persistence.createEntityManagerFactory("junitDroolsPU", droolsPersistenceProperties);
144 // Create an entity manager to use the DB
145 emd = emfd.createEntityManager();
149 public void tearDown() throws Exception {
153 public void cleanXacmlDb(){
154 et = emx.getTransaction();
157 // Make sure we leave the DB clean
158 emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
159 emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
160 emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
161 emx.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate();
166 public void cleanDroolsDb(){
167 et = emd.getTransaction();
170 // Make sure we leave the DB clean
171 emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
172 emd.createQuery("DELETE FROM DroolsSessionEntity").executeUpdate();
173 emd.createQuery("DELETE FROM SessionInfo").executeUpdate();
174 emd.createQuery("DELETE FROM WorkItemInfo").executeUpdate();
175 emd.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate();
182 public void runAllTests() throws Exception {
188 testSanitizeDesignatedList();
189 testComputeMostRecentPrimary();
190 testComputeDesignatedPdp();
195 public void testSanitizeDesignatedList() throws Exception {
197 PolicyLogger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
200 * Get a DroolsPdpsConnector
202 PolicyLogger.debug("testSanitizeDesignatedList: Reading droolsPersistenceProperties");
203 Properties droolsPersistenceProperties = new Properties();
204 droolsPersistenceProperties.load(new FileInputStream(new File(
205 "src/test/server/config/droolsPersistence.properties")));
206 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
208 PolicyLogger.debug("testSanitizeDesignatedList: Creating emfDrools");
209 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
210 "junitDroolsPU", droolsPersistenceProperties);
212 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
215 * Create 4 pdpd all not designated
217 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
218 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
219 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
220 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
222 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
223 listOfDesignated.add(pdp1);
224 listOfDesignated.add(pdp2);
225 listOfDesignated.add(pdp3);
226 listOfDesignated.add(pdp4);
228 DroolsPDPIntegrityMonitor droolsPDPIntegrityMonitor;
230 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.init("src/test/server/config");
232 //If it already exists, just get it
233 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
235 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1, droolsPDPIntegrityMonitor);
237 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
239 PolicyLogger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = " + listOfDesignated.size() + "\n\n");
241 assertTrue(listOfDesignated.size()==4);
244 * Now make 2 designated
246 pdp1.setDesignated(true);
247 pdp2.setDesignated(true);
249 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
251 PolicyLogger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = " + listOfDesignated.size() + "\n\n");
253 assertTrue(listOfDesignated.size()==2);
254 assertTrue(listOfDesignated.contains(pdp1));
255 assertTrue(listOfDesignated.contains(pdp2));
258 * Now all are designated. But, we have to add back the previously non-designated nodes
260 pdp3.setDesignated(true);
261 pdp4.setDesignated(true);
262 listOfDesignated.add(pdp3);
263 listOfDesignated.add(pdp4);
265 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
267 PolicyLogger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = " + listOfDesignated.size() + "\n\n");
269 assertTrue(listOfDesignated.size()==4);
275 public void testComputeMostRecentPrimary() throws Exception {
277 PolicyLogger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
280 * Get a DroolsPdpsConnector
282 PolicyLogger.debug("testComputeMostRecentPrimary: Reading droolsPersistenceProperties");
283 Properties droolsPersistenceProperties = new Properties();
284 droolsPersistenceProperties.load(new FileInputStream(new File(
285 "src/test/server/config/droolsPersistence.properties")));
286 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
288 PolicyLogger.debug("testComputeMostRecentPrimary: Creating emfDrools");
289 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
290 "junitDroolsPU", droolsPersistenceProperties);
292 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
295 * Create 4 pdpd all not designated
297 long designatedDateMS = new Date().getTime();
298 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
299 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
301 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
303 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
305 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
306 pdp3.setDesignatedDate(new Date(designatedDateMS - 1));
308 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
310 pdp4.setDesignatedDate(new Date(designatedDateMS));
312 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
313 listOfAllPdps.add(pdp1);
314 listOfAllPdps.add(pdp2);
315 listOfAllPdps.add(pdp3);
316 listOfAllPdps.add(pdp4);
319 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
320 listOfDesignated.add(pdp1);
321 listOfDesignated.add(pdp2);
322 listOfDesignated.add(pdp3);
323 listOfDesignated.add(pdp4);
327 * Because the way we sanitize the listOfDesignated, it will always contain all hot standby
328 * or all designated members.
330 DroolsPDPIntegrityMonitor droolsPDPIntegrityMonitor;
332 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.init("src/test/server/config");
334 //If it already exists, just get it
335 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
337 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1, droolsPDPIntegrityMonitor);
339 DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
341 PolicyLogger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
344 * If all of the pdps are included in the listOfDesignated and none are designated, it will choose
345 * the one which has the most recent designated date.
347 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
350 * Now let's designate all of those on the listOfDesignated. It will choose the first one designated
352 pdp1.setDesignated(true);
353 pdp2.setDesignated(true);
354 pdp3.setDesignated(true);
355 pdp4.setDesignated(true);
357 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
359 PolicyLogger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
362 * If all of the pdps are included in the listOfDesignated and all are designated, it will choose
363 * the one which was designated first
365 assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
368 * Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
369 * look for the most recently designated pdp which is not currently designated.
371 pdp3.setDesignated(false);
372 pdp4.setDesignated(false);
374 listOfDesignated.remove(pdp3);
375 listOfDesignated.remove(pdp4);
377 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
379 PolicyLogger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
381 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
385 * Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
386 * look for the most recently designated pdp regardless of whether it is currently marked as designated.
388 pdp1.setDesignated(false);
389 pdp2.setDesignated(false);
391 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
393 PolicyLogger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
395 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
398 * If we have only one pdp on in the listOfDesignated, the most recently designated pdp will be chosen, regardless
399 * of its designation status
401 listOfDesignated.remove(pdp1);
403 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
405 PolicyLogger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
407 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
410 * Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
412 listOfDesignated.remove(pdp2);
414 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
416 PolicyLogger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = " + mostRecentPrimary.getPdpId() + "\n\n");
418 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
424 public void testComputeDesignatedPdp() throws Exception{
426 PolicyLogger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
429 * Get a DroolsPdpsConnector
431 PolicyLogger.debug("testComputeDesignatedPdp: Reading droolsPersistenceProperties");
432 Properties droolsPersistenceProperties = new Properties();
433 droolsPersistenceProperties.load(new FileInputStream(new File(
434 "src/test/server/config/droolsPersistence.properties")));
435 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
437 PolicyLogger.debug("testComputeDesignatedPdp: Creating emfDrools");
438 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
439 "junitDroolsPU", droolsPersistenceProperties);
441 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
444 * Create 4 pdpd all not designated. Two on site1. Two on site2
446 long designatedDateMS = new Date().getTime();
447 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
448 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
449 pdp1.setSiteName("site1");
451 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
452 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
453 pdp2.setSiteName("site1");
456 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
457 pdp3.setDesignatedDate(new Date(designatedDateMS - 4));
458 pdp3.setSiteName("site2");
460 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
462 pdp4.setDesignatedDate(new Date(designatedDateMS));
463 pdp4.setSiteName("site2");
465 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
466 listOfAllPdps.add(pdp1);
467 listOfAllPdps.add(pdp2);
468 listOfAllPdps.add(pdp3);
469 listOfAllPdps.add(pdp4);
472 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
475 * We will first test an empty listOfDesignated. As we know from the previous JUnit,
476 * the pdp with the most designated date will be chosen for mostRecentPrimary
479 DroolsPDPIntegrityMonitor droolsPDPIntegrityMonitor;
481 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.init("src/test/server/config");
483 //If it already exists, just get it
484 droolsPDPIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
486 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1, droolsPDPIntegrityMonitor);
488 DroolsPdp mostRecentPrimary = pdp4;
490 DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
493 * The designatedPdp should be null
495 assertTrue(designatedPdp==null);
498 * Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
501 listOfDesignated.add(pdp2);
503 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
506 * Now the designatedPdp should be the one and only selection in the listOfDesignated
508 assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
511 * Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
513 listOfDesignated.add(pdp1);
515 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
518 * The designatedPdp should now be the one with the lowest lexiographic score - pdp1
520 assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
523 * Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
525 listOfDesignated.remove(pdp1);
526 listOfDesignated.add(pdp3);
528 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
531 * The designatedPdp should now be the one on the same site as the mostRecentPrimary
533 assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
539 public void testColdStandby() throws Exception {
541 PolicyLogger.debug("\n\ntestColdStandby: Entering\n\n");
545 PolicyLogger.debug("testColdStandby: Reading IntegrityMonitorProperties");
546 Properties integrityMonitorProperties = new Properties();
547 integrityMonitorProperties.load(new FileInputStream(new File(
548 "src/test/server/config/IntegrityMonitor.properties")));
549 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
550 String thisPdpId = IntegrityMonitorProperties
551 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
553 PolicyLogger.debug("testColdStandby: Reading xacmlPersistenceProperties");
554 Properties xacmlPersistenceProperties = new Properties();
555 xacmlPersistenceProperties.load(new FileInputStream(new File(
556 "src/test/server/config/xacmlPersistence.properties")));
557 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
559 PolicyLogger.debug("testColdStandby: Creating emfXacml");
560 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
561 "junitXacmlPU", xacmlPersistenceProperties);
563 PolicyLogger.debug("testColdStandby: Reading droolsPersistenceProperties");
564 Properties droolsPersistenceProperties = new Properties();
565 droolsPersistenceProperties.load(new FileInputStream(new File(
566 "src/test/server/config/droolsPersistence.properties")));
567 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
569 PolicyLogger.debug("testColdStandby: Creating emfDrools");
570 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
571 "junitDroolsPU", droolsPersistenceProperties);
573 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
575 PolicyLogger.debug("testColdStandby: Cleaning up tables");
576 conn.deleteAllSessions();
577 conn.deleteAllPdps();
579 PolicyLogger.debug("testColdStandby: Inserting PDP=" + thisPdpId + " as designated");
580 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
582 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
583 PolicyLogger.debug("testColdStandby: After insertion, DESIGNATED="
584 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
585 assertTrue(droolsPdpEntity.isDesignated() == true);
588 * When the Standby Status changes (from providingservice) to hotstandby
589 * or coldstandby,the Active/Standby selection algorithm must stand down
590 * if thePDP-D is currently the lead/active node and allow another PDP-D
593 * It must also call lock on all engines in the engine management.
595 * Yes, this is kludgy, but we have a chicken and egg problem here: we
596 * need a StateManagement object to invoke the
597 * deleteAllStateManagementEntities method.
599 PolicyLogger.debug("testColdStandby: Instantiating stateManagement object");
600 StateManagement sm = new StateManagement(emfXacml, "dummy");
601 sm.deleteAllStateManagementEntities();
602 sm = new StateManagement(emfXacml, thisPdpId);
603 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
604 sm.addObserver(pmStandbyStateChangeNotifier);
606 // Artificially putting a PDP into service is really a two step process, 1)
607 // inserting it as designated and 2) promoting it so that its standbyStatus
608 // is providing service.
610 PolicyLogger.debug("testColdStandby: Running policy-management.Main class");
611 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
612 policyManagementRunner.start();
614 PolicyLogger.debug("testColdStandby: Runner started; Sleeping "
615 + interruptRecoveryTime + "ms before promoting PDP="
617 Thread.sleep(interruptRecoveryTime);
619 PolicyLogger.debug("testColdStandby: Promoting PDP=" + thisPdpId);
622 String standbyStatus = sm.getStandbyStatus(thisPdpId);
623 PolicyLogger.debug("testColdStandby: Before locking, PDP=" + thisPdpId + " has standbyStatus="
626 PolicyLogger.debug("testColdStandby: Locking sm");
629 Thread.sleep(interruptRecoveryTime);
631 * Verify that the PDP is no longer designated.
633 droolsPdpEntity = conn.getPdp(thisPdpId);
634 PolicyLogger.debug("testColdStandby: After lock sm.lock() invoked, DESIGNATED="
635 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
636 assertTrue(droolsPdpEntity.isDesignated() == false);
638 PolicyLogger.debug("testColdStandby: Stopping policyManagementRunner");
639 //policyManagementRunner.stopRunner();
641 PolicyLogger.debug("\n\ntestColdStandby: Exiting\n\n");
642 Thread.sleep(interruptRecoveryTime);
647 * Tests hot standby when there is only one PDP.
651 public void testHotStandby1() throws Exception {
653 PolicyLogger.debug("\n\ntestHotStandby1: Entering\n\n");
657 PolicyLogger.debug("testHotStandby1: Reading IntegrityMonitorProperties");
658 Properties integrityMonitorProperties = new Properties();
659 integrityMonitorProperties.load(new FileInputStream(new File(
660 "src/test/server/config/IntegrityMonitor.properties")));
661 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
662 String thisPdpId = IntegrityMonitorProperties
663 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
665 PolicyLogger.debug("testHotStandby1: Reading xacmlPersistenceProperties");
666 Properties xacmlPersistenceProperties = new Properties();
667 xacmlPersistenceProperties.load(new FileInputStream(new File(
668 "src/test/server/config/xacmlPersistence.properties")));
669 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
671 PolicyLogger.debug("testHotStandby1: Creating emfXacml");
672 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
673 "junitXacmlPU", xacmlPersistenceProperties);
675 PolicyLogger.debug("testHotStandby1: Reading droolsPersistenceProperties");
676 Properties droolsPersistenceProperties = new Properties();
677 droolsPersistenceProperties.load(new FileInputStream(new File(
678 "src/test/server/config/droolsPersistence.properties")));
679 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
681 PolicyLogger.debug("testHotStandby1: Creating emfDrools");
682 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
683 "junitDroolsPU", droolsPersistenceProperties);
685 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
687 PolicyLogger.debug("testHotStandby1: Cleaning up tables");
688 conn.deleteAllSessions();
689 conn.deleteAllPdps();
692 * Insert this PDP as not designated. Initial standby state will be
693 * either null or cold standby. Demoting should transit state to
696 PolicyLogger.debug("testHotStandby1: Inserting PDP=" + thisPdpId + " as not designated");
697 Date yesterday = DateUtils.addDays(new Date(), -1);
698 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
700 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
701 PolicyLogger.debug("testHotStandby1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
702 + droolsPdpEntity.isDesignated());
703 assertTrue(droolsPdpEntity.isDesignated() == false);
705 PolicyLogger.debug("testHotStandby1: Instantiating stateManagement object");
706 StateManagement sm = new StateManagement(emfXacml, "dummy");
707 sm.deleteAllStateManagementEntities();
708 sm = new StateManagement(emfXacml, thisPdpId);
709 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
710 sm.addObserver(pmStandbyStateChangeNotifier);
712 PolicyLogger.debug("testHotStandby1: Demoting PDP=" + thisPdpId);
713 // demoting should cause state to transit to hotstandby
716 PolicyLogger.debug("testHotStandby1: Running policy-management.Main class");
717 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
718 policyManagementRunner.start();
720 PolicyLogger.debug("testHotStandby1: Sleeping "
722 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
723 Thread.sleep(sleepTime);
726 * Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
728 droolsPdpEntity = conn.getPdp(thisPdpId);
729 PolicyLogger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED="
730 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
731 assertTrue(droolsPdpEntity.isDesignated() == true);
732 String standbyStatus = sm.getStandbyStatus(thisPdpId);
733 PolicyLogger.debug("testHotStandby1: After demotion, PDP=" + thisPdpId + " has standbyStatus="
735 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
737 PolicyLogger.debug("testHotStandby1: Stopping policyManagementRunner");
738 //policyManagementRunner.stopRunner();
740 PolicyLogger.debug("\n\ntestHotStandby1: Exiting\n\n");
741 Thread.sleep(interruptRecoveryTime);
746 * Tests hot standby when two PDPs are involved.
750 public void testHotStandby2() throws Exception {
752 PolicyLogger.info("\n\ntestHotStandby2: Entering\n\n");
756 PolicyLogger.info("testHotStandby2: Reading IntegrityMonitorProperties");
757 Properties integrityMonitorProperties = new Properties();
758 integrityMonitorProperties.load(new FileInputStream(new File(
759 "src/test/server/config/IntegrityMonitor.properties")));
760 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
761 String thisPdpId = IntegrityMonitorProperties
762 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
764 PolicyLogger.info("testHotStandby2: Reading xacmlPersistenceProperties");
765 Properties xacmlPersistenceProperties = new Properties();
766 xacmlPersistenceProperties.load(new FileInputStream(new File(
767 "src/test/server/config/xacmlPersistence.properties")));
768 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
770 PolicyLogger.info("testHotStandby2: Creating emfXacml");
771 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
772 "junitXacmlPU", xacmlPersistenceProperties);
774 PolicyLogger.info("testHotStandby2: Reading droolsPersistenceProperties");
775 Properties droolsPersistenceProperties = new Properties();
776 droolsPersistenceProperties.load(new FileInputStream(new File(
777 "src/test/server/config/droolsPersistence.properties")));
778 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
780 PolicyLogger.info("testHotStandby2: Creating emfDrools");
781 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
782 "junitDroolsPU", droolsPersistenceProperties);
784 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
786 PolicyLogger.info("testHotStandby2: Cleaning up tables");
787 conn.deleteAllSessions();
788 conn.deleteAllPdps();
791 * Insert a PDP that's designated but not current.
793 String activePdpId = "pdp2";
794 PolicyLogger.info("testHotStandby2: Inserting PDP=" + activePdpId + " as stale, designated PDP");
795 Date yesterday = DateUtils.addDays(new Date(), -1);
796 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
798 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
799 PolicyLogger.info("testHotStandby2: After insertion, PDP=" + activePdpId + ", which is not current, has DESIGNATED="
800 + droolsPdpEntity.isDesignated());
801 assertTrue(droolsPdpEntity.isDesignated() == true);
804 * Promote the designated PDP.
806 * We have a chicken and egg problem here: we need a StateManagement
807 * object to invoke the deleteAllStateManagementEntities method.
809 PolicyLogger.info("testHotStandy2: Promoting PDP=" + activePdpId);
810 StateManagement sm = new StateManagement(emfXacml, "dummy");
811 sm.deleteAllStateManagementEntities();
812 sm = new StateManagement(emfXacml, activePdpId);//pdp2
813 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
814 sm.addObserver(pmStandbyStateChangeNotifier);
816 // Artificially putting a PDP into service is really a two step process, 1)
817 // inserting it as designated and 2) promoting it so that its standbyStatus
818 // is providing service.
821 * Insert this PDP as not designated. Initial standby state will be
822 * either null or cold standby. Demoting should transit state to
825 PolicyLogger.info("testHotStandby2: Inserting PDP=" + thisPdpId + " as not designated");
826 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
828 droolsPdpEntity = conn.getPdp(thisPdpId);
829 PolicyLogger.info("testHotStandby2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
830 + droolsPdpEntity.isDesignated());
831 assertTrue(droolsPdpEntity.isDesignated() == false);
833 PolicyLogger.info("testHotStandby2: Demoting PDP=" + thisPdpId);//pdp1
834 StateManagement sm2 = new StateManagement(emfXacml, thisPdpId);
835 sm2.addObserver(pmStandbyStateChangeNotifier);
837 PolicyLogger.info("testHotStandby2: Running policy-management.Main class");
838 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner(); //pdp1
839 policyManagementRunner.start();
841 PolicyLogger.info("testHotStandby2: Runner started; Sleeping "
842 + interruptRecoveryTime + "ms before promoting/demoting");
843 Thread.sleep(interruptRecoveryTime);
845 PolicyLogger.info("testHotStandby2: Runner started; promoting PDP=" + activePdpId);//pdpd2xs
846 //at this point, the newly created pdp will have set the state to disabled/failed/cold standby
847 //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
848 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
849 sm.enableNotFailed();//pdp1
851 String standbyStatus = sm.getStandbyStatus(activePdpId);
852 PolicyLogger.info("testHotStandby2: After promoting, PDP=" + activePdpId + " has standbyStatus="
855 // demoting PDP should ensure that state transits to hotstandby
856 PolicyLogger.info("testHotStandby2: Runner started; demoting PDP=" + thisPdpId);
858 standbyStatus = sm.getStandbyStatus(thisPdpId);
859 PolicyLogger.info("testHotStandby2: After demoting, PDP=" + thisPdpId + " has standbyStatus="
862 PolicyLogger.info("testHotStandby2: Sleeping "
864 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
865 Thread.sleep(sleepTime);
868 * Verify that this PDP, demoted to HOT_STANDBY, is now
869 * re-designated and providing service.
871 droolsPdpEntity = conn.getPdp(thisPdpId);
872 PolicyLogger.info("testHotStandby2: After demoting PDP=" + activePdpId
873 + ", DESIGNATED=" + droolsPdpEntity.isDesignated()
874 + " for PDP=" + thisPdpId);
875 assertTrue(droolsPdpEntity.isDesignated() == true);
876 standbyStatus = sm2.getStandbyStatus(thisPdpId);
877 PolicyLogger.info("testHotStandby2: After demoting PDP=" + activePdpId
878 + ", PDP=" + thisPdpId + " has standbyStatus=" + standbyStatus);
879 assertTrue(standbyStatus != null
880 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
882 PolicyLogger.info("testHotStandby2: Stopping policyManagementRunner");
883 //policyManagementRunner.stopRunner();
885 PolicyLogger.info("\n\ntestHotStandby2: Exiting\n\n");
886 Thread.sleep(interruptRecoveryTime);
891 * 1) Inserts and designates this PDP, then verifies that startTransaction
894 * 2) Demotes PDP, and verifies that because there is only one PDP, it will
895 * be immediately re-promoted, thus allowing startTransaction to be
898 * 3) Locks PDP and verifies that startTransaction results in
899 * AdministrativeStateException.
901 * 4) Unlocks PDP and verifies that startTransaction results in
902 * StandbyStatusException.
904 * 5) Promotes PDP and verifies that startTransaction is once again
909 public void testLocking1() throws Exception {
910 PolicyLogger.debug("testLocking1: Entry");
914 PolicyLogger.debug("testLocking1: Reading IntegrityMonitorProperties");
915 Properties integrityMonitorProperties = new Properties();
916 integrityMonitorProperties.load(new FileInputStream(new File(
917 "src/test/server/config/IntegrityMonitor.properties")));
918 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
919 String thisPdpId = IntegrityMonitorProperties
920 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
922 PolicyLogger.debug("testLocking1: Reading xacmlPersistenceProperties");
923 Properties xacmlPersistenceProperties = new Properties();
924 xacmlPersistenceProperties.load(new FileInputStream(new File(
925 "src/test/server/config/xacmlPersistence.properties")));
926 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
928 PolicyLogger.debug("testLocking1: Creating emfXacml");
929 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
930 "junitXacmlPU", xacmlPersistenceProperties);
932 PolicyLogger.debug("testLocking1: Reading droolsPersistenceProperties");
933 Properties droolsPersistenceProperties = new Properties();
934 droolsPersistenceProperties.load(new FileInputStream(new File(
935 "src/test/server/config/droolsPersistence.properties")));
936 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
938 PolicyLogger.debug("testLocking1: Creating emfDrools");
939 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
940 "junitDroolsPU", droolsPersistenceProperties);
942 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
944 PolicyLogger.debug("testLocking1: Cleaning up tables");
945 conn.deleteAllSessions();
946 conn.deleteAllPdps();
949 * Insert this PDP as designated. Initial standby state will be
950 * either null or cold standby.
952 PolicyLogger.debug("testLocking1: Inserting PDP=" + thisPdpId + " as designated");
953 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
955 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
956 PolicyLogger.debug("testLocking1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
957 + droolsPdpEntity.isDesignated());
958 assertTrue(droolsPdpEntity.isDesignated() == true);
960 PolicyLogger.debug("testLocking1: Instantiating stateManagement object");
961 StateManagement sm = new StateManagement(emfXacml, "dummy");
962 sm.deleteAllStateManagementEntities();
963 sm = new StateManagement(emfXacml, thisPdpId);
964 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
965 sm.addObserver(pmStandbyStateChangeNotifier);
967 PolicyLogger.debug("testLocking1: Running policy-management.Main class, designated="
968 + conn.getPdp(thisPdpId).isDesignated());
969 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
970 policyManagementRunner.start();
972 PolicyLogger.debug("testLocking1: Runner started; Sleeping "
973 + interruptRecoveryTime + "ms before promoting PDP="
975 Thread.sleep(interruptRecoveryTime);
977 PolicyLogger.debug("testLocking1: Promoting PDP=" + thisPdpId);
980 PolicyLogger.debug("testLocking1: Sleeping "
982 + "ms, to allow time for policy-management.Main class to come up, designated="
983 + conn.getPdp(thisPdpId).isDesignated());
984 Thread.sleep(sleepTime);
986 PolicyLogger.debug("testLocking1: Waking up and invoking startTransaction on active PDP="
989 + conn.getPdp(thisPdpId).isDesignated());
990 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
993 droolsPdpIntegrityMonitor.startTransaction();
994 droolsPdpIntegrityMonitor.endTransaction();
995 PolicyLogger.debug("testLocking1: As expected, transaction successful");
996 } catch (AdministrativeStateException e) {
997 PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
999 } catch (StandbyStatusException e) {
1000 PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1002 } catch (Exception e) {
1003 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1007 // demoting should cause state to transit to hotstandby, followed by re-promotion,
1008 // since there is only one PDP.
1009 PolicyLogger.debug("testLocking1: demoting PDP=" + thisPdpId);
1010 sm = droolsPdpIntegrityMonitor.getStateManager();
1013 PolicyLogger.debug("testLocking1: sleeping" + electionWaitSleepTime
1014 + " to allow election handler to re-promote PDP=" + thisPdpId);
1015 Thread.sleep(electionWaitSleepTime);
1017 PolicyLogger.debug("testLocking1: Invoking startTransaction on re-promoted PDP="
1020 + conn.getPdp(thisPdpId).isDesignated());
1022 droolsPdpIntegrityMonitor.startTransaction();
1023 droolsPdpIntegrityMonitor.endTransaction();
1024 PolicyLogger.debug("testLocking1: As expected, transaction successful");
1025 } catch (AdministrativeStateException e) {
1026 PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1028 } catch (StandbyStatusException e) {
1029 PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1031 } catch (Exception e) {
1032 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1036 // locking should cause state to transit to cold standby
1037 PolicyLogger.debug("testLocking1: locking PDP=" + thisPdpId);
1040 // Just to avoid any race conditions, sleep a little after locking
1041 PolicyLogger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
1044 PolicyLogger.debug("testLocking1: Invoking startTransaction on locked PDP="
1047 + conn.getPdp(thisPdpId).isDesignated());
1049 droolsPdpIntegrityMonitor.startTransaction();
1050 PolicyLogger.error("testLocking1: startTransaction unexpectedly successful");
1052 } catch (AdministrativeStateException e) {
1053 PolicyLogger.debug("testLocking1: As expected, caught AdministrativeStateException, message=" + e.getMessage());
1054 } catch (StandbyStatusException e) {
1055 PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1057 } catch (Exception e) {
1058 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1061 droolsPdpIntegrityMonitor.endTransaction();
1064 // unlocking should cause state to transit to hot standby and then providing service
1065 PolicyLogger.debug("testLocking1: unlocking PDP=" + thisPdpId);
1068 // Just to avoid any race conditions, sleep a little after locking
1069 PolicyLogger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1070 Thread.sleep(electionWaitSleepTime);
1072 PolicyLogger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1075 + conn.getPdp(thisPdpId).isDesignated());
1077 droolsPdpIntegrityMonitor.startTransaction();
1078 PolicyLogger.error("testLocking1: startTransaction successful as expected");
1079 } catch (AdministrativeStateException e) {
1080 PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1082 } catch (StandbyStatusException e) {
1083 PolicyLogger.debug("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1085 } catch (Exception e) {
1086 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1089 droolsPdpIntegrityMonitor.endTransaction();
1092 // demoting should cause state to transit to providing service
1093 PolicyLogger.debug("testLocking1: demoting PDP=" + thisPdpId);
1096 // Just to avoid any race conditions, sleep a little after promoting
1097 PolicyLogger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
1100 PolicyLogger.debug("testLocking1: Invoking startTransaction on demoted PDP="
1103 + conn.getPdp(thisPdpId).isDesignated());
1105 droolsPdpIntegrityMonitor.startTransaction();
1106 droolsPdpIntegrityMonitor.endTransaction();
1107 PolicyLogger.debug("testLocking1: Unexpectedly, transaction successful");
1109 } catch (AdministrativeStateException e) {
1110 PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1112 } catch (StandbyStatusException e) {
1113 PolicyLogger.error("testLocking1: As expected caught StandbyStatusException, message=" + e.getMessage());
1114 } catch (Exception e) {
1115 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1119 PolicyLogger.debug("testLocking1: Stopping policyManagementRunner");
1120 //policyManagementRunner.stopRunner();
1122 PolicyLogger.debug("\n\ntestLocking1: Exiting\n\n");
1123 Thread.sleep(interruptRecoveryTime);
1128 * 1) Inserts and designates this PDP, then verifies that startTransaction
1131 * 2) Inserts another PDP in hotstandby.
1133 * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1134 * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1138 public void testLocking2() throws Exception {
1140 PolicyLogger.debug("\n\ntestLocking2: Entering\n\n");
1144 PolicyLogger.debug("testLocking2: Reading IntegrityMonitorProperties");
1145 Properties integrityMonitorProperties = new Properties();
1146 integrityMonitorProperties.load(new FileInputStream(new File(
1147 "src/test/server/config/IntegrityMonitor.properties")));
1148 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
1149 String thisPdpId = IntegrityMonitorProperties
1150 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
1152 PolicyLogger.debug("testLocking2: Reading xacmlPersistenceProperties");
1153 Properties xacmlPersistenceProperties = new Properties();
1154 xacmlPersistenceProperties.load(new FileInputStream(new File(
1155 "src/test/server/config/xacmlPersistence.properties")));
1156 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
1158 PolicyLogger.debug("testLocking2: Creating emfXacml");
1159 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1160 "junitXacmlPU", xacmlPersistenceProperties);
1162 PolicyLogger.debug("testLocking2: Reading droolsPersistenceProperties");
1163 Properties droolsPersistenceProperties = new Properties();
1164 droolsPersistenceProperties.load(new FileInputStream(new File(
1165 "src/test/server/config/droolsPersistence.properties")));
1166 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
1168 PolicyLogger.debug("testLocking2: Creating emfDrools");
1169 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1170 "junitDroolsPU", droolsPersistenceProperties);
1172 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1174 PolicyLogger.debug("testLocking2: Cleaning up tables");
1175 conn.deleteAllSessions();
1176 conn.deleteAllPdps();
1179 * Insert this PDP as designated. Initial standby state will be
1180 * either null or cold standby. Demoting should transit state to
1183 PolicyLogger.debug("testLocking2: Inserting PDP=" + thisPdpId + " as designated");
1184 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
1185 conn.insertPdp(pdp);
1186 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1187 PolicyLogger.debug("testLocking2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
1188 + droolsPdpEntity.isDesignated());
1189 assertTrue(droolsPdpEntity.isDesignated() == true);
1191 PolicyLogger.debug("testLocking2: Instantiating stateManagement object and promoting PDP=" + thisPdpId);
1192 StateManagement sm = new StateManagement(emfXacml, "dummy");
1193 sm.deleteAllStateManagementEntities();
1194 sm = new StateManagement(emfXacml, thisPdpId);
1195 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
1196 sm.addObserver(pmStandbyStateChangeNotifier);
1199 * Insert another PDP as not designated. Initial standby state will be
1200 * either null or cold standby. Demoting should transit state to
1203 String standbyPdpId = "pdp2";
1204 PolicyLogger.debug("testLocking2: Inserting PDP=" + standbyPdpId + " as not designated");
1205 Date yesterday = DateUtils.addDays(new Date(), -1);
1206 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1207 conn.insertPdp(pdp);
1208 droolsPdpEntity = conn.getPdp(standbyPdpId);
1209 PolicyLogger.debug("testLocking2: After insertion, PDP=" + standbyPdpId + " has DESIGNATED="
1210 + droolsPdpEntity.isDesignated());
1211 assertTrue(droolsPdpEntity.isDesignated() == false);
1213 PolicyLogger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
1214 StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
1215 sm2.addObserver(pmStandbyStateChangeNotifier);
1217 PolicyLogger.debug("testLocking2: Running policy-management.Main class");
1218 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
1219 policyManagementRunner.start();
1221 PolicyLogger.debug("testLocking2: Runner started; Sleeping "
1222 + interruptRecoveryTime + "ms before promoting/demoting");
1223 Thread.sleep(interruptRecoveryTime);
1225 PolicyLogger.debug("testLocking2: Promoting PDP=" + thisPdpId);
1228 // demoting PDP should ensure that state transits to hotstandby
1229 PolicyLogger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
1232 PolicyLogger.debug("testLocking2: Sleeping "
1234 + "ms, to allow time for policy-management.Main class to come up");
1235 Thread.sleep(sleepTime);
1237 PolicyLogger.debug("testLocking2: Waking up and invoking startTransaction on active PDP="
1240 + conn.getPdp(thisPdpId).isDesignated());
1241 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
1244 droolsPdpIntegrityMonitor.startTransaction();
1245 droolsPdpIntegrityMonitor.endTransaction();
1246 PolicyLogger.debug("testLocking2: As expected, transaction successful");
1247 } catch (AdministrativeStateException e) {
1248 PolicyLogger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1250 } catch (StandbyStatusException e) {
1251 PolicyLogger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1253 } catch (Exception e) {
1254 PolicyLogger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
1258 // demoting should cause state to transit to hotstandby followed by re-promotion.
1259 PolicyLogger.debug("testLocking2: demoting PDP=" + thisPdpId);
1260 sm = droolsPdpIntegrityMonitor.getStateManager();
1263 PolicyLogger.debug("testLocking2: sleeping" + electionWaitSleepTime
1264 + " to allow election handler to re-promote PDP=" + thisPdpId);
1265 Thread.sleep(electionWaitSleepTime);
1267 PolicyLogger.debug("testLocking2: Waking up and invoking startTransaction on re-promoted PDP="
1268 + thisPdpId + ", designated="
1269 + conn.getPdp(thisPdpId).isDesignated());
1271 droolsPdpIntegrityMonitor.startTransaction();
1272 droolsPdpIntegrityMonitor.endTransaction();
1273 PolicyLogger.debug("testLocking2: As expected, transaction successful");
1274 } catch (AdministrativeStateException e) {
1275 PolicyLogger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1277 } catch (StandbyStatusException e) {
1278 PolicyLogger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1280 } catch (Exception e) {
1281 PolicyLogger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
1285 PolicyLogger.debug("testLocking2: Verifying designated status for PDP="
1287 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
1288 assertTrue(standbyPdpDesignated == false);
1290 PolicyLogger.debug("testLocking2: Stopping policyManagementRunner");
1291 //policyManagementRunner.stopRunner();
1293 PolicyLogger.debug("\n\ntestLocking2: Exiting\n\n");
1294 Thread.sleep(interruptRecoveryTime);
1298 private class PolicyManagementRunner extends Thread {
1301 PolicyLogger.info("PolicyManagementRunner.run: Entering");
1302 String args[] = { "src/main/server/config" };
1305 } catch (Exception e) {
1307 .info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
1311 PolicyLogger.info("PolicyManagementRunner.run: Exiting");
1314 public void stopRunner() {
1315 PolicyEngine.manager.shutdown();