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