Open Kilda Java Documentation
PathVerificationPacketInTest.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 
25 
26 import net.floodlightcontroller.core.FloodlightContext;
27 import net.floodlightcontroller.core.IFloodlightProviderService;
28 import net.floodlightcontroller.core.IOFSwitch;
29 import net.floodlightcontroller.core.internal.IOFSwitchService;
30 import net.floodlightcontroller.core.module.FloodlightModuleContext;
31 import net.floodlightcontroller.packet.Data;
32 import net.floodlightcontroller.packet.Ethernet;
33 import net.floodlightcontroller.packet.IPacket;
34 import net.floodlightcontroller.packet.IPv4;
35 import net.floodlightcontroller.packet.LLDPTLV;
36 import net.floodlightcontroller.packet.UDP;
37 import org.apache.commons.codec.binary.Hex;
38 import org.easymock.EasyMock;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
43 import org.projectfloodlight.openflow.protocol.OFPacketIn;
44 import org.projectfloodlight.openflow.protocol.OFPacketInReason;
45 import org.projectfloodlight.openflow.protocol.OFPortDesc;
46 import org.projectfloodlight.openflow.protocol.OFVersion;
47 import org.projectfloodlight.openflow.protocol.match.MatchField;
48 import org.projectfloodlight.openflow.types.EthType;
49 import org.projectfloodlight.openflow.types.IpProtocol;
50 import org.projectfloodlight.openflow.types.MacAddress;
51 import org.projectfloodlight.openflow.types.OFPort;
52 import org.projectfloodlight.openflow.types.TransportPort;
53 
54 import java.net.InetSocketAddress;
55 
57 
58  protected FloodlightContext cntx;
59  protected OFDescStatsReply swDescription;
61  protected String sw1HwAddrTarget, sw2HwAddrTarget;
62  protected IOFSwitch sw1, sw2;
63  protected OFPacketIn pktIn;
64  protected InetSocketAddress srcIpTarget, dstIpTarget;
65  protected InetSocketAddress swIp = new InetSocketAddress("192.168.10.1", 200);
66 
67  private byte[] pkt = {(byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD, (byte) 0xEE, (byte) 0xFF, // src mac
68  0x11, 0x22, 0x33, 0x44, 0x55, 0x66, // dst mac
69  0x08, 0x00, // ether-type
70  // IP
71  0x45, 0x00, // ver,ihl, dscp, ecn
72  0x00, 0x30, // total length
73  0x00, 0x00, // tcp ident
74  0x00, 0x00, // flags, frag offset
75  0x00, // ttl
76  0x11, // protocol
77  0x38, (byte) 0x6d, // header checksum
78  (byte) 0xC0, (byte) 0xA8, 0x00, 0x01, // src ip
79  (byte) 0xC0, (byte) 0xA8, 0x00, (byte) 0xFF, // dst ip
80  // UDP
81  (byte) 0xEF, 0x2F, // src port
82  (byte) 0xEF, 0x2F, // dst port
83  0x00, 0x1c, // length
84  (byte) 0x8e, 0x7D, // checksum
85  // LLDP TLVs
86  0x02, 0x07, // type, len
87  0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // chassisid
88  0x04, 0x03, // type, len
89  0x02, 0x00, 0x01, // port id
90  0x06, 0x02, // type, len
91  0x00, 0x78, // ttl
92  0x00, 0x00
93  };
94 
95  protected IPacket getPacket() {
96  UDP udp = new UDP()
97  .setDestinationPort(
99  .setSourcePort(
101 
102  VerificationPacket verificationPacket = new VerificationPacket()
103  .setChassisId(new LLDPTLV().setType((byte) 1).setLength((short) 7)
104  .setValue(new byte[] {0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}))
105  .setPortId(new LLDPTLV().setType((byte) 2).setLength((short) 3)
106  .setValue(new byte[] {0x02, 0x00, 0x01}))
107  .setTtl(new LLDPTLV().setType((byte) 3).setLength((short) 2)
108  .setValue(new byte[] {0x00, 0x78}));
109 
110  udp.setPayload(new Data(verificationPacket.serialize()));
111 
112  IPv4 ip = new IPv4()
113  .setSourceAddress("192.168.0.1")
115  .setProtocol(IpProtocol.UDP);
116 
117  Ethernet eth = new Ethernet()
118  .setDestinationMACAddress("AA:BB:CC:DD:EE:FF")
119  .setSourceMACAddress("11:22:33:44:55:66")
120  .setEtherType(EthType.IPv4);
121 
122  eth.setPayload(ip);
123  ip.setPayload(udp);
124 
125  return eth;
126  }
127 
128  @Before
129  public void setUp() throws Exception {
130 
131  super.setUp();
132 
133  cntx = new FloodlightContext();
135  swFeatures = factory.buildFeaturesReply().setNBuffers(1000).build();
136  swDescription = factory.buildDescStatsReply().build();
137 
138  FloodlightModuleContext fmc = new FloodlightModuleContext();
139  fmc.addService(IFloodlightProviderService.class, mockFloodlightProvider);
140  fmc.addService(IOFSwitchService.class, getMockSwitchService());
141 
142 
143  OFPacketIn.Builder packetInBuilder = factory.buildPacketIn();
144  packetInBuilder
145  .setMatch(factory.buildMatch().setExact(MatchField.IN_PORT, OFPort.of(1)).build())
146  .setData(pkt)
147  .setReason(OFPacketInReason.NO_MATCH);
148  pktIn = packetInBuilder.build();
149  System.out.print(Hex.encodeHexString(pktIn.getData()));
150 
152 
153  fmc.addConfigParam(pvs, "isl_bandwidth_quotient", "0.0");
154  fmc.addConfigParam(pvs, "hmac256-secret", "secret");
155  fmc.addConfigParam(pvs, "bootstrap-servers", "");
157  KafkaTopicsConfig topicsConfig = provider.getConfiguration(KafkaTopicsConfig.class);
158  PathVerificationServiceConfig serviceConfig = provider.getConfiguration(PathVerificationServiceConfig.class);
159 
160  pvs.initConfiguration(topicsConfig, serviceConfig);
161  pvs.initServices(fmc);
162 
163  srcIpTarget = new InetSocketAddress("192.168.10.1", 200);
164  dstIpTarget = new InetSocketAddress("192.168.10.101", 100);
165  sw1HwAddrTarget = "11:22:33:44:55:66";
166  sw2HwAddrTarget = "AA:BB:CC:DD:EE:FF";
167 
168  OFPortDesc sw1Port1 = EasyMock.createMock(OFPortDesc.class);
169  expect(sw1Port1.getHwAddr()).andReturn(MacAddress.of(sw1HwAddrTarget)).anyTimes();
170  expect(sw1Port1.getVersion()).andReturn(OFVersion.OF_12).anyTimes();
171  expect(sw1Port1.getCurrSpeed()).andReturn(100000L).anyTimes();
172 
173  OFPortDesc sw2Port1 = EasyMock.createMock(OFPortDesc.class);
174  expect(sw2Port1.getHwAddr()).andReturn(MacAddress.of(sw2HwAddrTarget)).anyTimes();
175  expect(sw2Port1.getVersion()).andReturn(OFVersion.OF_12).anyTimes();
176  expect(sw2Port1.getCurrSpeed()).andReturn(100000L).anyTimes();
177  replay(sw1Port1);
178  replay(sw2Port1);
179 
181  sw2 = buildMockIOFSwitch(2L, sw2Port1, factory, swDescription, dstIpTarget);
182  replay(sw1);
183  replay(sw2);
184  }
185 
186  @After
187  public void tearDown() throws Exception {
188  }
189 
190  @Test
191  public void testDeserialize() {
192  Ethernet ethernet = (Ethernet) new Ethernet().deserialize(pkt, 0, pkt.length);
193  assertArrayEquals(pkt, ethernet.serialize());
194 
195  IPacket expected = getPacket();
196  assertArrayEquals(expected.serialize(), ethernet.serialize());
197  }
198 
199 
200 
201 
202 }
static ConfigurationProvider of(FloodlightModuleContext moduleContext, IFloodlightModule module)
IOFSwitch buildMockIOFSwitch(Long id, OFPortDesc portDesc, OFFactory factory, OFDescStatsReply swDesc, InetSocketAddress inetAddr)
def build()
Definition: plan-e.py:73
net
Definition: plan-b.py:46