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