Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.packetservice; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | import java.util.concurrent.CopyOnWriteArrayList; |
| 9 | |
| 10 | import net.floodlightcontroller.core.FloodlightContext; |
| 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 12 | import net.floodlightcontroller.core.IOFMessageListener; |
| 13 | import net.floodlightcontroller.core.IOFSwitch; |
| 14 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 15 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 17 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 18 | import net.onrc.onos.api.packet.IPacketListener; |
| 19 | import net.onrc.onos.api.packet.IPacketService; |
| 20 | import net.onrc.onos.core.packet.Ethernet; |
| 21 | import net.onrc.onos.core.topology.INetworkGraphService; |
| 22 | import net.onrc.onos.core.topology.NetworkGraph; |
| 23 | import net.onrc.onos.core.topology.Port; |
| 24 | import net.onrc.onos.core.topology.Switch; |
| 25 | |
| 26 | import org.openflow.protocol.OFMessage; |
| 27 | import org.openflow.protocol.OFPacketIn; |
| 28 | import org.openflow.protocol.OFType; |
| 29 | |
| 30 | public class PacketModule implements IOFMessageListener, IPacketService, |
| 31 | IFloodlightModule { |
| 32 | |
| 33 | private final CopyOnWriteArrayList<IPacketListener> listeners; |
| 34 | |
| 35 | private IFloodlightProviderService floodlightProvider; |
| 36 | private NetworkGraph networkGraph; |
| 37 | |
| 38 | public PacketModule() { |
| 39 | listeners = new CopyOnWriteArrayList<>(); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void registerPacketListener(IPacketListener listener) { |
| 44 | listeners.addIfAbsent(listener); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void sendPacket(Port port, Ethernet eth) { |
| 49 | // TODO Auto-generated method stub |
| 50 | |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void sendPacket(List<Port> ports, Ethernet eth) { |
| 55 | // TODO Auto-generated method stub |
| 56 | |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void broadcastPacket(Ethernet eth) { |
| 61 | // TODO Auto-generated method stub |
| 62 | |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void broadcastPacket(Ethernet eth, Port inPort) { |
| 67 | // TODO Auto-generated method stub |
| 68 | |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public String getName() { |
| 73 | // TODO Auto-generated method stub |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 79 | // TODO Auto-generated method stub |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 85 | // TODO Auto-generated method stub |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Command receive(IOFSwitch sw, OFMessage msg, |
| 91 | FloodlightContext cntx) { |
| 92 | if (!(msg instanceof OFPacketIn)) { |
| 93 | return Command.CONTINUE; |
| 94 | } |
| 95 | |
| 96 | OFPacketIn pi = (OFPacketIn) msg; |
| 97 | |
| 98 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 99 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
| 100 | |
| 101 | Switch networkGraphSwitch; |
| 102 | Port inPort; |
| 103 | try { |
| 104 | networkGraph.acquireReadLock(); |
| 105 | networkGraphSwitch = networkGraph.getSwitch(sw.getId()); |
| 106 | inPort = networkGraph.getPort(sw.getId(), (long) pi.getInPort()); |
| 107 | } finally { |
| 108 | networkGraph.releaseReadLock(); |
| 109 | } |
| 110 | |
| 111 | if (networkGraphSwitch == null || inPort == null) { |
| 112 | return Command.CONTINUE; |
| 113 | } |
| 114 | |
| 115 | for (IPacketListener listener : listeners) { |
| 116 | listener.receive(networkGraphSwitch, inPort, eth); |
| 117 | } |
| 118 | |
| 119 | return Command.CONTINUE; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 124 | List<Class<? extends IFloodlightService>> services = new ArrayList<>(); |
| 125 | services.add(IPacketService.class); |
| 126 | return services; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 131 | getServiceImpls() { |
| 132 | Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 133 | serviceImpls = new HashMap<>(); |
| 134 | serviceImpls.put(IPacketService.class, this); |
| 135 | return serviceImpls; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 140 | List<Class<? extends IFloodlightService>> dependencies = new ArrayList<>(); |
| 141 | dependencies.add(IFloodlightProviderService.class); |
| 142 | dependencies.add(INetworkGraphService.class); |
| 143 | return dependencies; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void init(FloodlightModuleContext context) |
| 148 | throws FloodlightModuleException { |
| 149 | floodlightProvider = |
| 150 | context.getServiceImpl(IFloodlightProviderService.class); |
| 151 | networkGraph = context.getServiceImpl(INetworkGraphService.class) |
| 152 | .getNetworkGraph(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void startUp(FloodlightModuleContext context) { |
| 157 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
| 158 | } |
| 159 | } |