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 LOGGER = LoggerFactory.getLogger(ScenarioHandler.class);
29 private Deque<ClientEvent> scenario;
30 private BlockingQueue<byte[]> ofMsg;
31 private ChannelHandlerContext ctx;
32 private int eventNumber;
33 private boolean scenarioFinished = false;
39 public ScenarioHandler(Deque<ClientEvent> scenario) {
40 this.scenario = scenario;
41 ofMsg = new LinkedBlockingQueue<>();
46 int freezeCounter = 0;
47 while (!scenario.isEmpty()) {
48 LOGGER.debug("Running event #" + eventNumber);
49 ClientEvent peek = scenario.peekLast();
50 if (peek instanceof WaitForMessageEvent) {
51 LOGGER.debug("WaitForMessageEvent");
53 WaitForMessageEvent event = (WaitForMessageEvent) peek;
54 event.setHeaderReceived(ofMsg.poll(2000, TimeUnit.MILLISECONDS));
55 } catch (InterruptedException e) {
56 LOGGER.error(e.getMessage(), e);
59 } else if (peek instanceof SendEvent) {
60 LOGGER.debug("Proceed - sendevent");
61 SendEvent event = (SendEvent) peek;
64 if (peek.eventExecuted()) {
65 scenario.removeLast();
71 if (freezeCounter > 2) {
72 LOGGER.warn("Scenario freezed: " + freezeCounter);
77 } catch (InterruptedException e) {
78 LOGGER.error(e.getMessage(), e);
81 LOGGER.debug("Scenario finished");
83 scenarioFinished = true;
89 * @return true if scenario is done / empty
91 public boolean isEmpty() {
92 return scenario.isEmpty();
98 public Deque<ClientEvent> getScenario() {
103 * @param scenario scenario filled with desired events
105 public void setScenario(Deque<ClientEvent> scenario) {
106 this.scenario = scenario;
110 * @param ctx context which will be used for sending messages (SendEvents)
112 public void setCtx(ChannelHandlerContext ctx) {
117 * @param message received message that is compared to expected message
119 public void addOfMsg(byte[] message) {
124 * @return true is scenario is finished
126 public boolean isScenarioFinished() {
127 return scenarioFinished;