Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | |
| 9 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 10 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 11 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 13 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 14 | import net.onrc.onos.api.packet.IPacketService; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.datastore.serializers.Topology; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
| 17 | import net.onrc.onos.core.main.config.IConfigInfoService; |
| 18 | import net.onrc.onos.core.packet.ARP; |
| 19 | import net.onrc.onos.core.topology.ITopologyService; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.topology.ITopologyListener; |
| 21 | import net.onrc.onos.core.topology.MutableTopology; |
| 22 | import net.onrc.onos.core.topology.TopologyEvents; |
| 23 | import net.onrc.onos.core.topology.Switch; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 24 | |
| 25 | import org.projectfloodlight.openflow.types.IPv4Address; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.util.HexString; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 30 | public class SegmentRoutingManager implements IFloodlightModule, ITopologyListener { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 31 | |
| 32 | private static final Logger log = LoggerFactory |
| 33 | .getLogger(SegmentRoutingManager.class); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 34 | private ITopologyService topologyService; |
| 35 | private MutableTopology mutableTopology; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 36 | |
| 37 | private List<ArpEntry> arpEntries; |
| 38 | |
| 39 | @Override |
| 40 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 41 | // TODO Auto-generated method stub |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 47 | // TODO Auto-generated method stub |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 53 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>(); |
| 54 | |
| 55 | l.add(IFloodlightProviderService.class); |
| 56 | l.add(IConfigInfoService.class); |
| 57 | l.add(ITopologyService.class); |
| 58 | l.add(IPacketService.class); |
| 59 | l.add(IFlowPusherService.class); |
| 60 | l.add(ITopologyService.class); |
| 61 | |
| 62 | return l; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
| 68 | |
| 69 | ArpHandler aprHandler = new ArpHandler(context, this); |
| 70 | IcmpHandler icmpHandler = new IcmpHandler(context, this); |
| 71 | arpEntries = new ArrayList<ArpEntry>(); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 72 | topologyService = context.getServiceImpl(ITopologyService.class); |
| 73 | mutableTopology = topologyService.getTopology(); |
| 74 | topologyService.addListener(this, false); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 75 | |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { |
| 80 | // TODO Auto-generated method stub |
| 81 | |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Update ARP Cache using ARP packets |
| 86 | * It is used to set destination MAC address to forward packets to known hosts. |
| 87 | * But, it will be replace with Host information of Topology service later. |
| 88 | * |
| 89 | * @param arp APR packets to use for updating ARP entries |
| 90 | */ |
| 91 | public void updateArpCache(ARP arp) { |
| 92 | |
| 93 | ArpEntry arpEntry = new ArpEntry(arp.getSenderHardwareAddress(), arp.getSenderProtocolAddress()); |
| 94 | // TODO: Need to check the duplication |
| 95 | arpEntries.add(arpEntry); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get MAC address to known hosts |
| 100 | * |
| 101 | * @param destinationAddress IP address to get MAC address |
| 102 | * @return MAC Address to given IP address |
| 103 | */ |
| 104 | public byte[] getMacAddressFromIpAddress(int destinationAddress) { |
| 105 | |
| 106 | // Can't we get the host IP address from the TopologyService ?? |
| 107 | |
| 108 | Iterator<ArpEntry> iterator = arpEntries.iterator(); |
| 109 | |
| 110 | IPv4Address ipAddress = IPv4Address.of(destinationAddress); |
| 111 | byte[] ipAddressInByte = ipAddress.getBytes(); |
| 112 | |
| 113 | while (iterator.hasNext() ) { |
| 114 | ArpEntry arpEntry = iterator.next(); |
| 115 | byte[] address = arpEntry.targetIpAddress; |
| 116 | |
| 117 | IPv4Address a = IPv4Address.of(address); |
| 118 | IPv4Address b = IPv4Address.of(ipAddressInByte); |
| 119 | |
| 120 | if ( a.equals(b)) { |
| 121 | log.debug("Found an arp entry"); |
| 122 | return arpEntry.targetMacAddress; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Temporary class to to keep ARP entry |
| 132 | * |
| 133 | */ |
| 134 | private class ArpEntry { |
| 135 | |
| 136 | byte[] targetMacAddress; |
| 137 | byte[] targetIpAddress; |
| 138 | |
| 139 | private ArpEntry(byte[] macAddress, byte[] ipAddress) { |
| 140 | this.targetMacAddress = macAddress; |
| 141 | this.targetIpAddress = ipAddress; |
| 142 | } |
| 143 | |
| 144 | } |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Topology events that have been generated. |
| 147 | * |
| 148 | * @param topologyEvents the generated Topology Events |
| 149 | * @see TopologyEvents |
| 150 | */ |
| 151 | public void topologyEvents(TopologyEvents topologyEvents) |
| 152 | { |
| 153 | /** |
| 154 | * Any Link update events, compute the ECMP path graph for all switch nodes |
| 155 | */ |
| 156 | if ((topologyEvents.getAddedLinkDataEntries() != null) || |
| 157 | (topologyEvents.getRemovedLinkDataEntries() != null)) |
| 158 | { |
| 159 | Iterable<Switch> switches= mutableTopology.getSwitches(); |
| 160 | for (Switch sw : switches) { |
| 161 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(sw); |
| 162 | log.debug("ECMPShortestPathGraph for switch {}", |
| 163 | HexString.toHexString(sw.getDpid().value())); |
| 164 | } |
| 165 | } |
| 166 | } |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 167 | } |