2 * ============LICENSE_START=======================================================
3 * feature-active-standby-management
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.onap.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.List;
30 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.Test;
42 import org.onap.policy.common.im.AdministrativeStateException;
43 import org.onap.policy.common.im.IntegrityMonitor;
44 import org.onap.policy.common.im.StandbyStatusException;
45 import org.onap.policy.common.im.StateManagement;
46 import org.onap.policy.drools.activestandby.ActiveStandbyFeatureApi;
47 import org.onap.policy.drools.activestandby.ActiveStandbyProperties;
48 import org.onap.policy.drools.activestandby.DroolsPdp;
49 import org.onap.policy.drools.activestandby.DroolsPdpEntity;
50 import org.onap.policy.drools.activestandby.DroolsPdpImpl;
51 import org.onap.policy.drools.activestandby.DroolsPdpsConnector;
52 import org.onap.policy.drools.activestandby.DroolsPdpsElectionHandler;
53 import org.onap.policy.drools.activestandby.JpaDroolsPdpsConnector;
54 import org.onap.policy.drools.activestandby.PMStandbyStateChangeNotifier;
55 import org.onap.policy.drools.core.PolicySessionFeatureApi;
56 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * All JUnits are designed to run in the local development environment
62 * where they have write privileges and can execute time-sensitive
65 * These tests can be run as JUnits, but there is some issue with running them
66 * as part of a "mvn install" build. Also, they take a very long time to run
67 * due to many real time breaks. Consequently, they are marked as @Ignore and
68 * only run from the desktop.
71 public class StandbyStateManagementTest {
72 private static final Logger logger = LoggerFactory.getLogger(StandbyStateManagementTest.class);
74 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
75 * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
76 * to ensure that we wait for the DesignationWaiter to do its job, before
77 * checking the results. Add a few seconds for safety
80 long sleepTime = 10000;
83 * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be
84 * plenty to ensure it has time to re-promote this PDP.
87 long electionWaitSleepTime = 6000;
90 * Sleep 1 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;
101 private final String configDir = "src/test/resources";
104 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
110 * @throws Exception exception
113 public static void setUpClass() throws Exception {
115 String userDir = System.getProperty("user.dir");
116 logger.debug("setUpClass: userDir={}", userDir);
117 System.setProperty("com.sun.management.jmxremote.port", "9980");
118 System.setProperty("com.sun.management.jmxremote.authenticate","false");
123 public static void tearDownClass() throws Exception {
129 * @throws Exception exception
132 public void setUp() throws Exception {
133 //Create teh data access for xaml db
134 Properties stateManagementProperties = new Properties();
135 stateManagementProperties.load(new FileInputStream(new File(
136 "src/test/resources/feature-state-management.properties")));
138 emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
140 // Create an entity manager to use the DB
141 emx = emfx.createEntityManager();
143 //Create the data access for drools db
144 Properties activeStandbyProperties = new Properties();
145 activeStandbyProperties.load(new FileInputStream(new File(
146 "src/test/resources/feature-active-standby-management.properties")));
148 emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
150 // Create an entity manager to use the DB
151 emd = emfd.createEntityManager();
155 public void tearDown() throws Exception {
160 * Clean up the xacml database.
163 public void cleanXacmlDb() {
164 et = emx.getTransaction();
167 // Make sure we leave the DB clean
168 emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
169 emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
170 emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
176 * Clean up the drools db.
178 public void cleanDroolsDb() {
179 et = emd.getTransaction();
182 // Make sure we leave the DB clean
183 emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
189 * These JUnit tests must be run one at a time in an eclipse environment
190 * by right-clicking StandbyStateManagementTest and selecting
191 * "Run As" -> "JUnit Test".
193 * They will run successfully when you run all of them under runAllTests(),
194 * however, you will get all sorts of non-fatal errors in the log and on the
195 * console that result from overlapping threads that are not terminated at the
196 * end of each test. The problem is that the JUnit environment does not terminate
197 * all the test threads between tests. This is true even if you break each JUnit
198 * into a separate file. Consequently, all the tests would have to be refactored
199 * so all test object initializations are coordinated. In other words, you
200 * retrieve the ActiveStandbyFeature instance and other class instances only once
201 * at the beginning of the JUnits and then reuse them throughout the tests.
202 * Initialization of the state of the objects is pretty straight forward as it
203 * just amounts to manipulating the entries in StateManagementEntity and
204 * DroolsPdpEntity tables. However, some thought needs to be given to how to
205 * "pause" the processing in ActiveStandbyFeature class. I think we could "pause"
206 * it by calling globalInit() which will, I think, restart it. So long as it
207 * does not create a new instance, it will force it to go through an initialization
208 * cycle which includes a "pause" at the beginning of proecessing. We just must
209 * be sure it does not create another instance - which may mean we need to add
210 * a factory interface instead of calling the constructor directly.
216 public void runAllTests() throws Exception {
222 testPmStandbyStateChangeNotifier();
223 testSanitizeDesignatedList();
224 testComputeMostRecentPrimary();
225 testComputeDesignatedPdp();
229 * Test the standby state change notifier.
231 * @throws Exception exception
235 public void testPmStandbyStateChangeNotifier() throws Exception {
236 logger.debug("\n\ntestPMStandbyStateChangeNotifier: Entering\n\n");
239 logger.debug("testPMStandbyStateChangeNotifier: Reading activeStandbyProperties");
241 Properties activeStandbyProperties = new Properties();
242 activeStandbyProperties.load(new FileInputStream(new File(
243 configDir + "/feature-active-standby-management.properties")));
245 String resourceName = "testPMS";
246 activeStandbyProperties.setProperty("resource.name", resourceName);
247 ActiveStandbyProperties.initProperties(activeStandbyProperties);
249 logger.debug("testPMStandbyStateChangeNotifier: Getting StateManagement instance");
251 StateManagement sm = new StateManagement(emfx, resourceName);
253 //Create an instance of the Observer
254 PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
256 //Register the PMStandbyStateChangeNotifier Observer
257 sm.addObserver(pmNotifier);
259 //At this point the standbystatus = 'null'
261 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
264 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
266 //Adding standbystatus=hotstandby
268 System.out.println(pmNotifier.getPreviousStandbyStatus());
269 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
270 PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
272 //Now making standbystatus=coldstandby
274 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
275 PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
277 //standbystatus = hotstandby
279 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
280 PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
282 //standbystatus = providingservice
284 //The previousStandbyStatus is not updated until after the delay activation expires
285 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
286 PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
288 //Sleep long enough for the delayActivationTimer to run
290 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
292 //standbystatus = providingservice
294 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
296 //standbystatus = coldstandby
298 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
299 PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
301 //standbystatus = hotstandby
303 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
304 PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
306 //standbystatus = hotstandby
308 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
309 PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
313 * Test sanitize designated list.
315 * @throws Exception exception
319 public void testSanitizeDesignatedList() throws Exception {
321 logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
323 // Get a DroolsPdpsConnector
325 logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties");
326 Properties activeStandbyProperties = new Properties();
327 activeStandbyProperties.load(new FileInputStream(new File(
328 configDir + "/feature-active-standby-management.properties")));
329 String thisPdpId = activeStandbyProperties
330 .getProperty(ActiveStandbyProperties.NODE_NAME);
332 logger.debug("testSanitizeDesignatedList: Creating emfDrools");
333 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
334 "junitDroolsPU", activeStandbyProperties);
336 final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
338 // Create 4 pdpd all not designated
340 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
341 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
342 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
343 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
345 List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
346 listOfDesignated.add(pdp1);
347 listOfDesignated.add(pdp2);
348 listOfDesignated.add(pdp3);
349 listOfDesignated.add(pdp4);
351 // Now we want to create a StateManagementFeature and initialize it. It will be
352 // discovered by the ActiveStandbyFeature when the election handler initializes.
354 StateManagementFeatureApi stateManagementFeature = null;
355 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
356 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
357 stateManagementFeature = feature;
358 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}",
359 stateManagementFeature.getResourceName());
362 if (stateManagementFeature == null) {
363 logger.error("testColdStandby failed to initialize. "
364 + "Unable to get instance of StateManagementFeatureApi "
365 + "with resourceID: {}", thisPdpId);
366 logger.debug("testColdStandby failed to initialize. "
367 + "Unable to get instance of StateManagementFeatureApi "
368 + "with resourceID: {}", thisPdpId);
372 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
374 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
376 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
378 assertTrue(listOfDesignated.size() == 4);
380 // Now make 2 designated
382 pdp1.setDesignated(true);
383 pdp2.setDesignated(true);
385 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
387 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n",
388 listOfDesignated.size());
390 assertTrue(listOfDesignated.size() == 2);
391 assertTrue(listOfDesignated.contains(pdp1));
392 assertTrue(listOfDesignated.contains(pdp2));
395 // Now all are designated. But, we have to add back the previously non-designated nodes
397 pdp3.setDesignated(true);
398 pdp4.setDesignated(true);
399 listOfDesignated.add(pdp3);
400 listOfDesignated.add(pdp4);
402 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
404 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n",
405 listOfDesignated.size());
407 assertTrue(listOfDesignated.size() == 4);
412 * Test Compute most recent primary.
414 * @throws Exception exception
418 public void testComputeMostRecentPrimary() throws Exception {
420 logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
422 logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties");
423 Properties activeStandbyProperties = new Properties();
424 activeStandbyProperties.load(new FileInputStream(new File(
425 configDir + "/feature-active-standby-management.properties")));
426 String thisPdpId = activeStandbyProperties
427 .getProperty(ActiveStandbyProperties.NODE_NAME);
429 logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
430 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
431 "junitDroolsPU", activeStandbyProperties);
433 final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
436 // Create 4 pdpd all not designated
439 long designatedDateMs = new Date().getTime();
440 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
441 pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
443 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
445 pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
447 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
448 pdp3.setDesignatedDate(new Date(designatedDateMs - 1));
450 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
452 pdp4.setDesignatedDate(new Date(designatedDateMs));
454 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
455 listOfAllPdps.add(pdp1);
456 listOfAllPdps.add(pdp2);
457 listOfAllPdps.add(pdp3);
458 listOfAllPdps.add(pdp4);
461 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
462 listOfDesignated.add(pdp1);
463 listOfDesignated.add(pdp2);
464 listOfDesignated.add(pdp3);
465 listOfDesignated.add(pdp4);
467 // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
468 // or all designated members.
470 // Now we want to create a StateManagementFeature and initialize it. It will be
471 // discovered by the ActiveStandbyFeature when the election handler initializes.
473 StateManagementFeatureApi stateManagementFeature = null;
474 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
475 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
476 stateManagementFeature = feature;
477 logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}",
478 stateManagementFeature.getResourceName());
481 if (stateManagementFeature == null) {
482 logger.error("testComputeMostRecentPrimary failed to initialize. "
483 + "Unable to get instance of StateManagementFeatureApi "
484 + "with resourceID: {}", thisPdpId);
485 logger.debug("testComputeMostRecentPrimary failed to initialize. "
486 + "Unable to get instance of StateManagementFeatureApi "
487 + "with resourceID: {}", thisPdpId);
490 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
492 DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(
493 listOfAllPdps, listOfDesignated);
495 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
496 mostRecentPrimary.getPdpId());
499 // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
500 // the one which has the most recent designated date.
503 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
506 // Now let's designate all of those on the listOfDesignated. It will choose the first one designated
509 pdp1.setDesignated(true);
510 pdp2.setDesignated(true);
511 pdp3.setDesignated(true);
512 pdp4.setDesignated(true);
514 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
516 logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, "
517 + "mostRecentPrimary.getPdpId() = {}\n\n",
518 mostRecentPrimary.getPdpId());
521 // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
522 // the one which was designated first
525 assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
528 // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
529 // look for the most recently designated pdp which is not currently designated.
532 pdp3.setDesignated(false);
533 pdp4.setDesignated(false);
535 listOfDesignated.remove(pdp3);
536 listOfDesignated.remove(pdp4);
538 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
540 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
541 mostRecentPrimary.getPdpId());
543 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
547 // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
548 // look for the most recently designated pdp regardless of whether it is currently marked as designated.
551 pdp1.setDesignated(false);
552 pdp2.setDesignated(false);
554 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
556 logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n",
557 mostRecentPrimary.getPdpId());
559 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
562 // If we have only one pdp on in the listOfDesignated,
563 // the most recently designated pdp will be chosen, regardless
564 // of its designation status
567 listOfDesignated.remove(pdp1);
569 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
571 logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n",
572 mostRecentPrimary.getPdpId());
574 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
577 // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
580 listOfDesignated.remove(pdp2);
582 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
584 logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n",
585 mostRecentPrimary.getPdpId());
587 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
592 * Test compute designated PDP.
594 * @throws Exception exception
598 public void testComputeDesignatedPdp() throws Exception {
600 logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
602 logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties");
603 Properties activeStandbyProperties = new Properties();
604 activeStandbyProperties.load(new FileInputStream(new File(
605 configDir + "/feature-active-standby-management.properties")));
606 String thisPdpId = activeStandbyProperties
607 .getProperty(ActiveStandbyProperties.NODE_NAME);
610 logger.debug("testComputeDesignatedPdp: Creating emfDrools");
611 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
612 "junitDroolsPU", activeStandbyProperties);
614 final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
617 // Create 4 pdpd all not designated. Two on site1. Two on site2
620 long designatedDateMs = new Date().getTime();
621 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
622 pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
623 pdp1.setSiteName("site1");
625 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
626 pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
627 pdp2.setSiteName("site1");
630 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
631 pdp3.setDesignatedDate(new Date(designatedDateMs - 4));
632 pdp3.setSiteName("site2");
634 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
636 pdp4.setDesignatedDate(new Date(designatedDateMs));
637 pdp4.setSiteName("site2");
639 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
640 listOfAllPdps.add(pdp1);
641 listOfAllPdps.add(pdp2);
642 listOfAllPdps.add(pdp3);
643 listOfAllPdps.add(pdp4);
646 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
649 // We will first test an empty listOfDesignated. As we know from the previous JUnit,
650 // the pdp with the most designated date will be chosen for mostRecentPrimary
652 // Now we want to create a StateManagementFeature and initialize it. It will be
653 // discovered by the ActiveStandbyFeature when the election handler initializes.
655 StateManagementFeatureApi stateManagementFeature = null;
656 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
657 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
658 stateManagementFeature = feature;
659 logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}",
660 stateManagementFeature.getResourceName());
663 if (stateManagementFeature == null) {
664 logger.error("testComputeDesignatedPdp failed to initialize. "
665 + "Unable to get instance of StateManagementFeatureApi "
666 + "with resourceID: {}", thisPdpId);
667 logger.debug("testComputeDesignatedPdp failed to initialize. "
668 + "Unable to get instance of StateManagementFeatureApi "
669 + "with resourceID: {}", thisPdpId);
673 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
675 DroolsPdp mostRecentPrimary = pdp4;
677 DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
680 // The designatedPdp should be null
682 assertTrue(designatedPdp == null);
685 // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
687 listOfDesignated.add(pdp2);
689 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
692 // Now the designatedPdp should be the one and only selection in the listOfDesignated
695 assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
698 // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
701 listOfDesignated.add(pdp1);
703 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
706 // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
709 assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
712 // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
715 listOfDesignated.remove(pdp1);
716 listOfDesignated.add(pdp3);
718 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
721 // The designatedPdp should now be the one on the same site as the mostRecentPrimary
724 assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
730 * @throws Exception exception
734 public void testColdStandby() throws Exception {
736 logger.debug("\n\ntestColdStandby: Entering\n\n");
740 logger.debug("testColdStandby: Reading stateManagementProperties");
741 Properties stateManagementProperties = new Properties();
742 stateManagementProperties.load(new FileInputStream(new File(
743 configDir + "/feature-state-management.properties")));
745 logger.debug("testColdStandby: Creating emfXacml");
746 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
747 "junitXacmlPU", stateManagementProperties);
749 logger.debug("testColdStandby: Reading activeStandbyProperties");
750 Properties activeStandbyProperties = new Properties();
751 activeStandbyProperties.load(new FileInputStream(new File(
752 configDir + "/feature-active-standby-management.properties")));
753 final String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
755 logger.debug("testColdStandby: Creating emfDrools");
756 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
757 "junitDroolsPU", activeStandbyProperties);
759 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
761 logger.debug("testColdStandby: Cleaning up tables");
762 conn.deleteAllPdps();
764 logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
765 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
767 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
768 logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
769 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
770 assertTrue(droolsPdpEntity.isDesignated() == true);
773 * When the Standby Status changes (from providingservice) to hotstandby
774 * or coldstandby,the Active/Standby selection algorithm must stand down
775 * if thePDP-D is currently the lead/active node and allow another PDP-D
778 * It must also call lock on all engines in the engine management.
783 * Yes, this is kludgy, but we have a chicken and egg problem here: we
784 * need a StateManagement object to invoke the
785 * deleteAllStateManagementEntities method.
787 logger.debug("testColdStandby: Instantiating stateManagement object");
789 StateManagement sm = new StateManagement(emfXacml, "dummy");
790 sm.deleteAllStateManagementEntities();
792 // Now we want to create a StateManagementFeature and initialize it. It will be
793 // discovered by the ActiveStandbyFeature when the election handler initializes.
795 StateManagementFeatureApi smf = null;
796 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
797 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
799 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
803 logger.error("testColdStandby failed to initialize. "
804 + "Unable to get instance of StateManagementFeatureApi "
805 + "with resourceID: {}", thisPdpId);
806 logger.debug("testColdStandby failed to initialize. "
807 + "Unable to get instance of StateManagementFeatureApi "
808 + "with resourceID: {}", thisPdpId);
811 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
812 // that has been created.
813 ActiveStandbyFeatureApi activeStandbyFeature = null;
814 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApi.impl.getList()) {
815 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
816 activeStandbyFeature = feature;
817 logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}",
818 activeStandbyFeature.getResourceName());
821 if (activeStandbyFeature == null) {
822 logger.error("testColdStandby failed to initialize. "
823 + "Unable to get instance of ActiveStandbyFeatureAPI "
824 + "with resourceID:{}", thisPdpId);
825 logger.debug("testColdStandby failed to initialize. "
826 + "Unable to get instance of ActiveStandbyFeatureAPI "
827 + "with resourceID:{}", thisPdpId);
830 // Artificially putting a PDP into service is really a two step process, 1)
831 // inserting it as designated and 2) promoting it so that its standbyStatus
832 // is providing service.
834 logger.debug("testColdStandby: Runner started; Sleeping "
835 + interruptRecoveryTime + "ms before promoting PDP= {}",
837 sleep(interruptRecoveryTime);
839 logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
842 String standbyStatus = sm.getStandbyStatus(thisPdpId);
843 logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}",
844 thisPdpId, standbyStatus);
846 logger.debug("testColdStandby: Locking smf");
849 sleep(interruptRecoveryTime);
851 // Verify that the PDP is no longer designated.
853 droolsPdpEntity = conn.getPdp(thisPdpId);
854 logger.debug("testColdStandby: After lock sm.lock() invoked, "
855 + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
856 assertTrue(droolsPdpEntity.isDesignated() == false);
858 logger.debug("\n\ntestColdStandby: Exiting\n\n");
859 sleep(interruptRecoveryTime);
863 // Tests hot standby when there is only one PDP.
866 * Test hot standby 1.
868 * @throws Exception exception
872 public void testHotStandby1() throws Exception {
874 logger.debug("\n\ntestHotStandby1: Entering\n\n");
878 logger.debug("testHotStandby1: Reading stateManagementProperties");
879 Properties stateManagementProperties = new Properties();
880 stateManagementProperties.load(new FileInputStream(new File(
881 configDir + "/feature-state-management.properties")));
883 logger.debug("testHotStandby1: Creating emfXacml");
884 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
885 "junitXacmlPU", stateManagementProperties);
887 logger.debug("testHotStandby1: Reading activeStandbyProperties");
888 Properties activeStandbyProperties = new Properties();
889 activeStandbyProperties.load(new FileInputStream(new File(
890 configDir + "/feature-active-standby-management.properties")));
891 final String thisPdpId = activeStandbyProperties
892 .getProperty(ActiveStandbyProperties.NODE_NAME);
894 logger.debug("testHotStandby1: Creating emfDrools");
895 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
896 "junitDroolsPU", activeStandbyProperties);
898 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
900 logger.debug("testHotStandby1: Cleaning up tables");
901 conn.deleteAllPdps();
904 * Insert this PDP as not designated. Initial standby state will be
905 * either null or cold standby. Demoting should transit state to
909 logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
910 Date yesterday = DateUtils.addDays(new Date(), -1);
911 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
913 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
914 logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
915 thisPdpId, droolsPdpEntity.isDesignated());
916 assertTrue(droolsPdpEntity.isDesignated() == false);
918 logger.debug("testHotStandby1: Instantiating stateManagement object");
919 StateManagement sm = new StateManagement(emfXacml, "dummy");
920 sm.deleteAllStateManagementEntities();
923 // Now we want to create a StateManagementFeature and initialize it. It will be
924 // discovered by the ActiveStandbyFeature when the election handler initializes.
926 StateManagementFeatureApi smf = null;
927 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
928 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
930 logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
934 logger.error("testHotStandby1 failed to initialize. "
935 + "Unable to get instance of StateManagementFeatureApi "
936 + "with resourceID: {}", thisPdpId);
937 logger.debug("testHotStandby1 failed to initialize. "
938 + "Unable to get instance of StateManagementFeatureApi "
939 + "with resourceID: {}", thisPdpId);
942 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
943 // that has been created.
944 ActiveStandbyFeatureApi activeStandbyFeature = null;
945 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApi.impl.getList()) {
946 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
947 activeStandbyFeature = feature;
948 logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}",
949 activeStandbyFeature.getResourceName());
952 if (activeStandbyFeature == null) {
953 logger.error("testHotStandby1 failed to initialize. "
954 + "Unable to get instance of ActiveStandbyFeatureAPI "
955 + "with resourceID: {}", thisPdpId);
956 logger.debug("testHotStandby1 failed to initialize. "
957 + "Unable to get instance of ActiveStandbyFeatureAPI "
958 + "with resourceID: {}", thisPdpId);
962 logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
963 // demoting should cause state to transit to hotstandby
967 logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
968 + "time to check droolspdpentity table", sleepTime);
972 // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
974 droolsPdpEntity = conn.getPdp(thisPdpId);
975 logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
976 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
977 assertTrue(droolsPdpEntity.isDesignated() == true);
978 String standbyStatus = smf.getStandbyStatus(thisPdpId);
979 logger.debug("testHotStandby1: After demotion, PDP= {} "
980 + "has standbyStatus= {}", thisPdpId, standbyStatus);
981 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
983 logger.debug("testHotStandby1: Stopping policyManagementRunner");
985 logger.debug("\n\ntestHotStandby1: Exiting\n\n");
986 sleep(interruptRecoveryTime);
991 * Tests hot standby when two PDPs are involved.
995 * Test hot standby 2.
997 * @throws Exception exception
1001 public void testHotStandby2() throws Exception {
1003 logger.info("\n\ntestHotStandby2: Entering\n\n");
1007 logger.info("testHotStandby2: Reading stateManagementProperties");
1008 Properties stateManagementProperties = new Properties();
1009 stateManagementProperties.load(new FileInputStream(new File(
1010 configDir + "/feature-state-management.properties")));
1012 logger.info("testHotStandby2: Creating emfXacml");
1013 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1014 "junitXacmlPU", stateManagementProperties);
1016 logger.info("testHotStandby2: Reading activeStandbyProperties");
1017 Properties activeStandbyProperties = new Properties();
1018 activeStandbyProperties.load(new FileInputStream(new File(
1019 configDir + "/feature-active-standby-management.properties")));
1020 final String thisPdpId = activeStandbyProperties
1021 .getProperty(ActiveStandbyProperties.NODE_NAME);
1023 logger.info("testHotStandby2: Creating emfDrools");
1024 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1025 "junitDroolsPU", activeStandbyProperties);
1027 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1029 logger.info("testHotStandby2: Cleaning up tables");
1030 conn.deleteAllPdps();
1033 // Insert a PDP that's designated but not current.
1035 String activePdpId = "pdp2";
1036 logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
1037 Date yesterday = DateUtils.addDays(new Date(), -1);
1038 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
1039 conn.insertPdp(pdp);
1040 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
1041 logger.info("testHotStandby2: After insertion, PDP= {}, which is "
1042 + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
1043 assertTrue(droolsPdpEntity.isDesignated() == true);
1046 * Promote the designated PDP.
1048 * We have a chicken and egg problem here: we need a StateManagement
1049 * object to invoke the deleteAllStateManagementEntities method.
1053 logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
1054 StateManagement sm = new StateManagement(emfXacml, "dummy");
1055 sm.deleteAllStateManagementEntities();
1058 sm = new StateManagement(emfXacml, activePdpId);//pdp2
1060 // Artificially putting a PDP into service is really a two step process, 1)
1061 // inserting it as designated and 2) promoting it so that its standbyStatus
1062 // is providing service.
1065 * Insert this PDP as not designated. Initial standby state will be
1066 * either null or cold standby. Demoting should transit state to
1071 logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
1072 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
1073 conn.insertPdp(pdp);
1074 droolsPdpEntity = conn.getPdp(thisPdpId);
1075 logger.info("testHotStandby2: After insertion, PDP={} "
1076 + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
1077 assertTrue(droolsPdpEntity.isDesignated() == false);
1080 // Now we want to create a StateManagementFeature and initialize it. It will be
1081 // discovered by the ActiveStandbyFeature when the election handler initializes.
1083 StateManagementFeatureApi sm2 = null;
1084 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
1085 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1087 logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
1091 logger.error("testHotStandby2 failed to initialize. "
1092 + "Unable to get instance of StateManagementFeatureApi "
1093 + "with resourceID: {}", thisPdpId);
1094 logger.debug("testHotStandby2 failed to initialize. "
1095 + "Unable to get instance of StateManagementFeatureApi "
1096 + "with resourceID: {}", thisPdpId);
1099 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1100 // that has been created.
1101 ActiveStandbyFeatureApi activeStandbyFeature = null;
1102 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApi.impl.getList()) {
1103 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1104 activeStandbyFeature = feature;
1105 logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}",
1106 activeStandbyFeature.getResourceName());
1109 if (activeStandbyFeature == null) {
1110 logger.error("testHotStandby2 failed to initialize. "
1111 + "Unable to get instance of ActiveStandbyFeatureAPI "
1112 + "with resourceID: {}", thisPdpId);
1113 logger.debug("testHotStandby2 failed to initialize. "
1114 + "Unable to get instance of ActiveStandbyFeatureAPI "
1115 + "with resourceID: {}", thisPdpId);
1118 logger.info("testHotStandby2: Runner started; Sleeping {} "
1119 + "ms before promoting/demoting", interruptRecoveryTime);
1120 sleep(interruptRecoveryTime);
1122 logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
1123 //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
1124 //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
1125 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
1126 sm.enableNotFailed();//pdp2
1128 String standbyStatus = sm.getStandbyStatus(activePdpId);
1129 logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
1131 // demoting PDP should ensure that state transits to hotstandby
1132 logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
1134 standbyStatus = sm.getStandbyStatus(thisPdpId);
1135 logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
1137 logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
1138 + "time to check droolspdpentity table", sleepTime);
1142 * Verify that this PDP, demoted to HOT_STANDBY, is now
1143 * re-designated and providing service.
1146 droolsPdpEntity = conn.getPdp(thisPdpId);
1147 logger.info("testHotStandby2: After demoting PDP={}"
1148 + ", DESIGNATED= {}"
1149 + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
1150 assertTrue(droolsPdpEntity.isDesignated() == true);
1151 standbyStatus = sm2.getStandbyStatus(thisPdpId);
1152 logger.info("testHotStandby2: After demoting PDP={}"
1153 + ", PDP={} has standbyStatus= {}",
1154 activePdpId, thisPdpId, standbyStatus);
1155 assertTrue(standbyStatus != null
1156 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
1158 logger.info("testHotStandby2: Stopping policyManagementRunner");
1160 logger.info("\n\ntestHotStandby2: Exiting\n\n");
1161 sleep(interruptRecoveryTime);
1166 * 1) Inserts and designates this PDP, then verifies that startTransaction
1169 * 2) Demotes PDP, and verifies that because there is only one PDP, it will
1170 * be immediately re-promoted, thus allowing startTransaction to be
1173 * 3) Locks PDP and verifies that startTransaction results in
1174 * AdministrativeStateException.
1176 * 4) Unlocks PDP and verifies that startTransaction results in
1177 * StandbyStatusException.
1179 * 5) Promotes PDP and verifies that startTransaction is once again
1186 * @throws Exception exception
1190 public void testLocking1() throws Exception {
1191 logger.debug("testLocking1: Entry");
1195 logger.debug("testLocking1: Reading stateManagementProperties");
1196 Properties stateManagementProperties = new Properties();
1197 stateManagementProperties.load(new FileInputStream(new File(
1198 configDir + "/feature-state-management.properties")));
1200 logger.debug("testLocking1: Creating emfXacml");
1201 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1202 "junitXacmlPU", stateManagementProperties);
1204 logger.debug("testLocking1: Reading activeStandbyProperties");
1205 Properties activeStandbyProperties = new Properties();
1206 activeStandbyProperties.load(new FileInputStream(new File(
1207 configDir + "/feature-active-standby-management.properties")));
1208 final String thisPdpId = activeStandbyProperties
1209 .getProperty(ActiveStandbyProperties.NODE_NAME);
1211 logger.debug("testLocking1: Creating emfDrools");
1212 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1213 "junitDroolsPU", activeStandbyProperties);
1215 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1217 logger.debug("testLocking1: Cleaning up tables");
1218 conn.deleteAllPdps();
1221 * Insert this PDP as designated. Initial standby state will be
1222 * either null or cold standby.
1225 logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
1226 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
1227 conn.insertPdp(pdp);
1228 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1229 logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
1230 thisPdpId, droolsPdpEntity.isDesignated());
1231 assertTrue(droolsPdpEntity.isDesignated() == true);
1233 logger.debug("testLocking1: Instantiating stateManagement object");
1234 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1235 smDummy.deleteAllStateManagementEntities();
1237 // Now we want to create a StateManagementFeature and initialize it. It will be
1238 // discovered by the ActiveStandbyFeature when the election handler initializes.
1240 StateManagementFeatureApi sm = null;
1241 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
1242 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1244 logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1248 logger.error("testLocking1 failed to initialize. "
1249 + "Unable to get instance of StateManagementFeatureApi "
1250 + "with resourceID: {}", thisPdpId);
1251 logger.debug("testLocking1 failed to initialize. "
1252 + "Unable to get instance of StateManagementFeatureApi "
1253 + "with resourceID: {}", thisPdpId);
1256 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1257 // that has been created.
1258 ActiveStandbyFeatureApi activeStandbyFeature = null;
1259 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApi.impl.getList()) {
1260 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1261 activeStandbyFeature = feature;
1262 logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}",
1263 activeStandbyFeature.getResourceName());
1266 if (activeStandbyFeature == null) {
1267 logger.error("testLocking1 failed to initialize. "
1268 + "Unable to get instance of ActiveStandbyFeatureAPI "
1269 + "with resourceID: {}", thisPdpId);
1270 logger.debug("testLocking1 failed to initialize. "
1271 + "Unable to get instance of ActiveStandbyFeatureAPI "
1272 + "with resourceID: {}", thisPdpId);
1275 logger.debug("testLocking1: Runner started; Sleeping "
1276 + interruptRecoveryTime + "ms before promoting PDP={}",
1278 sleep(interruptRecoveryTime);
1280 logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
1283 logger.debug("testLocking1: Sleeping {} ms, to allow time for "
1284 + "policy-management.Main class to come up, designated= {}",
1285 sleepTime, conn.getPdp(thisPdpId).isDesignated());
1288 logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
1289 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1292 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1294 droolsPdpIntegrityMonitor.startTransaction();
1295 droolsPdpIntegrityMonitor.endTransaction();
1296 logger.debug("testLocking1: As expected, transaction successful");
1297 } catch (AdministrativeStateException e) {
1298 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1300 } catch (StandbyStatusException e) {
1301 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1303 } catch (Exception e) {
1304 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1308 // demoting should cause state to transit to hotstandby, followed by re-promotion,
1309 // since there is only one PDP.
1310 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1313 logger.debug("testLocking1: sleeping" + electionWaitSleepTime
1314 + " to allow election handler to re-promote PDP={}", thisPdpId);
1315 sleep(electionWaitSleepTime);
1317 logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
1318 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1320 droolsPdpIntegrityMonitor.startTransaction();
1321 droolsPdpIntegrityMonitor.endTransaction();
1322 logger.debug("testLocking1: As expected, transaction successful");
1323 } catch (AdministrativeStateException e) {
1324 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1326 } catch (StandbyStatusException e) {
1327 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1329 } catch (Exception e) {
1330 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1334 // locking should cause state to transit to cold standby
1335 logger.debug("testLocking1: locking PDP={}", thisPdpId);
1338 // Just to avoid any race conditions, sleep a little after locking
1339 logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
1342 logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
1343 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1345 droolsPdpIntegrityMonitor.startTransaction();
1346 logger.error("testLocking1: startTransaction unexpectedly successful");
1348 } catch (AdministrativeStateException e) {
1349 logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
1350 } catch (StandbyStatusException e) {
1351 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1353 } catch (Exception e) {
1354 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1357 droolsPdpIntegrityMonitor.endTransaction();
1360 // unlocking should cause state to transit to hot standby and then providing service
1361 logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
1364 // Just to avoid any race conditions, sleep a little after locking
1365 logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1366 sleep(electionWaitSleepTime);
1368 logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1371 + conn.getPdp(thisPdpId).isDesignated());
1373 droolsPdpIntegrityMonitor.startTransaction();
1374 logger.error("testLocking1: startTransaction successful as expected");
1375 } catch (AdministrativeStateException e) {
1376 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1378 } catch (StandbyStatusException e) {
1379 logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1381 } catch (Exception e) {
1382 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1385 droolsPdpIntegrityMonitor.endTransaction();
1388 // demoting should cause state to transit to hot standby
1389 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1392 // Just to avoid any race conditions, sleep a little after promoting
1393 logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
1396 logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
1397 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1399 droolsPdpIntegrityMonitor.startTransaction();
1400 droolsPdpIntegrityMonitor.endTransaction();
1401 logger.debug("testLocking1: Unexpectedly, transaction successful");
1403 } catch (AdministrativeStateException e) {
1404 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1406 } catch (StandbyStatusException e) {
1407 logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
1408 } catch (Exception e) {
1409 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1413 logger.debug("\n\ntestLocking1: Exiting\n\n");
1414 sleep(interruptRecoveryTime);
1420 * 1) Inserts and designates this PDP, then verifies that startTransaction
1423 * 2) Inserts another PDP in hotstandby.
1425 * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1426 * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1432 * @throws Exception exception
1436 public void testLocking2() throws Exception {
1438 logger.debug("\n\ntestLocking2: Entering\n\n");
1442 logger.debug("testLocking2: Reading stateManagementProperties");
1443 Properties stateManagementProperties = new Properties();
1444 stateManagementProperties.load(new FileInputStream(new File(
1445 configDir + "/feature-state-management.properties")));
1447 logger.debug("testLocking2: Creating emfXacml");
1448 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1449 "junitXacmlPU", stateManagementProperties);
1451 logger.debug("testLocking2: Reading activeStandbyProperties");
1452 Properties activeStandbyProperties = new Properties();
1453 activeStandbyProperties.load(new FileInputStream(new File(
1454 configDir + "/feature-active-standby-management.properties")));
1455 final String thisPdpId = activeStandbyProperties
1456 .getProperty(ActiveStandbyProperties.NODE_NAME);
1458 logger.debug("testLocking2: Creating emfDrools");
1459 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1460 "junitDroolsPU", activeStandbyProperties);
1462 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1464 logger.debug("testLocking2: Cleaning up tables");
1465 conn.deleteAllPdps();
1468 * Insert this PDP as designated. Initial standby state will be
1469 * either null or cold standby. Demoting should transit state to
1473 logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
1474 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
1475 conn.insertPdp(pdp);
1476 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1477 logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
1478 thisPdpId, droolsPdpEntity.isDesignated());
1479 assertTrue(droolsPdpEntity.isDesignated() == true);
1481 logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
1482 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1483 smDummy.deleteAllStateManagementEntities();
1485 // Now we want to create a StateManagementFeature and initialize it. It will be
1486 // discovered by the ActiveStandbyFeature when the election handler initializes.
1488 StateManagementFeatureApi sm = null;
1489 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
1490 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1492 logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1496 logger.error("testLocking2 failed to initialize. "
1497 + "Unable to get instance of StateManagementFeatureApi "
1498 + "with resourceID: {}", thisPdpId);
1499 logger.debug("testLocking2 failed to initialize. "
1500 + "Unable to get instance of StateManagementFeatureApi "
1501 + "with resourceID: {}", thisPdpId);
1504 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1505 // that has been created.
1506 ActiveStandbyFeatureApi activeStandbyFeature = null;
1507 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApi.impl.getList()) {
1508 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1509 activeStandbyFeature = feature;
1510 logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}",
1511 activeStandbyFeature.getResourceName());
1514 if (activeStandbyFeature == null) {
1515 logger.error("testLocking2 failed to initialize. "
1516 + "Unable to get instance of ActiveStandbyFeatureAPI "
1517 + "with resourceID: {}", thisPdpId);
1518 logger.debug("testLocking2 failed to initialize. "
1519 + "Unable to get instance of ActiveStandbyFeatureAPI "
1520 + "with resourceID: {}", thisPdpId);
1524 * Insert another PDP as not designated. Initial standby state will be
1525 * either null or cold standby. Demoting should transit state to
1529 String standbyPdpId = "pdp2";
1530 logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
1531 Date yesterday = DateUtils.addDays(new Date(), -1);
1532 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1533 conn.insertPdp(pdp);
1534 droolsPdpEntity = conn.getPdp(standbyPdpId);
1535 logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
1536 standbyPdpId, droolsPdpEntity.isDesignated());
1537 assertTrue(droolsPdpEntity.isDesignated() == false);
1539 logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
1540 final StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
1542 logger.debug("testLocking2: Runner started; Sleeping {} ms "
1543 + "before promoting/demoting", interruptRecoveryTime);
1544 sleep(interruptRecoveryTime);
1546 logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
1549 // demoting PDP should ensure that state transits to hotstandby
1550 logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
1553 logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime);
1556 logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
1557 + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1559 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1562 droolsPdpIntegrityMonitor.startTransaction();
1563 droolsPdpIntegrityMonitor.endTransaction();
1564 logger.debug("testLocking2: As expected, transaction successful");
1565 } catch (AdministrativeStateException e) {
1566 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1568 } catch (StandbyStatusException e) {
1569 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1571 } catch (Exception e) {
1572 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1576 // demoting should cause state to transit to hotstandby followed by re-promotion.
1577 logger.debug("testLocking2: demoting PDP={}", thisPdpId);
1580 logger.debug("testLocking2: sleeping {}"
1581 + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId);
1582 sleep(electionWaitSleepTime);
1584 logger.debug("testLocking2: Waking up and invoking startTransaction "
1585 + "on re-promoted PDP= {}, designated= {}",
1586 thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1588 droolsPdpIntegrityMonitor.startTransaction();
1589 droolsPdpIntegrityMonitor.endTransaction();
1590 logger.debug("testLocking2: As expected, transaction successful");
1591 } catch (AdministrativeStateException e) {
1592 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1594 } catch (StandbyStatusException e) {
1595 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1597 } catch (Exception e) {
1598 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1602 logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
1603 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
1604 assertTrue(standbyPdpDesignated == false);
1606 logger.debug("\n\ntestLocking2: Exiting\n\n");
1607 sleep(interruptRecoveryTime);
1610 private void sleep(long sleepms) throws InterruptedException {
1611 Thread.sleep(sleepms);