BUG-2661:Sonar issue
[openflowplugin.git] / openflowplugin-it / src / test / java / org / opendaylight / openflowplugin / openflow / md / it / ScenarioFactory.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.openflow.md.it;
9
10 import java.util.ArrayDeque;
11 import java.util.Deque;
12
13 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
14 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
15 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
16 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18
19 /**
20  * provisioning of most common scenarios used by testing of integration between OFLibrary, OFPlugin and MD-SAL
21  */
22 public abstract class ScenarioFactory {
23
24     /** default sleep time [ms] - at scenario end */
25     public static final int DEFAULT_SCENARIO_SLEEP = 2000;
26     /** version bitmap hex-string containing version 1.3 */
27     public static final String VERSION_BITMAP_13 = "00 01 00 08 00 00 00 10";
28     /** version bitmap hex-string containing versions: 1.0 + 1.3 */
29     public static final String VERSION_BITMAP_10_13 = "00 01 00 08 00 00 00 12";
30
31     /**
32      * Creates stack with handshake needed messages.
33      * <ol> XID of messages:
34      *   <li> hello sent - 00000001
35      *   <li> hello waiting - 00000015
36      *   <li> featuresrequest waiting - 00000002
37      *   <li> featuresreply sent - 00000002
38      * </ol>
39      * @param switchVersionBitmap
40      * @param auxId
41      * @param pluginVersionBitmap
42      * @param addSleep if true - then add final sleep {@link #DEFAULT_SCENARIO_SLEEP} 
43      * @return stack filled with Handshake messages
44      */
45     public static Deque<ClientEvent> createHandshakeScenarioVBM(
46             String switchVersionBitmap, short auxId, String pluginVersionBitmap, boolean addSleep) {
47         Deque<ClientEvent> stack = new ArrayDeque<>();
48
49         stack.addFirst(new SendEvent(ByteBufUtils
50                 .hexStringToBytes("04 00 00 10 00 00 00 01 "
51                         + switchVersionBitmap)));
52         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
53                 .hexStringToBytes("04 00 00 10 00 00 00 15 "
54                         + pluginVersionBitmap)));
55         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
56                 .hexStringToBytes("04 05 00 08 00 00 00 02")));
57         stack.addFirst(new SendEvent(ByteBufUtils
58                 .hexStringToBytes("04 06 00 20 00 00 00 02 "
59                         + "00 01 02 03 04 05 06 07 " + "00 01 02 03 01 "
60                         + Integer.toHexString(auxId)
61                         + " 00 00 00 01 02 03 00 01 02 03")));
62         
63         if (addSleep) {
64             addSleep(stack);
65         }
66         
67         return stack;
68     }
69     
70     /**
71      * @param stack
72      * @param addSleep if true - then add final sleep {@link #DEFAULT_SCENARIO_SLEEP}
73      * @return
74      */
75     public static Deque<ClientEvent> appendPostHandshakeScenario(Deque<ClientEvent> stack, boolean addSleep) {
76         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
77                 .hexStringToBytes("04 12 00 10 00 00 00 01 "+
78                                   "00 0d 00 00 00 00 00 00"  )));
79         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
80                 .hexStringToBytes("04 12 00 10 00 00 00 02 "+
81                                   "00 08 00 00 00 00 00 00"  )));
82         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
83                 .hexStringToBytes("04 12 00 10 00 00 00 03 "+
84                                   "00 0b 00 00 00 00 00 00"  )));
85         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
86                 .hexStringToBytes("04 12 00 10 00 00 00 04 "+
87                                   "00 00 00 00 00 00 00 00"  )));
88         
89         if (addSleep) {
90             addSleep(stack);
91         }
92         
93         return stack;
94     }
95     
96     /**
97      * @param stack
98      */
99     private static void addSleep(Deque<ClientEvent> stack) {
100         stack.addFirst(new SleepEvent(DEFAULT_SCENARIO_SLEEP));
101     }
102
103     /**
104      * @param auxId
105      * @param pluginVersionBitmap
106      * @return handshake scenario without switch version bitmap
107      */
108     public static Deque<ClientEvent> createHandshakeScenario(short auxId,
109             String pluginVersionBitmap) {
110         Deque<ClientEvent> stack = new ArrayDeque<>();
111
112         stack.addFirst(new SendEvent(ByteBufUtils
113                 .hexStringToBytes("04 00 00 08 00 00 00 01")));
114         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
115                 .hexStringToBytes("04 00 00 10 00 00 00 15 "
116                         + pluginVersionBitmap)));
117         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
118                 .hexStringToBytes("04 05 00 08 00 00 00 02")));
119         stack.addFirst(new SendEvent(ByteBufUtils
120                 .hexStringToBytes("04 06 00 20 00 00 00 02 "
121                         + "00 01 02 03 04 05 06 07 " + "00 01 02 03 01 "
122                         + Integer.toHexString(auxId)
123                         + " 00 00 00 01 02 03 00 01 02 03")));
124         addSleep(stack);
125         return stack;
126     }
127
128     /**
129      * Attempt to simulate the MLX running 1.0 talking to ODL
130      *
131      * @param auxId
132      * @param pluginVersionBitmap
133      * @return handshake scenario without switch version bitmap
134      */
135     public static Deque<ClientEvent> createHandshakeScenarioNoVBM_OF10_TwoHello() {
136         Deque<ClientEvent> stack = new ArrayDeque<>();
137
138         stack.addFirst(new SendEvent(ByteBufUtils
139                 .hexStringToBytes("01 00 00 08 00 00 01 67")));
140         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
141                         .hexStringToBytes("04 00 00 10 00 00 00 15 00 01 00 08 00 00 00 12")));
142         stack.addFirst(new SendEvent(ByteBufUtils
143                 .hexStringToBytes("01 01 00 0c 00 00 00 15 00 00 00 00")));
144         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
145                 .hexStringToBytes("01 00 00 08 00 00 01 68")));
146         stack.addFirst(new SendEvent(ByteBufUtils
147                 .hexStringToBytes("01 00 00 08 00 00 01 68")));
148         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
149                 .hexStringToBytes("01 05 00 08 00 00 01 69")));
150
151         stack.addFirst(new SendEvent(
152                 ByteBufUtils
153                         .hexStringToBytes("01 06 00 80 00 00 01 69 cc 4e 24 1c 4a 00 00 00"
154                                 + " 00 00 01 00 01 00 00 00 00 00 00 07 00 00 01 0f"
155                                 + " 00 61 cc 4e 24 1c 4a 60 65 74 68 33 2f 31 00 00"
156                                 + " 00 00 00 00 00 00 00 00 00 00 00 11 00 00 00 01"
157                                 + " 00 00 01 40 00 00 01 40 00 00 01 40 00 00 01 40"
158                                 + " 00 62 cc 4e 24 1c 4a 61 65 74 68 33 2f 32 00 00"
159                                 + " 00 00 00 00 00 00 00 00 00 00 00 11 00 00 00 01"
160                                 + " 00 00 01 40 00 00 01 40 00 00 01 40 00 00 01 40")));
161
162         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
163                 .hexStringToBytes("01 10 00 0c 00 00 00 01 00 00 00 00")));
164
165         stack.addFirst(new SendEvent(
166                 ByteBufUtils
167                         .hexStringToBytes("01 11 04 2c 00 00 00 01 00 00 00 00 42 72 6f 63"
168                                 + " 61 64 65 20 43 6f 6d 6d 75 6e 69 63 61 74 69"
169                                 + " 6f 6e 73 2c 20 49 6e 63 00 00 00 00 00 00 00"
170                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
171                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
172                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
173                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
174                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
175                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
176                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
177                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
178                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
179                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
180                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
181                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
182                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
183                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
184                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 4d 75 6c"
185                                 + " 74 69 2d 53 65 72 76 69 63 65 20 49 72 6f 6e"
186                                 + " 77 61 72 65 00 00 00 00 00 00 00 00 00 00 00"
187                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
188                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
189                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
190                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
191                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
192                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
193                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
194                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
195                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
196                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
197                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
198                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
199                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
200                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
201                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 4e 49"
202                                 + " 20 35 2e 37 00 00 00 00 00 00 00 00 00 00 00"
203                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
204                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
205                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
206                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
207                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
208                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
209                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
210                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
211                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
212                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
213                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
214                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
215                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
216                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
217                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
218                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 4e"
219                                 + " 6f 6e 65 00 00 00 00 00 00 00 00 00 00 00 00"
220                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
221                                 + " 00 4e 6f 6e 65 00 00 00 00 00 00 00 00 00 00"
222                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
223                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
224                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
225                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
226                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
227                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
228                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
229                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
230                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
231                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
232                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
233                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
234                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
235                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
236                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
237                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
238                                 + " 00 00")));
239
240         stack.addFirst(new SleepEvent(5000));
241
242         addSleep(stack);
243         return stack;
244     }
245
246     /**
247      * Attempt to simulate the MLX running 1.0 talking to ODL
248      *
249      * @param auxId
250      * @param pluginVersionBitmap
251      * @return handshake scenario without switch version bitmap
252      */
253     public static Deque<ClientEvent> createHandshakeScenarioNOVBM_OF10_OneHello() {
254         System.out.println("createHandshakeScenarioMininet");
255         Deque<ClientEvent> stack = new ArrayDeque<>();
256
257         stack.addFirst(new SendEvent(ByteBufUtils
258                 .hexStringToBytes("01 00 00 08 00 00 00 0d")));
259         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
260                 .hexStringToBytes("04 00 00 10 00 00 00 15")));
261         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
262                 .hexStringToBytes("01 00 00 08 00 00 00 0e")));
263         stack.addFirst(new WaitForMessageEvent(ByteBufUtils
264                 .hexStringToBytes("01 05 00 08 00 00 00 0f")));
265
266         stack.addFirst(new SendEvent(
267                 ByteBufUtils
268                         .hexStringToBytes("01 01 00 14 00 00 00 0e 00 01 00 01 01 00 00 08 00 00 00 0e")));
269
270         stack.addFirst(new SendEvent(
271                 ByteBufUtils
272                         .hexStringToBytes(" 01 06 00 b0 00 00 00 0f 00 00 00 00 00 00 00 01 00 00 01 00 fe 00"
273                                 + " 00 00 00 00 00 c7 00 00 0f ff 00 01 fa 01 ff 57 86 aa 73 31 2d"
274                                 + " 65 74 68 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01"
275                                 + " 00 00 00 c0 00 00 00 00 00 00 00 00 00 00 00 00 00 02 c2 51 d8"
276                                 + " 24 38 97 73 31 2d 65 74 68 32 00 00 00 00 00 00 00 00 00 00 00"
277                                 + " 00 00 00 00 00 00 00 00 00 c0 00 00 00 00 00 00 00 00 00 00 00"
278                                 + " 00 ff fe ea bd f8 db 41 40 73 31 00 00 00 00 00 00 00 00 00 00"
279                                 + " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
280                                 + " 00 00 00 00 00 00 00")));
281         addSleep(stack);
282         return stack;
283     }
284 }