Open Kilda Java Documentation
AbstractSerializerTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.messaging.command.flow;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
21 
54 
55 import org.junit.Ignore;
56 import org.junit.Test;
57 
58 import java.io.IOException;
59 import java.util.Arrays;
60 import java.util.Collections;
61 import java.util.HashSet;
62 import java.util.List;
63 import java.util.UUID;
64 
65 @Ignore
66 public abstract class AbstractSerializerTest implements AbstractSerializer {
67  private static final String FLOW_NAME = "test_flow";
68  private static final SwitchId SWITCH_ID = new SwitchId("00:00:00:00:00:00:00:00");
69  private static final String CORRELATION_ID = UUID.randomUUID().toString();
70  private static final long TIMESTAMP = System.currentTimeMillis();
71  private static final int INPUT_PORT = 1;
72  private static final int OUTPUT_PORT = 2;
73  private static final int INPUT_VLAN_ID = 101;
74  private static final int OUTPUT_VLAN_ID = 102;
75  private static final int TRANSIT_VLAN_ID = 103;
76  private static final long BANDWIDTH = 10000L;
77  private static final long COOKIE = 0x1L;
78  private static final long METER_ID = 0L;
79  private static final OutputVlanType OUTPUT_VLAN_TYPE = OutputVlanType.REPLACE;
80  private static final FlowState FLOW_STATUS = FlowState.UP;
81  private static final PortChangeType PORT_CHANGE = PortChangeType.OTHER_UPDATE;
82  private static final SwitchState SWITCH_EVENT = SwitchState.CHANGED;
83  private static final Destination DESTINATION = null;
84 
85  private static final FlowIdStatusPayload flowIdStatusResponse = new FlowIdStatusPayload(FLOW_NAME, FLOW_STATUS);
86 
87  private static final String requester = "requester-id";
88  private static final SwitchInfoData sw1 = new SwitchInfoData(new SwitchId("ff:01"),
89  SwitchState.ACTIVATED, "1.1.1.1", "ff:01", "switch-1", "kilda");
90  private static final SwitchInfoData sw2 = new SwitchInfoData(new SwitchId("ff:02"),
91  SwitchState.ACTIVATED, "2.2.2.2", "ff:02", "switch-2", "kilda");
92  private static final List<PathNode> nodes = Arrays.asList(
93  new PathNode(new SwitchId("ff:01"), 1, 0, 0L),
94  new PathNode(new SwitchId("ff:02"), 2, 1, 0L));
95  private static final IslInfoData isl = new IslInfoData(0L, nodes, 1000L, IslChangeType.DISCOVERED, 900L);
96  private static final PathInfoData path = new PathInfoData(0L, nodes);
97  private static final Flow flowModel = new Flow(FLOW_NAME, 1000, false, COOKIE, FLOW_NAME, String.valueOf(TIMESTAMP),
98  new SwitchId("ff:01"), new SwitchId("ff:02"), 10, 20, 100, 200, 1, 1024, path, FLOW_STATUS);
99 
100  @Test
101  public void serializeInstallEgressFlowMessageTest() throws IOException, ClassNotFoundException {
102  InstallEgressFlow data = new InstallEgressFlow(TIMESTAMP, FLOW_NAME, COOKIE,
103  SWITCH_ID, INPUT_PORT, OUTPUT_PORT, TRANSIT_VLAN_ID, OUTPUT_VLAN_ID, OUTPUT_VLAN_TYPE);
104  System.out.println(data);
105 
106  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
108 
109  Message message = (Message) deserialize();
110  assertTrue(message instanceof CommandMessage);
111 
112  CommandMessage resultCommand = (CommandMessage) message;
113  assertTrue(resultCommand.getData() instanceof InstallEgressFlow);
114 
115  InstallEgressFlow resultData = (InstallEgressFlow) resultCommand.getData();
116  System.out.println(resultData);
117  assertEquals(data, resultData);
118  assertEquals(data.hashCode(), resultData.hashCode());
119  }
120 
121  @Test
122  public void serializeInstallIngressFlowMessageTest() throws IOException, ClassNotFoundException {
123  InstallIngressFlow data = new InstallIngressFlow(TIMESTAMP, FLOW_NAME, COOKIE, SWITCH_ID,
124  INPUT_PORT, OUTPUT_PORT, INPUT_VLAN_ID, TRANSIT_VLAN_ID, OUTPUT_VLAN_TYPE, BANDWIDTH, METER_ID);
125  System.out.println(data);
126 
127  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
129 
130  Message message = (Message) deserialize();
131  assertTrue(message instanceof CommandMessage);
132 
133  CommandMessage resultCommand = (CommandMessage) message;
134  assertTrue(resultCommand.getData() instanceof InstallIngressFlow);
135 
136  InstallIngressFlow resultData = (InstallIngressFlow) resultCommand.getData();
137  System.out.println(resultData);
138  assertEquals(data, resultData);
139  assertEquals(data.hashCode(), resultData.hashCode());
140  }
141 
142  @Test
143  public void serializeInstallTransitFlowMessageTest() throws IOException, ClassNotFoundException {
144  InstallTransitFlow data = new InstallTransitFlow(TIMESTAMP, FLOW_NAME, COOKIE,
145  SWITCH_ID, INPUT_PORT, OUTPUT_PORT, TRANSIT_VLAN_ID);
146  System.out.println(data);
147 
148  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
150 
151  Message message = (Message) deserialize();
152  assertTrue(message instanceof CommandMessage);
153 
154  CommandMessage resultCommand = (CommandMessage) message;
155  assertTrue(resultCommand.getData() instanceof InstallTransitFlow);
156 
157  InstallTransitFlow resultData = (InstallTransitFlow) resultCommand.getData();
158  System.out.println(resultData);
159  assertEquals(data, resultData);
160  assertEquals(data.hashCode(), resultData.hashCode());
161  }
162 
163  @Test
164  public void serializeInstallOneSwitchFlowMessageTest() throws IOException, ClassNotFoundException {
165  InstallOneSwitchFlow data = new InstallOneSwitchFlow(TIMESTAMP, FLOW_NAME, COOKIE, SWITCH_ID, INPUT_PORT,
166  OUTPUT_PORT, INPUT_VLAN_ID, OUTPUT_VLAN_ID, OUTPUT_VLAN_TYPE, BANDWIDTH, METER_ID);
167  System.out.println(data);
168 
169  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
171 
172  Message message = (Message) deserialize();
173  assertTrue(message instanceof CommandMessage);
174 
175  CommandMessage resultCommand = (CommandMessage) message;
176  assertTrue(resultCommand.getData() instanceof InstallOneSwitchFlow);
177 
178  InstallOneSwitchFlow resultData = (InstallOneSwitchFlow) resultCommand.getData();
179  System.out.println(resultData);
180  assertEquals(data, resultData);
181  assertEquals(data.hashCode(), resultData.hashCode());
182  }
183 
184  @Test
185  public void flowCreateRequestTest() throws IOException, ClassNotFoundException {
186  FlowCreateRequest data = new FlowCreateRequest(flowModel);
187  System.out.println(data);
188 
189  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
191 
192  Message message = (Message) deserialize();
193  assertTrue(message instanceof CommandMessage);
194 
195  CommandMessage resultCommand = (CommandMessage) message;
196  assertTrue(resultCommand.getData() instanceof FlowCreateRequest);
197 
198  FlowCreateRequest resultData = (FlowCreateRequest) resultCommand.getData();
199  System.out.println(resultData);
200  assertEquals(data, resultData);
201  assertEquals(data.hashCode(), resultData.hashCode());
202  assertEquals(flowModel.hashCode(), resultData.getPayload().hashCode());
203  }
204 
205  @Test
206  public void flowUpdateRequestTest() throws IOException, ClassNotFoundException {
207  FlowUpdateRequest data = new FlowUpdateRequest(flowModel);
208  System.out.println(data);
209 
210  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
212 
213  Message message = (Message) deserialize();
214  assertTrue(message instanceof CommandMessage);
215 
216  CommandMessage resultCommand = (CommandMessage) message;
217  assertTrue(resultCommand.getData() instanceof FlowUpdateRequest);
218 
219  FlowUpdateRequest resultData = (FlowUpdateRequest) resultCommand.getData();
220  System.out.println(resultData);
221  assertEquals(data, resultData);
222  assertEquals(data.hashCode(), resultData.hashCode());
223  assertEquals(flowModel.hashCode(), resultData.getPayload().hashCode());
224  }
225 
226  @Test
227  public void flowDeleteRequestTest() throws IOException, ClassNotFoundException {
228  Flow deleteFlow = new Flow();
229  deleteFlow.setFlowId(flowName);
230  FlowDeleteRequest data = new FlowDeleteRequest(deleteFlow);
231  System.out.println(data);
232 
233  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
235 
236  Message message = (Message) deserialize();
237  assertTrue(message instanceof CommandMessage);
238 
239  CommandMessage resultCommand = (CommandMessage) message;
240  assertTrue(resultCommand.getData() instanceof FlowDeleteRequest);
241 
242  FlowDeleteRequest resultData = (FlowDeleteRequest) resultCommand.getData();
243  System.out.println(resultData);
244  assertEquals(data, resultData);
245  assertEquals(data.hashCode(), resultData.hashCode());
246  assertEquals(deleteFlow.hashCode(), resultData.getPayload().hashCode());
247  }
248 
249  @Test
250  public void flowGetBidirectionalRequestTest() throws IOException, ClassNotFoundException {
251  FlowReadRequest data = new FlowReadRequest(FLOW_NAME);
252  System.out.println(data);
253 
254  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
256 
257  Message message = (Message) deserialize();
258  assertTrue(message instanceof CommandMessage);
259 
260  CommandMessage resultCommand = (CommandMessage) message;
261  assertTrue(resultCommand.getData() instanceof FlowReadRequest);
262 
263  FlowReadRequest resultData = (FlowReadRequest) resultCommand.getData();
264  System.out.println(resultData);
265  assertEquals(data, resultData);
266  assertEquals(data.hashCode(), resultData.hashCode());
267  assertEquals(FLOW_NAME, resultData.getFlowId());
268  }
269 
270  @Test
271  public void flowGetBidirectionalResponseTest() throws IOException, ClassNotFoundException {
272  Flow flow = Flow.builder().flowPath(path).build();
273  BidirectionalFlow bidirectionalFlow = BidirectionalFlow.builder().forward(flow).reverse(flow).build();
274  FlowReadResponse data = new FlowReadResponse(bidirectionalFlow);
275  System.out.println(data);
276 
277  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
278  serialize(info);
279 
280  Message message = (Message) deserialize();
281  assertTrue(message instanceof InfoMessage);
282 
283  InfoMessage resultInfo = (InfoMessage) message;
284  assertTrue(resultInfo.getData() instanceof FlowReadResponse);
285 
286  FlowReadResponse resultData = (FlowReadResponse) resultInfo.getData();
287  System.out.println(resultData);
288  assertEquals(data, resultData);
289  assertEquals(data.hashCode(), resultData.hashCode());
290  assertEquals(path, resultData.getPayload().getForward().getFlowPath());
291  assertEquals(path, resultData.getPayload().getReverse().getFlowPath());
292  }
293 
294  @Test
295  public void flowRerouteResponseTest() throws IOException, ClassNotFoundException {
296  FlowRerouteResponse data = new FlowRerouteResponse(path, true);
297  System.out.println(data);
298 
299  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
300  serialize(info);
301 
302  Message message = (Message) deserialize();
303  assertTrue(message instanceof InfoMessage);
304 
305  InfoMessage resultInfo = (InfoMessage) message;
306  assertTrue(resultInfo.getData() instanceof FlowRerouteResponse);
307 
308  FlowRerouteResponse resultData = (FlowRerouteResponse) resultInfo.getData();
309  System.out.println(resultData);
310  assertEquals(data, resultData);
311  assertEquals(data.hashCode(), resultData.hashCode());
312  assertEquals(path.hashCode(), resultData.getPayload().hashCode());
313  }
314 
315  @Test
316  public void flowStatusResponseTest() throws IOException, ClassNotFoundException {
317  FlowStatusResponse data = new FlowStatusResponse(flowIdStatusResponse);
318  System.out.println(data);
319 
320  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
321  serialize(info);
322 
323  Message message = (Message) deserialize();
324  assertTrue(message instanceof InfoMessage);
325 
326  InfoMessage resultInfo = (InfoMessage) message;
327  assertTrue(resultInfo.getData() instanceof FlowStatusResponse);
328 
329  FlowStatusResponse resultData = (FlowStatusResponse) resultInfo.getData();
330  System.out.println(resultData);
331  assertEquals(data, resultData);
332  assertEquals(data.hashCode(), resultData.hashCode());
333  assertEquals(flowIdStatusResponse.hashCode(), resultData.getPayload().hashCode());
334  }
335 
336  @Test
337  public void flowResponseTest() throws IOException, ClassNotFoundException {
338  FlowResponse data = new FlowResponse(flowModel);
339  System.out.println(data);
340 
341  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
342  serialize(info);
343 
344  Message message = (Message) deserialize();
345  assertTrue(message instanceof InfoMessage);
346 
347  InfoMessage resultInfo = (InfoMessage) message;
348  assertTrue(resultInfo.getData() instanceof FlowResponse);
349 
350  FlowResponse resultData = (FlowResponse) resultInfo.getData();
351  System.out.println(resultData);
352  assertEquals(data, resultData);
353  assertEquals(data.hashCode(), resultData.hashCode());
354  assertEquals(flowModel.hashCode(), resultData.getPayload().hashCode());
355  }
356 
357  @Test
358  public void flowsResponseTest() throws IOException, ClassNotFoundException {
359  FlowsResponse data = new FlowsResponse(Collections.singletonList(flowModel.getFlowId()));
360  System.out.println(data);
361 
362  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
363  serialize(info);
364 
365  Message message = (Message) deserialize();
366  assertTrue(message instanceof InfoMessage);
367 
368  InfoMessage resultInfo = (InfoMessage) message;
369  assertTrue(resultInfo.getData() instanceof FlowsResponse);
370 
371  FlowsResponse resultData = (FlowsResponse) resultInfo.getData();
372  System.out.println(resultData);
373  assertEquals(data, resultData);
374  assertEquals(data.hashCode(), resultData.hashCode());
375  assertEquals(Collections.singletonList(flowModel.getFlowId()).hashCode(), resultData.getFlowIds().hashCode());
376  }
377 
378  @Test
379  public void eventIslInfoTest() throws IOException, ClassNotFoundException {
380  PathNode payload = new PathNode(SWITCH_ID, INPUT_PORT, 0);
381  IslInfoData data = new IslInfoData(0L, Collections.singletonList(payload),
382  1000000L, IslChangeType.DISCOVERED, 900000L);
383  assertEquals(SWITCH_ID + "_" + String.valueOf(INPUT_PORT), data.getId());
384  System.out.println(data);
385 
386  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
387  serialize(info);
388 
389  Message message = (Message) deserialize();
390  assertTrue(message instanceof InfoMessage);
391 
392  InfoMessage resultInfo = (InfoMessage) message;
393  assertTrue(resultInfo.getData() instanceof IslInfoData);
394 
395  IslInfoData resultData = (IslInfoData) resultInfo.getData();
396  System.out.println(resultData);
397  assertEquals(data, resultData);
398  assertEquals(data.hashCode(), resultData.hashCode());
399  assertEquals(payload.hashCode(), resultData.getPath().get(0).hashCode());
400  }
401 
402  @Test
403  public void eventPathInfoTest() throws IOException, ClassNotFoundException {
405  System.out.println(data);
406 
407  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
408  serialize(info);
409 
410  Message message = (Message) deserialize();
411  assertTrue(message instanceof InfoMessage);
412 
413  InfoMessage resultInfo = (InfoMessage) message;
414  assertTrue(resultInfo.getData() instanceof PathInfoData);
415 
416  PathInfoData resultData = (PathInfoData) resultInfo.getData();
417  System.out.println(resultData);
418  assertEquals(data, resultData);
419  assertEquals(data.hashCode(), resultData.hashCode());
420  }
421 
422  @Test
423  public void eventPortInfoTest() throws IOException, ClassNotFoundException {
424  PortInfoData data = new PortInfoData(SWITCH_ID, INPUT_PORT, 0, PORT_CHANGE);
425  System.out.println(data);
426 
427  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
428  serialize(info);
429 
430  Message message = (Message) deserialize();
431  assertTrue(message instanceof InfoMessage);
432 
433  InfoMessage resultInfo = (InfoMessage) message;
434  assertTrue(resultInfo.getData() instanceof PortInfoData);
435 
436  PortInfoData resultData = (PortInfoData) resultInfo.getData();
437  System.out.println(resultData);
438  assertEquals(data, resultData);
439  assertEquals(data.hashCode(), resultData.hashCode());
440  }
441 
442  @Test
443  public void eventSwitchInfoTest() throws IOException, ClassNotFoundException {
444  SwitchInfoData data = new SwitchInfoData(SWITCH_ID, SWITCH_EVENT, "127.0.0.1", "localhost", "sw", "controller");
445  System.out.println(data);
446 
447  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
448  serialize(info);
449 
450  Message message = (Message) deserialize();
451  assertTrue(message instanceof InfoMessage);
452 
453  InfoMessage resultInfo = (InfoMessage) message;
454  assertTrue(resultInfo.getData() instanceof SwitchInfoData);
455 
456  SwitchInfoData resultData = (SwitchInfoData) resultInfo.getData();
457  System.out.println(resultData);
458  assertEquals(data, resultData);
459  assertEquals(data.hashCode(), resultData.hashCode());
460  }
461 
462  @Test
463  public void errorMessageTest() throws IOException, ClassNotFoundException {
464  ErrorData data = new ErrorData(ErrorType.AUTH_FAILED, FLOW_NAME, "Bad credentials");
465  System.out.println(data);
466 
467  ErrorMessage info = new ErrorMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
468  info.setData(data);
469  serialize(info);
470 
471  Message message = (Message) deserialize();
472  assertTrue(message instanceof ErrorMessage);
473 
474  ErrorMessage resultInfo = (ErrorMessage) message;
475  assertTrue(resultInfo.getData() != null);
476 
477  ErrorData resultData = resultInfo.getData();
478  System.out.println(resultData);
479  assertEquals(data, resultData);
480  assertEquals(data.hashCode(), resultData.hashCode());
481  }
482 
483  @Test
484  public void removeCommandTest() throws IOException, ClassNotFoundException {
485  RemoveFlow data = new RemoveFlow(TIMESTAMP, FLOW_NAME, COOKIE, SWITCH_ID, METER_ID,
486  DeleteRulesCriteria.builder().cookie(COOKIE).build());
487  System.out.println(data);
488 
489  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
490  command.setData(data);
492 
493  Message message = (Message) deserialize();
494  assertTrue(message instanceof CommandMessage);
495 
496  CommandMessage resultCommand = (CommandMessage) message;
497  assertTrue(resultCommand.getData() != null);
498 
499  RemoveFlow resultData = (RemoveFlow) resultCommand.getData();
500  System.out.println(resultData);
501  assertEquals(data, resultData);
502  assertEquals(data.hashCode(), resultData.hashCode());
503  }
504 
505  @Test
506  public void flowRerouteCommandTest() throws IOException, ClassNotFoundException {
508  System.out.println(data);
509 
510  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
511  command.setData(data);
513 
514  Message message = (Message) deserialize();
515  assertTrue(message instanceof CommandMessage);
516 
517  CommandMessage resultCommand = (CommandMessage) message;
518  assertTrue(resultCommand.getData() != null);
519 
520  FlowRerouteRequest resultData = (FlowRerouteRequest) resultCommand.getData();
521  System.out.println(resultData);
522  assertEquals(data, resultData);
523  assertEquals(data.hashCode(), resultData.hashCode());
524  }
525 
526  @Test
527  public void dumpNetworkCommandTest() throws IOException, ClassNotFoundException {
529  System.out.println(data);
530 
531  CommandMessage command = new CommandMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
532  command.setData(data);
534 
535  Message message = (Message) deserialize();
536  assertTrue(message instanceof CommandMessage);
537 
538  CommandMessage resultCommand = (CommandMessage) message;
539  assertTrue(resultCommand.getData() != null);
540 
541  HealthCheckCommandData resultData = (HealthCheckCommandData) resultCommand.getData();
542  System.out.println(resultData);
543  assertEquals(data, resultData);
544  assertEquals(data.hashCode(), resultData.hashCode());
545  }
546 
547  @Test
548  public void dumpNetworkResponseTest() throws IOException, ClassNotFoundException {
549  NetworkInfoData data = new NetworkInfoData(requester,
550  new HashSet<>(Arrays.asList(sw1, sw2)),
551  new HashSet<>(),
552  Collections.singleton(isl),
553  Collections.singleton(new ImmutablePair<>(flowModel, flowModel)));
554  System.out.println(data);
555 
556  InfoMessage info = new InfoMessage(data, System.currentTimeMillis(), CORRELATION_ID, DESTINATION);
557  info.setData(data);
558  serialize(info);
559 
560  Message message = (Message) deserialize();
561  assertTrue(message instanceof InfoMessage);
562 
563  InfoMessage resultInfo = (InfoMessage) message;
564  assertTrue(resultInfo.getData() != null);
565 
566  NetworkInfoData resultData = (NetworkInfoData) resultInfo.getData();
567  System.out.println(resultData);
568  assertEquals(data, resultData);
569  assertEquals(data.hashCode(), resultData.hashCode());
570  }
571 }
Definition: nodes.py:1
def command(payload, fields)
Definition: share.py:102