Remove trailing whitespace
[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     private boolean scenarioFinished = false;
34
35     /**
36      *
37      * @param scenario
38      */
39     public ScenarioHandler(Stack<ClientEvent> scenario) {
40         this.scenario = scenario;
41         ofMsg = new LinkedBlockingQueue<>();
42     }
43
44     @Override
45     public void run() {
46         int freezeCounter = 0;
47         while (!scenario.isEmpty()) {
48             LOGGER.debug("Running event #" + eventNumber);
49             ClientEvent peek = scenario.peek();
50             if (peek instanceof WaitForMessageEvent) {
51                 LOGGER.debug("WaitForMessageEvent");
52                 try {
53                     WaitForMessageEvent event = (WaitForMessageEvent) peek;
54                     event.setHeaderReceived(ofMsg.poll(2000, TimeUnit.MILLISECONDS));
55                 } catch (InterruptedException e) {
56                     LOGGER.error(e.getMessage(), e);
57                     break;
58                 }
59             } else if (peek instanceof SendEvent) {
60                 LOGGER.debug("Proceed - sendevent");
61                 SendEvent event = (SendEvent) peek;
62                 event.setCtx(ctx);
63             }
64             if (peek.eventExecuted()) {
65                 scenario.pop();
66                 eventNumber++;
67                 freezeCounter = 0;
68             } else {
69                 freezeCounter++;
70             }
71             if (freezeCounter > 2) {
72                 LOGGER.warn("Scenario freezed: " + freezeCounter);
73                 break;
74             }
75             try {
76                 sleep(100);
77             } catch (InterruptedException e) {
78                 LOGGER.error(e.getMessage(), e);
79             }
80         }
81         LOGGER.debug("Scenario finished");
82         synchronized (this) {
83             scenarioFinished = true;
84             this.notify();
85         }
86     }
87
88     /**
89      * @return true if scenario is done / empty
90      */
91     public boolean isEmpty() {
92         return scenario.isEmpty();
93     }
94
95     /**
96      * @return scenario
97      */
98     public Stack<ClientEvent> getScenario() {
99         return scenario;
100     }
101
102     /**
103      * @param scenario scenario filled with desired events
104      */
105     public void setScenario(Stack<ClientEvent> scenario) {
106         this.scenario = scenario;
107     }
108
109     /**
110      * @param ctx context which will be used for sending messages (SendEvents)
111      */
112     public void setCtx(ChannelHandlerContext ctx) {
113         this.ctx = ctx;
114     }
115
116     /**
117      * @param message received message that is compared to expected message
118      */
119     public void addOfMsg(byte[] message) {
120         ofMsg.add(message);
121     }
122
123     /**
124      * @return true is scenario is finished
125      */
126     public boolean isScenarioFinished() {
127         return scenarioFinished;
128     }
129 }