2 * ============LICENSE_START=======================================================
3 * feature-active-standby-management
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.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.
108 public static void setUpClass() throws Exception {
110 String userDir = System.getProperty("user.dir");
111 logger.debug("setUpClass: userDir={}", userDir);
112 System.setProperty("com.sun.management.jmxremote.port", "9980");
113 System.setProperty("com.sun.management.jmxremote.authenticate","false");
118 public static void tearDownClass() throws Exception {
122 public void setUp() throws Exception {
123 //Create teh data access for xaml db
124 Properties stateManagementProperties = new Properties();
125 stateManagementProperties.load(new FileInputStream(new File(
126 "src/test/resources/feature-state-management.properties")));
128 emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
130 // Create an entity manager to use the DB
131 emx = emfx.createEntityManager();
133 //Create the data access for drools db
134 Properties activeStandbyProperties = new Properties();
135 activeStandbyProperties.load(new FileInputStream(new File(
136 "src/test/resources/feature-active-standby-management.properties")));
138 emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
140 // Create an entity manager to use the DB
141 emd = emfd.createEntityManager();
145 public void tearDown() throws Exception {
149 public void cleanXacmlDb(){
150 et = emx.getTransaction();
153 // Make sure we leave the DB clean
154 emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
155 emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
156 emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
161 public void cleanDroolsDb(){
162 et = emd.getTransaction();
165 // Make sure we leave the DB clean
166 emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
172 * These JUnit tests must be run one at a time in an eclipse environment
173 * by right-clicking StandbyStateManagementTest and selecting
174 * "Run As" -> "JUnit Test".
176 * They will run successfully when you run all of them under runAllTests(),
177 * however, you will get all sorts of non-fatal errors in the log and on the
178 * console that result from overlapping threads that are not terminated at the
179 * end of each test. The problem is that the JUnit environment does not terminate
180 * all the test threads between tests. This is true even if you break each JUnit
181 * into a separate file. Consequently, all the tests would have to be refactored
182 * so all test object initializations are coordinated. In other words, you
183 * retrieve the ActiveStandbyFeature instance and other class instances only once
184 * at the beginning of the JUnits and then reuse them throughout the tests.
185 * Initialization of the state of the objects is pretty straight forward as it
186 * just amounts to manipulating the entries in StateManagementEntity and
187 * DroolsPdpEntity tables. However, some thought needs to be given to how to
188 * "pause" the processing in ActiveStandbyFeature class. I think we could "pause"
189 * it by calling globalInit() which will, I think, restart it. So long as it
190 * does not create a new instance, it will force it to go through an initialization
191 * cycle which includes a "pause" at the beginning of proecessing. We just must
192 * be sure it does not create another instance - which may mean we need to add
193 * a factory interface instead of calling the constructor directly.
199 public void runAllTests() throws Exception {
205 testPMStandbyStateChangeNotifier();
206 testSanitizeDesignatedList();
207 testComputeMostRecentPrimary();
208 testComputeDesignatedPdp();
213 public void testPMStandbyStateChangeNotifier() throws Exception {
214 logger.debug("\n\ntestPMStandbyStateChangeNotifier: Entering\n\n");
217 logger.debug("testPMStandbyStateChangeNotifier: Reading activeStandbyProperties");
219 Properties activeStandbyProperties = new Properties();
220 activeStandbyProperties.load(new FileInputStream(new File(
221 configDir + "/feature-active-standby-management.properties")));
223 String resourceName = "testPMS";
224 activeStandbyProperties.setProperty("resource.name", resourceName);
225 ActiveStandbyProperties.initProperties(activeStandbyProperties);
227 logger.debug("testPMStandbyStateChangeNotifier: Getting StateManagement instance");
229 StateManagement sm = new StateManagement(emfx, resourceName);
231 //Create an instance of the Observer
232 PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
234 //Register the PMStandbyStateChangeNotifier Observer
235 sm.addObserver(pmNotifier);
237 //At this point the standbystatus = 'null'
239 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
242 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
244 //Adding standbystatus=hotstandby
246 System.out.println(pmNotifier.getPreviousStandbyStatus());
247 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
249 //Now making standbystatus=coldstandby
251 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
253 //standbystatus = hotstandby
255 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
257 //standbystatus = providingservice
259 //The previousStandbyStatus is not updated until after the delay activation expires
260 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
262 //Sleep long enough for the delayActivationTimer to run
264 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
266 //standbystatus = providingservice
268 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
270 //standbystatus = coldstandby
272 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
274 //standbystatus = hotstandby
276 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
278 //standbystatus = hotstandby
280 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
285 public void testSanitizeDesignatedList() throws Exception {
287 logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
289 // Get a DroolsPdpsConnector
291 logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties");
292 Properties activeStandbyProperties = new Properties();
293 activeStandbyProperties.load(new FileInputStream(new File(
294 configDir + "/feature-active-standby-management.properties")));
295 String thisPdpId = activeStandbyProperties
296 .getProperty(ActiveStandbyProperties.NODE_NAME);
298 logger.debug("testSanitizeDesignatedList: Creating emfDrools");
299 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
300 "junitDroolsPU", activeStandbyProperties);
302 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
304 // Create 4 pdpd all not designated
306 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
307 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
308 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
309 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
311 List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
312 listOfDesignated.add(pdp1);
313 listOfDesignated.add(pdp2);
314 listOfDesignated.add(pdp3);
315 listOfDesignated.add(pdp4);
317 // Now we want to create a StateManagementFeature and initialize it. It will be
318 // discovered by the ActiveStandbyFeature when the election handler initializes.
320 StateManagementFeatureAPI stateManagementFeature = null;
321 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
323 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
324 stateManagementFeature = feature;
325 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
328 if(stateManagementFeature == null){
329 logger.error("testColdStandby failed to initialize. "
330 + "Unable to get instance of StateManagementFeatureAPI "
331 + "with resourceID: {}", thisPdpId);
332 logger.debug("testColdStandby failed to initialize. "
333 + "Unable to get instance of StateManagementFeatureAPI "
334 + "with resourceID: {}", thisPdpId);
338 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
340 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
342 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
344 assertTrue(listOfDesignated.size()==4);
346 // Now make 2 designated
348 pdp1.setDesignated(true);
349 pdp2.setDesignated(true);
351 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
353 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n", listOfDesignated.size());
355 assertTrue(listOfDesignated.size()==2);
356 assertTrue(listOfDesignated.contains(pdp1));
357 assertTrue(listOfDesignated.contains(pdp2));
360 // Now all are designated. But, we have to add back the previously non-designated nodes
362 pdp3.setDesignated(true);
363 pdp4.setDesignated(true);
364 listOfDesignated.add(pdp3);
365 listOfDesignated.add(pdp4);
367 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
369 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n", listOfDesignated.size());
371 assertTrue(listOfDesignated.size()==4);
378 public void testComputeMostRecentPrimary() throws Exception {
380 logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
382 logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties");
383 Properties activeStandbyProperties = new Properties();
384 activeStandbyProperties.load(new FileInputStream(new File(
385 configDir + "/feature-active-standby-management.properties")));
386 String thisPdpId = activeStandbyProperties
387 .getProperty(ActiveStandbyProperties.NODE_NAME);
389 logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
390 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
391 "junitDroolsPU", activeStandbyProperties);
393 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
396 // Create 4 pdpd all not designated
399 long designatedDateMS = new Date().getTime();
400 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
401 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
403 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
405 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
407 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
408 pdp3.setDesignatedDate(new Date(designatedDateMS - 1));
410 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
412 pdp4.setDesignatedDate(new Date(designatedDateMS));
414 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
415 listOfAllPdps.add(pdp1);
416 listOfAllPdps.add(pdp2);
417 listOfAllPdps.add(pdp3);
418 listOfAllPdps.add(pdp4);
421 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
422 listOfDesignated.add(pdp1);
423 listOfDesignated.add(pdp2);
424 listOfDesignated.add(pdp3);
425 listOfDesignated.add(pdp4);
427 // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
428 // or all designated members.
430 // Now we want to create a StateManagementFeature and initialize it. It will be
431 // discovered by the ActiveStandbyFeature when the election handler initializes.
433 StateManagementFeatureAPI stateManagementFeature = null;
434 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
436 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
437 stateManagementFeature = feature;
438 logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
441 if(stateManagementFeature == null){
442 logger.error("testComputeMostRecentPrimary failed to initialize. "
443 + "Unable to get instance of StateManagementFeatureAPI "
444 + "with resourceID: {}", thisPdpId);
445 logger.debug("testComputeMostRecentPrimary failed to initialize. "
446 + "Unable to get instance of StateManagementFeatureAPI "
447 + "with resourceID: {}", thisPdpId);
450 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
452 DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
454 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
457 // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
458 // the one which has the most recent designated date.
461 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
464 // Now let's designate all of those on the listOfDesignated. It will choose the first one designated
467 pdp1.setDesignated(true);
468 pdp2.setDesignated(true);
469 pdp3.setDesignated(true);
470 pdp4.setDesignated(true);
472 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
474 logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
477 // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
478 // the one which was designated first
481 assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
484 // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
485 // look for the most recently designated pdp which is not currently designated.
488 pdp3.setDesignated(false);
489 pdp4.setDesignated(false);
491 listOfDesignated.remove(pdp3);
492 listOfDesignated.remove(pdp4);
494 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
496 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
498 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
502 // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
503 // look for the most recently designated pdp regardless of whether it is currently marked as designated.
506 pdp1.setDesignated(false);
507 pdp2.setDesignated(false);
509 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
511 logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
513 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
516 // If we have only one pdp on in the listOfDesignated, the most recently designated pdp will be chosen, regardless
517 // of its designation status
520 listOfDesignated.remove(pdp1);
522 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
524 logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
526 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
529 // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
532 listOfDesignated.remove(pdp2);
534 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
536 logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
538 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
544 public void testComputeDesignatedPdp() throws Exception{
546 logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
548 logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties");
549 Properties activeStandbyProperties = new Properties();
550 activeStandbyProperties.load(new FileInputStream(new File(
551 configDir + "/feature-active-standby-management.properties")));
552 String thisPdpId = activeStandbyProperties
553 .getProperty(ActiveStandbyProperties.NODE_NAME);
556 logger.debug("testComputeDesignatedPdp: Creating emfDrools");
557 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
558 "junitDroolsPU", activeStandbyProperties);
560 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
563 // Create 4 pdpd all not designated. Two on site1. Two on site2
566 long designatedDateMS = new Date().getTime();
567 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
568 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
569 pdp1.setSiteName("site1");
571 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
572 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
573 pdp2.setSiteName("site1");
576 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
577 pdp3.setDesignatedDate(new Date(designatedDateMS - 4));
578 pdp3.setSiteName("site2");
580 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
582 pdp4.setDesignatedDate(new Date(designatedDateMS));
583 pdp4.setSiteName("site2");
585 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
586 listOfAllPdps.add(pdp1);
587 listOfAllPdps.add(pdp2);
588 listOfAllPdps.add(pdp3);
589 listOfAllPdps.add(pdp4);
592 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
595 // We will first test an empty listOfDesignated. As we know from the previous JUnit,
596 // the pdp with the most designated date will be chosen for mostRecentPrimary
598 // Now we want to create a StateManagementFeature and initialize it. It will be
599 // discovered by the ActiveStandbyFeature when the election handler initializes.
601 StateManagementFeatureAPI stateManagementFeature = null;
602 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
604 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
605 stateManagementFeature = feature;
606 logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
609 if(stateManagementFeature == null){
610 logger.error("testComputeDesignatedPdp failed to initialize. "
611 + "Unable to get instance of StateManagementFeatureAPI "
612 + "with resourceID: {}", thisPdpId);
613 logger.debug("testComputeDesignatedPdp failed to initialize. "
614 + "Unable to get instance of StateManagementFeatureAPI "
615 + "with resourceID: {}", thisPdpId);
619 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
621 DroolsPdp mostRecentPrimary = pdp4;
623 DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
626 // The designatedPdp should be null
628 assertTrue(designatedPdp==null);
631 // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
633 listOfDesignated.add(pdp2);
635 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
638 // Now the designatedPdp should be the one and only selection in the listOfDesignated
641 assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
644 // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
647 listOfDesignated.add(pdp1);
649 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
652 // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
655 assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
658 // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
661 listOfDesignated.remove(pdp1);
662 listOfDesignated.add(pdp3);
664 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
667 // The designatedPdp should now be the one on the same site as the mostRecentPrimary
670 assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
675 public void testColdStandby() throws Exception {
677 logger.debug("\n\ntestColdStandby: Entering\n\n");
681 logger.debug("testColdStandby: Reading stateManagementProperties");
682 Properties stateManagementProperties = new Properties();
683 stateManagementProperties.load(new FileInputStream(new File(
684 configDir + "/feature-state-management.properties")));
686 logger.debug("testColdStandby: Creating emfXacml");
687 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
688 "junitXacmlPU", stateManagementProperties);
690 logger.debug("testColdStandby: Reading activeStandbyProperties");
691 Properties activeStandbyProperties = new Properties();
692 activeStandbyProperties.load(new FileInputStream(new File(
693 configDir + "/feature-active-standby-management.properties")));
694 String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
696 logger.debug("testColdStandby: Creating emfDrools");
697 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
698 "junitDroolsPU", activeStandbyProperties);
700 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
702 logger.debug("testColdStandby: Cleaning up tables");
703 conn.deleteAllPdps();
705 logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
706 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
708 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
709 logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
710 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
711 assertTrue(droolsPdpEntity.isDesignated() == true);
714 * When the Standby Status changes (from providingservice) to hotstandby
715 * or coldstandby,the Active/Standby selection algorithm must stand down
716 * if thePDP-D is currently the lead/active node and allow another PDP-D
719 * It must also call lock on all engines in the engine management.
724 * Yes, this is kludgy, but we have a chicken and egg problem here: we
725 * need a StateManagement object to invoke the
726 * deleteAllStateManagementEntities method.
728 logger.debug("testColdStandby: Instantiating stateManagement object");
730 StateManagement sm = new StateManagement(emfXacml, "dummy");
731 sm.deleteAllStateManagementEntities();
733 // Now we want to create a StateManagementFeature and initialize it. It will be
734 // discovered by the ActiveStandbyFeature when the election handler initializes.
736 StateManagementFeatureAPI smf = null;
737 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
739 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
741 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
745 logger.error("testColdStandby failed to initialize. "
746 + "Unable to get instance of StateManagementFeatureAPI "
747 + "with resourceID: {}", thisPdpId);
748 logger.debug("testColdStandby failed to initialize. "
749 + "Unable to get instance of StateManagementFeatureAPI "
750 + "with resourceID: {}", thisPdpId);
753 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
754 // that has been created.
755 ActiveStandbyFeatureAPI activeStandbyFeature = null;
756 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
758 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
759 activeStandbyFeature = feature;
760 logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
763 if(activeStandbyFeature == null){
764 logger.error("testColdStandby failed to initialize. "
765 + "Unable to get instance of ActiveStandbyFeatureAPI "
766 + "with resourceID:{}", thisPdpId);
767 logger.debug("testColdStandby failed to initialize. "
768 + "Unable to get instance of ActiveStandbyFeatureAPI "
769 + "with resourceID:{}", thisPdpId);
772 // Artificially putting a PDP into service is really a two step process, 1)
773 // inserting it as designated and 2) promoting it so that its standbyStatus
774 // is providing service.
776 logger.debug("testColdStandby: Runner started; Sleeping "
777 + interruptRecoveryTime + "ms before promoting PDP= {}",
779 sleep(interruptRecoveryTime);
781 logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
784 String standbyStatus = sm.getStandbyStatus(thisPdpId);
785 logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}",
786 thisPdpId, standbyStatus);
788 logger.debug("testColdStandby: Locking smf");
791 sleep(interruptRecoveryTime);
793 // Verify that the PDP is no longer designated.
795 droolsPdpEntity = conn.getPdp(thisPdpId);
796 logger.debug("testColdStandby: After lock sm.lock() invoked, "
797 + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
798 assertTrue(droolsPdpEntity.isDesignated() == false);
800 logger.debug("\n\ntestColdStandby: Exiting\n\n");
801 sleep(interruptRecoveryTime);
805 // Tests hot standby when there is only one PDP.
809 public void testHotStandby1() throws Exception {
811 logger.debug("\n\ntestHotStandby1: Entering\n\n");
815 logger.debug("testHotStandby1: Reading stateManagementProperties");
816 Properties stateManagementProperties = new Properties();
817 stateManagementProperties.load(new FileInputStream(new File(
818 configDir + "/feature-state-management.properties")));
820 logger.debug("testHotStandby1: Creating emfXacml");
821 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
822 "junitXacmlPU", stateManagementProperties);
824 logger.debug("testHotStandby1: Reading activeStandbyProperties");
825 Properties activeStandbyProperties = new Properties();
826 activeStandbyProperties.load(new FileInputStream(new File(
827 configDir + "/feature-active-standby-management.properties")));
828 String thisPdpId = activeStandbyProperties
829 .getProperty(ActiveStandbyProperties.NODE_NAME);
831 logger.debug("testHotStandby1: Creating emfDrools");
832 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
833 "junitDroolsPU", activeStandbyProperties);
835 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
837 logger.debug("testHotStandby1: Cleaning up tables");
838 conn.deleteAllPdps();
841 * Insert this PDP as not designated. Initial standby state will be
842 * either null or cold standby. Demoting should transit state to
846 logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
847 Date yesterday = DateUtils.addDays(new Date(), -1);
848 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
850 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
851 logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
852 thisPdpId, droolsPdpEntity.isDesignated());
853 assertTrue(droolsPdpEntity.isDesignated() == false);
855 logger.debug("testHotStandby1: Instantiating stateManagement object");
856 StateManagement sm = new StateManagement(emfXacml, "dummy");
857 sm.deleteAllStateManagementEntities();
860 // Now we want to create a StateManagementFeature and initialize it. It will be
861 // discovered by the ActiveStandbyFeature when the election handler initializes.
863 StateManagementFeatureAPI smf = null;
864 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
866 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
868 logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
872 logger.error("testHotStandby1 failed to initialize. "
873 + "Unable to get instance of StateManagementFeatureAPI "
874 + "with resourceID: {}", thisPdpId);
875 logger.debug("testHotStandby1 failed to initialize. "
876 + "Unable to get instance of StateManagementFeatureAPI "
877 + "with resourceID: {}", thisPdpId);
880 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
881 // that has been created.
882 ActiveStandbyFeatureAPI activeStandbyFeature = null;
883 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
885 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
886 activeStandbyFeature = feature;
887 logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
890 if(activeStandbyFeature == null){
891 logger.error("testHotStandby1 failed to initialize. "
892 + "Unable to get instance of ActiveStandbyFeatureAPI "
893 + "with resourceID: {}", thisPdpId);
894 logger.debug("testHotStandby1 failed to initialize. "
895 + "Unable to get instance of ActiveStandbyFeatureAPI "
896 + "with resourceID: {}", thisPdpId);
900 logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
901 // demoting should cause state to transit to hotstandby
905 logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
906 + "time to check droolspdpentity table", sleepTime);
910 // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
912 droolsPdpEntity = conn.getPdp(thisPdpId);
913 logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
914 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
915 assertTrue(droolsPdpEntity.isDesignated() == true);
916 String standbyStatus = smf.getStandbyStatus(thisPdpId);
917 logger.debug("testHotStandby1: After demotion, PDP= {} "
918 + "has standbyStatus= {}", thisPdpId, standbyStatus);
919 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
921 logger.debug("testHotStandby1: Stopping policyManagementRunner");
922 //policyManagementRunner.stopRunner();
924 logger.debug("\n\ntestHotStandby1: Exiting\n\n");
925 sleep(interruptRecoveryTime);
930 * Tests hot standby when two PDPs are involved.
935 public void testHotStandby2() throws Exception {
937 logger.info("\n\ntestHotStandby2: Entering\n\n");
941 logger.info("testHotStandby2: Reading stateManagementProperties");
942 Properties stateManagementProperties = new Properties();
943 stateManagementProperties.load(new FileInputStream(new File(
944 configDir + "/feature-state-management.properties")));
946 logger.info("testHotStandby2: Creating emfXacml");
947 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
948 "junitXacmlPU", stateManagementProperties);
950 logger.info("testHotStandby2: Reading activeStandbyProperties");
951 Properties activeStandbyProperties = new Properties();
952 activeStandbyProperties.load(new FileInputStream(new File(
953 configDir + "/feature-active-standby-management.properties")));
954 String thisPdpId = activeStandbyProperties
955 .getProperty(ActiveStandbyProperties.NODE_NAME);
957 logger.info("testHotStandby2: Creating emfDrools");
958 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
959 "junitDroolsPU", activeStandbyProperties);
961 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
963 logger.info("testHotStandby2: Cleaning up tables");
964 conn.deleteAllPdps();
967 // Insert a PDP that's designated but not current.
969 String activePdpId = "pdp2";
970 logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
971 Date yesterday = DateUtils.addDays(new Date(), -1);
972 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
974 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
975 logger.info("testHotStandby2: After insertion, PDP= {}, which is "
976 + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
977 assertTrue(droolsPdpEntity.isDesignated() == true);
980 * Promote the designated PDP.
982 * We have a chicken and egg problem here: we need a StateManagement
983 * object to invoke the deleteAllStateManagementEntities method.
987 logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
988 StateManagement sm = new StateManagement(emfXacml, "dummy");
989 sm.deleteAllStateManagementEntities();
992 sm = new StateManagement(emfXacml, activePdpId);//pdp2
994 // Artificially putting a PDP into service is really a two step process, 1)
995 // inserting it as designated and 2) promoting it so that its standbyStatus
996 // is providing service.
999 * Insert this PDP as not designated. Initial standby state will be
1000 * either null or cold standby. Demoting should transit state to
1005 logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
1006 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
1007 conn.insertPdp(pdp);
1008 droolsPdpEntity = conn.getPdp(thisPdpId);
1009 logger.info("testHotStandby2: After insertion, PDP={} "
1010 + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
1011 assertTrue(droolsPdpEntity.isDesignated() == false);
1014 // Now we want to create a StateManagementFeature and initialize it. It will be
1015 // discovered by the ActiveStandbyFeature when the election handler initializes.
1017 StateManagementFeatureAPI sm2 = null;
1018 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1020 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1022 logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
1026 logger.error("testHotStandby2 failed to initialize. "
1027 + "Unable to get instance of StateManagementFeatureAPI "
1028 + "with resourceID: {}", thisPdpId);
1029 logger.debug("testHotStandby2 failed to initialize. "
1030 + "Unable to get instance of StateManagementFeatureAPI "
1031 + "with resourceID: {}", thisPdpId);
1034 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1035 // that has been created.
1036 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1037 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1039 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1040 activeStandbyFeature = feature;
1041 logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1044 if(activeStandbyFeature == null){
1045 logger.error("testHotStandby2 failed to initialize. "
1046 + "Unable to get instance of ActiveStandbyFeatureAPI "
1047 + "with resourceID: {}", thisPdpId);
1048 logger.debug("testHotStandby2 failed to initialize. "
1049 + "Unable to get instance of ActiveStandbyFeatureAPI "
1050 + "with resourceID: {}", thisPdpId);
1053 logger.info("testHotStandby2: Runner started; Sleeping {} "
1054 + "ms before promoting/demoting", interruptRecoveryTime);
1055 sleep(interruptRecoveryTime);
1057 logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
1058 //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
1059 //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
1060 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
1061 sm.enableNotFailed();//pdp2
1063 String standbyStatus = sm.getStandbyStatus(activePdpId);
1064 logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
1066 // demoting PDP should ensure that state transits to hotstandby
1067 logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
1069 standbyStatus = sm.getStandbyStatus(thisPdpId);
1070 logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
1072 logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
1073 + "time to check droolspdpentity table", sleepTime);
1077 * Verify that this PDP, demoted to HOT_STANDBY, is now
1078 * re-designated and providing service.
1081 droolsPdpEntity = conn.getPdp(thisPdpId);
1082 logger.info("testHotStandby2: After demoting PDP={}"
1083 + ", DESIGNATED= {}"
1084 + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
1085 assertTrue(droolsPdpEntity.isDesignated() == true);
1086 standbyStatus = sm2.getStandbyStatus(thisPdpId);
1087 logger.info("testHotStandby2: After demoting PDP={}"
1088 + ", PDP={} has standbyStatus= {}",
1089 activePdpId, thisPdpId, standbyStatus);
1090 assertTrue(standbyStatus != null
1091 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
1093 logger.info("testHotStandby2: Stopping policyManagementRunner");
1094 //policyManagementRunner.stopRunner();
1096 logger.info("\n\ntestHotStandby2: Exiting\n\n");
1097 sleep(interruptRecoveryTime);
1102 * 1) Inserts and designates this PDP, then verifies that startTransaction
1105 * 2) Demotes PDP, and verifies that because there is only one PDP, it will
1106 * be immediately re-promoted, thus allowing startTransaction to be
1109 * 3) Locks PDP and verifies that startTransaction results in
1110 * AdministrativeStateException.
1112 * 4) Unlocks PDP and verifies that startTransaction results in
1113 * StandbyStatusException.
1115 * 5) Promotes PDP and verifies that startTransaction is once again
1121 public void testLocking1() throws Exception {
1122 logger.debug("testLocking1: Entry");
1126 logger.debug("testLocking1: Reading stateManagementProperties");
1127 Properties stateManagementProperties = new Properties();
1128 stateManagementProperties.load(new FileInputStream(new File(
1129 configDir + "/feature-state-management.properties")));
1131 logger.debug("testLocking1: Creating emfXacml");
1132 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1133 "junitXacmlPU", stateManagementProperties);
1135 logger.debug("testLocking1: Reading activeStandbyProperties");
1136 Properties activeStandbyProperties = new Properties();
1137 activeStandbyProperties.load(new FileInputStream(new File(
1138 configDir + "/feature-active-standby-management.properties")));
1139 String thisPdpId = activeStandbyProperties
1140 .getProperty(ActiveStandbyProperties.NODE_NAME);
1142 logger.debug("testLocking1: Creating emfDrools");
1143 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1144 "junitDroolsPU", activeStandbyProperties);
1146 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1148 logger.debug("testLocking1: Cleaning up tables");
1149 conn.deleteAllPdps();
1152 * Insert this PDP as designated. Initial standby state will be
1153 * either null or cold standby.
1156 logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
1157 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
1158 conn.insertPdp(pdp);
1159 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1160 logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
1161 thisPdpId, droolsPdpEntity.isDesignated());
1162 assertTrue(droolsPdpEntity.isDesignated() == true);
1164 logger.debug("testLocking1: Instantiating stateManagement object");
1165 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1166 smDummy.deleteAllStateManagementEntities();
1168 // Now we want to create a StateManagementFeature and initialize it. It will be
1169 // discovered by the ActiveStandbyFeature when the election handler initializes.
1171 StateManagementFeatureAPI sm = null;
1172 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1174 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1176 logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1180 logger.error("testLocking1 failed to initialize. "
1181 + "Unable to get instance of StateManagementFeatureAPI "
1182 + "with resourceID: {}", thisPdpId);
1183 logger.debug("testLocking1 failed to initialize. "
1184 + "Unable to get instance of StateManagementFeatureAPI "
1185 + "with resourceID: {}", thisPdpId);
1188 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1189 // that has been created.
1190 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1191 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1193 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1194 activeStandbyFeature = feature;
1195 logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1198 if(activeStandbyFeature == null){
1199 logger.error("testLocking1 failed to initialize. "
1200 + "Unable to get instance of ActiveStandbyFeatureAPI "
1201 + "with resourceID: {}", thisPdpId);
1202 logger.debug("testLocking1 failed to initialize. "
1203 + "Unable to get instance of ActiveStandbyFeatureAPI "
1204 + "with resourceID: {}", thisPdpId);
1207 logger.debug("testLocking1: Runner started; Sleeping "
1208 + interruptRecoveryTime + "ms before promoting PDP={}",
1210 sleep(interruptRecoveryTime);
1212 logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
1215 logger.debug("testLocking1: Sleeping {} ms, to allow time for "
1216 + "policy-management.Main class to come up, designated= {}",
1217 sleepTime, conn.getPdp(thisPdpId).isDesignated());
1220 logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
1221 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1224 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1226 droolsPdpIntegrityMonitor.startTransaction();
1227 droolsPdpIntegrityMonitor.endTransaction();
1228 logger.debug("testLocking1: As expected, transaction successful");
1229 } catch (AdministrativeStateException e) {
1230 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1232 } catch (StandbyStatusException e) {
1233 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1235 } catch (Exception e) {
1236 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1240 // demoting should cause state to transit to hotstandby, followed by re-promotion,
1241 // since there is only one PDP.
1242 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1245 logger.debug("testLocking1: sleeping" + electionWaitSleepTime
1246 + " to allow election handler to re-promote PDP={}", thisPdpId);
1247 sleep(electionWaitSleepTime);
1249 logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
1250 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1252 droolsPdpIntegrityMonitor.startTransaction();
1253 droolsPdpIntegrityMonitor.endTransaction();
1254 logger.debug("testLocking1: As expected, transaction successful");
1255 } catch (AdministrativeStateException e) {
1256 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1258 } catch (StandbyStatusException e) {
1259 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1261 } catch (Exception e) {
1262 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1266 // locking should cause state to transit to cold standby
1267 logger.debug("testLocking1: locking PDP={}", thisPdpId);
1270 // Just to avoid any race conditions, sleep a little after locking
1271 logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
1274 logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
1275 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1277 droolsPdpIntegrityMonitor.startTransaction();
1278 logger.error("testLocking1: startTransaction unexpectedly successful");
1280 } catch (AdministrativeStateException e) {
1281 logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
1282 } catch (StandbyStatusException e) {
1283 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1285 } catch (Exception e) {
1286 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1289 droolsPdpIntegrityMonitor.endTransaction();
1292 // unlocking should cause state to transit to hot standby and then providing service
1293 logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
1296 // Just to avoid any race conditions, sleep a little after locking
1297 logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1298 sleep(electionWaitSleepTime);
1300 logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1303 + conn.getPdp(thisPdpId).isDesignated());
1305 droolsPdpIntegrityMonitor.startTransaction();
1306 logger.error("testLocking1: startTransaction successful as expected");
1307 } catch (AdministrativeStateException e) {
1308 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1310 } catch (StandbyStatusException e) {
1311 logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1313 } catch (Exception e) {
1314 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1317 droolsPdpIntegrityMonitor.endTransaction();
1320 // demoting should cause state to transit to hot standby
1321 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1324 // Just to avoid any race conditions, sleep a little after promoting
1325 logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
1328 logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
1329 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1331 droolsPdpIntegrityMonitor.startTransaction();
1332 droolsPdpIntegrityMonitor.endTransaction();
1333 logger.debug("testLocking1: Unexpectedly, transaction successful");
1335 } catch (AdministrativeStateException e) {
1336 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1338 } catch (StandbyStatusException e) {
1339 logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
1340 } catch (Exception e) {
1341 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1345 logger.debug("\n\ntestLocking1: Exiting\n\n");
1346 sleep(interruptRecoveryTime);
1352 * 1) Inserts and designates this PDP, then verifies that startTransaction
1355 * 2) Inserts another PDP in hotstandby.
1357 * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1358 * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1363 public void testLocking2() throws Exception {
1365 logger.debug("\n\ntestLocking2: Entering\n\n");
1369 logger.debug("testLocking2: Reading stateManagementProperties");
1370 Properties stateManagementProperties = new Properties();
1371 stateManagementProperties.load(new FileInputStream(new File(
1372 configDir + "/feature-state-management.properties")));
1374 logger.debug("testLocking2: Creating emfXacml");
1375 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1376 "junitXacmlPU", stateManagementProperties);
1378 logger.debug("testLocking2: Reading activeStandbyProperties");
1379 Properties activeStandbyProperties = new Properties();
1380 activeStandbyProperties.load(new FileInputStream(new File(
1381 configDir + "/feature-active-standby-management.properties")));
1382 String thisPdpId = activeStandbyProperties
1383 .getProperty(ActiveStandbyProperties.NODE_NAME);
1385 logger.debug("testLocking2: Creating emfDrools");
1386 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1387 "junitDroolsPU", activeStandbyProperties);
1389 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1391 logger.debug("testLocking2: Cleaning up tables");
1392 conn.deleteAllPdps();
1395 * Insert this PDP as designated. Initial standby state will be
1396 * either null or cold standby. Demoting should transit state to
1400 logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
1401 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
1402 conn.insertPdp(pdp);
1403 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1404 logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
1405 thisPdpId, droolsPdpEntity.isDesignated());
1406 assertTrue(droolsPdpEntity.isDesignated() == true);
1408 logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
1409 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1410 smDummy.deleteAllStateManagementEntities();
1412 // Now we want to create a StateManagementFeature and initialize it. It will be
1413 // discovered by the ActiveStandbyFeature when the election handler initializes.
1415 StateManagementFeatureAPI sm = null;
1416 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1418 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1420 logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1424 logger.error("testLocking2 failed to initialize. "
1425 + "Unable to get instance of StateManagementFeatureAPI "
1426 + "with resourceID: {}", thisPdpId);
1427 logger.debug("testLocking2 failed to initialize. "
1428 + "Unable to get instance of StateManagementFeatureAPI "
1429 + "with resourceID: {}", thisPdpId);
1432 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1433 // that has been created.
1434 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1435 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1437 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1438 activeStandbyFeature = feature;
1439 logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1442 if(activeStandbyFeature == null){
1443 logger.error("testLocking2 failed to initialize. "
1444 + "Unable to get instance of ActiveStandbyFeatureAPI "
1445 + "with resourceID: {}", thisPdpId);
1446 logger.debug("testLocking2 failed to initialize. "
1447 + "Unable to get instance of ActiveStandbyFeatureAPI "
1448 + "with resourceID: {}", thisPdpId);
1452 * Insert another PDP as not designated. Initial standby state will be
1453 * either null or cold standby. Demoting should transit state to
1457 String standbyPdpId = "pdp2";
1458 logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
1459 Date yesterday = DateUtils.addDays(new Date(), -1);
1460 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1461 conn.insertPdp(pdp);
1462 droolsPdpEntity = conn.getPdp(standbyPdpId);
1463 logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
1464 standbyPdpId, droolsPdpEntity.isDesignated());
1465 assertTrue(droolsPdpEntity.isDesignated() == false);
1467 logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
1468 StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
1470 logger.debug("testLocking2: Runner started; Sleeping {} ms "
1471 + "before promoting/demoting", interruptRecoveryTime);
1472 sleep(interruptRecoveryTime);
1474 logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
1477 // demoting PDP should ensure that state transits to hotstandby
1478 logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
1481 logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime);
1484 logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
1485 + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1487 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1490 droolsPdpIntegrityMonitor.startTransaction();
1491 droolsPdpIntegrityMonitor.endTransaction();
1492 logger.debug("testLocking2: As expected, transaction successful");
1493 } catch (AdministrativeStateException e) {
1494 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1496 } catch (StandbyStatusException e) {
1497 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1499 } catch (Exception e) {
1500 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1504 // demoting should cause state to transit to hotstandby followed by re-promotion.
1505 logger.debug("testLocking2: demoting PDP={}", thisPdpId);
1508 logger.debug("testLocking2: sleeping {}"
1509 + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId);
1510 sleep(electionWaitSleepTime);
1512 logger.debug("testLocking2: Waking up and invoking startTransaction "
1513 + "on re-promoted PDP= {}, designated= {}",
1514 thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1516 droolsPdpIntegrityMonitor.startTransaction();
1517 droolsPdpIntegrityMonitor.endTransaction();
1518 logger.debug("testLocking2: As expected, transaction successful");
1519 } catch (AdministrativeStateException e) {
1520 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1522 } catch (StandbyStatusException e) {
1523 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1525 } catch (Exception e) {
1526 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1530 logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
1531 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
1532 assertTrue(standbyPdpDesignated == false);
1534 logger.debug("\n\ntestLocking2: Exiting\n\n");
1535 sleep(interruptRecoveryTime);
1538 private void sleep(long sleepms) throws InterruptedException {
1539 Thread.sleep(sleepms);