2 * ============LICENSE_START=======================================================
3 * feature-active-standby-management
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.activestandby;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.util.Date;
31 import java.util.Properties;
32 import java.util.concurrent.Callable;
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.EntityTransaction;
36 import javax.persistence.Persistence;
37 import org.apache.commons.lang3.time.DateUtils;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.policy.common.im.IntegrityMonitor;
43 import org.onap.policy.common.im.IntegrityMonitorException;
44 import org.onap.policy.common.im.MonitorTime;
45 import org.onap.policy.common.im.StateManagement;
46 import org.onap.policy.common.utils.time.CurrentTime;
47 import org.onap.policy.common.utils.time.PseudoTimer;
48 import org.onap.policy.common.utils.time.TestTimeMulti;
49 import org.onap.policy.drools.core.PolicySessionFeatureApi;
50 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
51 import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants;
52 import org.powermock.reflect.Whitebox;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * Testing the allSeemsWell interface to verify that it correctly affects the
61 public class AllSeemsWellTest {
62 private static final Logger logger = LoggerFactory.getLogger(AllSeemsWellTest.class);
64 private static final String MONITOR_FIELD_NAME = "instance";
65 private static final String HANDLER_INSTANCE_FIELD = "electionHandler";
68 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
69 * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
70 * to ensure that we wait for the DesignationWaiter to do its job, before
71 * checking the results. Add a few seconds for safety
74 private static final int SLEEP_TIME_SEC = 10;
77 * DroolsPdpsElectionHandler runs every 1 seconds, so it takes 10 seconds for the
78 * checkWaitTimer() method to time out and call allSeemsWell which then requires
79 * the forward progress counter to go stale which should add an additional 5 sec.
82 private static final int STALLED_ELECTION_HANDLER_SLEEP_TIME_SEC = 15;
85 * As soon as the election hander successfully runs, it will resume the forward progress.
86 * If the election handler runs ever 1 sec and test transaction is run every 1 sec and
87 * then fpc is written every 1 sec and then the fpc is checked every 2 sec, that could
88 * take a total of 5 sec to recognize the resumption of progress. So, add 1 for safety.
90 private static final int RESUMED_ELECTION_HANDLER_SLEEP_TIME_SEC = 6;
92 private static EntityManagerFactory emfx;
93 private static EntityManagerFactory emfd;
94 private static EntityManager emx;
95 private static EntityManager emd;
96 private static EntityTransaction et;
98 private static final String CONFIG_DIR = "src/test/resources/asw";
100 private static CurrentTime saveTime;
101 private static Factory saveFactory;
103 private TestTimeMulti testTime;
106 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
112 * @throws Exception exception
115 public static void setUpClass() throws Exception {
117 String userDir = System.getProperty("user.dir");
118 logger.debug("setUpClass: userDir={}", userDir);
119 System.setProperty("com.sun.management.jmxremote.port", "9980");
120 System.setProperty("com.sun.management.jmxremote.authenticate","false");
122 DroolsPdpsElectionHandler.setIsUnitTesting(true);
124 saveTime = Whitebox.getInternalState(MonitorTime.class, MONITOR_FIELD_NAME);
125 saveFactory = Factory.getInstance();
127 resetInstanceObjects();
129 //Create the data access for xacml db
130 Properties stateManagementProperties = loadStateManagementProperties();
132 emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
134 // Create an entity manager to use the DB
135 emx = emfx.createEntityManager();
137 //Create the data access for drools db
138 Properties activeStandbyProperties = loadActiveStandbyProperties();
140 emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
142 // Create an entity manager to use the DB
143 emd = emfd.createEntityManager();
147 * Restores the system state.
149 * @throws IntegrityMonitorException if the integrity monitor cannot be shut down
152 public static void tearDownClass() throws IntegrityMonitorException {
153 resetInstanceObjects();
155 Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, saveTime);
156 Factory.setInstance(saveFactory);
158 DroolsPdpsElectionHandler.setIsUnitTesting(false);
170 * @throws Exception exception
173 public void setUp() throws Exception {
174 resetInstanceObjects();
177 testTime = new TestTimeMulti();
178 Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, testTime);
180 Factory factory = mock(Factory.class);
181 when(factory.makeTimer()).thenAnswer(ans -> new PseudoTimer(testTime));
182 Factory.setInstance(factory);
185 private static void resetInstanceObjects() throws IntegrityMonitorException {
186 IntegrityMonitor.setUnitTesting(true);
187 IntegrityMonitor.deleteInstance();
188 IntegrityMonitor.setUnitTesting(false);
190 Whitebox.setInternalState(ActiveStandbyFeature.class, HANDLER_INSTANCE_FIELD, (Object) null);
195 * Clean the xacml database.
197 public void cleanXacmlDb() {
198 et = emx.getTransaction();
201 // Make sure we leave the DB clean
202 emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
203 emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
204 emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
210 * Clean the drools database.
212 public void cleanDroolsDb() {
213 et = emd.getTransaction();
216 // Make sure we leave the DB clean
217 emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
223 // Tests hot standby when there is only one PDP.
227 public void testAllSeemsWell() throws Exception {
229 logger.debug("\n\ntestAllSeemsWell: Entering\n\n");
233 Properties stateManagementProperties = loadStateManagementProperties();
235 logger.debug("testAllSeemsWell: Creating emfXacml");
236 final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
237 "junitXacmlPU", stateManagementProperties);
239 Properties activeStandbyProperties = loadActiveStandbyProperties();
240 final String thisPdpId = activeStandbyProperties
241 .getProperty(ActiveStandbyProperties.NODE_NAME);
243 logger.debug("testAllSeemsWell: Creating emfDrools");
244 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
245 "junitDroolsPU", activeStandbyProperties);
247 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
249 logger.debug("testAllSeemsWell: Cleaning up tables");
250 conn.deleteAllPdps();
253 * Insert this PDP as not designated. Initial standby state will be
254 * either null or cold standby. Demoting should transit state to
258 logger.debug("testAllSeemsWell: Inserting PDP={} as not designated", thisPdpId);
259 Date yesterday = DateUtils.addDays(testTime.getDate(), -1);
260 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
262 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
263 logger.debug("testAllSeemsWell: After insertion, PDP={} has DESIGNATED={}",
264 thisPdpId, droolsPdpEntity.isDesignated());
265 assertTrue(droolsPdpEntity.isDesignated() == false);
267 logger.debug("testAllSeemsWell: Instantiating stateManagement object");
268 StateManagement sm = new StateManagement(emfXacml, "dummy");
269 sm.deleteAllStateManagementEntities();
272 // Now we want to create a StateManagementFeature and initialize it. It will be
273 // discovered by the ActiveStandbyFeature when the election handler initializes.
275 StateManagementFeatureApi stateManagementFeatureApi = null;
276 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
277 ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
278 stateManagementFeatureApi = feature;
279 logger.debug("testAllSeemsWell stateManagementFeature.getResourceName(): {}",
280 stateManagementFeatureApi.getResourceName());
283 assertNotNull(stateManagementFeatureApi);
285 final StateManagementFeatureApi smf = stateManagementFeatureApi;
287 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
288 // that has been created.
289 ActiveStandbyFeatureApi activeStandbyFeature = null;
290 for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
291 ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
292 activeStandbyFeature = feature;
293 logger.debug("testAllSeemsWell activeStandbyFeature.getResourceName(): {}",
294 activeStandbyFeature.getResourceName());
297 assertNotNull(activeStandbyFeature);
300 logger.debug("testAllSeemsWell: Demoting PDP={}", thisPdpId);
301 // demoting should cause state to transit to hotstandby
305 logger.debug("testAllSeemsWell: Sleeping {} s, to allow JpaDroolsPdpsConnector "
306 + "time to check droolspdpentity table", SLEEP_TIME_SEC);
307 waitForCondition(() -> conn.getPdp(thisPdpId).isDesignated(), SLEEP_TIME_SEC);
309 // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
311 droolsPdpEntity = conn.getPdp(thisPdpId);
312 logger.debug("testAllSeemsWell: After sm.demote() invoked, DESIGNATED= {} "
313 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
314 assertTrue(droolsPdpEntity.isDesignated() == true);
315 String standbyStatus = smf.getStandbyStatus(thisPdpId);
316 logger.debug("testAllSeemsWell: After demotion, PDP= {} "
317 + "has standbyStatus= {}", thisPdpId, standbyStatus);
318 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
320 //Now we want to stall the election handler and see the if AllSeemsWell will make the
321 //standbystatus = coldstandby
323 DroolsPdpsElectionHandler.setIsStalled(true);
325 logger.debug("testAllSeemsWell: Sleeping {} s, to allow checkWaitTimer to recognize "
326 + "the election handler has stalled and for the testTransaction to fail to "
327 + "increment forward progress and for the lack of forward progress to be recognized.",
328 STALLED_ELECTION_HANDLER_SLEEP_TIME_SEC);
331 //It takes 10x the update interval (1 sec) before the watcher will declare the election handler dead
332 //and that just stops forward progress counter. So, the fp monitor must then run to determine
333 // if the fpc has stalled. That will take about another 5 sec.
334 waitForCondition(() -> smf.getStandbyStatus().equals(StateManagement.COLD_STANDBY),
335 STALLED_ELECTION_HANDLER_SLEEP_TIME_SEC);
337 logger.debug("testAllSeemsWell: After isStalled=true, PDP= {} "
338 + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId));
340 assertTrue(smf.getStandbyStatus().equals(StateManagement.COLD_STANDBY));
342 //Now lets resume the election handler
343 DroolsPdpsElectionHandler.setIsStalled(false);
345 waitForCondition(() -> smf.getStandbyStatus().equals(StateManagement.PROVIDING_SERVICE),
346 RESUMED_ELECTION_HANDLER_SLEEP_TIME_SEC);
348 logger.debug("testAllSeemsWell: After isStalled=false, PDP= {} "
349 + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId));
351 assertTrue(smf.getStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
353 //resumedElectionHandlerSleepTime = 5000;
354 logger.debug("\n\ntestAllSeemsWell: Exiting\n\n");
358 private static Properties loadStateManagementProperties() throws IOException {
359 try (FileInputStream input = new FileInputStream(CONFIG_DIR + "/feature-state-management.properties")) {
360 Properties props = new Properties();
366 private static Properties loadActiveStandbyProperties() throws IOException {
367 try (FileInputStream input =
368 new FileInputStream(CONFIG_DIR + "/feature-active-standby-management.properties")) {
369 Properties props = new Properties();
375 private void waitForCondition(Callable<Boolean> testCondition, int timeoutInSeconds) throws InterruptedException {
376 testTime.waitUntil(testCondition);