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.Stack;
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 LOGGER = LoggerFactory.getLogger(ScenarioHandler.class);
29 private Stack<ClientEvent> scenario;
30 private BlockingQueue<byte[]> ofMsg;
31 private ChannelHandlerContext ctx;
32 private int eventNumber;
38 public ScenarioHandler(Stack<ClientEvent> scenario) {
39 this.scenario = scenario;
40 ofMsg = new LinkedBlockingQueue<>();
45 int freezeCounter = 0;
46 while (!scenario.isEmpty()) {
47 LOGGER.debug("Running event #" + eventNumber);
48 ClientEvent peek = scenario.peek();
49 if (peek instanceof WaitForMessageEvent) {
50 LOGGER.debug("WaitForMessageEvent");
52 WaitForMessageEvent event = (WaitForMessageEvent) peek;
53 event.setHeaderReceived(ofMsg.poll(2000, TimeUnit.MILLISECONDS));
54 } catch (InterruptedException e) {
55 LOGGER.error(e.getMessage(), e);
58 } else if (peek instanceof SendEvent) {
59 LOGGER.debug("Proceed - sendevent");
60 SendEvent event = (SendEvent) peek;
63 if (peek.eventExecuted()) {
70 if (freezeCounter > 2) {
71 LOGGER.warn("Scenario freezed: " + freezeCounter);
76 } catch (InterruptedException e) {
77 LOGGER.error(e.getMessage(), e);
80 LOGGER.info("Scenario finished");
87 * @return true if scenario is done / empty
89 public boolean isEmpty() {
90 return scenario.isEmpty();
96 public Stack<ClientEvent> getScenario() {
101 * @param scenario scenario filled with desired events
103 public void setScenario(Stack<ClientEvent> scenario) {
104 this.scenario = scenario;
108 * @param ctx context which will be used for sending messages (SendEvents)
110 public void setCtx(ChannelHandlerContext ctx) {
115 * @param message received message that is compared to expected message
117 public void addOfMsg(byte[] message) {