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.Date;
28 import java.util.Properties;
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.Persistence;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Ignore;
38 import org.junit.Test;
40 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
41 import org.openecomp.policy.common.im.AdministrativeStateException;
42 import org.openecomp.policy.common.im.IntegrityMonitor;
43 import org.openecomp.policy.common.im.StandbyStatusException;
44 import org.openecomp.policy.common.im.StateManagement;
45 import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
46 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
47 import org.openecomp.policy.drools.core.PolicyContainer;
48 import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
49 import org.openecomp.policy.drools.persistence.DroolsPdp;
50 import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
51 import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
52 import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
53 import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
54 import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
55 import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
56 import org.openecomp.policy.drools.system.Main;
57 import org.openecomp.policy.drools.system.PolicyEngine;
59 import org.apache.commons.lang3.time.DateUtils;
62 * Cloned from StandbyStateManagement.java in support of US673632.
63 * See MultiSite_v1-10.ppt, slide 38
65 public class StandbyStateManagementTest {
68 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every ten seconds, starting
69 * at ten seconds after the minute boundary (e.g. 13:05:10). So, an 80 second sleep should be
70 * sufficient to ensure that we wait for the DesignationWaiter to do its job, before
71 * checking the results.
73 long sleepTime = 80000;
76 * DroolsPdpsElectionHandler runs every ten seconds, so a 15 second sleep should be
77 * plenty to ensure it has time to re-promote this PDP.
79 long electionWaitSleepTime = 15000;
82 * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
84 long interruptRecoveryTime = 5000;
87 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
90 public static void setUpClass() throws Exception {
92 String userDir = System.getProperty("user.dir");
93 PolicyLogger.debug("setUpClass: userDir=" + userDir);
94 System.setProperty("com.sun.management.jmxremote.port", "9980");
95 System.setProperty("com.sun.management.jmxremote.authenticate","false");
97 // Make sure path to config directory is set correctly in PolicyContainer.main
98 // Also make sure we ignore HTTP server failures resulting from port conflicts.
99 PolicyContainer.isUnitTesting = true;
104 public static void tearDownClass() throws Exception {
108 public void setUp() throws Exception {
112 public void tearDown() throws Exception {
118 public void testColdStandby() throws Exception {
120 PolicyLogger.debug("\n\ntestColdStandby: Entering\n\n");
122 PolicyLogger.debug("testColdStandby: Reading IntegrityMonitorProperties");
123 Properties integrityMonitorProperties = new Properties();
124 integrityMonitorProperties.load(new FileInputStream(new File(
125 "src/test/server/config/IntegrityMonitor.properties")));
126 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
127 String thisPdpId = IntegrityMonitorProperties
128 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
130 PolicyLogger.debug("testColdStandby: Reading xacmlPersistenceProperties");
131 Properties xacmlPersistenceProperties = new Properties();
132 xacmlPersistenceProperties.load(new FileInputStream(new File(
133 "src/test/server/config/xacmlPersistence.properties")));
134 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
136 PolicyLogger.debug("testColdStandby: Creating emfXacml");
137 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
138 "junitXacmlPU", xacmlPersistenceProperties);
140 PolicyLogger.debug("testColdStandby: Reading droolsPersistenceProperties");
141 Properties droolsPersistenceProperties = new Properties();
142 droolsPersistenceProperties.load(new FileInputStream(new File(
143 "src/test/server/config/droolsPersistence.properties")));
144 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
146 PolicyLogger.debug("testColdStandby: Creating emfDrools");
147 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
148 "junitDroolsPU", droolsPersistenceProperties);
150 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
152 PolicyLogger.debug("testColdStandby: Cleaning up tables");
153 conn.deleteAllSessions();
154 conn.deleteAllPdps();
156 PolicyLogger.debug("testColdStandby: Inserting PDP=" + thisPdpId + " as designated");
157 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
159 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
160 PolicyLogger.debug("testColdStandby: After insertion, DESIGNATED="
161 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
162 assertTrue(droolsPdpEntity.isDesignated() == true);
165 * When the Standby Status changes (from providingservice) to hotstandby
166 * or coldstandby,the Active/Standby selection algorithm must stand down
167 * if thePDP-D is currently the lead/active node and allow another PDP-D
170 * It must also call lock on all engines in the engine management.
172 * Yes, this is kludgy, but we have a chicken and egg problem here: we
173 * need a StateManagement object to invoke the
174 * deleteAllStateManagementEntities method.
176 PolicyLogger.debug("testColdStandby: Instantiating stateManagement object");
177 StateManagement sm = new StateManagement(emfXacml, "dummy");
178 sm.deleteAllStateManagementEntities();
179 sm = new StateManagement(emfXacml, thisPdpId);
180 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
181 sm.addObserver(pmStandbyStateChangeNotifier);
183 // Artificially putting a PDP into service is really a two step process, 1)
184 // inserting it as designated and 2) promoting it so that its standbyStatus
185 // is providing service.
187 PolicyLogger.debug("testColdStandby: Running policy-management.Main class");
188 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
189 policyManagementRunner.start();
191 PolicyLogger.debug("testColdStandby: Runner started; Sleeping "
192 + interruptRecoveryTime + "ms before promoting PDP="
194 Thread.sleep(interruptRecoveryTime);
196 PolicyLogger.debug("testColdStandby: Promoting PDP=" + thisPdpId);
199 String standbyStatus = sm.getStandbyStatus(thisPdpId);
200 PolicyLogger.debug("testColdStandby: Before locking, PDP=" + thisPdpId + " has standbyStatus="
203 PolicyLogger.debug("testColdStandby: Locking sm");
206 Thread.sleep(interruptRecoveryTime);
208 * Verify that the PDP is no longer designated.
210 droolsPdpEntity = conn.getPdp(thisPdpId);
211 PolicyLogger.debug("testColdStandby: After lock sm.lock() invoked, DESIGNATED="
212 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
213 assertTrue(droolsPdpEntity.isDesignated() == false);
215 PolicyLogger.debug("testColdStandby: Stopping policyManagementRunner");
216 policyManagementRunner.stopRunner();
218 PolicyLogger.debug("\n\ntestColdStandby: Exiting\n\n");
219 Thread.sleep(interruptRecoveryTime);
224 * Tests hot standby when there is only one PDP.
228 public void testHotStandby1() throws Exception {
230 PolicyLogger.debug("\n\ntestHotStandby1: Entering\n\n");
232 PolicyLogger.debug("testHotStandby1: Reading IntegrityMonitorProperties");
233 Properties integrityMonitorProperties = new Properties();
234 integrityMonitorProperties.load(new FileInputStream(new File(
235 "src/test/server/config/IntegrityMonitor.properties")));
236 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
237 String thisPdpId = IntegrityMonitorProperties
238 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
240 PolicyLogger.debug("testHotStandby1: Reading xacmlPersistenceProperties");
241 Properties xacmlPersistenceProperties = new Properties();
242 xacmlPersistenceProperties.load(new FileInputStream(new File(
243 "src/test/server/config/xacmlPersistence.properties")));
244 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
246 PolicyLogger.debug("testHotStandby1: Creating emfXacml");
247 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
248 "junitXacmlPU", xacmlPersistenceProperties);
250 PolicyLogger.debug("testHotStandby1: Reading droolsPersistenceProperties");
251 Properties droolsPersistenceProperties = new Properties();
252 droolsPersistenceProperties.load(new FileInputStream(new File(
253 "src/test/server/config/droolsPersistence.properties")));
254 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
256 PolicyLogger.debug("testHotStandby1: Creating emfDrools");
257 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
258 "junitDroolsPU", droolsPersistenceProperties);
260 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
262 PolicyLogger.debug("testHotStandby1: Cleaning up tables");
263 conn.deleteAllSessions();
264 conn.deleteAllPdps();
267 * Insert this PDP as not designated. Initial standby state will be
268 * either null or cold standby. Demoting should transit state to
271 PolicyLogger.debug("testHotStandby1: Inserting PDP=" + thisPdpId + " as not designated");
272 Date yesterday = DateUtils.addDays(new Date(), -1);
273 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
275 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
276 PolicyLogger.debug("testHotStandby1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
277 + droolsPdpEntity.isDesignated());
278 assertTrue(droolsPdpEntity.isDesignated() == false);
280 PolicyLogger.debug("testHotStandby1: Instantiating stateManagement object");
281 StateManagement sm = new StateManagement(emfXacml, "dummy");
282 sm.deleteAllStateManagementEntities();
283 sm = new StateManagement(emfXacml, thisPdpId);
284 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
285 sm.addObserver(pmStandbyStateChangeNotifier);
287 PolicyLogger.debug("testHotStandby1: Demoting PDP=" + thisPdpId);
288 // demoting should cause state to transit to hotstandby
291 PolicyLogger.debug("testHotStandby1: Running policy-management.Main class");
292 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
293 policyManagementRunner.start();
295 PolicyLogger.debug("testHotStandby1: Sleeping "
297 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
298 Thread.sleep(sleepTime);
301 * Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
303 droolsPdpEntity = conn.getPdp(thisPdpId);
304 PolicyLogger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED="
305 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
306 assertTrue(droolsPdpEntity.isDesignated() == true);
307 String standbyStatus = sm.getStandbyStatus(thisPdpId);
308 PolicyLogger.debug("testHotStandby1: After demotion, PDP=" + thisPdpId + " has standbyStatus="
310 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
312 PolicyLogger.debug("testHotStandby1: Stopping policyManagementRunner");
313 policyManagementRunner.stopRunner();
315 PolicyLogger.debug("\n\ntestHotStandby1: Exiting\n\n");
316 Thread.sleep(interruptRecoveryTime);
321 * Tests hot standby when two PDPs are involved.
325 public void testHotStandby2() throws Exception {
327 PolicyLogger.info("\n\ntestHotStandby2: Entering\n\n");
329 PolicyLogger.info("testHotStandby2: Reading IntegrityMonitorProperties");
330 Properties integrityMonitorProperties = new Properties();
331 integrityMonitorProperties.load(new FileInputStream(new File(
332 "src/test/server/config/IntegrityMonitor.properties")));
333 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
334 String thisPdpId = IntegrityMonitorProperties
335 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
337 PolicyLogger.info("testHotStandby2: Reading xacmlPersistenceProperties");
338 Properties xacmlPersistenceProperties = new Properties();
339 xacmlPersistenceProperties.load(new FileInputStream(new File(
340 "src/test/server/config/xacmlPersistence.properties")));
341 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
343 PolicyLogger.info("testHotStandby2: Creating emfXacml");
344 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
345 "junitXacmlPU", xacmlPersistenceProperties);
347 PolicyLogger.info("testHotStandby2: Reading droolsPersistenceProperties");
348 Properties droolsPersistenceProperties = new Properties();
349 droolsPersistenceProperties.load(new FileInputStream(new File(
350 "src/test/server/config/droolsPersistence.properties")));
351 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
353 PolicyLogger.info("testHotStandby2: Creating emfDrools");
354 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
355 "junitDroolsPU", droolsPersistenceProperties);
357 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
359 PolicyLogger.info("testHotStandby2: Cleaning up tables");
360 conn.deleteAllSessions();
361 conn.deleteAllPdps();
364 * Insert a PDP that's designated but not current.
366 String activePdpId = "pdp2";
367 PolicyLogger.info("testHotStandby2: Inserting PDP=" + activePdpId + " as stale, designated PDP");
368 Date yesterday = DateUtils.addDays(new Date(), -1);
369 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
371 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
372 PolicyLogger.info("testHotStandby2: After insertion, PDP=" + activePdpId + ", which is not current, has DESIGNATED="
373 + droolsPdpEntity.isDesignated());
374 assertTrue(droolsPdpEntity.isDesignated() == true);
377 * Promote the designated PDP.
379 * We have a chicken and egg problem here: we need a StateManagement
380 * object to invoke the deleteAllStateManagementEntities method.
382 PolicyLogger.info("testHotStandy2: Promoting PDP=" + activePdpId);
383 StateManagement sm = new StateManagement(emfXacml, "dummy");
384 sm.deleteAllStateManagementEntities();
385 sm = new StateManagement(emfXacml, activePdpId);//pdp2
386 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
387 sm.addObserver(pmStandbyStateChangeNotifier);
389 // Artificially putting a PDP into service is really a two step process, 1)
390 // inserting it as designated and 2) promoting it so that its standbyStatus
391 // is providing service.
394 * Insert this PDP as not designated. Initial standby state will be
395 * either null or cold standby. Demoting should transit state to
398 PolicyLogger.info("testHotStandby2: Inserting PDP=" + thisPdpId + " as not designated");
399 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
401 droolsPdpEntity = conn.getPdp(thisPdpId);
402 PolicyLogger.info("testHotStandby2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
403 + droolsPdpEntity.isDesignated());
404 assertTrue(droolsPdpEntity.isDesignated() == false);
406 PolicyLogger.info("testHotStandby2: Demoting PDP=" + thisPdpId);//pdp1
407 StateManagement sm2 = new StateManagement(emfXacml, thisPdpId);
408 sm2.addObserver(pmStandbyStateChangeNotifier);
410 PolicyLogger.info("testHotStandby2: Running policy-management.Main class");
411 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner(); //pdp1
412 policyManagementRunner.start();
414 PolicyLogger.info("testHotStandby2: Runner started; Sleeping "
415 + interruptRecoveryTime + "ms before promoting/demoting");
416 Thread.sleep(interruptRecoveryTime);
418 PolicyLogger.info("testHotStandby2: Runner started; promoting PDP=" + activePdpId);//pdpd2xs
419 //at this point, the newly created pdp will have set the state to disabled/failed/cold standby
420 //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
421 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
422 sm.enableNotFailed();//pdp1
424 String standbyStatus = sm.getStandbyStatus(activePdpId);
425 PolicyLogger.info("testHotStandby2: After promoting, PDP=" + activePdpId + " has standbyStatus="
428 // demoting PDP should ensure that state transits to hotstandby
429 PolicyLogger.info("testHotStandby2: Runner started; demoting PDP=" + thisPdpId);
431 standbyStatus = sm.getStandbyStatus(thisPdpId);
432 PolicyLogger.info("testHotStandby2: After demoting, PDP=" + thisPdpId + " has standbyStatus="
435 PolicyLogger.info("testHotStandby2: Sleeping "
437 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
438 Thread.sleep(sleepTime);
441 * Verify that this PDP, demoted to HOT_STANDBY, is now
442 * re-designated and providing service.
444 droolsPdpEntity = conn.getPdp(thisPdpId);
445 PolicyLogger.info("testHotStandby2: After demoting PDP=" + activePdpId
446 + ", DESIGNATED=" + droolsPdpEntity.isDesignated()
447 + " for PDP=" + thisPdpId);
448 assertTrue(droolsPdpEntity.isDesignated() == true);
449 standbyStatus = sm2.getStandbyStatus(thisPdpId);
450 PolicyLogger.info("testHotStandby2: After demoting PDP=" + activePdpId
451 + ", PDP=" + thisPdpId + " has standbyStatus=" + standbyStatus);
452 assertTrue(standbyStatus != null
453 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
455 PolicyLogger.info("testHotStandby2: Stopping policyManagementRunner");
456 policyManagementRunner.stopRunner();
458 PolicyLogger.info("\n\ntestHotStandby2: Exiting\n\n");
459 Thread.sleep(interruptRecoveryTime);
464 * 1) Inserts and designates this PDP, then verifies that startTransaction
467 * 2) Demotes PDP, and verifies that because there is only one PDP, it will
468 * be immediately re-promoted, thus allowing startTransaction to be
471 * 3) Locks PDP and verifies that startTransaction results in
472 * AdministrativeStateException.
474 * 4) Unlocks PDP and verifies that startTransaction results in
475 * StandbyStatusException.
477 * 5) Promotes PDP and verifies that startTransaction is once again
482 public void testLocking1() throws Exception {
484 PolicyLogger.debug("testLocking1: Reading IntegrityMonitorProperties");
485 Properties integrityMonitorProperties = new Properties();
486 integrityMonitorProperties.load(new FileInputStream(new File(
487 "src/test/server/config/IntegrityMonitor.properties")));
488 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
489 String thisPdpId = IntegrityMonitorProperties
490 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
492 PolicyLogger.debug("testLocking1: Reading xacmlPersistenceProperties");
493 Properties xacmlPersistenceProperties = new Properties();
494 xacmlPersistenceProperties.load(new FileInputStream(new File(
495 "src/test/server/config/xacmlPersistence.properties")));
496 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
498 PolicyLogger.debug("testLocking1: Creating emfXacml");
499 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
500 "junitXacmlPU", xacmlPersistenceProperties);
502 PolicyLogger.debug("testLocking1: Reading droolsPersistenceProperties");
503 Properties droolsPersistenceProperties = new Properties();
504 droolsPersistenceProperties.load(new FileInputStream(new File(
505 "src/test/server/config/droolsPersistence.properties")));
506 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
508 PolicyLogger.debug("testLocking1: Creating emfDrools");
509 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
510 "junitDroolsPU", droolsPersistenceProperties);
512 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
514 PolicyLogger.debug("testLocking1: Cleaning up tables");
515 conn.deleteAllSessions();
516 conn.deleteAllPdps();
519 * Insert this PDP as designated. Initial standby state will be
520 * either null or cold standby.
522 PolicyLogger.debug("testLocking1: Inserting PDP=" + thisPdpId + " as designated");
523 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
525 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
526 PolicyLogger.debug("testLocking1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
527 + droolsPdpEntity.isDesignated());
528 assertTrue(droolsPdpEntity.isDesignated() == true);
530 PolicyLogger.debug("testLocking1: Instantiating stateManagement object");
531 StateManagement sm = new StateManagement(emfXacml, "dummy");
532 sm.deleteAllStateManagementEntities();
533 sm = new StateManagement(emfXacml, thisPdpId);
534 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
535 sm.addObserver(pmStandbyStateChangeNotifier);
537 PolicyLogger.debug("testLocking1: Running policy-management.Main class, designated="
538 + conn.getPdp(thisPdpId).isDesignated());
539 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
540 policyManagementRunner.start();
542 PolicyLogger.debug("testLocking1: Runner started; Sleeping "
543 + interruptRecoveryTime + "ms before promoting PDP="
545 Thread.sleep(interruptRecoveryTime);
547 PolicyLogger.debug("testLocking1: Promoting PDP=" + thisPdpId);
550 PolicyLogger.debug("testLocking1: Sleeping "
552 + "ms, to allow time for policy-management.Main class to come up, designated="
553 + conn.getPdp(thisPdpId).isDesignated());
554 Thread.sleep(sleepTime);
556 PolicyLogger.debug("testLocking1: Waking up and invoking startTransaction on active PDP="
559 + conn.getPdp(thisPdpId).isDesignated());
560 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
563 droolsPdpIntegrityMonitor.startTransaction();
564 droolsPdpIntegrityMonitor.endTransaction();
565 PolicyLogger.debug("testLocking1: As expected, transaction successful");
566 } catch (AdministrativeStateException e) {
567 PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
569 } catch (StandbyStatusException e) {
570 PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
572 } catch (Exception e) {
573 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
577 // demoting should cause state to transit to hotstandby, followed by re-promotion,
578 // since there is only one PDP.
579 PolicyLogger.debug("testLocking1: demoting PDP=" + thisPdpId);
580 sm = droolsPdpIntegrityMonitor.getStateManager();
583 PolicyLogger.debug("testLocking1: sleeping" + electionWaitSleepTime
584 + " to allow election handler to re-promote PDP=" + thisPdpId);
585 Thread.sleep(electionWaitSleepTime);
587 PolicyLogger.debug("testLocking1: Invoking startTransaction on re-promoted PDP="
590 + conn.getPdp(thisPdpId).isDesignated());
592 droolsPdpIntegrityMonitor.startTransaction();
593 droolsPdpIntegrityMonitor.endTransaction();
594 PolicyLogger.debug("testLocking1: As expected, transaction successful");
595 } catch (AdministrativeStateException e) {
596 PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
598 } catch (StandbyStatusException e) {
599 PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
601 } catch (Exception e) {
602 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
606 // locking should cause state to transit to cold standby
607 PolicyLogger.debug("testLocking1: locking PDP=" + thisPdpId);
610 // Just to avoid any race conditions, sleep a little after locking
611 PolicyLogger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
614 PolicyLogger.debug("testLocking1: Invoking startTransaction on locked PDP="
617 + conn.getPdp(thisPdpId).isDesignated());
619 droolsPdpIntegrityMonitor.startTransaction();
620 PolicyLogger.error("testLocking1: startTransaction unexpectedly successful");
622 } catch (AdministrativeStateException e) {
623 PolicyLogger.debug("testLocking1: As expected, caught AdministrativeStateException, message=" + e.getMessage());
624 } catch (StandbyStatusException e) {
625 PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
627 } catch (Exception e) {
628 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
631 droolsPdpIntegrityMonitor.endTransaction();
634 // unlocking should cause state to transit to hot standby
635 PolicyLogger.debug("testLocking1: unlocking PDP=" + thisPdpId);
638 // Just to avoid any race conditions, sleep a little after locking
639 PolicyLogger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
642 PolicyLogger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
645 + conn.getPdp(thisPdpId).isDesignated());
647 droolsPdpIntegrityMonitor.startTransaction();
648 PolicyLogger.error("testLocking1: startTransaction unexpectedly successful");
650 } catch (AdministrativeStateException e) {
651 PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
653 } catch (StandbyStatusException e) {
654 PolicyLogger.debug("testLocking1: As expected, caught StandbyStatusException, message=" + e.getMessage());
655 } catch (Exception e) {
656 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
659 droolsPdpIntegrityMonitor.endTransaction();
662 // promoting should cause state to transit to providing service
663 PolicyLogger.debug("testLocking1: promoting PDP=" + thisPdpId);
666 // Just to avoid any race conditions, sleep a little after promoting
667 PolicyLogger.debug("testLocking1: Sleeping a few millis after promoting, to avoid race condition");
670 PolicyLogger.debug("testLocking1: Invoking startTransaction on promoted PDP="
673 + conn.getPdp(thisPdpId).isDesignated());
675 droolsPdpIntegrityMonitor.startTransaction();
676 droolsPdpIntegrityMonitor.endTransaction();
677 PolicyLogger.debug("testLocking1: As expected, transaction successful");
678 } catch (AdministrativeStateException e) {
679 PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
681 } catch (StandbyStatusException e) {
682 PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
684 } catch (Exception e) {
685 PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
689 PolicyLogger.debug("testLocking1: Stopping policyManagementRunner");
690 policyManagementRunner.stopRunner();
692 PolicyLogger.debug("\n\ntestLocking1: Exiting\n\n");
693 Thread.sleep(interruptRecoveryTime);
698 * 1) Inserts and designates this PDP, then verifies that startTransaction
701 * 2) Inserts another PDP in hotstandby.
703 * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
704 * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
708 public void testLocking2() throws Exception {
710 PolicyLogger.debug("\n\ntestLocking2: Entering\n\n");
712 PolicyLogger.debug("testLocking2: Reading IntegrityMonitorProperties");
713 Properties integrityMonitorProperties = new Properties();
714 integrityMonitorProperties.load(new FileInputStream(new File(
715 "src/test/server/config/IntegrityMonitor.properties")));
716 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
717 String thisPdpId = IntegrityMonitorProperties
718 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
720 PolicyLogger.debug("testLocking2: Reading xacmlPersistenceProperties");
721 Properties xacmlPersistenceProperties = new Properties();
722 xacmlPersistenceProperties.load(new FileInputStream(new File(
723 "src/test/server/config/xacmlPersistence.properties")));
724 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
726 PolicyLogger.debug("testLocking2: Creating emfXacml");
727 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
728 "junitXacmlPU", xacmlPersistenceProperties);
730 PolicyLogger.debug("testLocking2: Reading droolsPersistenceProperties");
731 Properties droolsPersistenceProperties = new Properties();
732 droolsPersistenceProperties.load(new FileInputStream(new File(
733 "src/test/server/config/droolsPersistence.properties")));
734 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
736 PolicyLogger.debug("testLocking2: Creating emfDrools");
737 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
738 "junitDroolsPU", droolsPersistenceProperties);
740 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
742 PolicyLogger.debug("testLocking2: Cleaning up tables");
743 conn.deleteAllSessions();
744 conn.deleteAllPdps();
747 * Insert this PDP as designated. Initial standby state will be
748 * either null or cold standby. Demoting should transit state to
751 PolicyLogger.debug("testLocking2: Inserting PDP=" + thisPdpId + " as designated");
752 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
754 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
755 PolicyLogger.debug("testLocking2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
756 + droolsPdpEntity.isDesignated());
757 assertTrue(droolsPdpEntity.isDesignated() == true);
759 PolicyLogger.debug("testLocking2: Instantiating stateManagement object and promoting PDP=" + thisPdpId);
760 StateManagement sm = new StateManagement(emfXacml, "dummy");
761 sm.deleteAllStateManagementEntities();
762 sm = new StateManagement(emfXacml, thisPdpId);
763 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
764 sm.addObserver(pmStandbyStateChangeNotifier);
767 * Insert another PDP as not designated. Initial standby state will be
768 * either null or cold standby. Demoting should transit state to
771 String standbyPdpId = "pdp2";
772 PolicyLogger.debug("testLocking2: Inserting PDP=" + standbyPdpId + " as not designated");
773 Date yesterday = DateUtils.addDays(new Date(), -1);
774 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
776 droolsPdpEntity = conn.getPdp(standbyPdpId);
777 PolicyLogger.debug("testLocking2: After insertion, PDP=" + standbyPdpId + " has DESIGNATED="
778 + droolsPdpEntity.isDesignated());
779 assertTrue(droolsPdpEntity.isDesignated() == false);
781 PolicyLogger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
782 StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
783 sm2.addObserver(pmStandbyStateChangeNotifier);
785 PolicyLogger.debug("testLocking2: Running policy-management.Main class");
786 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
787 policyManagementRunner.start();
789 PolicyLogger.debug("testLocking2: Runner started; Sleeping "
790 + interruptRecoveryTime + "ms before promoting/demoting");
791 Thread.sleep(interruptRecoveryTime);
793 PolicyLogger.debug("testLocking2: Promoting PDP=" + thisPdpId);
796 // demoting PDP should ensure that state transits to hotstandby
797 PolicyLogger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
800 PolicyLogger.debug("testLocking2: Sleeping "
802 + "ms, to allow time for policy-management.Main class to come up");
803 Thread.sleep(sleepTime);
805 PolicyLogger.debug("testLocking2: Waking up and invoking startTransaction on active PDP="
808 + conn.getPdp(thisPdpId).isDesignated());
809 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
812 droolsPdpIntegrityMonitor.startTransaction();
813 droolsPdpIntegrityMonitor.endTransaction();
814 PolicyLogger.debug("testLocking2: As expected, transaction successful");
815 } catch (AdministrativeStateException e) {
816 PolicyLogger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
818 } catch (StandbyStatusException e) {
819 PolicyLogger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
821 } catch (Exception e) {
822 PolicyLogger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
826 // demoting should cause state to transit to hotstandby followed by re-promotion.
827 PolicyLogger.debug("testLocking2: demoting PDP=" + thisPdpId);
828 sm = droolsPdpIntegrityMonitor.getStateManager();
831 PolicyLogger.debug("testLocking2: sleeping" + electionWaitSleepTime
832 + " to allow election handler to re-promote PDP=" + thisPdpId);
833 Thread.sleep(electionWaitSleepTime);
835 PolicyLogger.debug("testLocking2: Waking up and invoking startTransaction on re-promoted PDP="
836 + thisPdpId + ", designated="
837 + conn.getPdp(thisPdpId).isDesignated());
839 droolsPdpIntegrityMonitor.startTransaction();
840 droolsPdpIntegrityMonitor.endTransaction();
841 PolicyLogger.debug("testLocking2: As expected, transaction successful");
842 } catch (AdministrativeStateException e) {
843 PolicyLogger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
845 } catch (StandbyStatusException e) {
846 PolicyLogger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
848 } catch (Exception e) {
849 PolicyLogger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
853 PolicyLogger.debug("testLocking2: Verifying designated status for PDP="
855 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
856 assertTrue(standbyPdpDesignated == false);
858 PolicyLogger.debug("testLocking2: Stopping policyManagementRunner");
859 policyManagementRunner.stopRunner();
861 PolicyLogger.debug("\n\ntestLocking2: Exiting\n\n");
862 Thread.sleep(interruptRecoveryTime);
866 private class PolicyManagementRunner extends Thread {
869 PolicyLogger.info("PolicyManagementRunner.run: Entering");
870 String args[] = { "src/main/server/config" };
873 } catch (Exception e) {
875 .info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
878 PolicyLogger.info("PolicyManagementRunner.run: Exiting");
881 public void stopRunner() {
882 PolicyEngine.manager.shutdown();