Open Kilda Java Documentation
PathVerificationPacketOutTest.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.floodlight.pathverification;
17 
18 import static org.easymock.EasyMock.expect;
19 import static org.easymock.EasyMock.replay;
20 import static org.junit.Assert.assertArrayEquals;
21 
22 import net.floodlightcontroller.core.FloodlightContext;
23 import net.floodlightcontroller.core.IFloodlightProviderService;
24 import net.floodlightcontroller.core.IOFSwitch;
25 import net.floodlightcontroller.core.internal.IOFSwitchService;
26 import net.floodlightcontroller.core.module.FloodlightModuleContext;
27 import org.apache.commons.codec.binary.Hex;
28 import org.easymock.EasyMock;
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
35 import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
36 import org.projectfloodlight.openflow.protocol.OFPacketOut;
37 import org.projectfloodlight.openflow.protocol.OFPortDesc;
38 import org.projectfloodlight.openflow.types.MacAddress;
39 import org.projectfloodlight.openflow.types.OFPort;
40 
41 import java.net.InetSocketAddress;
42 import java.util.Arrays;
43 
45  protected FloodlightContext cntx;
46  protected OFDescStatsReply swDescription;
48  protected InetSocketAddress srcIpTarget, dstIpTarget;
49  protected String sw1HwAddrTarget, sw2HwAddrTarget;
50  protected IOFSwitch sw1, sw2;
51 
52  @BeforeClass
53  public static void setUpBeforeClass() throws Exception {
54  }
55 
56  @AfterClass
57  public static void tearDownAfterClass() throws Exception {
58  }
59 
60  @Before
61  public void setUp() throws Exception {
62  super.setUp();
63  cntx = new FloodlightContext();
64  FloodlightModuleContext fmc = new FloodlightModuleContext();
65  fmc.addService(IFloodlightProviderService.class, mockFloodlightProvider);
66  fmc.addService(IOFSwitchService.class, getMockSwitchService());
67  swDescription = factory.buildDescStatsReply().build();
69 
70  pvs.initAlgorithm("secret");
71 
72  srcIpTarget = new InetSocketAddress("192.168.10.1", 200);
73  dstIpTarget = new InetSocketAddress("192.168.10.101", 100);
74  sw1HwAddrTarget = "11:22:33:44:55:66";
75  sw2HwAddrTarget = "AA:BB:CC:DD:EE:FF";
76 
77  OFPortDesc sw1Port1 = EasyMock.createMock(OFPortDesc.class);
78  expect(sw1Port1.getHwAddr()).andReturn(MacAddress.of(sw1HwAddrTarget)).anyTimes();
79  OFPortDesc sw2Port1 = EasyMock.createMock(OFPortDesc.class);
80  expect(sw2Port1.getHwAddr()).andReturn(MacAddress.of(sw2HwAddrTarget)).anyTimes();
81  replay(sw1Port1);
82  replay(sw2Port1);
83 
85  sw2 = buildMockIOFSwitch(2L, sw2Port1, factory, swDescription, dstIpTarget);
86  replay(sw1);
87  replay(sw2);
88  }
89 
90  @After
91  public void tearDown() throws Exception {
92  }
93 
94  @SuppressWarnings("static-access")
95  @Test
96  public void testBcastPacket() {
97  // This is Broadcast so set dstIpTarget to the broadcast IP
98  InetSocketAddress dstIpTarget = new InetSocketAddress(pvs.VERIFICATION_PACKET_IP_DST, 200);
99 
100  // Generate the VerificationPacket
101  OFPacketOut packet = pvs.generateVerificationPacket(sw1, OFPort.of(1));
102  System.out.println("packet: " + Hex.encodeHexString(packet.getData()));
103 
104  // Source MAC will always be that of sw1 for both Unicast and Broadcast
105  byte[] srcMac = Arrays.copyOfRange(packet.getData(), 6, 12);
106  assertArrayEquals(MacAddress.of(sw1HwAddrTarget).getBytes(), srcMac);
107 
108  // Destination MAC should be that of BROADCAST for Broadcast Packet
109  byte[] dstMac = Arrays.copyOfRange(packet.getData(), 0, 6);
110  assertArrayEquals(MacAddress.of(pvs.VERIFICATION_BCAST_PACKET_DST).getBytes(), dstMac);
111 
112  // Source IP is actual switch1 IP
113  byte[] srcIpActual = Arrays.copyOfRange(packet.getData(), 26, 30);
114  assertArrayEquals(srcIpTarget.getAddress().getAddress(), srcIpActual);
115 
116  // Destination IP is that of DESTINATION BROADCAST IP
117  byte[] dstIpActual = Arrays.copyOfRange(packet.getData(), 30, 34);
118  assertArrayEquals(dstIpTarget.getAddress().getAddress(), dstIpActual);
119  }
120 
121  @Test
122  public void testUncastPacket() {
123  // Generate the VerificationPacket
124  OFPacketOut packet = pvs.generateVerificationPacket(sw1, OFPort.of(1), sw2, true);
125 
126  // Source MAC will always be that of sw1 for both Unicast and Broadcast
127  byte[] srcMacActual = Arrays.copyOfRange(packet.getData(), 6, 12);
128  assertArrayEquals(MacAddress.of(sw1HwAddrTarget).getBytes(), srcMacActual);
129 
130  // Destination MAC should be that of sw2 for Unicast Packet
131  byte[] dstMacActual = Arrays.copyOfRange(packet.getData(), 0, 6);
132  assertArrayEquals(MacAddress.of(sw2HwAddrTarget).getBytes(), dstMacActual);
133 
134  // Source and Destination IP's are the respective switch IP's
135  byte[] srcIpActual = Arrays.copyOfRange(packet.getData(), 26, 30);
136  assertArrayEquals(srcIpTarget.getAddress().getAddress(), srcIpActual);
137  byte[] dstIpActual = Arrays.copyOfRange(packet.getData(), 30, 34);
138  assertArrayEquals(dstIpTarget.getAddress().getAddress(), dstIpActual);
139  }
140 }
IOFSwitch buildMockIOFSwitch(Long id, OFPortDesc portDesc, OFFactory factory, OFDescStatsReply swDesc, InetSocketAddress inetAddr)
net
Definition: plan-b.py:46