Merge "Bug 4957 No empty transaction for every connection fix"
[openflowplugin.git] / openflowplugin-it / src / test / java / org / opendaylight / openflowplugin / openflow / md / it / SimulatorAssistant.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.openflow.md.it;
9
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.ThreadPoolExecutor;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.TimeoutException;
14
15 import org.junit.Assert;
16 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * 
22  */
23 public abstract class SimulatorAssistant {
24     
25     private static final Logger LOG = LoggerFactory
26             .getLogger(SimulatorAssistant.class);
27
28     /**
29      * @param switchSim 
30      * @throws InterruptedException
31      */
32     public static void waitForSwitchSimulatorOn(SimpleClient switchSim) {
33         try {
34             switchSim.getIsOnlineFuture().get(6, TimeUnit.SECONDS); // intentionally ignoring future inner value
35         } catch (TimeoutException | ExecutionException | InterruptedException e) {
36             LOG.error("failed to start switch simulator: {}", e.getMessage(), e);
37             Assert.fail("failed to start switch simulator");
38         }
39     }
40
41     /**
42      * @param switchSim 
43      * @param scenarioPool 
44      * @param failsafeTimeout 
45      */
46     public static void tearDownSwitchSimulatorAfterScenario(SimpleClient switchSim, ThreadPoolExecutor scenarioPool, long failsafeTimeout) {
47         try {
48             LOG.debug("tearing down simulator");
49             switchSim.getScenarioDone().get(failsafeTimeout, TimeUnit.MILLISECONDS);
50         } catch (Exception e) {
51             String msg = "waiting for scenario to finish failed: "+e.getMessage();
52             LOG.error(msg, e);
53             //FIXME: Enable the assert.
54             //Assert.fail(msg);
55         } finally {
56             scenarioPool.shutdownNow();
57             scenarioPool.purge();
58         }
59     
60         try {
61             LOG.debug("checking if simulator succeeded to connect to controller");
62             boolean simulatorWasOnline = switchSim.getIsOnlineFuture().get(100, TimeUnit.MILLISECONDS);
63             Assert.assertTrue("simulator failed to connect to controller", simulatorWasOnline);
64         } catch (Exception e) {
65             String message = "simulator probably failed to connect to controller";
66             LOG.error(message, e);
67             Assert.fail(message);
68         }
69     }
70
71 }