866ad5efac1a56ca79e52a041b26be7f9d5aa753
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-active-standby-management
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.activestandby;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.Properties;
37 import javax.persistence.EntityManager;
38 import javax.persistence.EntityManagerFactory;
39 import javax.persistence.EntityTransaction;
40 import javax.persistence.Persistence;
41 import org.apache.commons.lang3.time.DateUtils;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.policy.common.im.AdministrativeStateException;
47 import org.onap.policy.common.im.IntegrityMonitor;
48 import org.onap.policy.common.im.IntegrityMonitorException;
49 import org.onap.policy.common.im.MonitorTime;
50 import org.onap.policy.common.im.StandbyStatusException;
51 import org.onap.policy.common.im.StateManagement;
52 import org.onap.policy.common.utils.time.CurrentTime;
53 import org.onap.policy.common.utils.time.PseudoTimer;
54 import org.onap.policy.common.utils.time.TestTimeMulti;
55 import org.onap.policy.drools.core.PolicySessionFeatureApi;
56 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
57 import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants;
58 import org.powermock.reflect.Whitebox;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 /*
63  * All JUnits are designed to run in the local development environment
64  * where they have write privileges and can execute time-sensitive
65  * tasks.
66  */
67
68 public class StandbyStateManagementTest {
69     private static final Logger  logger = LoggerFactory.getLogger(StandbyStateManagementTest.class);
70
71     private static final String MONITOR_FIELD_NAME = "instance";
72     private static final String HANDLER_INSTANCE_FIELD = "electionHandler";
73
74     /*
75      * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
76      * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
77      * to ensure that we wait for the DesignationWaiter to do its job, before
78      * checking the results. Add a few seconds for safety
79      */
80
81     private static final long SLEEP_TIME = 10000;
82
83     /*
84      * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be
85      * plenty to ensure it has time to re-promote this PDP.
86      */
87
88     private static final long ELECTION_WAIT_SLEEP_TIME = 6000;
89
90     /*
91      * Sleep a few seconds after each test to allow interrupt (shutdown) recovery.
92      */
93
94     private static final long INTERRUPT_RECOVERY_TIME = 5000;
95
96     private static EntityManagerFactory emfx;
97     private static EntityManagerFactory emfd;
98     private static EntityManager emx;
99     private static EntityManager emd;
100     private static EntityTransaction et;
101
102     private static final String CONFIG_DIR = "src/test/resources";
103
104     private static CurrentTime saveTime;
105     private static Factory saveFactory;
106
107     private TestTimeMulti testTime;
108
109     /*
110      * This cannot be shared by tests, as each integrity monitor may manipulate it by
111      * adding its own property values.
112      */
113     private Properties activeStandbyProperties;
114
115     /*
116      * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
117      */
118
119     /**
120      * Setup the class.
121      *
122      * @throws Exception exception
123      */
124     @BeforeClass
125     public static void setUpClass() throws Exception {
126
127         String userDir = System.getProperty("user.dir");
128         logger.debug("setUpClass: userDir={}", userDir);
129         System.setProperty("com.sun.management.jmxremote.port", "9980");
130         System.setProperty("com.sun.management.jmxremote.authenticate","false");
131
132         saveTime = Whitebox.getInternalState(MonitorTime.class, MONITOR_FIELD_NAME);
133         saveFactory = Factory.getInstance();
134
135         resetInstanceObjects();
136
137         //Create the data access for xacml db
138         Properties smProps = loadStateManagementProperties();
139
140         emfx = Persistence.createEntityManagerFactory("junitXacmlPU", smProps);
141
142         // Create an entity manager to use the DB
143         emx = emfx.createEntityManager();
144
145         //Create the data access for drools db
146         Properties asbProps = loadActiveStandbyProperties();
147
148         emfd = Persistence.createEntityManagerFactory("junitDroolsPU", asbProps);
149
150         // Create an entity manager to use the DB
151         emd = emfd.createEntityManager();
152     }
153
154     /**
155      * Restores the system state.
156      *
157      * @throws IntegrityMonitorException if the integrity monitor cannot be shut down
158      */
159     @AfterClass
160     public static void tearDownClass() throws IntegrityMonitorException {
161         resetInstanceObjects();
162
163         Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, saveTime);
164         Factory.setInstance(saveFactory);
165
166         emd.close();
167         emfd.close();
168
169         emx.close();
170         emfx.close();
171     }
172
173     /**
174      * Setup.
175      *
176      * @throws Exception exception
177      */
178     @Before
179     public void setUp() throws Exception {
180         resetInstanceObjects();
181         cleanXacmlDb();
182         cleanDroolsDb();
183
184         /*
185          * set test time
186          *
187          * All threads use the test time object for sleeping.  As a result, we don't have
188          * to wait more than an instant for them to complete their work, thus we'll use
189          * a very small REAL wait time in the constructor.
190          */
191         testTime = new TestTimeMulti(5);
192         Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, testTime);
193
194         Factory factory = mock(Factory.class);
195         when(factory.makeTimer()).thenAnswer(ans -> new PseudoTimer(testTime));
196         Factory.setInstance(factory);
197
198         activeStandbyProperties = loadActiveStandbyProperties();
199     }
200
201     private static void resetInstanceObjects() throws IntegrityMonitorException {
202         IntegrityMonitor.setUnitTesting(true);
203         IntegrityMonitor.deleteInstance();
204         IntegrityMonitor.setUnitTesting(false);
205
206         Whitebox.setInternalState(ActiveStandbyFeature.class, HANDLER_INSTANCE_FIELD, (Object) null);
207
208     }
209
210     /**
211      * Clean up the xacml database.
212      *
213      */
214     public void cleanXacmlDb() {
215         et = emx.getTransaction();
216
217         et.begin();
218         // Make sure we leave the DB clean
219         emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
220         emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
221         emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
222         emx.flush();
223         et.commit();
224     }
225
226     /**
227      * Clean up the drools db.
228      */
229     public void cleanDroolsDb() {
230         et = emd.getTransaction();
231
232         et.begin();
233         // Make sure we leave the DB clean
234         emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
235         emd.flush();
236         et.commit();
237     }
238
239     /**
240      * Test the standby state change notifier.
241      *
242      * @throws Exception exception
243      */
244     @Test
245     public void testPmStandbyStateChangeNotifier() throws Exception {
246         logger.debug("\n\ntestPmStandbyStateChangeNotifier: Entering\n\n");
247
248         logger.debug("testPmStandbyStateChangeNotifier: Reading activeStandbyProperties");
249
250         String resourceName = "testPMS";
251         activeStandbyProperties.setProperty("resource.name", resourceName);
252         ActiveStandbyProperties.initProperties(activeStandbyProperties);
253
254         logger.debug("testPmStandbyStateChangeNotifier: Getting StateManagement instance");
255
256         StateManagement sm = new StateManagement(emfx, resourceName);
257
258         //Create an instance of the Observer
259         PmStandbyStateChangeNotifier pmNotifier = new PmStandbyStateChangeNotifier();
260
261         //Register the PmStandbyStateChangeNotifier Observer
262         sm.addObserver(pmNotifier);
263
264         //At this point the standbystatus = 'null'
265         sm.lock();
266         assertEquals(StateManagement.NULL_VALUE, pmNotifier.getPreviousStandbyStatus());
267
268         sm.unlock();
269         assertEquals(StateManagement.NULL_VALUE, pmNotifier.getPreviousStandbyStatus());
270
271         //Adding standbystatus=hotstandby
272         sm.demote();
273         System.out.println(pmNotifier.getPreviousStandbyStatus());
274         assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY,
275                 pmNotifier.getPreviousStandbyStatus());
276
277         //Now making standbystatus=coldstandby
278         sm.lock();
279         assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY,
280                 pmNotifier.getPreviousStandbyStatus());
281
282         //standbystatus = hotstandby
283         sm.unlock();
284         assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY,
285                 pmNotifier.getPreviousStandbyStatus());
286
287         //standbystatus = providingservice
288         sm.promote();
289         //The previousStandbyStatus is not updated until after the delay activation expires
290         assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY,
291                 pmNotifier.getPreviousStandbyStatus());
292
293         //Sleep long enough for the delayActivationTimer to run
294         sleep(5000);
295         assertEquals(StateManagement.PROVIDING_SERVICE, pmNotifier.getPreviousStandbyStatus());
296
297         //standbystatus = providingservice
298         sm.promote();
299         assertEquals(StateManagement.PROVIDING_SERVICE, pmNotifier.getPreviousStandbyStatus());
300
301         //standbystatus = coldstandby
302         sm.lock();
303         assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY,
304                 pmNotifier.getPreviousStandbyStatus());
305
306         //standbystatus = hotstandby
307         sm.unlock();
308         assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY,
309                 pmNotifier.getPreviousStandbyStatus());
310
311         //standbystatus = hotstandby
312         sm.demote();
313         assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY,
314                 pmNotifier.getPreviousStandbyStatus());
315     }
316
317     /**
318      * Test sanitize designated list.
319      *
320      * @throws Exception exception
321      */
322     @Test
323     public void testSanitizeDesignatedList() throws Exception {
324
325         logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
326
327         // Get a DroolsPdpsConnector
328
329         final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfd);
330
331         // Create 4 pdpd all not designated
332
333         DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate());
334         DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate());
335         DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate());
336         DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate());
337
338         List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
339         listOfDesignated.add(pdp1);
340         listOfDesignated.add(pdp2);
341         listOfDesignated.add(pdp3);
342         listOfDesignated.add(pdp4);
343
344         // Now we want to create a StateManagementFeature and initialize it.  It will be
345         // discovered by the ActiveStandbyFeature when the election handler initializes.
346
347         StateManagementFeatureApi stateManagementFeature = null;
348         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
349             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
350             stateManagementFeature = feature;
351             logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}",
352                     stateManagementFeature.getResourceName());
353             break;
354         }
355         assertNotNull(stateManagementFeature);
356
357
358         DroolsPdpsElectionHandler droolsPdpsElectionHandler =  new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
359
360         listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
361
362         logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
363
364         assertEquals(4, listOfDesignated.size());
365
366         // Now make 2 designated
367
368         pdp1.setDesignated(true);
369         pdp2.setDesignated(true);
370
371         listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
372
373         logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n",
374                 listOfDesignated.size());
375
376         assertEquals(2, listOfDesignated.size());
377         assertTrue(listOfDesignated.contains(pdp1));
378         assertTrue(listOfDesignated.contains(pdp2));
379
380
381         // Now all are designated.  But, we have to add back the previously non-designated nodes
382
383         pdp3.setDesignated(true);
384         pdp4.setDesignated(true);
385         listOfDesignated.add(pdp3);
386         listOfDesignated.add(pdp4);
387
388         listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
389
390         logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n",
391                 listOfDesignated.size());
392
393         assertEquals(4, listOfDesignated.size());
394
395     }
396
397     /**
398     *  Test Compute most recent primary.
399     *
400     * @throws Exception exception
401     */
402     @Test
403     public void testComputeMostRecentPrimary() throws Exception {
404
405         logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
406
407         final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfd);
408
409
410         // Create 4 pdpd all not designated
411
412
413         long designatedDateMs = testTime.getMillis();
414         DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate());
415         pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
416
417         DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate());
418         //oldest
419         pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
420
421         DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate());
422         pdp3.setDesignatedDate(new Date(designatedDateMs - 1));
423
424         DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate());
425         //most recent
426         pdp4.setDesignatedDate(new Date(designatedDateMs));
427
428         ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
429         listOfAllPdps.add(pdp1);
430         listOfAllPdps.add(pdp2);
431         listOfAllPdps.add(pdp3);
432         listOfAllPdps.add(pdp4);
433
434
435         ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
436         listOfDesignated.add(pdp1);
437         listOfDesignated.add(pdp2);
438         listOfDesignated.add(pdp3);
439         listOfDesignated.add(pdp4);
440
441         // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
442         // or all designated members.
443
444         // Now we want to create a StateManagementFeature and initialize it.  It will be
445         // discovered by the ActiveStandbyFeature when the election handler initializes.
446
447         StateManagementFeatureApi stateManagementFeature = null;
448         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
449             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
450             stateManagementFeature = feature;
451             logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}",
452                     stateManagementFeature.getResourceName());
453             break;
454         }
455         assertNotNull(stateManagementFeature);
456
457         DroolsPdpsElectionHandler droolsPdpsElectionHandler =  new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
458
459         DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(
460                 listOfAllPdps, listOfDesignated);
461
462         logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
463                 mostRecentPrimary.getPdpId());
464
465
466         // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
467         // the one which has the most recent designated date.
468
469
470         assertEquals("pdp4", mostRecentPrimary.getPdpId());
471
472
473         // Now let's designate all of those on the listOfDesignated.  It will choose the first one designated
474
475
476         pdp1.setDesignated(true);
477         pdp2.setDesignated(true);
478         pdp3.setDesignated(true);
479         pdp4.setDesignated(true);
480
481         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
482
483         logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, "
484                 + "mostRecentPrimary.getPdpId() = {}\n\n",
485                 mostRecentPrimary.getPdpId());
486
487
488         // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
489         // the one which was designated first
490
491
492         assertEquals("pdp2", mostRecentPrimary.getPdpId());
493
494
495         // Now we will designate only 2 and put just them in the listOfDesignated.  The algorithm will now
496         // look for the most recently designated pdp which is not currently designated.
497
498
499         pdp3.setDesignated(false);
500         pdp4.setDesignated(false);
501
502         listOfDesignated.remove(pdp3);
503         listOfDesignated.remove(pdp4);
504
505         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
506
507         logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
508                 mostRecentPrimary.getPdpId());
509
510         assertEquals("pdp4", mostRecentPrimary.getPdpId());
511
512
513
514         // Now we will have none designated and put two of them in the listOfDesignated.  The algorithm will now
515         // look for the most recently designated pdp regardless of whether it is currently marked as designated.
516
517
518         pdp1.setDesignated(false);
519         pdp2.setDesignated(false);
520
521         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
522
523         logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n",
524                 mostRecentPrimary.getPdpId());
525
526         assertEquals("pdp4", mostRecentPrimary.getPdpId());
527
528
529         // If we have only one pdp on in the listOfDesignated,
530         // the most recently designated pdp will be chosen, regardless
531         // of its designation status
532
533
534         listOfDesignated.remove(pdp1);
535
536         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
537
538         logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n",
539                 mostRecentPrimary.getPdpId());
540
541         assertEquals("pdp4", mostRecentPrimary.getPdpId());
542
543
544         // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
545
546
547         listOfDesignated.remove(pdp2);
548
549         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
550
551         logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n",
552                 mostRecentPrimary.getPdpId());
553
554         assertEquals("pdp4", mostRecentPrimary.getPdpId());
555
556     }
557
558     /**
559      * Test compute designated PDP.
560      *
561      * @throws Exception exception
562      */
563     @Test
564     public void testComputeDesignatedPdp() throws Exception {
565
566         logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
567
568         final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfd);
569
570
571         // Create 4 pdpd all not designated.  Two on site1. Two on site2
572
573
574         long designatedDateMs = testTime.getMillis();
575         DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate());
576         pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
577         pdp1.setSite("site1");
578
579         DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate());
580         pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
581         pdp2.setSite("site1");
582
583         //oldest
584         DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate());
585         pdp3.setDesignatedDate(new Date(designatedDateMs - 4));
586         pdp3.setSite("site2");
587
588         DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate());
589         //most recent
590         pdp4.setDesignatedDate(new Date(designatedDateMs));
591         pdp4.setSite("site2");
592
593         ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
594         listOfAllPdps.add(pdp1);
595         listOfAllPdps.add(pdp2);
596         listOfAllPdps.add(pdp3);
597         listOfAllPdps.add(pdp4);
598
599
600         ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
601
602
603         // We will first test an empty listOfDesignated. As we know from the previous JUnit,
604         // the pdp with the most designated date will be chosen for mostRecentPrimary
605
606         // Now we want to create a StateManagementFeature and initialize it.  It will be
607         // discovered by the ActiveStandbyFeature when the election handler initializes.
608
609         StateManagementFeatureApi stateManagementFeature = null;
610         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
611             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
612             stateManagementFeature = feature;
613             logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}",
614                     stateManagementFeature.getResourceName());
615             break;
616         }
617         assertNotNull(stateManagementFeature);
618
619
620         DroolsPdpsElectionHandler droolsPdpsElectionHandler =  new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
621
622         DroolsPdp mostRecentPrimary = pdp4;
623
624         DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
625
626
627         // The designatedPdp should be null
628
629         assertNull(designatedPdp);
630
631
632         // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
633
634         listOfDesignated.add(pdp2);
635
636         designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
637
638
639         // Now the designatedPdp should be the one and only selection in the listOfDesignated
640
641
642         assertEquals(designatedPdp.getPdpId(), pdp2.getPdpId());
643
644
645         // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
646
647
648         listOfDesignated.add(pdp1);
649
650         designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
651
652
653         // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
654
655
656         assertEquals(designatedPdp.getPdpId(), pdp1.getPdpId());
657
658
659         // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
660
661
662         listOfDesignated.remove(pdp1);
663         listOfDesignated.add(pdp3);
664
665         designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
666
667
668         // The designatedPdp should now be the one on the same site as the mostRecentPrimary
669
670
671         assertEquals(designatedPdp.getPdpId(), pdp3.getPdpId());
672     }
673
674     /**
675      * Test cold standby.
676      *
677      * @throws Exception exception
678      */
679     @Test
680     public void testColdStandby() throws Exception {
681
682         logger.debug("\n\ntestColdStandby: Entering\n\n");
683
684         final String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
685
686         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfd);
687
688         logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
689         DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, testTime.getDate());
690         conn.insertPdp(pdp);
691         DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
692         logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
693                 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
694         assertTrue(droolsPdpEntity.isDesignated());
695
696         /*
697          * When the Standby Status changes (from providingservice) to hotstandby
698          * or coldstandby,the Active/Standby selection algorithm must stand down
699          * if thePDP-D is currently the lead/active node and allow another PDP-D
700          * to take over.
701          *
702          * It must also call lock on all engines in the engine management.
703          */
704
705
706         /*
707          * Yes, this is kludgy, but we have a chicken and egg problem here: we
708          * need a StateManagement object to invoke the
709          * deleteAllStateManagementEntities method.
710          */
711         logger.debug("testColdStandby: Instantiating stateManagement object");
712
713         StateManagement sm = new StateManagement(emfx, "dummy");
714         sm.deleteAllStateManagementEntities();
715
716         // Now we want to create a StateManagementFeature and initialize it.  It will be
717         // discovered by the ActiveStandbyFeature when the election handler initializes.
718
719         StateManagementFeatureApi smf = null;
720         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
721             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
722             smf = feature;
723             logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
724             break;
725         }
726         assertNotNull(smf);
727
728         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
729         // that has been created.
730         ActiveStandbyFeatureApi activeStandbyFeature = null;
731         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
732             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
733             activeStandbyFeature = feature;
734             logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}",
735                     activeStandbyFeature.getResourceName());
736             break;
737         }
738         assertNotNull(activeStandbyFeature);
739
740         // Artificially putting a PDP into service is really a two step process, 1)
741         // inserting it as designated and 2) promoting it so that its standbyStatus
742         // is providing service.
743
744         logger.debug("testColdStandby: Runner started; Sleeping "
745                 + INTERRUPT_RECOVERY_TIME + "ms before promoting PDP= {}",
746                 thisPdpId);
747         sleep(INTERRUPT_RECOVERY_TIME);
748
749         logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
750         smf.promote();
751
752         String standbyStatus = sm.getStandbyStatus(thisPdpId);
753         logger.debug("testColdStandby: Before locking, PDP= {}  has standbyStatus= {}",
754                 thisPdpId, standbyStatus);
755
756         logger.debug("testColdStandby: Locking smf");
757         smf.lock();
758
759         sleep(INTERRUPT_RECOVERY_TIME);
760
761         // Verify that the PDP is no longer designated.
762
763         droolsPdpEntity = conn.getPdp(thisPdpId);
764         logger.debug("testColdStandby: After lock sm.lock() invoked, "
765                 + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
766         assertFalse(droolsPdpEntity.isDesignated());
767
768         logger.debug("\n\ntestColdStandby: Exiting\n\n");
769     }
770
771     // Tests hot standby when there is only one PDP.
772
773     /**
774      * Test hot standby 1.
775      *
776      * @throws Exception exception
777      */
778     @Test
779     public void testHotStandby1() throws Exception {
780
781         logger.debug("\n\ntestHotStandby1: Entering\n\n");
782
783         final String thisPdpId = activeStandbyProperties
784                 .getProperty(ActiveStandbyProperties.NODE_NAME);
785
786         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfd);
787
788         /*
789          * Insert this PDP as not designated.  Initial standby state will be
790          * either null or cold standby.   Demoting should transit state to
791          * hot standby.
792          */
793
794         logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
795         Date yesterday = DateUtils.addDays(testTime.getDate(), -1);
796         DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
797         conn.insertPdp(pdp);
798         DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
799         logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
800                 thisPdpId, droolsPdpEntity.isDesignated());
801         assertFalse(droolsPdpEntity.isDesignated());
802
803         logger.debug("testHotStandby1: Instantiating stateManagement object");
804         StateManagement sm = new StateManagement(emfx, "dummy");
805         sm.deleteAllStateManagementEntities();
806
807
808         // Now we want to create a StateManagementFeature and initialize it.  It will be
809         // discovered by the ActiveStandbyFeature when the election handler initializes.
810
811         StateManagementFeatureApi smf = null;
812         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
813             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
814             smf = feature;
815             logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
816             break;
817         }
818         assertNotNull(smf);
819
820         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
821         // that has been created.
822         ActiveStandbyFeatureApi activeStandbyFeature = null;
823         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
824             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
825             activeStandbyFeature = feature;
826             logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}",
827                     activeStandbyFeature.getResourceName());
828             break;
829         }
830         assertNotNull(activeStandbyFeature);
831
832
833         logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
834         // demoting should cause state to transit to hotstandby
835         smf.demote();
836
837
838         logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
839                 + "time to check droolspdpentity table", SLEEP_TIME);
840         sleep(SLEEP_TIME);
841
842
843         // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
844
845         droolsPdpEntity = conn.getPdp(thisPdpId);
846         logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
847                 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
848         assertTrue(droolsPdpEntity.isDesignated());
849         String standbyStatus = smf.getStandbyStatus(thisPdpId);
850         logger.debug("testHotStandby1: After demotion, PDP= {} "
851                 + "has standbyStatus= {}", thisPdpId, standbyStatus);
852         assertTrue(standbyStatus != null  &&  standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
853
854         logger.debug("testHotStandby1: Stopping policyManagementRunner");
855
856         logger.debug("\n\ntestHotStandby1: Exiting\n\n");
857     }
858
859     /*
860      * Tests hot standby when two PDPs are involved.
861      */
862
863     /**
864      * Test hot standby 2.
865      *
866      * @throws Exception exception
867      */
868     @Test
869     public void testHotStandby2() throws Exception {
870
871         logger.info("\n\ntestHotStandby2: Entering\n\n");
872
873         final String thisPdpId = activeStandbyProperties
874                 .getProperty(ActiveStandbyProperties.NODE_NAME);
875
876         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfd);
877
878
879         // Insert a PDP that's designated but not current.
880
881         String activePdpId = "pdp2";
882         logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
883         Date yesterday = DateUtils.addDays(testTime.getDate(), -1);
884         DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
885         conn.insertPdp(pdp);
886         DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
887         logger.info("testHotStandby2: After insertion, PDP= {}, which is "
888                 + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
889         assertTrue(droolsPdpEntity.isDesignated());
890
891         /*
892          * Promote the designated PDP.
893          *
894          * We have a chicken and egg problem here: we need a StateManagement
895          * object to invoke the deleteAllStateManagementEntities method.
896          */
897
898
899         logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
900         StateManagement sm = new StateManagement(emfx, "dummy");
901         sm.deleteAllStateManagementEntities();
902
903
904         sm = new StateManagement(emfx, activePdpId);//pdp2
905
906         // Artificially putting a PDP into service is really a two step process, 1)
907         // inserting it as designated and 2) promoting it so that its standbyStatus
908         // is providing service.
909
910         /*
911          * Insert this PDP as not designated.  Initial standby state will be
912          * either null or cold standby.   Demoting should transit state to
913          * hot standby.
914          */
915
916
917         logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
918         pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
919         conn.insertPdp(pdp);
920         droolsPdpEntity = conn.getPdp(thisPdpId);
921         logger.info("testHotStandby2: After insertion, PDP={} "
922                 + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
923         assertFalse(droolsPdpEntity.isDesignated());
924
925
926         // Now we want to create a StateManagementFeature and initialize it.  It will be
927         // discovered by the ActiveStandbyFeature when the election handler initializes.
928
929         StateManagementFeatureApi sm2 = null;
930         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
931             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
932             sm2 = feature;
933             logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
934             break;
935         }
936         assertNotNull(sm2);
937
938         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
939         // that has been created.
940         ActiveStandbyFeatureApi activeStandbyFeature = null;
941         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
942             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
943             activeStandbyFeature = feature;
944             logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}",
945                     activeStandbyFeature.getResourceName());
946             break;
947         }
948         assertNotNull(activeStandbyFeature);
949
950         logger.info("testHotStandby2: Runner started; Sleeping {} "
951                 + "ms before promoting/demoting", INTERRUPT_RECOVERY_TIME);
952         sleep(INTERRUPT_RECOVERY_TIME);
953
954         logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
955         //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
956         //because it is stale. So, it cannot be promoted.  We need to call sm.enableNotFailed() so we
957         //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
958         sm.enableNotFailed();//pdp2
959         sm.promote();
960         String standbyStatus = sm.getStandbyStatus(activePdpId);
961         logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
962
963         // demoting PDP should ensure that state transits to hotstandby
964         logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
965         sm2.demote();//pdp1
966         standbyStatus = sm.getStandbyStatus(thisPdpId);
967         logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
968
969         logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
970                 + "time to check droolspdpentity table", SLEEP_TIME);
971         sleep(SLEEP_TIME);
972
973         /*
974          * Verify that this PDP, demoted to HOT_STANDBY, is now
975          * re-designated and providing service.
976          */
977
978         droolsPdpEntity = conn.getPdp(thisPdpId);
979         logger.info("testHotStandby2: After demoting PDP={}"
980                 + ", DESIGNATED= {}"
981                 + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
982         assertTrue(droolsPdpEntity.isDesignated());
983         standbyStatus = sm2.getStandbyStatus(thisPdpId);
984         logger.info("testHotStandby2: After demoting PDP={}"
985                 + ", PDP={} has standbyStatus= {}",
986                 activePdpId, thisPdpId, standbyStatus);
987         assertTrue(standbyStatus != null
988                 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
989
990         logger.info("testHotStandby2: Stopping policyManagementRunner");
991
992         logger.info("\n\ntestHotStandby2: Exiting\n\n");
993     }
994
995     /*
996      * 1) Inserts and designates this PDP, then verifies that startTransaction
997      * is successful.
998      *
999      * 2) Demotes PDP, and verifies that because there is only one PDP, it will
1000      * be immediately re-promoted, thus allowing startTransaction to be
1001      * successful.
1002      *
1003      * 3) Locks PDP and verifies that startTransaction results in
1004      * AdministrativeStateException.
1005      *
1006      * 4) Unlocks PDP and verifies that startTransaction results in
1007      * StandbyStatusException.
1008      *
1009      * 5) Promotes PDP and verifies that startTransaction is once again
1010      * successful.
1011      */
1012
1013     /**
1014      * Test locking.
1015      *
1016      * @throws Exception exception
1017      */
1018     @Test
1019     public void testLocking1() throws Exception {
1020         logger.debug("testLocking1: Entry");
1021
1022         final String thisPdpId = activeStandbyProperties
1023                 .getProperty(ActiveStandbyProperties.NODE_NAME);
1024
1025         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfd);
1026
1027         /*
1028          * Insert this PDP as designated.  Initial standby state will be
1029          * either null or cold standby.
1030          */
1031
1032         logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
1033         DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, testTime.getDate());
1034         conn.insertPdp(pdp);
1035         DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1036         logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
1037                 thisPdpId, droolsPdpEntity.isDesignated());
1038         assertTrue(droolsPdpEntity.isDesignated());
1039
1040         logger.debug("testLocking1: Instantiating stateManagement object");
1041         StateManagement smDummy = new StateManagement(emfx, "dummy");
1042         smDummy.deleteAllStateManagementEntities();
1043
1044         // Now we want to create a StateManagementFeature and initialize it.  It will be
1045         // discovered by the ActiveStandbyFeature when the election handler initializes.
1046
1047         StateManagementFeatureApi sm = null;
1048         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
1049             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
1050             sm = feature;
1051             logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1052             break;
1053         }
1054         assertNotNull(sm);
1055
1056         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1057         // that has been created.
1058         ActiveStandbyFeatureApi activeStandbyFeature = null;
1059         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
1060             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
1061             activeStandbyFeature = feature;
1062             logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}",
1063                     activeStandbyFeature.getResourceName());
1064             break;
1065         }
1066         assertNotNull(activeStandbyFeature);
1067
1068         logger.debug("testLocking1: Runner started; Sleeping "
1069                 + INTERRUPT_RECOVERY_TIME + "ms before promoting PDP={}",
1070                 thisPdpId);
1071         sleep(INTERRUPT_RECOVERY_TIME);
1072
1073         logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
1074         sm.promote();
1075
1076         logger.debug("testLocking1: Sleeping {} ms, to allow time for "
1077                 + "policy-management.Main class to come up, designated= {}",
1078                 SLEEP_TIME, conn.getPdp(thisPdpId).isDesignated());
1079         sleep(SLEEP_TIME);
1080
1081         logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
1082                 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1083
1084
1085         IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1086         try {
1087             droolsPdpIntegrityMonitor.startTransaction();
1088             droolsPdpIntegrityMonitor.endTransaction();
1089             logger.debug("testLocking1: As expected, transaction successful");
1090         } catch (AdministrativeStateException e) {
1091             logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1092             assertTrue(false);
1093         } catch (StandbyStatusException e) {
1094             logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1095             assertTrue(false);
1096         } catch (Exception e) {
1097             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1098             assertTrue(false);
1099         }
1100
1101         // demoting should cause state to transit to hotstandby, followed by re-promotion,
1102         // since there is only one PDP.
1103         logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1104         sm.demote();
1105
1106         logger.debug("testLocking1: sleeping" + ELECTION_WAIT_SLEEP_TIME
1107                 + " to allow election handler to re-promote PDP={}", thisPdpId);
1108         sleep(ELECTION_WAIT_SLEEP_TIME);
1109
1110         logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
1111                 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1112         try {
1113             droolsPdpIntegrityMonitor.startTransaction();
1114             droolsPdpIntegrityMonitor.endTransaction();
1115             logger.debug("testLocking1: As expected, transaction successful");
1116         } catch (AdministrativeStateException e) {
1117             logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1118             assertTrue(false);
1119         } catch (StandbyStatusException e) {
1120             logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1121             assertTrue(false);
1122         } catch (Exception e) {
1123             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1124             assertTrue(false);
1125         }
1126
1127         // locking should cause state to transit to cold standby
1128         logger.debug("testLocking1: locking PDP={}", thisPdpId);
1129         sm.lock();
1130
1131         // Just to avoid any race conditions, sleep a little after locking
1132         logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
1133         sleep(100);
1134
1135         logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
1136                 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1137         try {
1138             droolsPdpIntegrityMonitor.startTransaction();
1139             logger.error("testLocking1: startTransaction unexpectedly successful");
1140             assertTrue(false);
1141         } catch (AdministrativeStateException e) {
1142             logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
1143         } catch (StandbyStatusException e) {
1144             logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1145             assertTrue(false);
1146         } catch (Exception e) {
1147             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1148             assertTrue(false);
1149         } finally {
1150             droolsPdpIntegrityMonitor.endTransaction();
1151         }
1152
1153         // unlocking should cause state to transit to hot standby and then providing service
1154         logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
1155         sm.unlock();
1156
1157         // Just to avoid any race conditions, sleep a little after locking
1158         logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1159         sleep(ELECTION_WAIT_SLEEP_TIME);
1160
1161         logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1162                 + thisPdpId
1163                 + ", designated="
1164                 + conn.getPdp(thisPdpId).isDesignated());
1165         try {
1166             droolsPdpIntegrityMonitor.startTransaction();
1167             logger.error("testLocking1: startTransaction successful as expected");
1168         } catch (AdministrativeStateException e) {
1169             logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1170             assertTrue(false);
1171         } catch (StandbyStatusException e) {
1172             logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1173             assertTrue(false);
1174         } catch (Exception e) {
1175             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1176             assertTrue(false);
1177         } finally {
1178             droolsPdpIntegrityMonitor.endTransaction();
1179         }
1180
1181         // demoting should cause state to transit to hot standby
1182         logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1183         sm.demote();
1184
1185         logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
1186                 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1187         try {
1188             droolsPdpIntegrityMonitor.startTransaction();
1189             droolsPdpIntegrityMonitor.endTransaction();
1190             logger.debug("testLocking1: Unexpectedly, transaction successful");
1191             assertTrue(false);
1192         } catch (AdministrativeStateException e) {
1193             logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1194             assertTrue(false);
1195         } catch (StandbyStatusException e) {
1196             logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
1197         } catch (Exception e) {
1198             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1199             assertTrue(false);
1200         }
1201
1202         logger.debug("\n\ntestLocking1: Exiting\n\n");
1203     }
1204
1205
1206     /*
1207      * 1) Inserts and designates this PDP, then verifies that startTransaction
1208      * is successful.
1209      *
1210      * 2) Inserts another PDP in hotstandby.
1211      *
1212      * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1213      * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1214      */
1215
1216     /**
1217      * Test locking 2.
1218      *
1219      * @throws Exception exception
1220      */
1221     @Test
1222     public void testLocking2() throws Exception {
1223
1224         logger.debug("\n\ntestLocking2: Entering\n\n");
1225
1226         final String thisPdpId = activeStandbyProperties
1227                 .getProperty(ActiveStandbyProperties.NODE_NAME);
1228
1229         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfd);
1230
1231         /*
1232          * Insert this PDP as designated.  Initial standby state will be
1233          * either null or cold standby.   Demoting should transit state to
1234          * hot standby.
1235          */
1236
1237         logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
1238         DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, testTime.getDate());
1239         conn.insertPdp(pdp);
1240         DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1241         logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
1242                 thisPdpId, droolsPdpEntity.isDesignated());
1243         assertTrue(droolsPdpEntity.isDesignated());
1244
1245         logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
1246         StateManagement smDummy = new StateManagement(emfx, "dummy");
1247         smDummy.deleteAllStateManagementEntities();
1248
1249         // Now we want to create a StateManagementFeature and initialize it.  It will be
1250         // discovered by the ActiveStandbyFeature when the election handler initializes.
1251
1252         StateManagementFeatureApi sm = null;
1253         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
1254             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
1255             sm = feature;
1256             logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1257             break;
1258         }
1259         assertNotNull(sm);
1260
1261         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1262         // that has been created.
1263         ActiveStandbyFeatureApi activeStandbyFeature = null;
1264         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
1265             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
1266             activeStandbyFeature = feature;
1267             logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}",
1268                     activeStandbyFeature.getResourceName());
1269             break;
1270         }
1271         assertNotNull(activeStandbyFeature);
1272
1273         /*
1274          * Insert another PDP as not designated.  Initial standby state will be
1275          * either null or cold standby.   Demoting should transit state to
1276          * hot standby.
1277          */
1278
1279         String standbyPdpId = "pdp2";
1280         logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
1281         Date yesterday = DateUtils.addDays(testTime.getDate(), -1);
1282         pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1283         conn.insertPdp(pdp);
1284         droolsPdpEntity = conn.getPdp(standbyPdpId);
1285         logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
1286                 standbyPdpId, droolsPdpEntity.isDesignated());
1287         assertFalse(droolsPdpEntity.isDesignated());
1288
1289         logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
1290         final StateManagement sm2 = new StateManagement(emfx, standbyPdpId);
1291
1292         logger.debug("testLocking2: Runner started; Sleeping {} ms "
1293                 + "before promoting/demoting", INTERRUPT_RECOVERY_TIME);
1294         sleep(INTERRUPT_RECOVERY_TIME);
1295
1296         logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
1297         sm.promote();
1298
1299         // demoting PDP should ensure that state transits to hotstandby
1300         logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
1301         sm2.demote();
1302
1303         logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", SLEEP_TIME);
1304         sleep(SLEEP_TIME);
1305
1306         logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
1307                 + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1308
1309         IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1310
1311         try {
1312             droolsPdpIntegrityMonitor.startTransaction();
1313             droolsPdpIntegrityMonitor.endTransaction();
1314             logger.debug("testLocking2: As expected, transaction successful");
1315         } catch (AdministrativeStateException e) {
1316             logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1317             assertTrue(false);
1318         } catch (StandbyStatusException e) {
1319             logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1320             assertTrue(false);
1321         } catch (Exception e) {
1322             logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1323             assertTrue(false);
1324         }
1325
1326         // demoting should cause state to transit to hotstandby followed by re-promotion.
1327         logger.debug("testLocking2: demoting PDP={}", thisPdpId);
1328         sm.demote();
1329
1330         logger.debug("testLocking2: sleeping {}"
1331                 + " to allow election handler to re-promote PDP={}", ELECTION_WAIT_SLEEP_TIME, thisPdpId);
1332         sleep(ELECTION_WAIT_SLEEP_TIME);
1333
1334         logger.debug("testLocking2: Waking up and invoking startTransaction "
1335                 + "on re-promoted PDP= {}, designated= {}",
1336                 thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1337         try {
1338             droolsPdpIntegrityMonitor.startTransaction();
1339             droolsPdpIntegrityMonitor.endTransaction();
1340             logger.debug("testLocking2: As expected, transaction successful");
1341         } catch (AdministrativeStateException e) {
1342             logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1343             assertTrue(false);
1344         } catch (StandbyStatusException e) {
1345             logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1346             assertTrue(false);
1347         } catch (Exception e) {
1348             logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1349             assertTrue(false);
1350         }
1351
1352         logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
1353         assertFalse(conn.getPdp(standbyPdpId).isDesignated());
1354
1355         logger.debug("\n\ntestLocking2: Exiting\n\n");
1356     }
1357
1358     private static Properties loadStateManagementProperties() throws IOException {
1359         try (FileInputStream input = new FileInputStream(CONFIG_DIR + "/feature-state-management.properties")) {
1360             Properties props = new Properties();
1361             props.load(input);
1362             return props;
1363         }
1364     }
1365
1366     private static Properties loadActiveStandbyProperties() throws IOException {
1367         try (FileInputStream input =
1368                         new FileInputStream(CONFIG_DIR + "/feature-active-standby-management.properties")) {
1369             Properties props = new Properties();
1370             props.load(input);
1371             return props;
1372         }
1373     }
1374
1375     private void sleep(long sleepms) throws InterruptedException {
1376         testTime.waitFor(sleepms);
1377     }
1378 }