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.ActiveStandbyFeatureApiConstants;
48 import org.onap.policy.drools.activestandby.ActiveStandbyProperties;
49 import org.onap.policy.drools.activestandby.DroolsPdp;
50 import org.onap.policy.drools.activestandby.DroolsPdpEntity;
51 import org.onap.policy.drools.activestandby.DroolsPdpImpl;
52 import org.onap.policy.drools.activestandby.DroolsPdpsConnector;
53 import org.onap.policy.drools.activestandby.DroolsPdpsElectionHandler;
54 import org.onap.policy.drools.activestandby.JpaDroolsPdpsConnector;
55 import org.onap.policy.drools.activestandby.PmStandbyStateChangeNotifier;
56 import org.onap.policy.drools.core.PolicySessionFeatureApi;
57 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
58 import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 * All JUnits are designed to run in the local development environment
64 * where they have write privileges and can execute time-sensitive
67 * These tests can be run as JUnits, but there is some issue with running them
68 * as part of a "mvn install" build. Also, they take a very long time to run
69 * due to many real time breaks. Consequently, they are marked as @Ignore and
70 * only run from the desktop.
73 public class StandbyStateManagementTest {
74 private static final Logger logger = LoggerFactory.getLogger(StandbyStateManagementTest.class);
76 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
77 * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
78 * to ensure that we wait for the DesignationWaiter to do its job, before
79 * checking the results. Add a few seconds for safety
82 long sleepTime = 10000;
85 * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be
86 * plenty to ensure it has time to re-promote this PDP.
89 long electionWaitSleepTime = 6000;
92 * Sleep 1 seconds after each test to allow interrupt (shutdown) recovery.
95 long interruptRecoveryTime = 5000;
97 private static EntityManagerFactory emfx;
98 private static EntityManagerFactory emfd;
99 private static EntityManager emx;
100 private static EntityManager emd;
101 private static EntityTransaction et;
103 private final String configDir = "src/test/resources";
106 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
112 * @throws Exception exception
115 public static void setUpClass() throws Exception {
117 String userDir = System.getProperty("user.dir");
118 logger.debug("setUpClass: userDir={}", userDir);
119 System.setProperty("com.sun.management.jmxremote.port", "9980");
120 System.setProperty("com.sun.management.jmxremote.authenticate","false");
125 public static void tearDownClass() throws Exception {
131 * @throws Exception exception
134 public void setUp() throws Exception {
135 //Create teh data access for xaml db
136 Properties stateManagementProperties = new Properties();
137 stateManagementProperties.load(new FileInputStream(new File(
138 "src/test/resources/feature-state-management.properties")));
140 emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
142 // Create an entity manager to use the DB
143 emx = emfx.createEntityManager();
145 //Create the data access for drools db
146 Properties activeStandbyProperties = new Properties();
147 activeStandbyProperties.load(new FileInputStream(new File(
148 "src/test/resources/feature-active-standby-management.properties")));
150 emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
152 // Create an entity manager to use the DB
153 emd = emfd.createEntityManager();
157 public void tearDown() throws Exception {
162 * Clean up the xacml database.
165 public void cleanXacmlDb() {
166 et = emx.getTransaction();
169 // Make sure we leave the DB clean
170 emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
171 emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
172 emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
178 * Clean up the drools db.
180 public void cleanDroolsDb() {
181 et = emd.getTransaction();
184 // Make sure we leave the DB clean
185 emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
191 * These JUnit tests must be run one at a time in an eclipse environment
192 * by right-clicking StandbyStateManagementTest and selecting
193 * "Run As" -> "JUnit Test".
195 * They will run successfully when you run all of them under runAllTests(),
196 * however, you will get all sorts of non-fatal errors in the log and on the
197 * console that result from overlapping threads that are not terminated at the
198 * end of each test. The problem is that the JUnit environment does not terminate
199 * all the test threads between tests. This is true even if you break each JUnit
200 * into a separate file. Consequently, all the tests would have to be refactored
201 * so all test object initializations are coordinated. In other words, you
202 * retrieve the ActiveStandbyFeature instance and other class instances only once
203 * at the beginning of the JUnits and then reuse them throughout the tests.
204 * Initialization of the state of the objects is pretty straight forward as it
205 * just amounts to manipulating the entries in StateManagementEntity and
206 * DroolsPdpEntity tables. However, some thought needs to be given to how to
207 * "pause" the processing in ActiveStandbyFeature class. I think we could "pause"
208 * it by calling globalInit() which will, I think, restart it. So long as it
209 * does not create a new instance, it will force it to go through an initialization
210 * cycle which includes a "pause" at the beginning of proecessing. We just must
211 * be sure it does not create another instance - which may mean we need to add
212 * a factory interface instead of calling the constructor directly.
218 public void runAllTests() throws Exception {
224 testPmStandbyStateChangeNotifier();
225 testSanitizeDesignatedList();
226 testComputeMostRecentPrimary();
227 testComputeDesignatedPdp();
231 * Test the standby state change notifier.
233 * @throws Exception exception
237 public void testPmStandbyStateChangeNotifier() throws Exception {
238 logger.debug("\n\ntestPmStandbyStateChangeNotifier: Entering\n\n");
241 logger.debug("testPmStandbyStateChangeNotifier: Reading activeStandbyProperties");
243 Properties activeStandbyProperties = new Properties();
244 activeStandbyProperties.load(new FileInputStream(new File(
245 configDir + "/feature-active-standby-management.properties")));
247 String resourceName = "testPMS";
248 activeStandbyProperties.setProperty("resource.name", resourceName);
249 ActiveStandbyProperties.initProperties(activeStandbyProperties);
251 logger.debug("testPmStandbyStateChangeNotifier: Getting StateManagement instance");
253 StateManagement sm = new StateManagement(emfx, resourceName);
255 //Create an instance of the Observer
256 PmStandbyStateChangeNotifier pmNotifier = new PmStandbyStateChangeNotifier();
258 //Register the PmStandbyStateChangeNotifier Observer
259 sm.addObserver(pmNotifier);
261 //At this point the standbystatus = 'null'
263 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
266 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
268 //Adding standbystatus=hotstandby
270 System.out.println(pmNotifier.getPreviousStandbyStatus());
271 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
272 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
274 //Now making standbystatus=coldstandby
276 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
277 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
279 //standbystatus = hotstandby
281 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
282 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
284 //standbystatus = providingservice
286 //The previousStandbyStatus is not updated until after the delay activation expires
287 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
288 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
290 //Sleep long enough for the delayActivationTimer to run
292 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
294 //standbystatus = providingservice
296 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
298 //standbystatus = coldstandby
300 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
301 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
303 //standbystatus = hotstandby
305 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
306 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
308 //standbystatus = hotstandby
310 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
311 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
315 * Test sanitize designated list.
317 * @throws Exception exception
321 public void testSanitizeDesignatedList() throws Exception {
323 logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
325 // Get a DroolsPdpsConnector
327 logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties");
328 Properties activeStandbyProperties = new Properties();
329 activeStandbyProperties.load(new FileInputStream(new File(
330 configDir + "/feature-active-standby-management.properties")));
331 String thisPdpId = activeStandbyProperties
332 .getProperty(ActiveStandbyProperties.NODE_NAME);
334 logger.debug("testSanitizeDesignatedList: Creating emfDrools");
335 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
336 "junitDroolsPU", activeStandbyProperties);
338 final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
340 // Create 4 pdpd all not designated
342 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
343 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
344 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
345 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
347 List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
348 listOfDesignated.add(pdp1);
349 listOfDesignated.add(pdp2);
350 listOfDesignated.add(pdp3);
351 listOfDesignated.add(pdp4);
353 // Now we want to create a StateManagementFeature and initialize it. It will be
354 // discovered by the ActiveStandbyFeature when the election handler initializes.
356 StateManagementFeatureApi stateManagementFeature = null;
357 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
358 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
359 stateManagementFeature = feature;
360 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}",
361 stateManagementFeature.getResourceName());
364 if (stateManagementFeature == null) {
365 logger.error("testColdStandby failed to initialize. "
366 + "Unable to get instance of StateManagementFeatureApi "
367 + "with resourceID: {}", thisPdpId);
368 logger.debug("testColdStandby failed to initialize. "
369 + "Unable to get instance of StateManagementFeatureApi "
370 + "with resourceID: {}", thisPdpId);
374 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
376 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
378 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
380 assertTrue(listOfDesignated.size() == 4);
382 // Now make 2 designated
384 pdp1.setDesignated(true);
385 pdp2.setDesignated(true);
387 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
389 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n",
390 listOfDesignated.size());
392 assertTrue(listOfDesignated.size() == 2);
393 assertTrue(listOfDesignated.contains(pdp1));
394 assertTrue(listOfDesignated.contains(pdp2));
397 // Now all are designated. But, we have to add back the previously non-designated nodes
399 pdp3.setDesignated(true);
400 pdp4.setDesignated(true);
401 listOfDesignated.add(pdp3);
402 listOfDesignated.add(pdp4);
404 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
406 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n",
407 listOfDesignated.size());
409 assertTrue(listOfDesignated.size() == 4);
414 * Test Compute most recent primary.
416 * @throws Exception exception
420 public void testComputeMostRecentPrimary() throws Exception {
422 logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
424 logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties");
425 Properties activeStandbyProperties = new Properties();
426 activeStandbyProperties.load(new FileInputStream(new File(
427 configDir + "/feature-active-standby-management.properties")));
428 String thisPdpId = activeStandbyProperties
429 .getProperty(ActiveStandbyProperties.NODE_NAME);
431 logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
432 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
433 "junitDroolsPU", activeStandbyProperties);
435 final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
438 // Create 4 pdpd all not designated
441 long designatedDateMs = new Date().getTime();
442 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
443 pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
445 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
447 pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
449 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
450 pdp3.setDesignatedDate(new Date(designatedDateMs - 1));
452 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
454 pdp4.setDesignatedDate(new Date(designatedDateMs));
456 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
457 listOfAllPdps.add(pdp1);
458 listOfAllPdps.add(pdp2);
459 listOfAllPdps.add(pdp3);
460 listOfAllPdps.add(pdp4);
463 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
464 listOfDesignated.add(pdp1);
465 listOfDesignated.add(pdp2);
466 listOfDesignated.add(pdp3);
467 listOfDesignated.add(pdp4);
469 // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
470 // or all designated members.
472 // Now we want to create a StateManagementFeature and initialize it. It will be
473 // discovered by the ActiveStandbyFeature when the election handler initializes.
475 StateManagementFeatureApi stateManagementFeature = null;
476 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
477 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
478 stateManagementFeature = feature;
479 logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}",
480 stateManagementFeature.getResourceName());
483 if (stateManagementFeature == null) {
484 logger.error("testComputeMostRecentPrimary failed to initialize. "
485 + "Unable to get instance of StateManagementFeatureApi "
486 + "with resourceID: {}", thisPdpId);
487 logger.debug("testComputeMostRecentPrimary failed to initialize. "
488 + "Unable to get instance of StateManagementFeatureApi "
489 + "with resourceID: {}", thisPdpId);
492 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
494 DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(
495 listOfAllPdps, listOfDesignated);
497 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
498 mostRecentPrimary.getPdpId());
501 // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
502 // the one which has the most recent designated date.
505 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
508 // Now let's designate all of those on the listOfDesignated. It will choose the first one designated
511 pdp1.setDesignated(true);
512 pdp2.setDesignated(true);
513 pdp3.setDesignated(true);
514 pdp4.setDesignated(true);
516 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
518 logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, "
519 + "mostRecentPrimary.getPdpId() = {}\n\n",
520 mostRecentPrimary.getPdpId());
523 // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
524 // the one which was designated first
527 assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
530 // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
531 // look for the most recently designated pdp which is not currently designated.
534 pdp3.setDesignated(false);
535 pdp4.setDesignated(false);
537 listOfDesignated.remove(pdp3);
538 listOfDesignated.remove(pdp4);
540 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
542 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
543 mostRecentPrimary.getPdpId());
545 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
549 // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
550 // look for the most recently designated pdp regardless of whether it is currently marked as designated.
553 pdp1.setDesignated(false);
554 pdp2.setDesignated(false);
556 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
558 logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n",
559 mostRecentPrimary.getPdpId());
561 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
564 // If we have only one pdp on in the listOfDesignated,
565 // the most recently designated pdp will be chosen, regardless
566 // of its designation status
569 listOfDesignated.remove(pdp1);
571 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
573 logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n",
574 mostRecentPrimary.getPdpId());
576 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
579 // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
582 listOfDesignated.remove(pdp2);
584 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
586 logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n",
587 mostRecentPrimary.getPdpId());
589 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
594 * Test compute designated PDP.
596 * @throws Exception exception
600 public void testComputeDesignatedPdp() throws Exception {
602 logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
604 logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties");
605 Properties activeStandbyProperties = new Properties();
606 activeStandbyProperties.load(new FileInputStream(new File(
607 configDir + "/feature-active-standby-management.properties")));
608 String thisPdpId = activeStandbyProperties
609 .getProperty(ActiveStandbyProperties.NODE_NAME);
612 logger.debug("testComputeDesignatedPdp: Creating emfDrools");
613 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
614 "junitDroolsPU", activeStandbyProperties);
616 final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
619 // Create 4 pdpd all not designated. Two on site1. Two on site2
622 long designatedDateMs = new Date().getTime();
623 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
624 pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
625 pdp1.setSite("site1");
627 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
628 pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
629 pdp2.setSite("site1");
632 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
633 pdp3.setDesignatedDate(new Date(designatedDateMs - 4));
634 pdp3.setSite("site2");
636 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
638 pdp4.setDesignatedDate(new Date(designatedDateMs));
639 pdp4.setSite("site2");
641 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
642 listOfAllPdps.add(pdp1);
643 listOfAllPdps.add(pdp2);
644 listOfAllPdps.add(pdp3);
645 listOfAllPdps.add(pdp4);
648 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
651 // We will first test an empty listOfDesignated. As we know from the previous JUnit,
652 // the pdp with the most designated date will be chosen for mostRecentPrimary
654 // Now we want to create a StateManagementFeature and initialize it. It will be
655 // discovered by the ActiveStandbyFeature when the election handler initializes.
657 StateManagementFeatureApi stateManagementFeature = null;
658 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
659 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
660 stateManagementFeature = feature;
661 logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}",
662 stateManagementFeature.getResourceName());
665 if (stateManagementFeature == null) {
666 logger.error("testComputeDesignatedPdp failed to initialize. "
667 + "Unable to get instance of StateManagementFeatureApi "
668 + "with resourceID: {}", thisPdpId);
669 logger.debug("testComputeDesignatedPdp failed to initialize. "
670 + "Unable to get instance of StateManagementFeatureApi "
671 + "with resourceID: {}", thisPdpId);
675 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
677 DroolsPdp mostRecentPrimary = pdp4;
679 DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
682 // The designatedPdp should be null
684 assertTrue(designatedPdp == null);
687 // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
689 listOfDesignated.add(pdp2);
691 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
694 // Now the designatedPdp should be the one and only selection in the listOfDesignated
697 assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
700 // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
703 listOfDesignated.add(pdp1);
705 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
708 // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
711 assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
714 // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
717 listOfDesignated.remove(pdp1);
718 listOfDesignated.add(pdp3);
720 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
723 // The designatedPdp should now be the one on the same site as the mostRecentPrimary
726 assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
732 * @throws Exception exception
736 public void testColdStandby() throws Exception {
738 logger.debug("\n\ntestColdStandby: Entering\n\n");
742 logger.debug("testColdStandby: Reading stateManagementProperties");
743 Properties stateManagementProperties = new Properties();
744 stateManagementProperties.load(new FileInputStream(new File(
745 configDir + "/feature-state-management.properties")));
747 logger.debug("testColdStandby: Creating emfXacml");
748 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
749 "junitXacmlPU", stateManagementProperties);
751 logger.debug("testColdStandby: Reading activeStandbyProperties");
752 Properties activeStandbyProperties = new Properties();
753 activeStandbyProperties.load(new FileInputStream(new File(
754 configDir + "/feature-active-standby-management.properties")));
755 final String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
757 logger.debug("testColdStandby: Creating emfDrools");
758 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
759 "junitDroolsPU", activeStandbyProperties);
761 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
763 logger.debug("testColdStandby: Cleaning up tables");
764 conn.deleteAllPdps();
766 logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
767 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
769 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
770 logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
771 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
772 assertTrue(droolsPdpEntity.isDesignated() == true);
775 * When the Standby Status changes (from providingservice) to hotstandby
776 * or coldstandby,the Active/Standby selection algorithm must stand down
777 * if thePDP-D is currently the lead/active node and allow another PDP-D
780 * It must also call lock on all engines in the engine management.
785 * Yes, this is kludgy, but we have a chicken and egg problem here: we
786 * need a StateManagement object to invoke the
787 * deleteAllStateManagementEntities method.
789 logger.debug("testColdStandby: Instantiating stateManagement object");
791 StateManagement sm = new StateManagement(emfXacml, "dummy");
792 sm.deleteAllStateManagementEntities();
794 // Now we want to create a StateManagementFeature and initialize it. It will be
795 // discovered by the ActiveStandbyFeature when the election handler initializes.
797 StateManagementFeatureApi smf = null;
798 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
799 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
801 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
805 logger.error("testColdStandby failed to initialize. "
806 + "Unable to get instance of StateManagementFeatureApi "
807 + "with resourceID: {}", thisPdpId);
808 logger.debug("testColdStandby failed to initialize. "
809 + "Unable to get instance of StateManagementFeatureApi "
810 + "with resourceID: {}", thisPdpId);
813 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
814 // that has been created.
815 ActiveStandbyFeatureApi activeStandbyFeature = null;
816 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
817 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
818 activeStandbyFeature = feature;
819 logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}",
820 activeStandbyFeature.getResourceName());
823 if (activeStandbyFeature == null) {
824 logger.error("testColdStandby failed to initialize. "
825 + "Unable to get instance of ActiveStandbyFeatureAPI "
826 + "with resourceID:{}", thisPdpId);
827 logger.debug("testColdStandby failed to initialize. "
828 + "Unable to get instance of ActiveStandbyFeatureAPI "
829 + "with resourceID:{}", thisPdpId);
832 // Artificially putting a PDP into service is really a two step process, 1)
833 // inserting it as designated and 2) promoting it so that its standbyStatus
834 // is providing service.
836 logger.debug("testColdStandby: Runner started; Sleeping "
837 + interruptRecoveryTime + "ms before promoting PDP= {}",
839 sleep(interruptRecoveryTime);
841 logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
844 String standbyStatus = sm.getStandbyStatus(thisPdpId);
845 logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}",
846 thisPdpId, standbyStatus);
848 logger.debug("testColdStandby: Locking smf");
851 sleep(interruptRecoveryTime);
853 // Verify that the PDP is no longer designated.
855 droolsPdpEntity = conn.getPdp(thisPdpId);
856 logger.debug("testColdStandby: After lock sm.lock() invoked, "
857 + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
858 assertTrue(droolsPdpEntity.isDesignated() == false);
860 logger.debug("\n\ntestColdStandby: Exiting\n\n");
861 sleep(interruptRecoveryTime);
865 // Tests hot standby when there is only one PDP.
868 * Test hot standby 1.
870 * @throws Exception exception
874 public void testHotStandby1() throws Exception {
876 logger.debug("\n\ntestHotStandby1: Entering\n\n");
880 logger.debug("testHotStandby1: Reading stateManagementProperties");
881 Properties stateManagementProperties = new Properties();
882 stateManagementProperties.load(new FileInputStream(new File(
883 configDir + "/feature-state-management.properties")));
885 logger.debug("testHotStandby1: Creating emfXacml");
886 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
887 "junitXacmlPU", stateManagementProperties);
889 logger.debug("testHotStandby1: Reading activeStandbyProperties");
890 Properties activeStandbyProperties = new Properties();
891 activeStandbyProperties.load(new FileInputStream(new File(
892 configDir + "/feature-active-standby-management.properties")));
893 final String thisPdpId = activeStandbyProperties
894 .getProperty(ActiveStandbyProperties.NODE_NAME);
896 logger.debug("testHotStandby1: Creating emfDrools");
897 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
898 "junitDroolsPU", activeStandbyProperties);
900 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
902 logger.debug("testHotStandby1: Cleaning up tables");
903 conn.deleteAllPdps();
906 * Insert this PDP as not designated. Initial standby state will be
907 * either null or cold standby. Demoting should transit state to
911 logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
912 Date yesterday = DateUtils.addDays(new Date(), -1);
913 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
915 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
916 logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
917 thisPdpId, droolsPdpEntity.isDesignated());
918 assertTrue(droolsPdpEntity.isDesignated() == false);
920 logger.debug("testHotStandby1: Instantiating stateManagement object");
921 StateManagement sm = new StateManagement(emfXacml, "dummy");
922 sm.deleteAllStateManagementEntities();
925 // Now we want to create a StateManagementFeature and initialize it. It will be
926 // discovered by the ActiveStandbyFeature when the election handler initializes.
928 StateManagementFeatureApi smf = null;
929 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
930 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
932 logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
936 logger.error("testHotStandby1 failed to initialize. "
937 + "Unable to get instance of StateManagementFeatureApi "
938 + "with resourceID: {}", thisPdpId);
939 logger.debug("testHotStandby1 failed to initialize. "
940 + "Unable to get instance of StateManagementFeatureApi "
941 + "with resourceID: {}", thisPdpId);
944 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
945 // that has been created.
946 ActiveStandbyFeatureApi activeStandbyFeature = null;
947 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
948 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
949 activeStandbyFeature = feature;
950 logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}",
951 activeStandbyFeature.getResourceName());
954 if (activeStandbyFeature == null) {
955 logger.error("testHotStandby1 failed to initialize. "
956 + "Unable to get instance of ActiveStandbyFeatureAPI "
957 + "with resourceID: {}", thisPdpId);
958 logger.debug("testHotStandby1 failed to initialize. "
959 + "Unable to get instance of ActiveStandbyFeatureAPI "
960 + "with resourceID: {}", thisPdpId);
964 logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
965 // demoting should cause state to transit to hotstandby
969 logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
970 + "time to check droolspdpentity table", sleepTime);
974 // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
976 droolsPdpEntity = conn.getPdp(thisPdpId);
977 logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
978 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
979 assertTrue(droolsPdpEntity.isDesignated() == true);
980 String standbyStatus = smf.getStandbyStatus(thisPdpId);
981 logger.debug("testHotStandby1: After demotion, PDP= {} "
982 + "has standbyStatus= {}", thisPdpId, standbyStatus);
983 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
985 logger.debug("testHotStandby1: Stopping policyManagementRunner");
987 logger.debug("\n\ntestHotStandby1: Exiting\n\n");
988 sleep(interruptRecoveryTime);
993 * Tests hot standby when two PDPs are involved.
997 * Test hot standby 2.
999 * @throws Exception exception
1003 public void testHotStandby2() throws Exception {
1005 logger.info("\n\ntestHotStandby2: Entering\n\n");
1009 logger.info("testHotStandby2: Reading stateManagementProperties");
1010 Properties stateManagementProperties = new Properties();
1011 stateManagementProperties.load(new FileInputStream(new File(
1012 configDir + "/feature-state-management.properties")));
1014 logger.info("testHotStandby2: Creating emfXacml");
1015 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1016 "junitXacmlPU", stateManagementProperties);
1018 logger.info("testHotStandby2: Reading activeStandbyProperties");
1019 Properties activeStandbyProperties = new Properties();
1020 activeStandbyProperties.load(new FileInputStream(new File(
1021 configDir + "/feature-active-standby-management.properties")));
1022 final String thisPdpId = activeStandbyProperties
1023 .getProperty(ActiveStandbyProperties.NODE_NAME);
1025 logger.info("testHotStandby2: Creating emfDrools");
1026 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1027 "junitDroolsPU", activeStandbyProperties);
1029 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1031 logger.info("testHotStandby2: Cleaning up tables");
1032 conn.deleteAllPdps();
1035 // Insert a PDP that's designated but not current.
1037 String activePdpId = "pdp2";
1038 logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
1039 Date yesterday = DateUtils.addDays(new Date(), -1);
1040 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
1041 conn.insertPdp(pdp);
1042 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
1043 logger.info("testHotStandby2: After insertion, PDP= {}, which is "
1044 + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
1045 assertTrue(droolsPdpEntity.isDesignated() == true);
1048 * Promote the designated PDP.
1050 * We have a chicken and egg problem here: we need a StateManagement
1051 * object to invoke the deleteAllStateManagementEntities method.
1055 logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
1056 StateManagement sm = new StateManagement(emfXacml, "dummy");
1057 sm.deleteAllStateManagementEntities();
1060 sm = new StateManagement(emfXacml, activePdpId);//pdp2
1062 // Artificially putting a PDP into service is really a two step process, 1)
1063 // inserting it as designated and 2) promoting it so that its standbyStatus
1064 // is providing service.
1067 * Insert this PDP as not designated. Initial standby state will be
1068 * either null or cold standby. Demoting should transit state to
1073 logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
1074 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
1075 conn.insertPdp(pdp);
1076 droolsPdpEntity = conn.getPdp(thisPdpId);
1077 logger.info("testHotStandby2: After insertion, PDP={} "
1078 + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
1079 assertTrue(droolsPdpEntity.isDesignated() == false);
1082 // Now we want to create a StateManagementFeature and initialize it. It will be
1083 // discovered by the ActiveStandbyFeature when the election handler initializes.
1085 StateManagementFeatureApi sm2 = null;
1086 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
1087 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1089 logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
1093 logger.error("testHotStandby2 failed to initialize. "
1094 + "Unable to get instance of StateManagementFeatureApi "
1095 + "with resourceID: {}", thisPdpId);
1096 logger.debug("testHotStandby2 failed to initialize. "
1097 + "Unable to get instance of StateManagementFeatureApi "
1098 + "with resourceID: {}", thisPdpId);
1101 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1102 // that has been created.
1103 ActiveStandbyFeatureApi activeStandbyFeature = null;
1104 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
1105 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1106 activeStandbyFeature = feature;
1107 logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}",
1108 activeStandbyFeature.getResourceName());
1111 if (activeStandbyFeature == null) {
1112 logger.error("testHotStandby2 failed to initialize. "
1113 + "Unable to get instance of ActiveStandbyFeatureAPI "
1114 + "with resourceID: {}", thisPdpId);
1115 logger.debug("testHotStandby2 failed to initialize. "
1116 + "Unable to get instance of ActiveStandbyFeatureAPI "
1117 + "with resourceID: {}", thisPdpId);
1120 logger.info("testHotStandby2: Runner started; Sleeping {} "
1121 + "ms before promoting/demoting", interruptRecoveryTime);
1122 sleep(interruptRecoveryTime);
1124 logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
1125 //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
1126 //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
1127 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
1128 sm.enableNotFailed();//pdp2
1130 String standbyStatus = sm.getStandbyStatus(activePdpId);
1131 logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
1133 // demoting PDP should ensure that state transits to hotstandby
1134 logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
1136 standbyStatus = sm.getStandbyStatus(thisPdpId);
1137 logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
1139 logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
1140 + "time to check droolspdpentity table", sleepTime);
1144 * Verify that this PDP, demoted to HOT_STANDBY, is now
1145 * re-designated and providing service.
1148 droolsPdpEntity = conn.getPdp(thisPdpId);
1149 logger.info("testHotStandby2: After demoting PDP={}"
1150 + ", DESIGNATED= {}"
1151 + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
1152 assertTrue(droolsPdpEntity.isDesignated() == true);
1153 standbyStatus = sm2.getStandbyStatus(thisPdpId);
1154 logger.info("testHotStandby2: After demoting PDP={}"
1155 + ", PDP={} has standbyStatus= {}",
1156 activePdpId, thisPdpId, standbyStatus);
1157 assertTrue(standbyStatus != null
1158 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
1160 logger.info("testHotStandby2: Stopping policyManagementRunner");
1162 logger.info("\n\ntestHotStandby2: Exiting\n\n");
1163 sleep(interruptRecoveryTime);
1168 * 1) Inserts and designates this PDP, then verifies that startTransaction
1171 * 2) Demotes PDP, and verifies that because there is only one PDP, it will
1172 * be immediately re-promoted, thus allowing startTransaction to be
1175 * 3) Locks PDP and verifies that startTransaction results in
1176 * AdministrativeStateException.
1178 * 4) Unlocks PDP and verifies that startTransaction results in
1179 * StandbyStatusException.
1181 * 5) Promotes PDP and verifies that startTransaction is once again
1188 * @throws Exception exception
1192 public void testLocking1() throws Exception {
1193 logger.debug("testLocking1: Entry");
1197 logger.debug("testLocking1: Reading stateManagementProperties");
1198 Properties stateManagementProperties = new Properties();
1199 stateManagementProperties.load(new FileInputStream(new File(
1200 configDir + "/feature-state-management.properties")));
1202 logger.debug("testLocking1: Creating emfXacml");
1203 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1204 "junitXacmlPU", stateManagementProperties);
1206 logger.debug("testLocking1: Reading activeStandbyProperties");
1207 Properties activeStandbyProperties = new Properties();
1208 activeStandbyProperties.load(new FileInputStream(new File(
1209 configDir + "/feature-active-standby-management.properties")));
1210 final String thisPdpId = activeStandbyProperties
1211 .getProperty(ActiveStandbyProperties.NODE_NAME);
1213 logger.debug("testLocking1: Creating emfDrools");
1214 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1215 "junitDroolsPU", activeStandbyProperties);
1217 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1219 logger.debug("testLocking1: Cleaning up tables");
1220 conn.deleteAllPdps();
1223 * Insert this PDP as designated. Initial standby state will be
1224 * either null or cold standby.
1227 logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
1228 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
1229 conn.insertPdp(pdp);
1230 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1231 logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
1232 thisPdpId, droolsPdpEntity.isDesignated());
1233 assertTrue(droolsPdpEntity.isDesignated() == true);
1235 logger.debug("testLocking1: Instantiating stateManagement object");
1236 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1237 smDummy.deleteAllStateManagementEntities();
1239 // Now we want to create a StateManagementFeature and initialize it. It will be
1240 // discovered by the ActiveStandbyFeature when the election handler initializes.
1242 StateManagementFeatureApi sm = null;
1243 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
1244 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1246 logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1250 logger.error("testLocking1 failed to initialize. "
1251 + "Unable to get instance of StateManagementFeatureApi "
1252 + "with resourceID: {}", thisPdpId);
1253 logger.debug("testLocking1 failed to initialize. "
1254 + "Unable to get instance of StateManagementFeatureApi "
1255 + "with resourceID: {}", thisPdpId);
1258 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1259 // that has been created.
1260 ActiveStandbyFeatureApi activeStandbyFeature = null;
1261 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
1262 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1263 activeStandbyFeature = feature;
1264 logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}",
1265 activeStandbyFeature.getResourceName());
1268 if (activeStandbyFeature == null) {
1269 logger.error("testLocking1 failed to initialize. "
1270 + "Unable to get instance of ActiveStandbyFeatureAPI "
1271 + "with resourceID: {}", thisPdpId);
1272 logger.debug("testLocking1 failed to initialize. "
1273 + "Unable to get instance of ActiveStandbyFeatureAPI "
1274 + "with resourceID: {}", thisPdpId);
1277 logger.debug("testLocking1: Runner started; Sleeping "
1278 + interruptRecoveryTime + "ms before promoting PDP={}",
1280 sleep(interruptRecoveryTime);
1282 logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
1285 logger.debug("testLocking1: Sleeping {} ms, to allow time for "
1286 + "policy-management.Main class to come up, designated= {}",
1287 sleepTime, conn.getPdp(thisPdpId).isDesignated());
1290 logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
1291 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1294 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1296 droolsPdpIntegrityMonitor.startTransaction();
1297 droolsPdpIntegrityMonitor.endTransaction();
1298 logger.debug("testLocking1: As expected, transaction successful");
1299 } catch (AdministrativeStateException e) {
1300 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1302 } catch (StandbyStatusException e) {
1303 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1305 } catch (Exception e) {
1306 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1310 // demoting should cause state to transit to hotstandby, followed by re-promotion,
1311 // since there is only one PDP.
1312 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1315 logger.debug("testLocking1: sleeping" + electionWaitSleepTime
1316 + " to allow election handler to re-promote PDP={}", thisPdpId);
1317 sleep(electionWaitSleepTime);
1319 logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
1320 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1322 droolsPdpIntegrityMonitor.startTransaction();
1323 droolsPdpIntegrityMonitor.endTransaction();
1324 logger.debug("testLocking1: As expected, transaction successful");
1325 } catch (AdministrativeStateException e) {
1326 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1328 } catch (StandbyStatusException e) {
1329 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1331 } catch (Exception e) {
1332 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1336 // locking should cause state to transit to cold standby
1337 logger.debug("testLocking1: locking PDP={}", thisPdpId);
1340 // Just to avoid any race conditions, sleep a little after locking
1341 logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
1344 logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
1345 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1347 droolsPdpIntegrityMonitor.startTransaction();
1348 logger.error("testLocking1: startTransaction unexpectedly successful");
1350 } catch (AdministrativeStateException e) {
1351 logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
1352 } catch (StandbyStatusException e) {
1353 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1355 } catch (Exception e) {
1356 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1359 droolsPdpIntegrityMonitor.endTransaction();
1362 // unlocking should cause state to transit to hot standby and then providing service
1363 logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
1366 // Just to avoid any race conditions, sleep a little after locking
1367 logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1368 sleep(electionWaitSleepTime);
1370 logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1373 + conn.getPdp(thisPdpId).isDesignated());
1375 droolsPdpIntegrityMonitor.startTransaction();
1376 logger.error("testLocking1: startTransaction successful as expected");
1377 } catch (AdministrativeStateException e) {
1378 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1380 } catch (StandbyStatusException e) {
1381 logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1383 } catch (Exception e) {
1384 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1387 droolsPdpIntegrityMonitor.endTransaction();
1390 // demoting should cause state to transit to hot standby
1391 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1394 // Just to avoid any race conditions, sleep a little after promoting
1395 logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
1398 logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
1399 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1401 droolsPdpIntegrityMonitor.startTransaction();
1402 droolsPdpIntegrityMonitor.endTransaction();
1403 logger.debug("testLocking1: Unexpectedly, transaction successful");
1405 } catch (AdministrativeStateException e) {
1406 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1408 } catch (StandbyStatusException e) {
1409 logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
1410 } catch (Exception e) {
1411 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1415 logger.debug("\n\ntestLocking1: Exiting\n\n");
1416 sleep(interruptRecoveryTime);
1422 * 1) Inserts and designates this PDP, then verifies that startTransaction
1425 * 2) Inserts another PDP in hotstandby.
1427 * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1428 * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1434 * @throws Exception exception
1438 public void testLocking2() throws Exception {
1440 logger.debug("\n\ntestLocking2: Entering\n\n");
1444 logger.debug("testLocking2: Reading stateManagementProperties");
1445 Properties stateManagementProperties = new Properties();
1446 stateManagementProperties.load(new FileInputStream(new File(
1447 configDir + "/feature-state-management.properties")));
1449 logger.debug("testLocking2: Creating emfXacml");
1450 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1451 "junitXacmlPU", stateManagementProperties);
1453 logger.debug("testLocking2: Reading activeStandbyProperties");
1454 Properties activeStandbyProperties = new Properties();
1455 activeStandbyProperties.load(new FileInputStream(new File(
1456 configDir + "/feature-active-standby-management.properties")));
1457 final String thisPdpId = activeStandbyProperties
1458 .getProperty(ActiveStandbyProperties.NODE_NAME);
1460 logger.debug("testLocking2: Creating emfDrools");
1461 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1462 "junitDroolsPU", activeStandbyProperties);
1464 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1466 logger.debug("testLocking2: Cleaning up tables");
1467 conn.deleteAllPdps();
1470 * Insert this PDP as designated. Initial standby state will be
1471 * either null or cold standby. Demoting should transit state to
1475 logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
1476 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
1477 conn.insertPdp(pdp);
1478 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1479 logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
1480 thisPdpId, droolsPdpEntity.isDesignated());
1481 assertTrue(droolsPdpEntity.isDesignated() == true);
1483 logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
1484 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1485 smDummy.deleteAllStateManagementEntities();
1487 // Now we want to create a StateManagementFeature and initialize it. It will be
1488 // discovered by the ActiveStandbyFeature when the election handler initializes.
1490 StateManagementFeatureApi sm = null;
1491 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
1492 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1494 logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1498 logger.error("testLocking2 failed to initialize. "
1499 + "Unable to get instance of StateManagementFeatureApi "
1500 + "with resourceID: {}", thisPdpId);
1501 logger.debug("testLocking2 failed to initialize. "
1502 + "Unable to get instance of StateManagementFeatureApi "
1503 + "with resourceID: {}", thisPdpId);
1506 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1507 // that has been created.
1508 ActiveStandbyFeatureApi activeStandbyFeature = null;
1509 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
1510 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
1511 activeStandbyFeature = feature;
1512 logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}",
1513 activeStandbyFeature.getResourceName());
1516 if (activeStandbyFeature == null) {
1517 logger.error("testLocking2 failed to initialize. "
1518 + "Unable to get instance of ActiveStandbyFeatureAPI "
1519 + "with resourceID: {}", thisPdpId);
1520 logger.debug("testLocking2 failed to initialize. "
1521 + "Unable to get instance of ActiveStandbyFeatureAPI "
1522 + "with resourceID: {}", thisPdpId);
1526 * Insert another PDP as not designated. Initial standby state will be
1527 * either null or cold standby. Demoting should transit state to
1531 String standbyPdpId = "pdp2";
1532 logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
1533 Date yesterday = DateUtils.addDays(new Date(), -1);
1534 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1535 conn.insertPdp(pdp);
1536 droolsPdpEntity = conn.getPdp(standbyPdpId);
1537 logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
1538 standbyPdpId, droolsPdpEntity.isDesignated());
1539 assertTrue(droolsPdpEntity.isDesignated() == false);
1541 logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
1542 final StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
1544 logger.debug("testLocking2: Runner started; Sleeping {} ms "
1545 + "before promoting/demoting", interruptRecoveryTime);
1546 sleep(interruptRecoveryTime);
1548 logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
1551 // demoting PDP should ensure that state transits to hotstandby
1552 logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
1555 logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime);
1558 logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
1559 + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1561 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1564 droolsPdpIntegrityMonitor.startTransaction();
1565 droolsPdpIntegrityMonitor.endTransaction();
1566 logger.debug("testLocking2: As expected, transaction successful");
1567 } catch (AdministrativeStateException e) {
1568 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1570 } catch (StandbyStatusException e) {
1571 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1573 } catch (Exception e) {
1574 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1578 // demoting should cause state to transit to hotstandby followed by re-promotion.
1579 logger.debug("testLocking2: demoting PDP={}", thisPdpId);
1582 logger.debug("testLocking2: sleeping {}"
1583 + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId);
1584 sleep(electionWaitSleepTime);
1586 logger.debug("testLocking2: Waking up and invoking startTransaction "
1587 + "on re-promoted PDP= {}, designated= {}",
1588 thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1590 droolsPdpIntegrityMonitor.startTransaction();
1591 droolsPdpIntegrityMonitor.endTransaction();
1592 logger.debug("testLocking2: As expected, transaction successful");
1593 } catch (AdministrativeStateException e) {
1594 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1596 } catch (StandbyStatusException e) {
1597 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1599 } catch (Exception e) {
1600 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1604 logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
1605 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
1606 assertTrue(standbyPdpDesignated == false);
1608 logger.debug("\n\ntestLocking2: Exiting\n\n");
1609 sleep(interruptRecoveryTime);
1612 private void sleep(long sleepms) throws InterruptedException {
1613 Thread.sleep(sleepms);