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