f58d304e1b679d4ae158697f154f793e1056968a
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-persistence
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.openecomp.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.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.util.Date;
30 import java.util.Properties;
31
32 import javax.persistence.EntityManager;
33 import javax.persistence.EntityManagerFactory;
34 import javax.persistence.EntityTransaction;
35 import javax.persistence.Persistence;
36
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Ignore;
42 import org.junit.Test;
43
44 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
45 import org.openecomp.policy.common.im.AdministrativeStateException;
46 import org.openecomp.policy.common.im.IntegrityMonitor;
47 import org.openecomp.policy.common.im.StandbyStatusException;
48 import org.openecomp.policy.common.im.StateManagement;
49 import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
50 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
51 import org.openecomp.policy.drools.core.PolicyContainer;
52 import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
53 import org.openecomp.policy.drools.persistence.DroolsPdp;
54 import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
55 import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
56 import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
57 import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
58 import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
59 import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
60 import org.openecomp.policy.drools.system.Main;
61 import org.openecomp.policy.drools.system.PolicyEngine;
62
63 import org.apache.commons.lang3.time.DateUtils;
64
65 /*
66  * Cloned from StandbyStateManagement.java in support of US673632.
67  * See MultiSite_v1-10.ppt, slide 38
68  */
69 public class ResiliencyTestCases {
70                         
71         /*
72          * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every ten seconds, starting 
73          * at ten seconds after the minute boundary (e.g. 13:05:10). So, an 80 second sleep should be 
74          * sufficient to ensure that we wait for the DesignationWaiter to do its job, before 
75          * checking the results. 
76          */
77         long sleepTime = 80000;
78         
79         /*
80          * DroolsPdpsElectionHandler runs every ten seconds, so a 15 second sleep should be 
81          * plenty to ensure it has time to re-promote this PDP.
82          */
83         long electionWaitSleepTime = 15000;
84         
85         /*
86          * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
87          */
88         long interruptRecoveryTime = 5000;
89
90         /*
91          * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
92          */
93         @BeforeClass
94         public static void setUpClass() throws Exception {
95                 
96                 String userDir = System.getProperty("user.dir");
97                 PolicyLogger.debug("setUpClass: userDir=" + userDir);
98                 System.setProperty("com.sun.management.jmxremote.port", "9980");
99                 System.setProperty("com.sun.management.jmxremote.authenticate","false");
100                                 
101                 // Make sure path to config directory is set correctly in PolicyContainer.main
102                 // Also make sure we ignore HTTP server failures resulting from port conflicts.
103                 PolicyContainer.isUnitTesting = true;
104                 
105         }
106
107         @AfterClass
108         public static void tearDownClass() throws Exception {
109         }
110
111         @Before
112         public void setUp() throws Exception {
113         }
114
115         @After
116         public void tearDown() throws Exception {
117                                 
118         }
119         
120         public void cleanDroolsDB() throws Exception{
121                 PolicyLogger.debug("\n\ncleanDroolsDB: Entering\n\n");
122
123                 PolicyLogger.debug("cleanDroolsDB: Reading droolsPersistenceProperties");
124                 Properties droolsPersistenceProperties = new Properties();
125                 droolsPersistenceProperties.load(new FileInputStream(new File(
126                                 "src/test/server/config/droolsPersistence.properties")));
127
128                 PolicyLogger.debug("cleanDroolsDB: Creating emfDrools");
129                 EntityManagerFactory emf = Persistence.createEntityManagerFactory(
130                                 "junitDroolsPU", droolsPersistenceProperties);
131                 
132                 PolicyLogger.debug("cleanDroolsDB: Cleaning up tables");
133                 
134                 EntityManager em = emf.createEntityManager();
135                 EntityTransaction et = em.getTransaction();
136                 et.begin();
137                 
138                 // Make sure the DB is clean
139                 PolicyLogger.debug("cleanDroolsDB: clean DroolsPdpEntity");
140                 em.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
141                 PolicyLogger.debug("cleanDroolsDB: clean DroolsSessionEntity");
142                 em.createQuery("DELETE FROM DroolsSessionEntity").executeUpdate();
143                 
144                 em.flush(); 
145                 PolicyLogger.debug("cleanDroolsDB: after flush");
146
147                 et.commit(); 
148                 
149                 PolicyLogger.debug("\n\ncleanDroolsDB: Exiting\n\n");
150         }
151         
152         public void cleanXacmlDB() throws Exception {
153                 PolicyLogger.debug("\n\ncleanXacmlDB: Entering\n\n");
154
155                 PolicyLogger.debug("cleanXacmlDB: Reading IntegrityMonitorProperties");
156
157                 PolicyLogger.debug("cleanXacmlDB: Reading xacmlPersistenceProperties");
158                 Properties xacmlPersistenceProperties = new Properties();
159                 xacmlPersistenceProperties.load(new FileInputStream(new File(
160                                 "src/test/server/config/xacmlPersistence.properties")));
161                 
162                 PolicyLogger.debug("cleanXacmlDB: Creating emf");
163                 EntityManagerFactory emf = Persistence.createEntityManagerFactory(
164                                 "junitXacmlPU", xacmlPersistenceProperties);
165                 
166                 EntityManager em = emf.createEntityManager();
167                 EntityTransaction et = em.getTransaction();
168                 et.begin();
169                 
170                 // Make sure the DB is clean
171                 PolicyLogger.debug("cleanXacmlDB: clean StateManagementEntity");
172                 em.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
173                 PolicyLogger.debug("cleanXacmlDB: clean ResourceRegistrationEntity");
174                 em.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
175                 PolicyLogger.debug("cleanXacmlDB: clean ForwardProgressEntity");
176                 em.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
177                 
178                 em.flush(); 
179                 PolicyLogger.debug("cleandXacmlDB: after flush");
180
181                 et.commit(); 
182                 
183                 PolicyLogger.debug("\n\ncleanXacmlDB: Exiting\n\n");
184                 
185         }
186         
187         @Ignore
188         @Test
189         public void singleNodeTests() throws Exception{
190                 //snNewInstall();
191                 snNewInstallBadDepData();
192                 /*snRecoveryFromBadDepData();
193                 snLock();
194                 snLockRestart();
195                 snUnlock();
196                 snUnlockRestart();*/
197         }
198         
199         @Ignore
200         @Test
201         public void twoNodeTests() throws Exception{
202                 tnNewInstall();
203                 tnLockActive();
204                 tnUnlockColdStandby();
205                 tnFailActive();
206                 tnRecoverFailed();
207         }
208         
209         @Ignore
210         @Test
211         public void twoSitesTwoNodesPerSiteTests() throws Exception{
212                 tstnNewInstall();
213                 tstnLock1Site1();
214                 tstnLock2Site1();
215                 tstnFailActiveSite2();
216                 tstnRecoverFailedSite2();
217                 tstnUnlockSite1();
218                 tstnFailSite2();
219         }
220         
221
222         /*
223          * Single Node Tests
224          */
225         public void snNewInstall() throws Exception{
226                 PolicyLogger.debug("\n\nsnNewInstall: Entry\n\n");
227                 cleanDroolsDB();
228                 cleanXacmlDB();
229                 
230                 //*******************************************
231                 
232                 PolicyLogger.debug("snNewInstall: Reading IntegrityMonitorProperties");
233                 Properties integrityMonitorProperties = new Properties();
234                 integrityMonitorProperties.load(new FileInputStream(new File(
235                                 "src/test/server/config/IntegrityMonitor.properties")));
236                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
237                 String thisPdpId = IntegrityMonitorProperties
238                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
239
240                 PolicyLogger.debug("snNewInstall: Reading xacmlPersistenceProperties");
241                 Properties xacmlPersistenceProperties = new Properties();
242                 xacmlPersistenceProperties.load(new FileInputStream(new File(
243                                 "src/test/server/config/xacmlPersistence.properties")));
244                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
245                 
246                 PolicyLogger.debug("snNewInstall: Creating emfXacml");
247                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
248                                 "junitXacmlPU", xacmlPersistenceProperties);
249                 
250                 PolicyLogger.debug("snNewInstall: Reading droolsPersistenceProperties");
251                 Properties droolsPersistenceProperties = new Properties();
252                 droolsPersistenceProperties.load(new FileInputStream(new File(
253                                 "src/test/server/config/droolsPersistence.properties")));
254                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
255
256                 PolicyLogger.debug("snNewInstall: Creating emfDrools");
257                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
258                                 "junitDroolsPU", droolsPersistenceProperties);
259                 
260                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
261         
262                 PolicyLogger.debug("snNewInstall: Inserting PDP=" + thisPdpId + " as designated");
263                 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
264                 conn.insertPdp(pdp);
265                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
266                 PolicyLogger.debug("snNewInstall: After insertion, DESIGNATED="
267                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
268                 assertTrue(droolsPdpEntity.isDesignated() == true);
269
270                 /*
271                  * When the Standby Status changes (from providingservice) to hotstandby
272                  * or coldstandby,the Active/Standby selection algorithm must stand down
273                  * if thePDP-D is currently the lead/active node and allow another PDP-D
274                  * to take over.
275                  * 
276                  * It must also call lock on all engines in the engine management.
277                  * 
278                  */
279                 PolicyLogger.debug("snNewInstall: Instantiating stateManagement object");
280                 StateManagement sm = new StateManagement(emfXacml, thisPdpId);
281                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
282                 sm.addObserver(pmStandbyStateChangeNotifier);
283                 
284                 // Artificially putting a PDP into service is really a two step process, 1)
285                 // inserting it as designated and 2) promoting it so that its standbyStatus
286                 // is providing service.
287                 
288                 PolicyLogger.debug("snNewInstall: Running policy-management.Main class");
289                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
290                 policyManagementRunner.start();
291                 
292                 PolicyLogger.debug("snNewInstall: Runner started; Sleeping "
293                                 + interruptRecoveryTime + "ms before promoting PDP="
294                                 + thisPdpId);
295                 Thread.sleep(interruptRecoveryTime);
296
297                 PolicyLogger.debug("snNewInstall: Promoting PDP=" + thisPdpId);
298                 sm.promote();           
299                 
300                 String standbyStatus = sm.getStandbyStatus(thisPdpId);
301                 PolicyLogger.debug("snNewInstall: Before locking, PDP=" + thisPdpId + " has standbyStatus="
302                                 + standbyStatus);
303                 
304                 PolicyLogger.debug("snNewInstall: Locking sm");
305                 sm.lock();
306                 
307                 Thread.sleep(interruptRecoveryTime);
308                 /*
309                  * Verify that the PDP is no longer designated.
310                  */
311                 droolsPdpEntity = conn.getPdp(thisPdpId);
312                 PolicyLogger.debug("snNewInstall: After lock sm.lock() invoked, DESIGNATED="
313                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
314                 assertTrue(droolsPdpEntity.isDesignated() == false);
315                 
316                 PolicyLogger.debug("snNewInstall: Stopping policyManagementRunner");
317                 policyManagementRunner.stopRunner();
318         
319                 PolicyLogger.debug("\n\nsnNewInstall: Exiting\n\n");
320                 Thread.sleep(interruptRecoveryTime);
321                 
322                 //********************************************
323
324                 PolicyLogger.debug("\n\nsnNewInstall: Exit\n\n");
325         }
326         
327         public void snNewInstallBadDepData() throws Exception{
328                 PolicyLogger.debug("\n\nsnNewInstallBadDepData: Entry\n\n");
329                 cleanDroolsDB();
330                 cleanXacmlDB();
331                 
332                 //*******************************************
333                 
334                 PolicyLogger.debug("snNewInstallBadDepData: Reading IntegrityMonitor_BadDependencyData.properties");
335                 Properties integrityMonitorProperties = new Properties();
336                 integrityMonitorProperties.load(new FileInputStream(new File(
337                                 "src/test/server/config/IntegrityMonitor_BadDependencyData.properties")));
338                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
339                 String thisPdpId = IntegrityMonitorProperties
340                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
341
342                 PolicyLogger.debug("snNewInstallBadDepData: Reading xacmlPersistenceProperties");
343                 Properties xacmlPersistenceProperties = new Properties();
344                 xacmlPersistenceProperties.load(new FileInputStream(new File(
345                                 "src/test/server/config/xacmlPersistence.properties")));
346                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
347                 
348                 PolicyLogger.debug("snNewInstallBadDepData: Creating emfXacml");
349                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
350                                 "junitXacmlPU", xacmlPersistenceProperties);
351                 
352                 PolicyLogger.debug("snNewInstallBadDepData: Reading droolsPersistenceProperties");
353                 Properties droolsPersistenceProperties = new Properties();
354                 droolsPersistenceProperties.load(new FileInputStream(new File(
355                                 "src/test/server/config/droolsPersistence.properties")));
356                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
357
358                 PolicyLogger.debug("snNewInstallBadDepData: Creating emfDrools");
359                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
360                                 "junitDroolsPU", droolsPersistenceProperties);
361                 
362                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
363         
364                 PolicyLogger.debug("snNewInstallBadDepData: Inserting PDP=" + thisPdpId + " as designated");
365                 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
366                 conn.insertPdp(pdp);
367                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
368                 //PolicyLogger.debug
369                 System.out.println
370                 ("\n\nsnNewInstallBadDepData: After insertion, DESIGNATED="
371                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId + "\n\n********************");
372                 assertTrue(droolsPdpEntity.isDesignated() == true);
373                 
374                 /*
375                  * When the Standby Status changes (from providingservice) to hotstandby
376                  * or coldstandby,the Active/Standby selection algorithm must stand down
377                  * if thePDP-D is currently the lead/active node and allow another PDP-D
378                  * to take over.
379                  */
380                 PolicyLogger.debug("snNewInstall: Instantiating stateManagement object");
381                 StateManagement sm = new StateManagement(emfXacml, thisPdpId);
382                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
383                 sm.addObserver(pmStandbyStateChangeNotifier);
384                 
385                 // Artificially putting a PDP into service is really a two step process, 1)
386                 // inserting it as designated and 2) promoting it so that its standbyStatus
387                 // is providing service.
388                 
389                 PolicyLogger.debug("snNewInstall: Running policy-management.Main class");
390                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
391                 policyManagementRunner.start();
392                 
393                 PolicyLogger.debug("snNewInstall: Runner started; Sleeping "
394                                 + interruptRecoveryTime + "ms before promoting PDP="
395                                 + thisPdpId);
396                 Thread.sleep(interruptRecoveryTime);
397
398                 PolicyLogger.debug("snNewInstall: Promoting PDP=" + thisPdpId);
399                 sm.promote();           
400                 
401                 String standbyStatus = sm.getStandbyStatus(thisPdpId);
402                 PolicyLogger.debug("snNewInstall: Before locking, PDP=" + thisPdpId + " has standbyStatus="
403                                 + standbyStatus);
404                 
405                 /*
406                  * Verify that the PDP is no longer designated.
407                  */
408                 droolsPdpEntity = conn.getPdp(thisPdpId);
409                 PolicyLogger.debug("snNewInstall: After lock sm.lock() invoked, DESIGNATED="
410                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
411                 assertTrue(droolsPdpEntity.isDesignated() == false);
412                 
413                 PolicyLogger.debug("snNewInstall: Stopping policyManagementRunner");
414                 policyManagementRunner.stopRunner();
415         
416                 PolicyLogger.debug("\n\nsnNewInstall: Exiting\n\n");
417                 Thread.sleep(interruptRecoveryTime);
418                 
419                 //********************************************
420
421                 PolicyLogger.debug("\n\nsnNewInstallBadDepData: Exit\n\n");
422         }
423         
424         public void snRecoveryFromBadDepData() throws Exception{
425                 
426         }
427         
428         public void snLock() throws Exception {
429                 
430         }
431         
432         public void snLockRestart() throws Exception {
433                 
434         }
435         
436         public void snUnlock() throws Exception {
437                 
438         }
439         
440         public void snUnlockRestart() throws Exception {
441                 
442         }
443         
444         /*
445          * Two Nodes tests
446          */
447         public void tnNewInstall() throws Exception {
448                 
449         }
450         
451         public void tnLockActive() throws Exception {
452                 
453         }
454         
455         public void tnUnlockColdStandby() throws Exception {
456                 
457         }
458         
459         public void tnFailActive() throws Exception {
460                 
461         }
462         
463         public void tnRecoverFailed() throws Exception {
464                 
465         }
466         
467         /*
468          * Two Sites, Two Nodes Each Site tests
469          */
470         
471         public void tstnNewInstall() throws Exception {
472                 
473         }
474         
475         public void tstnLock1Site1() throws Exception {
476                 
477         }
478         
479         public void tstnLock2Site1() throws Exception {
480                 
481         }
482         
483         public void tstnFailActiveSite2() throws Exception {
484                 
485         }
486         
487         public void tstnRecoverFailedSite2() throws Exception {
488                 
489         }
490         
491         public void tstnUnlockSite1() throws Exception {
492                 
493         }
494         
495         public void tstnFailSite2() throws Exception {
496                 
497         }
498         
499         
500         @Ignore
501         @Test
502         public void testColdStandby() throws Exception {
503
504                 PolicyLogger.debug("\n\ntestColdStandby: Entering\n\n");
505
506                 PolicyLogger.debug("testColdStandby: Reading IntegrityMonitorProperties");
507                 Properties integrityMonitorProperties = new Properties();
508                 integrityMonitorProperties.load(new FileInputStream(new File(
509                                 "src/test/server/config/IntegrityMonitor.properties")));
510                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
511                 String thisPdpId = IntegrityMonitorProperties
512                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
513
514                 PolicyLogger.debug("testColdStandby: Reading xacmlPersistenceProperties");
515                 Properties xacmlPersistenceProperties = new Properties();
516                 xacmlPersistenceProperties.load(new FileInputStream(new File(
517                                 "src/test/server/config/xacmlPersistence.properties")));
518                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
519                 
520                 PolicyLogger.debug("testColdStandby: Creating emfXacml");
521                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
522                                 "junitXacmlPU", xacmlPersistenceProperties);
523                 
524                 PolicyLogger.debug("testColdStandby: Reading droolsPersistenceProperties");
525                 Properties droolsPersistenceProperties = new Properties();
526                 droolsPersistenceProperties.load(new FileInputStream(new File(
527                                 "src/test/server/config/droolsPersistence.properties")));
528                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
529
530                 PolicyLogger.debug("testColdStandby: Creating emfDrools");
531                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
532                                 "junitDroolsPU", droolsPersistenceProperties);
533                 
534                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
535                 
536                 PolicyLogger.debug("testColdStandby: Cleaning up tables");
537                 conn.deleteAllSessions();
538                 conn.deleteAllPdps();
539         
540                 PolicyLogger.debug("testColdStandby: Inserting PDP=" + thisPdpId + " as designated");
541                 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
542                 conn.insertPdp(pdp);
543                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
544                 PolicyLogger.debug("testColdStandby: After insertion, DESIGNATED="
545                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
546                 assertTrue(droolsPdpEntity.isDesignated() == true);
547
548                 /*
549                  * When the Standby Status changes (from providingservice) to hotstandby
550                  * or coldstandby,the Active/Standby selection algorithm must stand down
551                  * if thePDP-D is currently the lead/active node and allow another PDP-D
552                  * to take over.
553                  * 
554                  * It must also call lock on all engines in the engine management.
555                  * 
556                  * Yes, this is kludgy, but we have a chicken and egg problem here: we
557                  * need a StateManagement object to invoke the
558                  * deleteAllStateManagementEntities method.
559                  */
560                 PolicyLogger.debug("testColdStandby: Instantiating stateManagement object");
561                 StateManagement sm = new StateManagement(emfXacml, "dummy");
562                 sm.deleteAllStateManagementEntities();
563                 sm = new StateManagement(emfXacml, thisPdpId);
564                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
565                 sm.addObserver(pmStandbyStateChangeNotifier);
566                 
567                 // Artificially putting a PDP into service is really a two step process, 1)
568                 // inserting it as designated and 2) promoting it so that its standbyStatus
569                 // is providing service.
570                 
571                 PolicyLogger.debug("testColdStandby: Running policy-management.Main class");
572                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
573                 policyManagementRunner.start();
574                 
575                 PolicyLogger.debug("testColdStandby: Runner started; Sleeping "
576                                 + interruptRecoveryTime + "ms before promoting PDP="
577                                 + thisPdpId);
578                 Thread.sleep(interruptRecoveryTime);
579
580                 PolicyLogger.debug("testColdStandby: Promoting PDP=" + thisPdpId);
581                 sm.promote();           
582                 
583                 String standbyStatus = sm.getStandbyStatus(thisPdpId);
584                 PolicyLogger.debug("testColdStandby: Before locking, PDP=" + thisPdpId + " has standbyStatus="
585                                 + standbyStatus);
586                 
587                 PolicyLogger.debug("testColdStandby: Locking sm");
588                 sm.lock();
589                 
590                 Thread.sleep(interruptRecoveryTime);
591                 /*
592                  * Verify that the PDP is no longer designated.
593                  */
594                 droolsPdpEntity = conn.getPdp(thisPdpId);
595                 PolicyLogger.debug("testColdStandby: After lock sm.lock() invoked, DESIGNATED="
596                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
597                 assertTrue(droolsPdpEntity.isDesignated() == false);
598                 
599                 PolicyLogger.debug("testColdStandby: Stopping policyManagementRunner");
600                 policyManagementRunner.stopRunner();
601         
602                 PolicyLogger.debug("\n\ntestColdStandby: Exiting\n\n");
603                 Thread.sleep(interruptRecoveryTime);
604
605         }
606         
607         /*
608          * Tests hot standby when there is only one PDP.
609          */
610         @Ignore
611         @Test
612         public void testHotStandby1() throws Exception {
613         
614                 PolicyLogger.debug("\n\ntestHotStandby1: Entering\n\n");
615                 
616                 PolicyLogger.debug("testHotStandby1: Reading IntegrityMonitorProperties");
617                 Properties integrityMonitorProperties = new Properties();
618                 integrityMonitorProperties.load(new FileInputStream(new File(
619                                 "src/test/server/config/IntegrityMonitor.properties")));
620                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
621                 String thisPdpId = IntegrityMonitorProperties
622                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
623                 
624                 PolicyLogger.debug("testHotStandby1: Reading xacmlPersistenceProperties");
625                 Properties xacmlPersistenceProperties = new Properties();
626                 xacmlPersistenceProperties.load(new FileInputStream(new File(
627                                 "src/test/server/config/xacmlPersistence.properties")));
628                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
629                 
630                 PolicyLogger.debug("testHotStandby1: Creating emfXacml");
631                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
632                                 "junitXacmlPU", xacmlPersistenceProperties);
633                 
634                 PolicyLogger.debug("testHotStandby1: Reading droolsPersistenceProperties");
635                 Properties droolsPersistenceProperties = new Properties();
636                 droolsPersistenceProperties.load(new FileInputStream(new File(
637                                 "src/test/server/config/droolsPersistence.properties")));
638                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
639
640                 PolicyLogger.debug("testHotStandby1: Creating emfDrools");
641                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
642                                 "junitDroolsPU", droolsPersistenceProperties);
643                 
644                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
645                 
646                 PolicyLogger.debug("testHotStandby1: Cleaning up tables");
647                 conn.deleteAllSessions();
648                 conn.deleteAllPdps();
649                                         
650                 /*
651                  * Insert this PDP as not designated.  Initial standby state will be 
652                  * either null or cold standby.   Demoting should transit state to
653                  * hot standby.
654                  */
655                 PolicyLogger.debug("testHotStandby1: Inserting PDP=" + thisPdpId + " as not designated");
656                 Date yesterday = DateUtils.addDays(new Date(), -1);
657                 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
658                 conn.insertPdp(pdp);
659                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
660                 PolicyLogger.debug("testHotStandby1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
661                                 + droolsPdpEntity.isDesignated());
662                 assertTrue(droolsPdpEntity.isDesignated() == false);
663                 
664                 PolicyLogger.debug("testHotStandby1: Instantiating stateManagement object");
665                 StateManagement sm = new StateManagement(emfXacml, "dummy");
666                 sm.deleteAllStateManagementEntities();
667                 sm = new StateManagement(emfXacml, thisPdpId);
668                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
669                 sm.addObserver(pmStandbyStateChangeNotifier);
670
671                 PolicyLogger.debug("testHotStandby1: Demoting PDP=" + thisPdpId);
672                 // demoting should cause state to transit to hotstandby
673                 sm.demote();
674                 
675                 PolicyLogger.debug("testHotStandby1: Running policy-management.Main class");
676                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
677                 policyManagementRunner.start();
678                                 
679                 PolicyLogger.debug("testHotStandby1: Sleeping "
680                                 + sleepTime
681                                 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
682                 Thread.sleep(sleepTime);
683                 
684                 /*
685                  * Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
686                  */
687                 droolsPdpEntity = conn.getPdp(thisPdpId);
688                 PolicyLogger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED="
689                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
690                 assertTrue(droolsPdpEntity.isDesignated() == true);
691                 String standbyStatus = sm.getStandbyStatus(thisPdpId);
692                 PolicyLogger.debug("testHotStandby1: After demotion, PDP=" + thisPdpId + " has standbyStatus="
693                                 + standbyStatus);
694                 assertTrue(standbyStatus != null  &&  standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
695                                 
696                 PolicyLogger.debug("testHotStandby1: Stopping policyManagementRunner");
697                 policyManagementRunner.stopRunner();            
698         
699                 PolicyLogger.debug("\n\ntestHotStandby1: Exiting\n\n");
700                 Thread.sleep(interruptRecoveryTime);
701
702         }
703
704         /*
705          * Tests hot standby when two PDPs are involved.
706          */
707         @Ignore
708         @Test
709         public void testHotStandby2() throws Exception {
710
711                 PolicyLogger.debug("\n\ntestHotStandby2: Entering\n\n");
712                 
713                 PolicyLogger.debug("testHotStandby2: Reading IntegrityMonitorProperties");
714                 Properties integrityMonitorProperties = new Properties();
715                 integrityMonitorProperties.load(new FileInputStream(new File(
716                                 "src/test/server/config/IntegrityMonitor.properties")));
717                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
718                 String thisPdpId = IntegrityMonitorProperties
719                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
720                 
721                 PolicyLogger.debug("testHotStandby2: Reading xacmlPersistenceProperties");
722                 Properties xacmlPersistenceProperties = new Properties();
723                 xacmlPersistenceProperties.load(new FileInputStream(new File(
724                                 "src/test/server/config/xacmlPersistence.properties")));
725                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
726                 
727                 PolicyLogger.debug("testHotStandby2: Creating emfXacml");
728                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
729                                 "junitXacmlPU", xacmlPersistenceProperties);
730                 
731                 PolicyLogger.debug("testHotStandby2: Reading droolsPersistenceProperties");
732                 Properties droolsPersistenceProperties = new Properties();
733                 droolsPersistenceProperties.load(new FileInputStream(new File(
734                                 "src/test/server/config/droolsPersistence.properties")));
735                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
736
737                 PolicyLogger.debug("testHotStandby2: Creating emfDrools");
738                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
739                                 "junitDroolsPU", droolsPersistenceProperties);
740                 
741                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
742                 
743                 PolicyLogger.debug("testHotStandby2: Cleaning up tables");
744                 conn.deleteAllSessions();
745                 conn.deleteAllPdps();
746                 
747                 /*
748                  * Insert a PDP that's designated but not current.
749                  */
750                 String activePdpId = "pdp2";
751                 PolicyLogger.debug("testHotStandby2: Inserting PDP=" + activePdpId + " as stale, designated PDP");
752                 Date yesterday = DateUtils.addDays(new Date(), -1);
753                 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
754                 conn.insertPdp(pdp);
755                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
756                 PolicyLogger.debug("testHotStandby2: After insertion, PDP=" + activePdpId + ", which is not current, has DESIGNATED="
757                                 + droolsPdpEntity.isDesignated());
758                 assertTrue(droolsPdpEntity.isDesignated() == true);
759                 
760                 /*
761                  * Promote the designated PDP.
762                  * 
763                  * We have a chicken and egg problem here: we need a StateManagement
764                  * object to invoke the deleteAllStateManagementEntities method.
765                  */
766                 PolicyLogger.debug("testHotStandy2: Promoting PDP=" + activePdpId);
767                 StateManagement sm = new StateManagement(emfXacml, "dummy");
768                 sm.deleteAllStateManagementEntities();
769                 sm = new StateManagement(emfXacml, activePdpId);
770                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
771                 sm.addObserver(pmStandbyStateChangeNotifier);
772                 
773                 // Artificially putting a PDP into service is really a two step process, 1)
774                 // inserting it as designated and 2) promoting it so that its standbyStatus
775                 // is providing service.
776                                 
777                 /*
778                  * Insert this PDP as not designated.  Initial standby state will be 
779                  * either null or cold standby.   Demoting should transit state to
780                  * hot standby.
781                  */
782                 PolicyLogger.debug("testHotStandby2: Inserting PDP=" + thisPdpId + " as not designated");
783                 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
784                 conn.insertPdp(pdp);
785                 droolsPdpEntity = conn.getPdp(thisPdpId);
786                 PolicyLogger.debug("testHotStandby2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
787                                 + droolsPdpEntity.isDesignated());
788                 assertTrue(droolsPdpEntity.isDesignated() == false);
789                 
790                 PolicyLogger.debug("testHotStandby2: Demoting PDP=" + thisPdpId);
791                 StateManagement sm2 = new StateManagement(emfXacml, thisPdpId);
792                 sm2.addObserver(pmStandbyStateChangeNotifier);
793                 
794                 PolicyLogger.debug("testHotStandby2: Running policy-management.Main class");
795                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
796                 policyManagementRunner.start();
797                 
798                 PolicyLogger.debug("testHotStandby2: Runner started; Sleeping "
799                                 + interruptRecoveryTime + "ms before promoting/demoting");
800                 Thread.sleep(interruptRecoveryTime);
801
802                 PolicyLogger.debug("testHotStandby2: Runner started; promoting PDP=" + activePdpId);
803                 sm.promote();
804                 String standbyStatus = sm.getStandbyStatus(activePdpId);
805                 PolicyLogger.debug("testHotStandby2: After promoting, PDP=" + activePdpId + " has standbyStatus="
806                                 + standbyStatus);
807                 
808                 // demoting PDP should ensure that state transits to hotstandby
809                 PolicyLogger.debug("testHotStandby2: Runner started; demoting PDP=" + thisPdpId);
810                 sm2.demote();
811                 standbyStatus = sm.getStandbyStatus(thisPdpId);
812                 PolicyLogger.debug("testHotStandby2: After demoting, PDP=" + thisPdpId + " has standbyStatus="
813                                 + standbyStatus);
814                 
815                 PolicyLogger.debug("testHotStandby2: Sleeping "
816                                 + sleepTime
817                                 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
818                 Thread.sleep(sleepTime);
819                 
820                 /*
821                  * Verify that this PDP, demoted to HOT_STANDBY, is now
822                  * re-designated and providing service.
823                  */
824                 droolsPdpEntity = conn.getPdp(thisPdpId);
825                 PolicyLogger.debug("testHotStandby2: After demoting PDP=" + activePdpId
826                                 + ", DESIGNATED=" + droolsPdpEntity.isDesignated()
827                                 + " for PDP=" + thisPdpId);
828                 assertTrue(droolsPdpEntity.isDesignated() == true);
829                 standbyStatus = sm2.getStandbyStatus(thisPdpId);
830                 PolicyLogger.debug("testHotStandby2: After demoting PDP=" + activePdpId
831                                 + ", PDP=" + thisPdpId + " has standbyStatus=" + standbyStatus);
832                 assertTrue(standbyStatus != null
833                                 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
834                                 
835                 PolicyLogger.debug("testHotStandby2: Stopping policyManagementRunner");
836                 policyManagementRunner.stopRunner();            
837
838                 PolicyLogger.debug("\n\ntestHotStandby2: Exiting\n\n");
839                 Thread.sleep(interruptRecoveryTime);
840
841         }
842         
843         /*
844          * 1) Inserts and designates this PDP, then verifies that startTransaction
845          * is successful.
846          * 
847          * 2) Demotes PDP, and verifies that because there is only one PDP, it will
848          * be immediately re-promoted, thus allowing startTransaction to be
849          * successful.
850          * 
851          * 3) Locks PDP and verifies that startTransaction results in
852          * AdministrativeStateException.
853          * 
854          * 4) Unlocks PDP and verifies that startTransaction results in
855          * StandbyStatusException.
856          * 
857          * 5) Promotes PDP and verifies that startTransaction is once again
858          * successful.
859          */
860         @Ignore
861         @Test
862         public void testLocking1() throws Exception {
863                                 
864                 PolicyLogger.debug("testLocking1: Reading IntegrityMonitorProperties");
865                 Properties integrityMonitorProperties = new Properties();
866                 integrityMonitorProperties.load(new FileInputStream(new File(
867                                 "src/test/server/config/IntegrityMonitor.properties")));
868                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
869                 String thisPdpId = IntegrityMonitorProperties
870                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
871
872                 PolicyLogger.debug("testLocking1: Reading xacmlPersistenceProperties");
873                 Properties xacmlPersistenceProperties = new Properties();
874                 xacmlPersistenceProperties.load(new FileInputStream(new File(
875                                 "src/test/server/config/xacmlPersistence.properties")));
876                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
877                 
878                 PolicyLogger.debug("testLocking1: Creating emfXacml");
879                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
880                                 "junitXacmlPU", xacmlPersistenceProperties);
881                 
882                 PolicyLogger.debug("testLocking1: Reading droolsPersistenceProperties");
883                 Properties droolsPersistenceProperties = new Properties();
884                 droolsPersistenceProperties.load(new FileInputStream(new File(
885                                 "src/test/server/config/droolsPersistence.properties")));
886                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
887
888                 PolicyLogger.debug("testLocking1: Creating emfDrools");
889                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
890                                 "junitDroolsPU", droolsPersistenceProperties);
891                 
892                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
893                 
894                 PolicyLogger.debug("testLocking1: Cleaning up tables");
895                 conn.deleteAllSessions();
896                 conn.deleteAllPdps();
897                 
898                 /*
899                  * Insert this PDP as designated.  Initial standby state will be 
900                  * either null or cold standby.   
901                  */
902                 PolicyLogger.debug("testLocking1: Inserting PDP=" + thisPdpId + " as designated");
903                 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
904                 conn.insertPdp(pdp);
905                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
906                 PolicyLogger.debug("testLocking1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
907                                 + droolsPdpEntity.isDesignated());
908                 assertTrue(droolsPdpEntity.isDesignated() == true);
909                 
910                 PolicyLogger.debug("testLocking1: Instantiating stateManagement object");
911                 StateManagement sm = new StateManagement(emfXacml, "dummy");
912                 sm.deleteAllStateManagementEntities();
913                 sm = new StateManagement(emfXacml, thisPdpId);
914                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
915                 sm.addObserver(pmStandbyStateChangeNotifier);
916                                 
917                 PolicyLogger.debug("testLocking1: Running policy-management.Main class, designated="
918                                 + conn.getPdp(thisPdpId).isDesignated());
919                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
920                 policyManagementRunner.start();
921                 
922                 PolicyLogger.debug("testLocking1: Runner started; Sleeping "
923                                 + interruptRecoveryTime + "ms before promoting PDP="
924                                 + thisPdpId);
925                 Thread.sleep(interruptRecoveryTime);
926
927                 PolicyLogger.debug("testLocking1: Promoting PDP=" + thisPdpId);
928                 sm.promote();
929
930                 PolicyLogger.debug("testLocking1: Sleeping "
931                                 + sleepTime
932                                 + "ms, to allow time for policy-management.Main class to come up, designated="
933                                 + conn.getPdp(thisPdpId).isDesignated());
934                 Thread.sleep(sleepTime);
935                 
936                 PolicyLogger.debug("testLocking1: Waking up and invoking startTransaction on active PDP="
937                                 + thisPdpId
938                                 + ", designated="
939                                 + conn.getPdp(thisPdpId).isDesignated());
940                 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
941                                 .getInstance();
942                 try {
943                         droolsPdpIntegrityMonitor.startTransaction();
944                         droolsPdpIntegrityMonitor.endTransaction();
945                         PolicyLogger.debug("testLocking1: As expected, transaction successful");
946                 } catch (AdministrativeStateException e) {
947                         PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
948                         assertTrue(false);
949                 } catch (StandbyStatusException e) {
950                         PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
951                         assertTrue(false);
952                 } catch (Exception e) {
953                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
954                         assertTrue(false);
955                 }
956                 
957                 // demoting should cause state to transit to hotstandby, followed by re-promotion,
958                 // since there is only one PDP.
959                 PolicyLogger.debug("testLocking1: demoting PDP=" + thisPdpId);
960                 sm = droolsPdpIntegrityMonitor.getStateManager();
961                 sm.demote();
962                 
963                 PolicyLogger.debug("testLocking1: sleeping" + electionWaitSleepTime
964                                 + " to allow election handler to re-promote PDP=" + thisPdpId);
965                 Thread.sleep(electionWaitSleepTime);
966                                                                 
967                 PolicyLogger.debug("testLocking1: Invoking startTransaction on re-promoted PDP="
968                                 + thisPdpId
969                                 + ", designated="
970                                 + conn.getPdp(thisPdpId).isDesignated());
971                 try {
972                         droolsPdpIntegrityMonitor.startTransaction();
973                         droolsPdpIntegrityMonitor.endTransaction();
974                         PolicyLogger.debug("testLocking1: As expected, transaction successful");
975                 } catch (AdministrativeStateException e) {
976                         PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
977                         assertTrue(false);
978                 } catch (StandbyStatusException e) {
979                         PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
980                         assertTrue(false);
981                 } catch (Exception e) {
982                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
983                         assertTrue(false);
984                 }
985                 
986                 // locking should cause state to transit to cold standby
987                 PolicyLogger.debug("testLocking1: locking PDP=" + thisPdpId);
988                 sm.lock();
989                 
990                 // Just to avoid any race conditions, sleep a little after locking
991                 PolicyLogger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
992                 Thread.sleep(100);
993                 
994                 PolicyLogger.debug("testLocking1: Invoking startTransaction on locked PDP="
995                                 + thisPdpId
996                                 + ", designated="
997                                 + conn.getPdp(thisPdpId).isDesignated());
998                 try {
999                         droolsPdpIntegrityMonitor.startTransaction();
1000                         PolicyLogger.error("testLocking1: startTransaction unexpectedly successful");
1001                         assertTrue(false);
1002                 } catch (AdministrativeStateException e) {
1003                         PolicyLogger.debug("testLocking1: As expected, caught AdministrativeStateException, message=" + e.getMessage());
1004                 } catch (StandbyStatusException e) {
1005                         PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1006                         assertTrue(false);
1007                 } catch (Exception e) {
1008                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1009                         assertTrue(false);
1010                 } finally {
1011                         droolsPdpIntegrityMonitor.endTransaction();
1012                 }               
1013                 
1014                 // unlocking should cause state to transit to hot standby
1015                 PolicyLogger.debug("testLocking1: unlocking PDP=" + thisPdpId);
1016                 sm.unlock();
1017                 
1018                 // Just to avoid any race conditions, sleep a little after locking
1019                 PolicyLogger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1020                 Thread.sleep(100);
1021                 
1022                 PolicyLogger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1023                                 + thisPdpId
1024                                 + ", designated="
1025                                 + conn.getPdp(thisPdpId).isDesignated());
1026                 try {
1027                         droolsPdpIntegrityMonitor.startTransaction();
1028                         PolicyLogger.error("testLocking1: startTransaction unexpectedly successful");
1029                         assertTrue(false);
1030                 } catch (AdministrativeStateException e) {
1031                         PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1032                         assertTrue(false);
1033                 } catch (StandbyStatusException e) {
1034                         PolicyLogger.debug("testLocking1: As expected, caught StandbyStatusException, message=" + e.getMessage());
1035                 } catch (Exception e) {
1036                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1037                         assertTrue(false);
1038                 } finally {
1039                         droolsPdpIntegrityMonitor.endTransaction();
1040                 }
1041                 
1042                 // promoting should cause state to transit to providing service
1043                 PolicyLogger.debug("testLocking1: promoting PDP=" + thisPdpId);
1044                 sm.promote();
1045                 
1046                 // Just to avoid any race conditions, sleep a little after promoting
1047                 PolicyLogger.debug("testLocking1: Sleeping a few millis after promoting, to avoid race condition");
1048                 Thread.sleep(100);
1049                 
1050                 PolicyLogger.debug("testLocking1: Invoking startTransaction on promoted PDP="
1051                                 + thisPdpId
1052                                 + ", designated="
1053                                 + conn.getPdp(thisPdpId).isDesignated());
1054                 try {
1055                         droolsPdpIntegrityMonitor.startTransaction();
1056                         droolsPdpIntegrityMonitor.endTransaction();
1057                         PolicyLogger.debug("testLocking1: As expected, transaction successful");
1058                 } catch (AdministrativeStateException e) {
1059                         PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1060                         assertTrue(false);
1061                 } catch (StandbyStatusException e) {
1062                         PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1063                         assertTrue(false);
1064                 } catch (Exception e) {
1065                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
1066                         assertTrue(false);
1067                 }
1068                 
1069                 PolicyLogger.debug("testLocking1: Stopping policyManagementRunner");
1070                 policyManagementRunner.stopRunner();            
1071
1072                 PolicyLogger.debug("\n\ntestLocking1: Exiting\n\n");
1073                 Thread.sleep(interruptRecoveryTime);
1074
1075         }
1076         
1077         /*
1078          * 1) Inserts and designates this PDP, then verifies that startTransaction
1079          * is successful.
1080          * 
1081          * 2) Inserts another PDP in hotstandby.
1082          * 
1083          * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1084          * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1085          */
1086         @Ignore
1087         @Test
1088         public void testLocking2() throws Exception {
1089
1090                 PolicyLogger.debug("\n\ntestLocking2: Entering\n\n");
1091                 
1092                 PolicyLogger.debug("testLocking2: Reading IntegrityMonitorProperties");
1093                 Properties integrityMonitorProperties = new Properties();
1094                 integrityMonitorProperties.load(new FileInputStream(new File(
1095                                 "src/test/server/config/IntegrityMonitor.properties")));
1096                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
1097                 String thisPdpId = IntegrityMonitorProperties
1098                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
1099
1100                 PolicyLogger.debug("testLocking2: Reading xacmlPersistenceProperties");
1101                 Properties xacmlPersistenceProperties = new Properties();
1102                 xacmlPersistenceProperties.load(new FileInputStream(new File(
1103                                 "src/test/server/config/xacmlPersistence.properties")));
1104                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
1105                 
1106                 PolicyLogger.debug("testLocking2: Creating emfXacml");
1107                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1108                                 "junitXacmlPU", xacmlPersistenceProperties);
1109                 
1110                 PolicyLogger.debug("testLocking2: Reading droolsPersistenceProperties");
1111                 Properties droolsPersistenceProperties = new Properties();
1112                 droolsPersistenceProperties.load(new FileInputStream(new File(
1113                                 "src/test/server/config/droolsPersistence.properties")));
1114                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
1115
1116                 PolicyLogger.debug("testLocking2: Creating emfDrools");
1117                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1118                                 "junitDroolsPU", droolsPersistenceProperties);
1119                 
1120                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1121                 
1122                 PolicyLogger.debug("testLocking2: Cleaning up tables");
1123                 conn.deleteAllSessions();
1124                 conn.deleteAllPdps();
1125                 
1126                 /*
1127                  * Insert this PDP as designated.  Initial standby state will be 
1128                  * either null or cold standby.   Demoting should transit state to
1129                  * hot standby.
1130                  */
1131                 PolicyLogger.debug("testLocking2: Inserting PDP=" + thisPdpId + " as designated");
1132                 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
1133                 conn.insertPdp(pdp);
1134                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1135                 PolicyLogger.debug("testLocking2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
1136                                 + droolsPdpEntity.isDesignated());
1137                 assertTrue(droolsPdpEntity.isDesignated() == true);
1138                 
1139                 PolicyLogger.debug("testLocking2: Instantiating stateManagement object and promoting PDP=" + thisPdpId);
1140                 StateManagement sm = new StateManagement(emfXacml, "dummy");
1141                 sm.deleteAllStateManagementEntities();
1142                 sm = new StateManagement(emfXacml, thisPdpId);
1143                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
1144                 sm.addObserver(pmStandbyStateChangeNotifier);
1145                                 
1146                 /*
1147                  * Insert another PDP as not designated.  Initial standby state will be 
1148                  * either null or cold standby.   Demoting should transit state to
1149                  * hot standby.
1150                  */
1151                 String standbyPdpId = "pdp2";
1152                 PolicyLogger.debug("testLocking2: Inserting PDP=" + standbyPdpId + " as not designated");
1153                 Date yesterday = DateUtils.addDays(new Date(), -1);
1154                 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1155                 conn.insertPdp(pdp);
1156                 droolsPdpEntity = conn.getPdp(standbyPdpId);
1157                 PolicyLogger.debug("testLocking2: After insertion, PDP=" + standbyPdpId + " has DESIGNATED="
1158                                 + droolsPdpEntity.isDesignated());
1159                 assertTrue(droolsPdpEntity.isDesignated() == false);
1160                 
1161                 PolicyLogger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
1162                 StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
1163                 sm2.addObserver(pmStandbyStateChangeNotifier);
1164                                 
1165                 PolicyLogger.debug("testLocking2: Running policy-management.Main class");
1166                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
1167                 policyManagementRunner.start();
1168                 
1169                 PolicyLogger.debug("testLocking2: Runner started; Sleeping "
1170                                 + interruptRecoveryTime + "ms before promoting/demoting");
1171                 Thread.sleep(interruptRecoveryTime);
1172
1173                 PolicyLogger.debug("testLocking2: Promoting PDP=" + thisPdpId);
1174                 sm.promote();
1175
1176                 // demoting PDP should ensure that state transits to hotstandby
1177                 PolicyLogger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
1178                 sm2.demote();
1179                 
1180                 PolicyLogger.debug("testLocking2: Sleeping "
1181                                 + sleepTime
1182                                 + "ms, to allow time for policy-management.Main class to come up");
1183                 Thread.sleep(sleepTime);
1184                 
1185                 PolicyLogger.debug("testLocking2: Waking up and invoking startTransaction on active PDP="
1186                                 + thisPdpId
1187                                 + ", designated="
1188                                 + conn.getPdp(thisPdpId).isDesignated());
1189                 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
1190                                 .getInstance();
1191                 try {
1192                         droolsPdpIntegrityMonitor.startTransaction();
1193                         droolsPdpIntegrityMonitor.endTransaction();
1194                         PolicyLogger.debug("testLocking2: As expected, transaction successful");
1195                 } catch (AdministrativeStateException e) {
1196                         PolicyLogger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1197                         assertTrue(false);
1198                 } catch (StandbyStatusException e) {
1199                         PolicyLogger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1200                         assertTrue(false);
1201                 } catch (Exception e) {
1202                         PolicyLogger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
1203                         assertTrue(false);
1204                 }
1205                 
1206                 // demoting should cause state to transit to hotstandby followed by re-promotion.
1207                 PolicyLogger.debug("testLocking2: demoting PDP=" + thisPdpId);
1208                 sm = droolsPdpIntegrityMonitor.getStateManager();
1209                 sm.demote();
1210                 
1211                 PolicyLogger.debug("testLocking2: sleeping" + electionWaitSleepTime
1212                                 + " to allow election handler to re-promote PDP=" + thisPdpId);
1213                 Thread.sleep(electionWaitSleepTime);
1214                 
1215                 PolicyLogger.debug("testLocking2: Waking up and invoking startTransaction on re-promoted PDP="
1216                                 + thisPdpId + ", designated="
1217                                 + conn.getPdp(thisPdpId).isDesignated());
1218                 try {
1219                         droolsPdpIntegrityMonitor.startTransaction();
1220                         droolsPdpIntegrityMonitor.endTransaction();
1221                         PolicyLogger.debug("testLocking2: As expected, transaction successful");
1222                 } catch (AdministrativeStateException e) {
1223                         PolicyLogger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
1224                         assertTrue(false);
1225                 } catch (StandbyStatusException e) {
1226                         PolicyLogger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
1227                         assertTrue(false);
1228                 } catch (Exception e) {
1229                         PolicyLogger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
1230                         assertTrue(false);
1231                 }
1232                 
1233                 PolicyLogger.debug("testLocking2: Verifying designated status for PDP="
1234                                 + standbyPdpId);
1235                 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
1236                 assertTrue(standbyPdpDesignated == false);
1237                 
1238                 PolicyLogger.debug("testLocking2: Stopping policyManagementRunner");
1239                 policyManagementRunner.stopRunner();            
1240
1241                 PolicyLogger.debug("\n\ntestLocking2: Exiting\n\n");
1242                 Thread.sleep(interruptRecoveryTime);
1243
1244         }
1245         
1246         private class PolicyManagementRunner extends Thread {
1247
1248                 public void run() {
1249                         PolicyLogger.debug("PolicyManagementRunner.run: Entering");
1250                         String args[] = { "src/main/server/config" };
1251                         try {
1252                                 Main.main(args);
1253                         } catch (Exception e) {
1254                                 PolicyLogger
1255                                                 .debug("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
1256                                                                 + e.getMessage());
1257                         }
1258                         PolicyLogger.debug("PolicyManagementRunner.run: Exiting");
1259                 }
1260                 
1261                 public void stopRunner() {
1262                         PolicyEngine.manager.shutdown();
1263                 }
1264
1265         }
1266         
1267 }