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;
13 import java.util.Deque;
14 import java.util.concurrent.BlockingQueue;
15 import java.util.concurrent.LinkedBlockingQueue;
16 import java.util.concurrent.TimeUnit;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
23 * @author michal.polkorab
26 public class ScenarioHandler extends Thread {
28 private static final Logger LOG = LoggerFactory.getLogger(ScenarioHandler.class);
29 private Deque<ClientEvent> scenario;
30 private final BlockingQueue<byte[]> ofMsg;
31 private ChannelHandlerContext ctx;
32 private int eventNumber;
33 private boolean scenarioFinished = false;
34 private int freeze = 2;
35 private long sleepBetweenTries = 100L;
36 private boolean finishedOK = true;
40 * @param scenario {@link Deque}
42 public ScenarioHandler(Deque<ClientEvent> scenario) {
43 this.scenario = scenario;
44 ofMsg = new LinkedBlockingQueue<>();
47 public ScenarioHandler(Deque<ClientEvent> scenario, int freeze, long sleepBetweenTries){
48 this.scenario = scenario;
49 ofMsg = new LinkedBlockingQueue<>();
50 this.sleepBetweenTries = sleepBetweenTries;
56 int freezeCounter = 0;
57 while (!scenario.isEmpty()) {
58 LOG.debug("Running event #{}", eventNumber);
59 ClientEvent peek = scenario.peekLast();
60 if (peek instanceof WaitForMessageEvent) {
61 LOG.debug("WaitForMessageEvent");
63 WaitForMessageEvent event = (WaitForMessageEvent) peek;
64 event.setHeaderReceived(ofMsg.poll(2000, TimeUnit.MILLISECONDS));
65 } catch (InterruptedException e) {
66 LOG.error(e.getMessage(), e);
69 } else if (peek instanceof SendEvent) {
70 LOG.debug("Proceed - sendevent");
71 SendEvent event = (SendEvent) peek;
74 if (peek.eventExecuted()) {
75 LOG.info("Scenario step finished OK, moving to next step.");
76 scenario.removeLast();
83 if (freezeCounter > freeze) {
84 LOG.warn("Scenario frozen: {}", freezeCounter);
85 LOG.warn("Scenario step not finished NOT OK!", freezeCounter);
86 this.finishedOK = false;
90 sleep(sleepBetweenTries);
91 } catch (InterruptedException e) {
92 LOG.error(e.getMessage(), e);
95 LOG.debug("Scenario finished");
97 scenarioFinished = true;
103 * @return true if scenario is done / empty
105 public boolean isEmpty() {
106 return scenario.isEmpty();
112 public Deque<ClientEvent> getScenario() {
117 * @param scenario scenario filled with desired events
119 public void setScenario(Deque<ClientEvent> scenario) {
120 this.scenario = scenario;
124 * @param ctx context which will be used for sending messages (SendEvents)
126 public void setCtx(ChannelHandlerContext ctx) {
131 * @param message received message that is compared to expected message
133 public void addOfMsg(byte[] message) {
138 * @return true is scenario is finished
140 public boolean isScenarioFinished() {
141 return scenarioFinished;
144 public boolean isFinishedOK() {