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