Open Kilda Java Documentation
FloodlightTestCase.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 
32 package org.openkilda.floodlight;
33 
34 import static org.easymock.EasyMock.expect;
35 
36 import net.floodlightcontroller.core.SwitchDescription;
37 import org.easymock.EasyMock;
40 import org.junit.Before;
41 import net.floodlightcontroller.core.FloodlightContext;
42 import net.floodlightcontroller.core.IFloodlightProviderService;
43 import net.floodlightcontroller.core.IOFSwitch;
44 import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
45 import org.projectfloodlight.openflow.protocol.OFFactories;
46 import org.projectfloodlight.openflow.protocol.OFFactory;
47 import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
48 import org.projectfloodlight.openflow.protocol.OFMessage;
49 import org.projectfloodlight.openflow.protocol.OFPacketIn;
50 import org.projectfloodlight.openflow.protocol.OFPortDesc;
51 import org.projectfloodlight.openflow.protocol.OFType;
52 import org.projectfloodlight.openflow.protocol.OFVersion;
53 import org.projectfloodlight.openflow.types.DatapathId;
54 import org.projectfloodlight.openflow.types.MacAddress;
55 import org.projectfloodlight.openflow.types.OFPort;
56 import net.floodlightcontroller.packet.Ethernet;
57 import org.projectfloodlight.openflow.types.U64;
58 
59 import java.net.InetSocketAddress;
60 
67 public class FloodlightTestCase {
70  protected OFFeaturesReply swFeatures;
71  protected OFFactory factory = OFFactories.getFactory(OFVersion.OF_13);
72 
75  }
76 
78  return mockSwitchManager;
79  }
80 
82  this.mockFloodlightProvider = mockFloodlightProvider;
83  }
84 
85  public FloodlightContext parseAndAnnotate(OFMessage m) {
86  FloodlightContext bc = new FloodlightContext();
87  return parseAndAnnotate(bc, m);
88  }
89 
90  public static FloodlightContext parseAndAnnotate(FloodlightContext bc, OFMessage m) {
91  if (OFType.PACKET_IN.equals(m.getType())) {
92  OFPacketIn pi = (OFPacketIn)m;
93  Ethernet eth = new Ethernet();
94  eth.deserialize(pi.getData(), 0, pi.getData().length);
95  IFloodlightProviderService.bcStore.put(bc,
96  IFloodlightProviderService.CONTEXT_PI_PAYLOAD,
97  eth);
98  }
99  return bc;
100  }
101 
102  @Before
103  public void setUp() throws Exception {
106  swFeatures = factory.buildFeaturesReply().setNBuffers(1000).build();
107  }
108 
109  public static OFPortDesc createOFPortDesc(IOFSwitch sw, String name, int number) {
110  OFPortDesc portDesc = sw.getOFFactory().buildPortDesc()
111  .setHwAddr(MacAddress.NONE)
112  .setPortNo(OFPort.of(number))
113  .setName(name)
114  .build();
115  return portDesc;
116  }
117 
118  public IOFSwitch buildMockIOFSwitch(Long id, OFPortDesc portDesc, OFFactory factory,
119  OFDescStatsReply swDesc, InetSocketAddress inetAddr) {
120  IOFSwitch sw = EasyMock.createMock(IOFSwitch.class);
121  expect(sw.getId()).andReturn(DatapathId.of(id)).anyTimes();
122  expect(sw.getPort(OFPort.of(1))).andReturn(portDesc).anyTimes();
123  expect(sw.getOFFactory()).andReturn(factory).anyTimes();
124  expect(sw.getBuffers()).andReturn(swFeatures.getNBuffers()).anyTimes();
125  expect(sw.hasAttribute(IOFSwitch.PROP_SUPPORTS_OFPP_TABLE)).andReturn(true).anyTimes();
126  expect(sw.getSwitchDescription()).andReturn(new SwitchDescription(swDesc)).anyTimes();
127  expect(sw.isActive()).andReturn(true).anyTimes();
128  expect(sw.getLatency()).andReturn(U64.of(10L)).anyTimes();
129  expect(sw.getInetAddress()).andReturn(inetAddr).anyTimes();
130  return sw;
131  }
132 }
static OFPortDesc createOFPortDesc(IOFSwitch sw, String name, int number)
name
Definition: setup.py:24
static FloodlightContext parseAndAnnotate(FloodlightContext bc, OFMessage m)
FloodlightContext parseAndAnnotate(OFMessage m)
void setMockFloodlightProvider(MockFloodlightProvider mockFloodlightProvider)
IOFSwitch buildMockIOFSwitch(Long id, OFPortDesc portDesc, OFFactory factory, OFDescStatsReply swDesc, InetSocketAddress inetAddr)
net
Definition: plan-b.py:46