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