af6498661cf03fa3587789fec6d364d9ce3ebca5
[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.Date;
28 import java.util.Properties;
29
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.Persistence;
32
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Ignore;
38 import org.junit.Test;
39
40 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
41 import org.openecomp.policy.common.im.AdministrativeStateException;
42 import org.openecomp.policy.common.im.IntegrityMonitor;
43 import org.openecomp.policy.common.im.StandbyStatusException;
44 import org.openecomp.policy.common.im.StateManagement;
45 import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
46 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
47 import org.openecomp.policy.drools.core.PolicyContainer;
48 import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
49 import org.openecomp.policy.drools.persistence.DroolsPdp;
50 import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
51 import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
52 import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
53 import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
54 import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
55 import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
56 import org.openecomp.policy.drools.system.Main;
57 import org.openecomp.policy.drools.system.PolicyEngine;
58
59 import org.apache.commons.lang3.time.DateUtils;
60
61 /*
62  * Cloned from StandbyStateManagement.java in support of US673632.
63  * See MultiSite_v1-10.ppt, slide 38
64  */
65 public class StandbyStateManagementTest {
66                         
67         /*
68          * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every ten seconds, starting 
69          * at ten seconds after the minute boundary (e.g. 13:05:10). So, an 80 second sleep should be 
70          * sufficient to ensure that we wait for the DesignationWaiter to do its job, before 
71          * checking the results. 
72          */
73         long sleepTime = 80000;
74         
75         /*
76          * DroolsPdpsElectionHandler runs every ten seconds, so a 15 second sleep should be 
77          * plenty to ensure it has time to re-promote this PDP.
78          */
79         long electionWaitSleepTime = 15000;
80         
81         /*
82          * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
83          */
84         long interruptRecoveryTime = 5000;
85
86         /*
87          * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
88          */
89         @BeforeClass
90         public static void setUpClass() throws Exception {
91                 
92                 String userDir = System.getProperty("user.dir");
93                 PolicyLogger.debug("setUpClass: userDir=" + userDir);
94                 System.setProperty("com.sun.management.jmxremote.port", "9980");
95                 System.setProperty("com.sun.management.jmxremote.authenticate","false");
96                                 
97                 // Make sure path to config directory is set correctly in PolicyContainer.main
98                 // Also make sure we ignore HTTP server failures resulting from port conflicts.
99                 PolicyContainer.isUnitTesting = true;
100                 
101         }
102
103         @AfterClass
104         public static void tearDownClass() throws Exception {
105         }
106
107         @Before
108         public void setUp() throws Exception {
109         }
110
111         @After
112         public void tearDown() throws Exception {
113                                 
114         }
115
116         @Ignore
117         @Test
118         public void testColdStandby() throws Exception {
119
120                 PolicyLogger.debug("\n\ntestColdStandby: Entering\n\n");
121
122                 PolicyLogger.debug("testColdStandby: Reading IntegrityMonitorProperties");
123                 Properties integrityMonitorProperties = new Properties();
124                 integrityMonitorProperties.load(new FileInputStream(new File(
125                                 "src/test/server/config/IntegrityMonitor.properties")));
126                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
127                 String thisPdpId = IntegrityMonitorProperties
128                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
129
130                 PolicyLogger.debug("testColdStandby: Reading xacmlPersistenceProperties");
131                 Properties xacmlPersistenceProperties = new Properties();
132                 xacmlPersistenceProperties.load(new FileInputStream(new File(
133                                 "src/test/server/config/xacmlPersistence.properties")));
134                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
135                 
136                 PolicyLogger.debug("testColdStandby: Creating emfXacml");
137                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
138                                 "junitXacmlPU", xacmlPersistenceProperties);
139                 
140                 PolicyLogger.debug("testColdStandby: Reading droolsPersistenceProperties");
141                 Properties droolsPersistenceProperties = new Properties();
142                 droolsPersistenceProperties.load(new FileInputStream(new File(
143                                 "src/test/server/config/droolsPersistence.properties")));
144                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
145
146                 PolicyLogger.debug("testColdStandby: Creating emfDrools");
147                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
148                                 "junitDroolsPU", droolsPersistenceProperties);
149                 
150                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
151                 
152                 PolicyLogger.debug("testColdStandby: Cleaning up tables");
153                 conn.deleteAllSessions();
154                 conn.deleteAllPdps();
155         
156                 PolicyLogger.debug("testColdStandby: Inserting PDP=" + thisPdpId + " as designated");
157                 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
158                 conn.insertPdp(pdp);
159                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
160                 PolicyLogger.debug("testColdStandby: After insertion, DESIGNATED="
161                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
162                 assertTrue(droolsPdpEntity.isDesignated() == true);
163
164                 /*
165                  * When the Standby Status changes (from providingservice) to hotstandby
166                  * or coldstandby,the Active/Standby selection algorithm must stand down
167                  * if thePDP-D is currently the lead/active node and allow another PDP-D
168                  * to take over.
169                  * 
170                  * It must also call lock on all engines in the engine management.
171                  * 
172                  * Yes, this is kludgy, but we have a chicken and egg problem here: we
173                  * need a StateManagement object to invoke the
174                  * deleteAllStateManagementEntities method.
175                  */
176                 PolicyLogger.debug("testColdStandby: Instantiating stateManagement object");
177                 StateManagement sm = new StateManagement(emfXacml, "dummy");
178                 sm.deleteAllStateManagementEntities();
179                 sm = new StateManagement(emfXacml, thisPdpId);
180                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
181                 sm.addObserver(pmStandbyStateChangeNotifier);
182                 
183                 // Artificially putting a PDP into service is really a two step process, 1)
184                 // inserting it as designated and 2) promoting it so that its standbyStatus
185                 // is providing service.
186                 
187                 PolicyLogger.debug("testColdStandby: Running policy-management.Main class");
188                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
189                 policyManagementRunner.start();
190                 
191                 PolicyLogger.debug("testColdStandby: Runner started; Sleeping "
192                                 + interruptRecoveryTime + "ms before promoting PDP="
193                                 + thisPdpId);
194                 Thread.sleep(interruptRecoveryTime);
195
196                 PolicyLogger.debug("testColdStandby: Promoting PDP=" + thisPdpId);
197                 sm.promote();           
198                 
199                 String standbyStatus = sm.getStandbyStatus(thisPdpId);
200                 PolicyLogger.debug("testColdStandby: Before locking, PDP=" + thisPdpId + " has standbyStatus="
201                                 + standbyStatus);
202                 
203                 PolicyLogger.debug("testColdStandby: Locking sm");
204                 sm.lock();
205                 
206                 Thread.sleep(interruptRecoveryTime);
207                 /*
208                  * Verify that the PDP is no longer designated.
209                  */
210                 droolsPdpEntity = conn.getPdp(thisPdpId);
211                 PolicyLogger.debug("testColdStandby: After lock sm.lock() invoked, DESIGNATED="
212                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
213                 assertTrue(droolsPdpEntity.isDesignated() == false);
214                 
215                 PolicyLogger.debug("testColdStandby: Stopping policyManagementRunner");
216                 policyManagementRunner.stopRunner();
217         
218                 PolicyLogger.debug("\n\ntestColdStandby: Exiting\n\n");
219                 Thread.sleep(interruptRecoveryTime);
220
221         }
222         
223         /*
224          * Tests hot standby when there is only one PDP.
225          */
226         @Ignore
227         @Test
228         public void testHotStandby1() throws Exception {
229         
230                 PolicyLogger.debug("\n\ntestHotStandby1: Entering\n\n");
231                 
232                 PolicyLogger.debug("testHotStandby1: 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("testHotStandby1: 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("testHotStandby1: Creating emfXacml");
247                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
248                                 "junitXacmlPU", xacmlPersistenceProperties);
249                 
250                 PolicyLogger.debug("testHotStandby1: 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("testHotStandby1: Creating emfDrools");
257                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
258                                 "junitDroolsPU", droolsPersistenceProperties);
259                 
260                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
261                 
262                 PolicyLogger.debug("testHotStandby1: Cleaning up tables");
263                 conn.deleteAllSessions();
264                 conn.deleteAllPdps();
265                                         
266                 /*
267                  * Insert this PDP as not designated.  Initial standby state will be 
268                  * either null or cold standby.   Demoting should transit state to
269                  * hot standby.
270                  */
271                 PolicyLogger.debug("testHotStandby1: Inserting PDP=" + thisPdpId + " as not designated");
272                 Date yesterday = DateUtils.addDays(new Date(), -1);
273                 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
274                 conn.insertPdp(pdp);
275                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
276                 PolicyLogger.debug("testHotStandby1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
277                                 + droolsPdpEntity.isDesignated());
278                 assertTrue(droolsPdpEntity.isDesignated() == false);
279                 
280                 PolicyLogger.debug("testHotStandby1: Instantiating stateManagement object");
281                 StateManagement sm = new StateManagement(emfXacml, "dummy");
282                 sm.deleteAllStateManagementEntities();
283                 sm = new StateManagement(emfXacml, thisPdpId);
284                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
285                 sm.addObserver(pmStandbyStateChangeNotifier);
286
287                 PolicyLogger.debug("testHotStandby1: Demoting PDP=" + thisPdpId);
288                 // demoting should cause state to transit to hotstandby
289                 sm.demote();
290                 
291                 PolicyLogger.debug("testHotStandby1: Running policy-management.Main class");
292                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
293                 policyManagementRunner.start();
294                                 
295                 PolicyLogger.debug("testHotStandby1: Sleeping "
296                                 + sleepTime
297                                 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
298                 Thread.sleep(sleepTime);
299                 
300                 /*
301                  * Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
302                  */
303                 droolsPdpEntity = conn.getPdp(thisPdpId);
304                 PolicyLogger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED="
305                                 + droolsPdpEntity.isDesignated() + " for PDP=" + thisPdpId);
306                 assertTrue(droolsPdpEntity.isDesignated() == true);
307                 String standbyStatus = sm.getStandbyStatus(thisPdpId);
308                 PolicyLogger.debug("testHotStandby1: After demotion, PDP=" + thisPdpId + " has standbyStatus="
309                                 + standbyStatus);
310                 assertTrue(standbyStatus != null  &&  standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
311                                 
312                 PolicyLogger.debug("testHotStandby1: Stopping policyManagementRunner");
313                 policyManagementRunner.stopRunner();            
314         
315                 PolicyLogger.debug("\n\ntestHotStandby1: Exiting\n\n");
316                 Thread.sleep(interruptRecoveryTime);
317
318         }
319
320         /*
321          * Tests hot standby when two PDPs are involved.
322          */
323         @Ignore
324         @Test
325         public void testHotStandby2() throws Exception {
326
327                 PolicyLogger.info("\n\ntestHotStandby2: Entering\n\n");
328                 
329                 PolicyLogger.info("testHotStandby2: Reading IntegrityMonitorProperties");
330                 Properties integrityMonitorProperties = new Properties();
331                 integrityMonitorProperties.load(new FileInputStream(new File(
332                                 "src/test/server/config/IntegrityMonitor.properties")));
333                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
334                 String thisPdpId = IntegrityMonitorProperties
335                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
336                 
337                 PolicyLogger.info("testHotStandby2: Reading xacmlPersistenceProperties");
338                 Properties xacmlPersistenceProperties = new Properties();
339                 xacmlPersistenceProperties.load(new FileInputStream(new File(
340                                 "src/test/server/config/xacmlPersistence.properties")));
341                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
342                 
343                 PolicyLogger.info("testHotStandby2: Creating emfXacml");
344                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
345                                 "junitXacmlPU", xacmlPersistenceProperties);
346                 
347                 PolicyLogger.info("testHotStandby2: Reading droolsPersistenceProperties");
348                 Properties droolsPersistenceProperties = new Properties();
349                 droolsPersistenceProperties.load(new FileInputStream(new File(
350                                 "src/test/server/config/droolsPersistence.properties")));
351                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
352
353                 PolicyLogger.info("testHotStandby2: Creating emfDrools");
354                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
355                                 "junitDroolsPU", droolsPersistenceProperties);
356                 
357                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
358                 
359                 PolicyLogger.info("testHotStandby2: Cleaning up tables");
360                 conn.deleteAllSessions();
361                 conn.deleteAllPdps();
362                 
363                 /*
364                  * Insert a PDP that's designated but not current.
365                  */
366                 String activePdpId = "pdp2";
367                 PolicyLogger.info("testHotStandby2: Inserting PDP=" + activePdpId + " as stale, designated PDP");
368                 Date yesterday = DateUtils.addDays(new Date(), -1);
369                 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
370                 conn.insertPdp(pdp);
371                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
372                 PolicyLogger.info("testHotStandby2: After insertion, PDP=" + activePdpId + ", which is not current, has DESIGNATED="
373                                 + droolsPdpEntity.isDesignated());
374                 assertTrue(droolsPdpEntity.isDesignated() == true);
375                 
376                 /*
377                  * Promote the designated PDP.
378                  * 
379                  * We have a chicken and egg problem here: we need a StateManagement
380                  * object to invoke the deleteAllStateManagementEntities method.
381                  */
382                 PolicyLogger.info("testHotStandy2: Promoting PDP=" + activePdpId);
383                 StateManagement sm = new StateManagement(emfXacml, "dummy");
384                 sm.deleteAllStateManagementEntities();
385                 sm = new StateManagement(emfXacml, activePdpId);//pdp2
386                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
387                 sm.addObserver(pmStandbyStateChangeNotifier);
388                 
389                 // Artificially putting a PDP into service is really a two step process, 1)
390                 // inserting it as designated and 2) promoting it so that its standbyStatus
391                 // is providing service.
392                                 
393                 /*
394                  * Insert this PDP as not designated.  Initial standby state will be 
395                  * either null or cold standby.   Demoting should transit state to
396                  * hot standby.
397                  */
398                 PolicyLogger.info("testHotStandby2: Inserting PDP=" + thisPdpId + " as not designated");
399                 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
400                 conn.insertPdp(pdp);
401                 droolsPdpEntity = conn.getPdp(thisPdpId);
402                 PolicyLogger.info("testHotStandby2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
403                                 + droolsPdpEntity.isDesignated());
404                 assertTrue(droolsPdpEntity.isDesignated() == false);
405                 
406                 PolicyLogger.info("testHotStandby2: Demoting PDP=" + thisPdpId);//pdp1
407                 StateManagement sm2 = new StateManagement(emfXacml, thisPdpId);
408                 sm2.addObserver(pmStandbyStateChangeNotifier);
409                 
410                 PolicyLogger.info("testHotStandby2: Running policy-management.Main class");
411                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner(); //pdp1
412                 policyManagementRunner.start();
413                 
414                 PolicyLogger.info("testHotStandby2: Runner started; Sleeping "
415                                 + interruptRecoveryTime + "ms before promoting/demoting");
416                 Thread.sleep(interruptRecoveryTime);
417
418                 PolicyLogger.info("testHotStandby2: Runner started; promoting PDP=" + activePdpId);//pdpd2xs
419                 //at this point, the newly created pdp will have set the state to disabled/failed/cold standby
420                 //because it is stale. So, it cannot be promoted.  We need to call sm.enableNotFailed() so we
421                 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
422                 sm.enableNotFailed();//pdp1
423                 sm.promote();
424                 String standbyStatus = sm.getStandbyStatus(activePdpId);
425                 PolicyLogger.info("testHotStandby2: After promoting, PDP=" + activePdpId + " has standbyStatus="
426                                 + standbyStatus);
427                 
428                 // demoting PDP should ensure that state transits to hotstandby
429                 PolicyLogger.info("testHotStandby2: Runner started; demoting PDP=" + thisPdpId);
430                 sm2.demote();//pdp1
431                 standbyStatus = sm.getStandbyStatus(thisPdpId);
432                 PolicyLogger.info("testHotStandby2: After demoting, PDP=" + thisPdpId + " has standbyStatus="
433                                 + standbyStatus);
434                 
435                 PolicyLogger.info("testHotStandby2: Sleeping "
436                                 + sleepTime
437                                 + "ms, to allow JpaDroolsPdpsConnector time to check droolspdpentity table");
438                 Thread.sleep(sleepTime);
439                 
440                 /*
441                  * Verify that this PDP, demoted to HOT_STANDBY, is now
442                  * re-designated and providing service.
443                  */
444                 droolsPdpEntity = conn.getPdp(thisPdpId);
445                 PolicyLogger.info("testHotStandby2: After demoting PDP=" + activePdpId
446                                 + ", DESIGNATED=" + droolsPdpEntity.isDesignated()
447                                 + " for PDP=" + thisPdpId);
448                 assertTrue(droolsPdpEntity.isDesignated() == true);
449                 standbyStatus = sm2.getStandbyStatus(thisPdpId);
450                 PolicyLogger.info("testHotStandby2: After demoting PDP=" + activePdpId
451                                 + ", PDP=" + thisPdpId + " has standbyStatus=" + standbyStatus);
452                 assertTrue(standbyStatus != null
453                                 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
454                                 
455                 PolicyLogger.info("testHotStandby2: Stopping policyManagementRunner");
456                 policyManagementRunner.stopRunner();            
457
458                 PolicyLogger.info("\n\ntestHotStandby2: Exiting\n\n");
459                 Thread.sleep(interruptRecoveryTime);
460
461         }
462         
463         /*
464          * 1) Inserts and designates this PDP, then verifies that startTransaction
465          * is successful.
466          * 
467          * 2) Demotes PDP, and verifies that because there is only one PDP, it will
468          * be immediately re-promoted, thus allowing startTransaction to be
469          * successful.
470          * 
471          * 3) Locks PDP and verifies that startTransaction results in
472          * AdministrativeStateException.
473          * 
474          * 4) Unlocks PDP and verifies that startTransaction results in
475          * StandbyStatusException.
476          * 
477          * 5) Promotes PDP and verifies that startTransaction is once again
478          * successful.
479          */
480         @Ignore
481         @Test
482         public void testLocking1() throws Exception {
483                                 
484                 PolicyLogger.debug("testLocking1: Reading IntegrityMonitorProperties");
485                 Properties integrityMonitorProperties = new Properties();
486                 integrityMonitorProperties.load(new FileInputStream(new File(
487                                 "src/test/server/config/IntegrityMonitor.properties")));
488                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
489                 String thisPdpId = IntegrityMonitorProperties
490                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
491
492                 PolicyLogger.debug("testLocking1: Reading xacmlPersistenceProperties");
493                 Properties xacmlPersistenceProperties = new Properties();
494                 xacmlPersistenceProperties.load(new FileInputStream(new File(
495                                 "src/test/server/config/xacmlPersistence.properties")));
496                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
497                 
498                 PolicyLogger.debug("testLocking1: Creating emfXacml");
499                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
500                                 "junitXacmlPU", xacmlPersistenceProperties);
501                 
502                 PolicyLogger.debug("testLocking1: Reading droolsPersistenceProperties");
503                 Properties droolsPersistenceProperties = new Properties();
504                 droolsPersistenceProperties.load(new FileInputStream(new File(
505                                 "src/test/server/config/droolsPersistence.properties")));
506                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
507
508                 PolicyLogger.debug("testLocking1: Creating emfDrools");
509                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
510                                 "junitDroolsPU", droolsPersistenceProperties);
511                 
512                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
513                 
514                 PolicyLogger.debug("testLocking1: Cleaning up tables");
515                 conn.deleteAllSessions();
516                 conn.deleteAllPdps();
517                 
518                 /*
519                  * Insert this PDP as designated.  Initial standby state will be 
520                  * either null or cold standby.   
521                  */
522                 PolicyLogger.debug("testLocking1: Inserting PDP=" + thisPdpId + " as designated");
523                 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
524                 conn.insertPdp(pdp);
525                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
526                 PolicyLogger.debug("testLocking1: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
527                                 + droolsPdpEntity.isDesignated());
528                 assertTrue(droolsPdpEntity.isDesignated() == true);
529                 
530                 PolicyLogger.debug("testLocking1: Instantiating stateManagement object");
531                 StateManagement sm = new StateManagement(emfXacml, "dummy");
532                 sm.deleteAllStateManagementEntities();
533                 sm = new StateManagement(emfXacml, thisPdpId);
534                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
535                 sm.addObserver(pmStandbyStateChangeNotifier);
536                                 
537                 PolicyLogger.debug("testLocking1: Running policy-management.Main class, designated="
538                                 + conn.getPdp(thisPdpId).isDesignated());
539                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
540                 policyManagementRunner.start();
541                 
542                 PolicyLogger.debug("testLocking1: Runner started; Sleeping "
543                                 + interruptRecoveryTime + "ms before promoting PDP="
544                                 + thisPdpId);
545                 Thread.sleep(interruptRecoveryTime);
546
547                 PolicyLogger.debug("testLocking1: Promoting PDP=" + thisPdpId);
548                 sm.promote();
549
550                 PolicyLogger.debug("testLocking1: Sleeping "
551                                 + sleepTime
552                                 + "ms, to allow time for policy-management.Main class to come up, designated="
553                                 + conn.getPdp(thisPdpId).isDesignated());
554                 Thread.sleep(sleepTime);
555                 
556                 PolicyLogger.debug("testLocking1: Waking up and invoking startTransaction on active PDP="
557                                 + thisPdpId
558                                 + ", designated="
559                                 + conn.getPdp(thisPdpId).isDesignated());
560                 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
561                                 .getInstance();
562                 try {
563                         droolsPdpIntegrityMonitor.startTransaction();
564                         droolsPdpIntegrityMonitor.endTransaction();
565                         PolicyLogger.debug("testLocking1: As expected, transaction successful");
566                 } catch (AdministrativeStateException e) {
567                         PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
568                         assertTrue(false);
569                 } catch (StandbyStatusException e) {
570                         PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
571                         assertTrue(false);
572                 } catch (Exception e) {
573                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
574                         assertTrue(false);
575                 }
576                 
577                 // demoting should cause state to transit to hotstandby, followed by re-promotion,
578                 // since there is only one PDP.
579                 PolicyLogger.debug("testLocking1: demoting PDP=" + thisPdpId);
580                 sm = droolsPdpIntegrityMonitor.getStateManager();
581                 sm.demote();
582                 
583                 PolicyLogger.debug("testLocking1: sleeping" + electionWaitSleepTime
584                                 + " to allow election handler to re-promote PDP=" + thisPdpId);
585                 Thread.sleep(electionWaitSleepTime);
586                                                                 
587                 PolicyLogger.debug("testLocking1: Invoking startTransaction on re-promoted PDP="
588                                 + thisPdpId
589                                 + ", designated="
590                                 + conn.getPdp(thisPdpId).isDesignated());
591                 try {
592                         droolsPdpIntegrityMonitor.startTransaction();
593                         droolsPdpIntegrityMonitor.endTransaction();
594                         PolicyLogger.debug("testLocking1: As expected, transaction successful");
595                 } catch (AdministrativeStateException e) {
596                         PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
597                         assertTrue(false);
598                 } catch (StandbyStatusException e) {
599                         PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
600                         assertTrue(false);
601                 } catch (Exception e) {
602                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
603                         assertTrue(false);
604                 }
605                 
606                 // locking should cause state to transit to cold standby
607                 PolicyLogger.debug("testLocking1: locking PDP=" + thisPdpId);
608                 sm.lock();
609                 
610                 // Just to avoid any race conditions, sleep a little after locking
611                 PolicyLogger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
612                 Thread.sleep(100);
613                 
614                 PolicyLogger.debug("testLocking1: Invoking startTransaction on locked PDP="
615                                 + thisPdpId
616                                 + ", designated="
617                                 + conn.getPdp(thisPdpId).isDesignated());
618                 try {
619                         droolsPdpIntegrityMonitor.startTransaction();
620                         PolicyLogger.error("testLocking1: startTransaction unexpectedly successful");
621                         assertTrue(false);
622                 } catch (AdministrativeStateException e) {
623                         PolicyLogger.debug("testLocking1: As expected, caught AdministrativeStateException, message=" + e.getMessage());
624                 } catch (StandbyStatusException e) {
625                         PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
626                         assertTrue(false);
627                 } catch (Exception e) {
628                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
629                         assertTrue(false);
630                 } finally {
631                         droolsPdpIntegrityMonitor.endTransaction();
632                 }               
633                 
634                 // unlocking should cause state to transit to hot standby
635                 PolicyLogger.debug("testLocking1: unlocking PDP=" + thisPdpId);
636                 sm.unlock();
637                 
638                 // Just to avoid any race conditions, sleep a little after locking
639                 PolicyLogger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
640                 Thread.sleep(100);
641                 
642                 PolicyLogger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
643                                 + thisPdpId
644                                 + ", designated="
645                                 + conn.getPdp(thisPdpId).isDesignated());
646                 try {
647                         droolsPdpIntegrityMonitor.startTransaction();
648                         PolicyLogger.error("testLocking1: startTransaction unexpectedly successful");
649                         assertTrue(false);
650                 } catch (AdministrativeStateException e) {
651                         PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
652                         assertTrue(false);
653                 } catch (StandbyStatusException e) {
654                         PolicyLogger.debug("testLocking1: As expected, caught StandbyStatusException, message=" + e.getMessage());
655                 } catch (Exception e) {
656                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
657                         assertTrue(false);
658                 } finally {
659                         droolsPdpIntegrityMonitor.endTransaction();
660                 }
661                 
662                 // promoting should cause state to transit to providing service
663                 PolicyLogger.debug("testLocking1: promoting PDP=" + thisPdpId);
664                 sm.promote();
665                 
666                 // Just to avoid any race conditions, sleep a little after promoting
667                 PolicyLogger.debug("testLocking1: Sleeping a few millis after promoting, to avoid race condition");
668                 Thread.sleep(100);
669                 
670                 PolicyLogger.debug("testLocking1: Invoking startTransaction on promoted PDP="
671                                 + thisPdpId
672                                 + ", designated="
673                                 + conn.getPdp(thisPdpId).isDesignated());
674                 try {
675                         droolsPdpIntegrityMonitor.startTransaction();
676                         droolsPdpIntegrityMonitor.endTransaction();
677                         PolicyLogger.debug("testLocking1: As expected, transaction successful");
678                 } catch (AdministrativeStateException e) {
679                         PolicyLogger.error("testLocking1: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
680                         assertTrue(false);
681                 } catch (StandbyStatusException e) {
682                         PolicyLogger.error("testLocking1: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
683                         assertTrue(false);
684                 } catch (Exception e) {
685                         PolicyLogger.error("testLocking1: Unexpectedly caught Exception, message=" + e.getMessage());
686                         assertTrue(false);
687                 }
688                 
689                 PolicyLogger.debug("testLocking1: Stopping policyManagementRunner");
690                 policyManagementRunner.stopRunner();            
691
692                 PolicyLogger.debug("\n\ntestLocking1: Exiting\n\n");
693                 Thread.sleep(interruptRecoveryTime);
694
695         }
696         
697         /*
698          * 1) Inserts and designates this PDP, then verifies that startTransaction
699          * is successful.
700          * 
701          * 2) Inserts another PDP in hotstandby.
702          * 
703          * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
704          * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
705          */
706         @Ignore
707         @Test
708         public void testLocking2() throws Exception {
709
710                 PolicyLogger.debug("\n\ntestLocking2: Entering\n\n");
711                 
712                 PolicyLogger.debug("testLocking2: Reading IntegrityMonitorProperties");
713                 Properties integrityMonitorProperties = new Properties();
714                 integrityMonitorProperties.load(new FileInputStream(new File(
715                                 "src/test/server/config/IntegrityMonitor.properties")));
716                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
717                 String thisPdpId = IntegrityMonitorProperties
718                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
719
720                 PolicyLogger.debug("testLocking2: Reading xacmlPersistenceProperties");
721                 Properties xacmlPersistenceProperties = new Properties();
722                 xacmlPersistenceProperties.load(new FileInputStream(new File(
723                                 "src/test/server/config/xacmlPersistence.properties")));
724                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
725                 
726                 PolicyLogger.debug("testLocking2: Creating emfXacml");
727                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
728                                 "junitXacmlPU", xacmlPersistenceProperties);
729                 
730                 PolicyLogger.debug("testLocking2: Reading droolsPersistenceProperties");
731                 Properties droolsPersistenceProperties = new Properties();
732                 droolsPersistenceProperties.load(new FileInputStream(new File(
733                                 "src/test/server/config/droolsPersistence.properties")));
734                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
735
736                 PolicyLogger.debug("testLocking2: Creating emfDrools");
737                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
738                                 "junitDroolsPU", droolsPersistenceProperties);
739                 
740                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
741                 
742                 PolicyLogger.debug("testLocking2: Cleaning up tables");
743                 conn.deleteAllSessions();
744                 conn.deleteAllPdps();
745                 
746                 /*
747                  * Insert this PDP as designated.  Initial standby state will be 
748                  * either null or cold standby.   Demoting should transit state to
749                  * hot standby.
750                  */
751                 PolicyLogger.debug("testLocking2: Inserting PDP=" + thisPdpId + " as designated");
752                 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
753                 conn.insertPdp(pdp);
754                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
755                 PolicyLogger.debug("testLocking2: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
756                                 + droolsPdpEntity.isDesignated());
757                 assertTrue(droolsPdpEntity.isDesignated() == true);
758                 
759                 PolicyLogger.debug("testLocking2: Instantiating stateManagement object and promoting PDP=" + thisPdpId);
760                 StateManagement sm = new StateManagement(emfXacml, "dummy");
761                 sm.deleteAllStateManagementEntities();
762                 sm = new StateManagement(emfXacml, thisPdpId);
763                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
764                 sm.addObserver(pmStandbyStateChangeNotifier);
765                                 
766                 /*
767                  * Insert another PDP as not designated.  Initial standby state will be 
768                  * either null or cold standby.   Demoting should transit state to
769                  * hot standby.
770                  */
771                 String standbyPdpId = "pdp2";
772                 PolicyLogger.debug("testLocking2: Inserting PDP=" + standbyPdpId + " as not designated");
773                 Date yesterday = DateUtils.addDays(new Date(), -1);
774                 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
775                 conn.insertPdp(pdp);
776                 droolsPdpEntity = conn.getPdp(standbyPdpId);
777                 PolicyLogger.debug("testLocking2: After insertion, PDP=" + standbyPdpId + " has DESIGNATED="
778                                 + droolsPdpEntity.isDesignated());
779                 assertTrue(droolsPdpEntity.isDesignated() == false);
780                 
781                 PolicyLogger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
782                 StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
783                 sm2.addObserver(pmStandbyStateChangeNotifier);
784                                 
785                 PolicyLogger.debug("testLocking2: Running policy-management.Main class");
786                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
787                 policyManagementRunner.start();
788                 
789                 PolicyLogger.debug("testLocking2: Runner started; Sleeping "
790                                 + interruptRecoveryTime + "ms before promoting/demoting");
791                 Thread.sleep(interruptRecoveryTime);
792
793                 PolicyLogger.debug("testLocking2: Promoting PDP=" + thisPdpId);
794                 sm.promote();
795
796                 // demoting PDP should ensure that state transits to hotstandby
797                 PolicyLogger.debug("testLocking2: Demoting PDP=" + standbyPdpId);
798                 sm2.demote();
799                 
800                 PolicyLogger.debug("testLocking2: Sleeping "
801                                 + sleepTime
802                                 + "ms, to allow time for policy-management.Main class to come up");
803                 Thread.sleep(sleepTime);
804                 
805                 PolicyLogger.debug("testLocking2: Waking up and invoking startTransaction on active PDP="
806                                 + thisPdpId
807                                 + ", designated="
808                                 + conn.getPdp(thisPdpId).isDesignated());
809                 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = (DroolsPDPIntegrityMonitor) IntegrityMonitor
810                                 .getInstance();
811                 try {
812                         droolsPdpIntegrityMonitor.startTransaction();
813                         droolsPdpIntegrityMonitor.endTransaction();
814                         PolicyLogger.debug("testLocking2: As expected, transaction successful");
815                 } catch (AdministrativeStateException e) {
816                         PolicyLogger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
817                         assertTrue(false);
818                 } catch (StandbyStatusException e) {
819                         PolicyLogger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
820                         assertTrue(false);
821                 } catch (Exception e) {
822                         PolicyLogger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
823                         assertTrue(false);
824                 }
825                 
826                 // demoting should cause state to transit to hotstandby followed by re-promotion.
827                 PolicyLogger.debug("testLocking2: demoting PDP=" + thisPdpId);
828                 sm = droolsPdpIntegrityMonitor.getStateManager();
829                 sm.demote();
830                 
831                 PolicyLogger.debug("testLocking2: sleeping" + electionWaitSleepTime
832                                 + " to allow election handler to re-promote PDP=" + thisPdpId);
833                 Thread.sleep(electionWaitSleepTime);
834                 
835                 PolicyLogger.debug("testLocking2: Waking up and invoking startTransaction on re-promoted PDP="
836                                 + thisPdpId + ", designated="
837                                 + conn.getPdp(thisPdpId).isDesignated());
838                 try {
839                         droolsPdpIntegrityMonitor.startTransaction();
840                         droolsPdpIntegrityMonitor.endTransaction();
841                         PolicyLogger.debug("testLocking2: As expected, transaction successful");
842                 } catch (AdministrativeStateException e) {
843                         PolicyLogger.error("testLocking2: Unexpectedly caught AdministrativeStateException, message=" + e.getMessage());
844                         assertTrue(false);
845                 } catch (StandbyStatusException e) {
846                         PolicyLogger.error("testLocking2: Unexpectedly caught StandbyStatusException, message=" + e.getMessage());
847                         assertTrue(false);
848                 } catch (Exception e) {
849                         PolicyLogger.error("testLocking2: Unexpectedly caught Exception, message=" + e.getMessage());
850                         assertTrue(false);
851                 }
852                 
853                 PolicyLogger.debug("testLocking2: Verifying designated status for PDP="
854                                 + standbyPdpId);
855                 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
856                 assertTrue(standbyPdpDesignated == false);
857                 
858                 PolicyLogger.debug("testLocking2: Stopping policyManagementRunner");
859                 policyManagementRunner.stopRunner();            
860
861                 PolicyLogger.debug("\n\ntestLocking2: Exiting\n\n");
862                 Thread.sleep(interruptRecoveryTime);
863
864         }
865         
866         private class PolicyManagementRunner extends Thread {
867
868                 public void run() {
869                         PolicyLogger.info("PolicyManagementRunner.run: Entering");
870                         String args[] = { "src/main/server/config" };
871                         try {
872                                 Main.main(args);
873                         } catch (Exception e) {
874                                 PolicyLogger
875                                                 .info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
876                                                                 + e.getMessage());
877                         }
878                         PolicyLogger.info("PolicyManagementRunner.run: Exiting");
879                 }
880                 
881                 public void stopRunner() {
882                         PolicyEngine.manager.shutdown();
883                 }
884
885         }
886         
887 }