2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.clients;
11 import io.netty.channel.ChannelHandlerContext;
12 import java.util.Deque;
13 import java.util.concurrent.BlockingQueue;
14 import java.util.concurrent.LinkedBlockingQueue;
15 import java.util.concurrent.TimeUnit;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Scenario handler thread.
22 * @author michal.polkorab
24 public class ScenarioHandler extends Thread {
26 private static final Logger LOG = LoggerFactory.getLogger(ScenarioHandler.class);
27 private Deque<ClientEvent> scenario;
28 private final BlockingQueue<byte[]> ofMsg;
29 private ChannelHandlerContext ctx;
30 private int eventNumber;
31 private volatile boolean scenarioFinished = false;
32 private int freeze = 2;
33 private long sleepBetweenTries = 100L;
34 private boolean finishedOK = true;
39 * @param scenario {@link Deque}
41 public ScenarioHandler(Deque<ClientEvent> scenario) {
42 this.scenario = scenario;
43 ofMsg = new LinkedBlockingQueue<>();
46 public ScenarioHandler(Deque<ClientEvent> scenario, int freeze, long sleepBetweenTries) {
47 this.scenario = scenario;
48 ofMsg = new LinkedBlockingQueue<>();
49 this.sleepBetweenTries = sleepBetweenTries;
55 int freezeCounter = 0;
56 while (!scenario.isEmpty()) {
57 LOG.debug("Running event #{}", eventNumber);
58 ClientEvent peek = scenario.peekLast();
59 if (peek instanceof WaitForMessageEvent) {
60 LOG.debug("WaitForMessageEvent");
62 WaitForMessageEvent event = (WaitForMessageEvent) peek;
63 event.setHeaderReceived(ofMsg.poll(2000, TimeUnit.MILLISECONDS));
64 } catch (InterruptedException e) {
65 LOG.error("Error {}", e);
68 } else if (peek instanceof SendEvent) {
69 LOG.debug("Proceed - sendevent");
70 SendEvent event = (SendEvent) peek;
73 if (peek.eventExecuted()) {
74 LOG.info("Scenario step finished OK, moving to next step.");
75 scenario.removeLast();
82 if (freezeCounter > freeze) {
83 LOG.warn("Scenario frozen: {}", freezeCounter);
84 LOG.warn("Scenario step not finished NOT OK! {}", freezeCounter);
85 this.finishedOK = false;
89 sleep(sleepBetweenTries);
90 } catch (InterruptedException e) {
91 LOG.error("Error {}", e);
94 LOG.debug("Scenario finished");
96 scenarioFinished = true;
102 * Returns true if scenario is done / empty.
104 public boolean isEmpty() {
105 return scenario.isEmpty();
109 * Returns the scenario.
111 public Deque<ClientEvent> getScenario() {
118 * @param scenario scenario filled with desired events
120 public void setScenario(Deque<ClientEvent> scenario) {
121 this.scenario = scenario;
125 * Sets the ChannelHandlerContext.
127 * @param ctx context which will be used for sending messages (SendEvents)
129 public void setCtx(ChannelHandlerContext ctx) {
136 * @param message received message that is compared to expected message
138 public void addOfMsg(byte[] message) {
143 * Returns true is scenario is finished.
145 public boolean isScenarioFinished() {
146 return scenarioFinished;
149 public boolean isFinishedOK() {