Improved logging in ScenarioHandler
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / ScenarioHandler.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.clients;
3
4 import io.netty.channel.ChannelHandlerContext;
5
6 import java.util.Stack;
7 import java.util.concurrent.BlockingQueue;
8 import java.util.concurrent.LinkedBlockingQueue;
9 import java.util.concurrent.TimeUnit;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 /**
15  * 
16  * @author michal.polkorab
17  *
18  */
19 public class ScenarioHandler extends Thread {
20
21     private static final Logger LOGGER = LoggerFactory.getLogger(ScenarioHandler.class);
22     private Stack<ClientEvent> scenario;
23     private BlockingQueue<byte[]> ofMsg;
24     private ChannelHandlerContext ctx;
25     private int eventNumber;
26
27     /**
28      * 
29      * @param scenario
30      */
31     public ScenarioHandler(Stack<ClientEvent> scenario) {
32         this.scenario = scenario;
33         ofMsg = new LinkedBlockingQueue<>();
34     }
35
36     @Override
37     public void run() {
38         int freezeCounter = 0;
39         while (!scenario.isEmpty()) {
40             LOGGER.debug("Running event #" + eventNumber);
41             ClientEvent peek = scenario.peek();
42             if (peek instanceof WaitForMessageEvent) {
43                 LOGGER.debug("WaitForMessageEvent");
44                 try {
45                     WaitForMessageEvent event = (WaitForMessageEvent) peek;
46                     event.setHeaderReceived(ofMsg.poll(2000, TimeUnit.MILLISECONDS));
47                 } catch (InterruptedException e) {
48                     LOGGER.error(e.getMessage(), e);
49                     break;
50                 }
51             } else if (peek instanceof SendEvent) {
52                 LOGGER.debug("Proceed - sendevent");
53                 SendEvent event = (SendEvent) peek;
54                 event.setCtx(ctx);
55             }
56             if (peek.eventExecuted()) {
57                 scenario.pop();
58                 eventNumber++;
59                 freezeCounter = 0;
60             } else {
61                 freezeCounter++;
62             }
63             if (freezeCounter > 2) {
64                 LOGGER.warn("Scenario freezed: " + freezeCounter);
65                 break;
66             }
67             try {
68                 sleep(100);
69             } catch (InterruptedException e) {
70                 LOGGER.error(e.getMessage(), e);
71             }
72         }
73         LOGGER.info("Scenario finished");
74         synchronized (this) {
75             this.notify();
76         }
77     }
78     
79     /**
80      * @return true if scenario is done / empty
81      */
82     public boolean isEmpty() {
83         return scenario.isEmpty();
84     }
85
86     /**
87      * @return scenario
88      */
89     public Stack<ClientEvent> getScenario() {
90         return scenario;
91     }
92
93     /**
94      * @param scenario scenario filled with desired events
95      */
96     public void setScenario(Stack<ClientEvent> scenario) {
97         this.scenario = scenario;
98     }
99
100     /**
101      * @param ctx context which will be used for sending messages (SendEvents)
102      */
103     public void setCtx(ChannelHandlerContext ctx) {
104         this.ctx = ctx;
105     }
106
107     /**
108      * @param message received message that is compared to expected message
109      */
110     public void addOfMsg(byte[] message) {
111         ofMsg.add(message);
112     }
113 }