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