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