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")));
222 ActiveStandbyProperties.initProperties(activeStandbyProperties);
223 String thisPdpId = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
225 logger.debug("testPMStandbyStateChangeNotifier: Getting StateManagementFeatureAPI");
227 StateManagementFeatureAPI sm = null;
228 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
230 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
232 logger.debug("testPMStandbyStateChangeNotifier stateManagementFeature.getResourceName(): {}", sm.getResourceName());
236 logger.error("testPMStandbyStateChangeNotifier failed to initialize. "
237 + "Unable to get instance of StateManagementFeatureAPI "
238 + "with resourceID: {}", thisPdpId);
239 logger.debug("testPMStandbyStateChangeNotifier failed to initialize. "
240 + "Unable to get instance of StateManagementFeatureAPI "
241 + "with resourceID: {}", thisPdpId);
244 //Create an instance of the Observer
245 PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
247 //Register the PMStandbyStateChangeNotifier Observer
248 sm.addObserver(pmNotifier);
250 //At this point the standbystatus = 'null'
252 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
255 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
257 //Adding standbystatus=hotstandby
259 System.out.println(pmNotifier.getPreviousStandbyStatus());
260 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
262 //Now making standbystatus=coldstandby
264 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
266 //standbystatus = hotstandby
268 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
270 //standbystatus = providingservice
272 //The previousStandbyStatus is not updated until after the delay activation expires
273 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
275 //Sleep long enough for the delayActivationTimer to run
277 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
279 //standbystatus = providingservice
281 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
283 //standbystatus = coldstandby
285 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
287 //standbystatus = hotstandby
289 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
291 //standbystatus = hotstandby
293 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
298 public void testSanitizeDesignatedList() throws Exception {
300 logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
302 // Get a DroolsPdpsConnector
304 logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties");
305 Properties activeStandbyProperties = new Properties();
306 activeStandbyProperties.load(new FileInputStream(new File(
307 configDir + "/feature-active-standby-management.properties")));
308 String thisPdpId = activeStandbyProperties
309 .getProperty(ActiveStandbyProperties.NODE_NAME);
311 logger.debug("testSanitizeDesignatedList: Creating emfDrools");
312 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
313 "junitDroolsPU", activeStandbyProperties);
315 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
317 // Create 4 pdpd all not designated
319 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
320 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
321 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
322 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
324 List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
325 listOfDesignated.add(pdp1);
326 listOfDesignated.add(pdp2);
327 listOfDesignated.add(pdp3);
328 listOfDesignated.add(pdp4);
330 // Now we want to create a StateManagementFeature and initialize it. It will be
331 // discovered by the ActiveStandbyFeature when the election handler initializes.
333 StateManagementFeatureAPI stateManagementFeature = null;
334 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
336 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
337 stateManagementFeature = feature;
338 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
341 if(stateManagementFeature == null){
342 logger.error("testColdStandby failed to initialize. "
343 + "Unable to get instance of StateManagementFeatureAPI "
344 + "with resourceID: {}", thisPdpId);
345 logger.debug("testColdStandby failed to initialize. "
346 + "Unable to get instance of StateManagementFeatureAPI "
347 + "with resourceID: {}", thisPdpId);
351 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
353 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
355 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
357 assertTrue(listOfDesignated.size()==4);
359 // Now make 2 designated
361 pdp1.setDesignated(true);
362 pdp2.setDesignated(true);
364 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
366 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n", listOfDesignated.size());
368 assertTrue(listOfDesignated.size()==2);
369 assertTrue(listOfDesignated.contains(pdp1));
370 assertTrue(listOfDesignated.contains(pdp2));
373 // Now all are designated. But, we have to add back the previously non-designated nodes
375 pdp3.setDesignated(true);
376 pdp4.setDesignated(true);
377 listOfDesignated.add(pdp3);
378 listOfDesignated.add(pdp4);
380 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
382 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n", listOfDesignated.size());
384 assertTrue(listOfDesignated.size()==4);
391 public void testComputeMostRecentPrimary() throws Exception {
393 logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
395 logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties");
396 Properties activeStandbyProperties = new Properties();
397 activeStandbyProperties.load(new FileInputStream(new File(
398 configDir + "/feature-active-standby-management.properties")));
399 String thisPdpId = activeStandbyProperties
400 .getProperty(ActiveStandbyProperties.NODE_NAME);
402 logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
403 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
404 "junitDroolsPU", activeStandbyProperties);
406 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
409 // Create 4 pdpd all not designated
412 long designatedDateMS = new Date().getTime();
413 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
414 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
416 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
418 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
420 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
421 pdp3.setDesignatedDate(new Date(designatedDateMS - 1));
423 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
425 pdp4.setDesignatedDate(new Date(designatedDateMS));
427 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
428 listOfAllPdps.add(pdp1);
429 listOfAllPdps.add(pdp2);
430 listOfAllPdps.add(pdp3);
431 listOfAllPdps.add(pdp4);
434 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
435 listOfDesignated.add(pdp1);
436 listOfDesignated.add(pdp2);
437 listOfDesignated.add(pdp3);
438 listOfDesignated.add(pdp4);
440 // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
441 // or all designated members.
443 // Now we want to create a StateManagementFeature and initialize it. It will be
444 // discovered by the ActiveStandbyFeature when the election handler initializes.
446 StateManagementFeatureAPI stateManagementFeature = null;
447 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
449 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
450 stateManagementFeature = feature;
451 logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
454 if(stateManagementFeature == null){
455 logger.error("testComputeMostRecentPrimary failed to initialize. "
456 + "Unable to get instance of StateManagementFeatureAPI "
457 + "with resourceID: {}", thisPdpId);
458 logger.debug("testComputeMostRecentPrimary failed to initialize. "
459 + "Unable to get instance of StateManagementFeatureAPI "
460 + "with resourceID: {}", thisPdpId);
463 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
465 DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
467 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
470 // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
471 // the one which has the most recent designated date.
474 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
477 // Now let's designate all of those on the listOfDesignated. It will choose the first one designated
480 pdp1.setDesignated(true);
481 pdp2.setDesignated(true);
482 pdp3.setDesignated(true);
483 pdp4.setDesignated(true);
485 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
487 logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
490 // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
491 // the one which was designated first
494 assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
497 // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
498 // look for the most recently designated pdp which is not currently designated.
501 pdp3.setDesignated(false);
502 pdp4.setDesignated(false);
504 listOfDesignated.remove(pdp3);
505 listOfDesignated.remove(pdp4);
507 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
509 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
511 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
515 // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
516 // look for the most recently designated pdp regardless of whether it is currently marked as designated.
519 pdp1.setDesignated(false);
520 pdp2.setDesignated(false);
522 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
524 logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
526 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
529 // If we have only one pdp on in the listOfDesignated, the most recently designated pdp will be chosen, regardless
530 // of its designation status
533 listOfDesignated.remove(pdp1);
535 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
537 logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
539 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
542 // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
545 listOfDesignated.remove(pdp2);
547 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
549 logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
551 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
557 public void testComputeDesignatedPdp() throws Exception{
559 logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
561 logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties");
562 Properties activeStandbyProperties = new Properties();
563 activeStandbyProperties.load(new FileInputStream(new File(
564 configDir + "/feature-active-standby-management.properties")));
565 String thisPdpId = activeStandbyProperties
566 .getProperty(ActiveStandbyProperties.NODE_NAME);
569 logger.debug("testComputeDesignatedPdp: Creating emfDrools");
570 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
571 "junitDroolsPU", activeStandbyProperties);
573 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
576 // Create 4 pdpd all not designated. Two on site1. Two on site2
579 long designatedDateMS = new Date().getTime();
580 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
581 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
582 pdp1.setSiteName("site1");
584 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
585 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
586 pdp2.setSiteName("site1");
589 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
590 pdp3.setDesignatedDate(new Date(designatedDateMS - 4));
591 pdp3.setSiteName("site2");
593 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
595 pdp4.setDesignatedDate(new Date(designatedDateMS));
596 pdp4.setSiteName("site2");
598 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
599 listOfAllPdps.add(pdp1);
600 listOfAllPdps.add(pdp2);
601 listOfAllPdps.add(pdp3);
602 listOfAllPdps.add(pdp4);
605 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
608 // We will first test an empty listOfDesignated. As we know from the previous JUnit,
609 // the pdp with the most designated date will be chosen for mostRecentPrimary
611 // Now we want to create a StateManagementFeature and initialize it. It will be
612 // discovered by the ActiveStandbyFeature when the election handler initializes.
614 StateManagementFeatureAPI stateManagementFeature = null;
615 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
617 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
618 stateManagementFeature = feature;
619 logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
622 if(stateManagementFeature == null){
623 logger.error("testComputeDesignatedPdp failed to initialize. "
624 + "Unable to get instance of StateManagementFeatureAPI "
625 + "with resourceID: {}", thisPdpId);
626 logger.debug("testComputeDesignatedPdp failed to initialize. "
627 + "Unable to get instance of StateManagementFeatureAPI "
628 + "with resourceID: {}", thisPdpId);
632 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
634 DroolsPdp mostRecentPrimary = pdp4;
636 DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
639 // The designatedPdp should be null
641 assertTrue(designatedPdp==null);
644 // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
646 listOfDesignated.add(pdp2);
648 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
651 // Now the designatedPdp should be the one and only selection in the listOfDesignated
654 assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
657 // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
660 listOfDesignated.add(pdp1);
662 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
665 // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
668 assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
671 // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
674 listOfDesignated.remove(pdp1);
675 listOfDesignated.add(pdp3);
677 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
680 // The designatedPdp should now be the one on the same site as the mostRecentPrimary
683 assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
688 public void testColdStandby() throws Exception {
690 logger.debug("\n\ntestColdStandby: Entering\n\n");
694 logger.debug("testColdStandby: Reading stateManagementProperties");
695 Properties stateManagementProperties = new Properties();
696 stateManagementProperties.load(new FileInputStream(new File(
697 configDir + "/feature-state-management.properties")));
699 logger.debug("testColdStandby: Creating emfXacml");
700 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
701 "junitXacmlPU", stateManagementProperties);
703 logger.debug("testColdStandby: Reading activeStandbyProperties");
704 Properties activeStandbyProperties = new Properties();
705 activeStandbyProperties.load(new FileInputStream(new File(
706 configDir + "/feature-active-standby-management.properties")));
707 String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
709 logger.debug("testColdStandby: Creating emfDrools");
710 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
711 "junitDroolsPU", activeStandbyProperties);
713 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
715 logger.debug("testColdStandby: Cleaning up tables");
716 conn.deleteAllPdps();
718 logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
719 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
721 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
722 logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
723 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
724 assertTrue(droolsPdpEntity.isDesignated() == true);
727 * When the Standby Status changes (from providingservice) to hotstandby
728 * or coldstandby,the Active/Standby selection algorithm must stand down
729 * if thePDP-D is currently the lead/active node and allow another PDP-D
732 * It must also call lock on all engines in the engine management.
737 * Yes, this is kludgy, but we have a chicken and egg problem here: we
738 * need a StateManagement object to invoke the
739 * deleteAllStateManagementEntities method.
741 logger.debug("testColdStandby: Instantiating stateManagement object");
743 StateManagement sm = new StateManagement(emfXacml, "dummy");
744 sm.deleteAllStateManagementEntities();
746 // Now we want to create a StateManagementFeature and initialize it. It will be
747 // discovered by the ActiveStandbyFeature when the election handler initializes.
749 StateManagementFeatureAPI smf = null;
750 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
752 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
754 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
758 logger.error("testColdStandby failed to initialize. "
759 + "Unable to get instance of StateManagementFeatureAPI "
760 + "with resourceID: {}", thisPdpId);
761 logger.debug("testColdStandby failed to initialize. "
762 + "Unable to get instance of StateManagementFeatureAPI "
763 + "with resourceID: {}", thisPdpId);
766 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
767 // that has been created.
768 ActiveStandbyFeatureAPI activeStandbyFeature = null;
769 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
771 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
772 activeStandbyFeature = feature;
773 logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
776 if(activeStandbyFeature == null){
777 logger.error("testColdStandby failed to initialize. "
778 + "Unable to get instance of ActiveStandbyFeatureAPI "
779 + "with resourceID:{}", thisPdpId);
780 logger.debug("testColdStandby failed to initialize. "
781 + "Unable to get instance of ActiveStandbyFeatureAPI "
782 + "with resourceID:{}", thisPdpId);
785 // Artificially putting a PDP into service is really a two step process, 1)
786 // inserting it as designated and 2) promoting it so that its standbyStatus
787 // is providing service.
789 logger.debug("testColdStandby: Runner started; Sleeping "
790 + interruptRecoveryTime + "ms before promoting PDP= {}",
792 sleep(interruptRecoveryTime);
794 logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
797 String standbyStatus = sm.getStandbyStatus(thisPdpId);
798 logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}",
799 thisPdpId, standbyStatus);
801 logger.debug("testColdStandby: Locking smf");
804 sleep(interruptRecoveryTime);
806 // Verify that the PDP is no longer designated.
808 droolsPdpEntity = conn.getPdp(thisPdpId);
809 logger.debug("testColdStandby: After lock sm.lock() invoked, "
810 + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
811 assertTrue(droolsPdpEntity.isDesignated() == false);
813 logger.debug("\n\ntestColdStandby: Exiting\n\n");
814 sleep(interruptRecoveryTime);
818 // Tests hot standby when there is only one PDP.
822 public void testHotStandby1() throws Exception {
824 logger.debug("\n\ntestHotStandby1: Entering\n\n");
828 logger.debug("testHotStandby1: Reading stateManagementProperties");
829 Properties stateManagementProperties = new Properties();
830 stateManagementProperties.load(new FileInputStream(new File(
831 configDir + "/feature-state-management.properties")));
833 logger.debug("testHotStandby1: Creating emfXacml");
834 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
835 "junitXacmlPU", stateManagementProperties);
837 logger.debug("testHotStandby1: Reading activeStandbyProperties");
838 Properties activeStandbyProperties = new Properties();
839 activeStandbyProperties.load(new FileInputStream(new File(
840 configDir + "/feature-active-standby-management.properties")));
841 String thisPdpId = activeStandbyProperties
842 .getProperty(ActiveStandbyProperties.NODE_NAME);
844 logger.debug("testHotStandby1: Creating emfDrools");
845 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
846 "junitDroolsPU", activeStandbyProperties);
848 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
850 logger.debug("testHotStandby1: Cleaning up tables");
851 conn.deleteAllPdps();
854 * Insert this PDP as not designated. Initial standby state will be
855 * either null or cold standby. Demoting should transit state to
859 logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
860 Date yesterday = DateUtils.addDays(new Date(), -1);
861 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
863 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
864 logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
865 thisPdpId, droolsPdpEntity.isDesignated());
866 assertTrue(droolsPdpEntity.isDesignated() == false);
868 logger.debug("testHotStandby1: Instantiating stateManagement object");
869 StateManagement sm = new StateManagement(emfXacml, "dummy");
870 sm.deleteAllStateManagementEntities();
873 // Now we want to create a StateManagementFeature and initialize it. It will be
874 // discovered by the ActiveStandbyFeature when the election handler initializes.
876 StateManagementFeatureAPI smf = null;
877 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
879 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
881 logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
885 logger.error("testHotStandby1 failed to initialize. "
886 + "Unable to get instance of StateManagementFeatureAPI "
887 + "with resourceID: {}", thisPdpId);
888 logger.debug("testHotStandby1 failed to initialize. "
889 + "Unable to get instance of StateManagementFeatureAPI "
890 + "with resourceID: {}", thisPdpId);
893 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
894 // that has been created.
895 ActiveStandbyFeatureAPI activeStandbyFeature = null;
896 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
898 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
899 activeStandbyFeature = feature;
900 logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
903 if(activeStandbyFeature == null){
904 logger.error("testHotStandby1 failed to initialize. "
905 + "Unable to get instance of ActiveStandbyFeatureAPI "
906 + "with resourceID: {}", thisPdpId);
907 logger.debug("testHotStandby1 failed to initialize. "
908 + "Unable to get instance of ActiveStandbyFeatureAPI "
909 + "with resourceID: {}", thisPdpId);
913 logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
914 // demoting should cause state to transit to hotstandby
918 logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
919 + "time to check droolspdpentity table", sleepTime);
923 // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
925 droolsPdpEntity = conn.getPdp(thisPdpId);
926 logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
927 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
928 assertTrue(droolsPdpEntity.isDesignated() == true);
929 String standbyStatus = smf.getStandbyStatus(thisPdpId);
930 logger.debug("testHotStandby1: After demotion, PDP= {} "
931 + "has standbyStatus= {}", thisPdpId, standbyStatus);
932 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
934 logger.debug("testHotStandby1: Stopping policyManagementRunner");
935 //policyManagementRunner.stopRunner();
937 logger.debug("\n\ntestHotStandby1: Exiting\n\n");
938 sleep(interruptRecoveryTime);
943 * Tests hot standby when two PDPs are involved.
948 public void testHotStandby2() throws Exception {
950 logger.info("\n\ntestHotStandby2: Entering\n\n");
954 logger.info("testHotStandby2: Reading stateManagementProperties");
955 Properties stateManagementProperties = new Properties();
956 stateManagementProperties.load(new FileInputStream(new File(
957 configDir + "/feature-state-management.properties")));
959 logger.info("testHotStandby2: Creating emfXacml");
960 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
961 "junitXacmlPU", stateManagementProperties);
963 logger.info("testHotStandby2: Reading activeStandbyProperties");
964 Properties activeStandbyProperties = new Properties();
965 activeStandbyProperties.load(new FileInputStream(new File(
966 configDir + "/feature-active-standby-management.properties")));
967 String thisPdpId = activeStandbyProperties
968 .getProperty(ActiveStandbyProperties.NODE_NAME);
970 logger.info("testHotStandby2: Creating emfDrools");
971 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
972 "junitDroolsPU", activeStandbyProperties);
974 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
976 logger.info("testHotStandby2: Cleaning up tables");
977 conn.deleteAllPdps();
980 // Insert a PDP that's designated but not current.
982 String activePdpId = "pdp2";
983 logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
984 Date yesterday = DateUtils.addDays(new Date(), -1);
985 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
987 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
988 logger.info("testHotStandby2: After insertion, PDP= {}, which is "
989 + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
990 assertTrue(droolsPdpEntity.isDesignated() == true);
993 * Promote the designated PDP.
995 * We have a chicken and egg problem here: we need a StateManagement
996 * object to invoke the deleteAllStateManagementEntities method.
1000 logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
1001 StateManagement sm = new StateManagement(emfXacml, "dummy");
1002 sm.deleteAllStateManagementEntities();
1005 sm = new StateManagement(emfXacml, activePdpId);//pdp2
1007 // Artificially putting a PDP into service is really a two step process, 1)
1008 // inserting it as designated and 2) promoting it so that its standbyStatus
1009 // is providing service.
1012 * Insert this PDP as not designated. Initial standby state will be
1013 * either null or cold standby. Demoting should transit state to
1018 logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
1019 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
1020 conn.insertPdp(pdp);
1021 droolsPdpEntity = conn.getPdp(thisPdpId);
1022 logger.info("testHotStandby2: After insertion, PDP={} "
1023 + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
1024 assertTrue(droolsPdpEntity.isDesignated() == false);
1027 // Now we want to create a StateManagementFeature and initialize it. It will be
1028 // discovered by the ActiveStandbyFeature when the election handler initializes.
1030 StateManagementFeatureAPI sm2 = null;
1031 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1033 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1035 logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
1039 logger.error("testHotStandby2 failed to initialize. "
1040 + "Unable to get instance of StateManagementFeatureAPI "
1041 + "with resourceID: {}", thisPdpId);
1042 logger.debug("testHotStandby2 failed to initialize. "
1043 + "Unable to get instance of StateManagementFeatureAPI "
1044 + "with resourceID: {}", thisPdpId);
1047 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1048 // that has been created.
1049 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1050 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1052 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1053 activeStandbyFeature = feature;
1054 logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1057 if(activeStandbyFeature == null){
1058 logger.error("testHotStandby2 failed to initialize. "
1059 + "Unable to get instance of ActiveStandbyFeatureAPI "
1060 + "with resourceID: {}", thisPdpId);
1061 logger.debug("testHotStandby2 failed to initialize. "
1062 + "Unable to get instance of ActiveStandbyFeatureAPI "
1063 + "with resourceID: {}", thisPdpId);
1066 logger.info("testHotStandby2: Runner started; Sleeping {} "
1067 + "ms before promoting/demoting", interruptRecoveryTime);
1068 sleep(interruptRecoveryTime);
1070 logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
1071 //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
1072 //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
1073 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
1074 sm.enableNotFailed();//pdp2
1076 String standbyStatus = sm.getStandbyStatus(activePdpId);
1077 logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
1079 // demoting PDP should ensure that state transits to hotstandby
1080 logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
1082 standbyStatus = sm.getStandbyStatus(thisPdpId);
1083 logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
1085 logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
1086 + "time to check droolspdpentity table", sleepTime);
1090 * Verify that this PDP, demoted to HOT_STANDBY, is now
1091 * re-designated and providing service.
1094 droolsPdpEntity = conn.getPdp(thisPdpId);
1095 logger.info("testHotStandby2: After demoting PDP={}"
1096 + ", DESIGNATED= {}"
1097 + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
1098 assertTrue(droolsPdpEntity.isDesignated() == true);
1099 standbyStatus = sm2.getStandbyStatus(thisPdpId);
1100 logger.info("testHotStandby2: After demoting PDP={}"
1101 + ", PDP={} has standbyStatus= {}",
1102 activePdpId, thisPdpId, standbyStatus);
1103 assertTrue(standbyStatus != null
1104 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
1106 logger.info("testHotStandby2: Stopping policyManagementRunner");
1107 //policyManagementRunner.stopRunner();
1109 logger.info("\n\ntestHotStandby2: Exiting\n\n");
1110 sleep(interruptRecoveryTime);
1115 * 1) Inserts and designates this PDP, then verifies that startTransaction
1118 * 2) Demotes PDP, and verifies that because there is only one PDP, it will
1119 * be immediately re-promoted, thus allowing startTransaction to be
1122 * 3) Locks PDP and verifies that startTransaction results in
1123 * AdministrativeStateException.
1125 * 4) Unlocks PDP and verifies that startTransaction results in
1126 * StandbyStatusException.
1128 * 5) Promotes PDP and verifies that startTransaction is once again
1134 public void testLocking1() throws Exception {
1135 logger.debug("testLocking1: Entry");
1139 logger.debug("testLocking1: Reading stateManagementProperties");
1140 Properties stateManagementProperties = new Properties();
1141 stateManagementProperties.load(new FileInputStream(new File(
1142 configDir + "/feature-state-management.properties")));
1144 logger.debug("testLocking1: Creating emfXacml");
1145 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1146 "junitXacmlPU", stateManagementProperties);
1148 logger.debug("testLocking1: Reading activeStandbyProperties");
1149 Properties activeStandbyProperties = new Properties();
1150 activeStandbyProperties.load(new FileInputStream(new File(
1151 configDir + "/feature-active-standby-management.properties")));
1152 String thisPdpId = activeStandbyProperties
1153 .getProperty(ActiveStandbyProperties.NODE_NAME);
1155 logger.debug("testLocking1: Creating emfDrools");
1156 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1157 "junitDroolsPU", activeStandbyProperties);
1159 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1161 logger.debug("testLocking1: Cleaning up tables");
1162 conn.deleteAllPdps();
1165 * Insert this PDP as designated. Initial standby state will be
1166 * either null or cold standby.
1169 logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
1170 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
1171 conn.insertPdp(pdp);
1172 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1173 logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
1174 thisPdpId, droolsPdpEntity.isDesignated());
1175 assertTrue(droolsPdpEntity.isDesignated() == true);
1177 logger.debug("testLocking1: Instantiating stateManagement object");
1178 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1179 smDummy.deleteAllStateManagementEntities();
1181 // Now we want to create a StateManagementFeature and initialize it. It will be
1182 // discovered by the ActiveStandbyFeature when the election handler initializes.
1184 StateManagementFeatureAPI sm = null;
1185 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1187 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1189 logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1193 logger.error("testLocking1 failed to initialize. "
1194 + "Unable to get instance of StateManagementFeatureAPI "
1195 + "with resourceID: {}", thisPdpId);
1196 logger.debug("testLocking1 failed to initialize. "
1197 + "Unable to get instance of StateManagementFeatureAPI "
1198 + "with resourceID: {}", thisPdpId);
1201 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1202 // that has been created.
1203 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1204 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1206 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1207 activeStandbyFeature = feature;
1208 logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1211 if(activeStandbyFeature == null){
1212 logger.error("testLocking1 failed to initialize. "
1213 + "Unable to get instance of ActiveStandbyFeatureAPI "
1214 + "with resourceID: {}", thisPdpId);
1215 logger.debug("testLocking1 failed to initialize. "
1216 + "Unable to get instance of ActiveStandbyFeatureAPI "
1217 + "with resourceID: {}", thisPdpId);
1220 logger.debug("testLocking1: Runner started; Sleeping "
1221 + interruptRecoveryTime + "ms before promoting PDP={}",
1223 sleep(interruptRecoveryTime);
1225 logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
1228 logger.debug("testLocking1: Sleeping {} ms, to allow time for "
1229 + "policy-management.Main class to come up, designated= {}",
1230 sleepTime, conn.getPdp(thisPdpId).isDesignated());
1233 logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
1234 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1237 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1239 droolsPdpIntegrityMonitor.startTransaction();
1240 droolsPdpIntegrityMonitor.endTransaction();
1241 logger.debug("testLocking1: As expected, transaction successful");
1242 } catch (AdministrativeStateException e) {
1243 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1245 } catch (StandbyStatusException e) {
1246 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1248 } catch (Exception e) {
1249 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1253 // demoting should cause state to transit to hotstandby, followed by re-promotion,
1254 // since there is only one PDP.
1255 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1258 logger.debug("testLocking1: sleeping" + electionWaitSleepTime
1259 + " to allow election handler to re-promote PDP={}", thisPdpId);
1260 sleep(electionWaitSleepTime);
1262 logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
1263 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1265 droolsPdpIntegrityMonitor.startTransaction();
1266 droolsPdpIntegrityMonitor.endTransaction();
1267 logger.debug("testLocking1: As expected, transaction successful");
1268 } catch (AdministrativeStateException e) {
1269 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1271 } catch (StandbyStatusException e) {
1272 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1274 } catch (Exception e) {
1275 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1279 // locking should cause state to transit to cold standby
1280 logger.debug("testLocking1: locking PDP={}", thisPdpId);
1283 // Just to avoid any race conditions, sleep a little after locking
1284 logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
1287 logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
1288 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1290 droolsPdpIntegrityMonitor.startTransaction();
1291 logger.error("testLocking1: startTransaction unexpectedly successful");
1293 } catch (AdministrativeStateException e) {
1294 logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
1295 } catch (StandbyStatusException e) {
1296 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1298 } catch (Exception e) {
1299 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1302 droolsPdpIntegrityMonitor.endTransaction();
1305 // unlocking should cause state to transit to hot standby and then providing service
1306 logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
1309 // Just to avoid any race conditions, sleep a little after locking
1310 logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1311 sleep(electionWaitSleepTime);
1313 logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1316 + conn.getPdp(thisPdpId).isDesignated());
1318 droolsPdpIntegrityMonitor.startTransaction();
1319 logger.error("testLocking1: startTransaction successful as expected");
1320 } catch (AdministrativeStateException e) {
1321 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1323 } catch (StandbyStatusException e) {
1324 logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1326 } catch (Exception e) {
1327 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1330 droolsPdpIntegrityMonitor.endTransaction();
1333 // demoting should cause state to transit to hot standby
1334 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1337 // Just to avoid any race conditions, sleep a little after promoting
1338 logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
1341 logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
1342 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1344 droolsPdpIntegrityMonitor.startTransaction();
1345 droolsPdpIntegrityMonitor.endTransaction();
1346 logger.debug("testLocking1: Unexpectedly, transaction successful");
1348 } catch (AdministrativeStateException e) {
1349 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1351 } catch (StandbyStatusException e) {
1352 logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
1353 } catch (Exception e) {
1354 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1358 logger.debug("\n\ntestLocking1: Exiting\n\n");
1359 sleep(interruptRecoveryTime);
1365 * 1) Inserts and designates this PDP, then verifies that startTransaction
1368 * 2) Inserts another PDP in hotstandby.
1370 * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1371 * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1376 public void testLocking2() throws Exception {
1378 logger.debug("\n\ntestLocking2: Entering\n\n");
1382 logger.debug("testLocking2: Reading stateManagementProperties");
1383 Properties stateManagementProperties = new Properties();
1384 stateManagementProperties.load(new FileInputStream(new File(
1385 configDir + "/feature-state-management.properties")));
1387 logger.debug("testLocking2: Creating emfXacml");
1388 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1389 "junitXacmlPU", stateManagementProperties);
1391 logger.debug("testLocking2: Reading activeStandbyProperties");
1392 Properties activeStandbyProperties = new Properties();
1393 activeStandbyProperties.load(new FileInputStream(new File(
1394 configDir + "/feature-active-standby-management.properties")));
1395 String thisPdpId = activeStandbyProperties
1396 .getProperty(ActiveStandbyProperties.NODE_NAME);
1398 logger.debug("testLocking2: Creating emfDrools");
1399 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1400 "junitDroolsPU", activeStandbyProperties);
1402 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1404 logger.debug("testLocking2: Cleaning up tables");
1405 conn.deleteAllPdps();
1408 * Insert this PDP as designated. Initial standby state will be
1409 * either null or cold standby. Demoting should transit state to
1413 logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
1414 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
1415 conn.insertPdp(pdp);
1416 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1417 logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
1418 thisPdpId, droolsPdpEntity.isDesignated());
1419 assertTrue(droolsPdpEntity.isDesignated() == true);
1421 logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
1422 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1423 smDummy.deleteAllStateManagementEntities();
1425 // Now we want to create a StateManagementFeature and initialize it. It will be
1426 // discovered by the ActiveStandbyFeature when the election handler initializes.
1428 StateManagementFeatureAPI sm = null;
1429 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1431 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1433 logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1437 logger.error("testLocking2 failed to initialize. "
1438 + "Unable to get instance of StateManagementFeatureAPI "
1439 + "with resourceID: {}", thisPdpId);
1440 logger.debug("testLocking2 failed to initialize. "
1441 + "Unable to get instance of StateManagementFeatureAPI "
1442 + "with resourceID: {}", thisPdpId);
1445 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1446 // that has been created.
1447 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1448 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1450 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1451 activeStandbyFeature = feature;
1452 logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1455 if(activeStandbyFeature == null){
1456 logger.error("testLocking2 failed to initialize. "
1457 + "Unable to get instance of ActiveStandbyFeatureAPI "
1458 + "with resourceID: {}", thisPdpId);
1459 logger.debug("testLocking2 failed to initialize. "
1460 + "Unable to get instance of ActiveStandbyFeatureAPI "
1461 + "with resourceID: {}", thisPdpId);
1465 * Insert another PDP as not designated. Initial standby state will be
1466 * either null or cold standby. Demoting should transit state to
1470 String standbyPdpId = "pdp2";
1471 logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
1472 Date yesterday = DateUtils.addDays(new Date(), -1);
1473 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1474 conn.insertPdp(pdp);
1475 droolsPdpEntity = conn.getPdp(standbyPdpId);
1476 logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
1477 standbyPdpId, droolsPdpEntity.isDesignated());
1478 assertTrue(droolsPdpEntity.isDesignated() == false);
1480 logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
1481 StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
1483 logger.debug("testLocking2: Runner started; Sleeping {} ms "
1484 + "before promoting/demoting", interruptRecoveryTime);
1485 sleep(interruptRecoveryTime);
1487 logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
1490 // demoting PDP should ensure that state transits to hotstandby
1491 logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
1494 logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime);
1497 logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
1498 + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1500 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1503 droolsPdpIntegrityMonitor.startTransaction();
1504 droolsPdpIntegrityMonitor.endTransaction();
1505 logger.debug("testLocking2: As expected, transaction successful");
1506 } catch (AdministrativeStateException e) {
1507 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1509 } catch (StandbyStatusException e) {
1510 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1512 } catch (Exception e) {
1513 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1517 // demoting should cause state to transit to hotstandby followed by re-promotion.
1518 logger.debug("testLocking2: demoting PDP={}", thisPdpId);
1521 logger.debug("testLocking2: sleeping {}"
1522 + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId);
1523 sleep(electionWaitSleepTime);
1525 logger.debug("testLocking2: Waking up and invoking startTransaction "
1526 + "on re-promoted PDP= {}, designated= {}",
1527 thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1529 droolsPdpIntegrityMonitor.startTransaction();
1530 droolsPdpIntegrityMonitor.endTransaction();
1531 logger.debug("testLocking2: As expected, transaction successful");
1532 } catch (AdministrativeStateException e) {
1533 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1535 } catch (StandbyStatusException e) {
1536 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1538 } catch (Exception e) {
1539 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1543 logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
1544 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
1545 assertTrue(standbyPdpDesignated == false);
1547 logger.debug("\n\ntestLocking2: Exiting\n\n");
1548 sleep(interruptRecoveryTime);
1551 private void sleep(long sleepms) throws InterruptedException {
1552 Thread.sleep(sleepms);