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