Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package net.floodlightcontroller.hub; |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
| 23 | import java.util.Collections; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import net.floodlightcontroller.core.FloodlightContext; |
| 27 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 28 | import net.floodlightcontroller.core.IOFMessageListener; |
| 29 | import net.floodlightcontroller.core.IOFSwitch; |
| 30 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 31 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 32 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 33 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 34 | |
| 35 | import org.openflow.protocol.OFMessage; |
| 36 | import org.openflow.protocol.OFPacketIn; |
| 37 | import org.openflow.protocol.OFPacketOut; |
| 38 | import org.openflow.protocol.OFPort; |
| 39 | import org.openflow.protocol.OFType; |
| 40 | import org.openflow.protocol.action.OFAction; |
| 41 | import org.openflow.protocol.action.OFActionOutput; |
| 42 | import org.openflow.util.U16; |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
| 46 | /** |
| 47 | * |
| 48 | * @author David Erickson (daviderickson@cs.stanford.edu) - 04/04/10 |
| 49 | */ |
| 50 | public class Hub implements IFloodlightModule, IOFMessageListener { |
| 51 | protected static Logger log = LoggerFactory.getLogger(Hub.class); |
| 52 | |
| 53 | protected IFloodlightProviderService floodlightProvider; |
| 54 | |
| 55 | /** |
| 56 | * @param floodlightProvider the floodlightProvider to set |
| 57 | */ |
| 58 | public void setFloodlightProvider(IFloodlightProviderService floodlightProvider) { |
| 59 | this.floodlightProvider = floodlightProvider; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String getName() { |
| 64 | return Hub.class.getPackage().getName(); |
| 65 | } |
| 66 | |
| 67 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 68 | OFPacketIn pi = (OFPacketIn) msg; |
| 69 | OFPacketOut po = (OFPacketOut) floodlightProvider.getOFMessageFactory() |
| 70 | .getMessage(OFType.PACKET_OUT); |
| 71 | po.setBufferId(pi.getBufferId()) |
| 72 | .setInPort(pi.getInPort()); |
| 73 | |
| 74 | // set actions |
| 75 | OFActionOutput action = new OFActionOutput() |
| 76 | .setPort((short) OFPort.OFPP_FLOOD.getValue()); |
| 77 | po.setActions(Collections.singletonList((OFAction)action)); |
| 78 | po.setActionsLength((short) OFActionOutput.MINIMUM_LENGTH); |
| 79 | |
| 80 | // set data if is is included in the packetin |
| 81 | if (pi.getBufferId() == 0xffffffff) { |
| 82 | byte[] packetData = pi.getPacketData(); |
| 83 | po.setLength(U16.t(OFPacketOut.MINIMUM_LENGTH |
| 84 | + po.getActionsLength() + packetData.length)); |
| 85 | po.setPacketData(packetData); |
| 86 | } else { |
| 87 | po.setLength(U16.t(OFPacketOut.MINIMUM_LENGTH |
| 88 | + po.getActionsLength())); |
| 89 | } |
| 90 | try { |
| 91 | sw.write(po, cntx); |
| 92 | } catch (IOException e) { |
| 93 | log.error("Failure writing PacketOut", e); |
| 94 | } |
| 95 | |
| 96 | return Command.CONTINUE; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // IFloodlightModule |
| 110 | |
| 111 | @Override |
| 112 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 113 | // We don't provide any services, return null |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 119 | getServiceImpls() { |
| 120 | // We don't provide any services, return null |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Collection<Class<? extends IFloodlightService>> |
| 126 | getModuleDependencies() { |
| 127 | Collection<Class<? extends IFloodlightService>> l = |
| 128 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 129 | l.add(IFloodlightProviderService.class); |
| 130 | return l; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void init(FloodlightModuleContext context) |
| 135 | throws FloodlightModuleException { |
| 136 | floodlightProvider = |
| 137 | context.getServiceImpl(IFloodlightProviderService.class); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void startUp(FloodlightModuleContext context) { |
| 142 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
| 143 | } |
| 144 | } |