blob: 257d143aa3454109fae421ad07dad10b2a89647a [file] [log] [blame]
Sangho Shin2f263692014-09-15 14:09:41 -07001package net.onrc.onos.apps.segmentrouting;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Iterator;
6import java.util.List;
7import java.util.Map;
8
9import net.floodlightcontroller.core.IFloodlightProviderService;
10import net.floodlightcontroller.core.module.FloodlightModuleContext;
11import net.floodlightcontroller.core.module.FloodlightModuleException;
12import net.floodlightcontroller.core.module.IFloodlightModule;
13import net.floodlightcontroller.core.module.IFloodlightService;
14import net.onrc.onos.api.packet.IPacketService;
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070015import net.onrc.onos.core.datastore.serializers.Topology;
Sangho Shin2f263692014-09-15 14:09:41 -070016import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
17import net.onrc.onos.core.main.config.IConfigInfoService;
18import net.onrc.onos.core.packet.ARP;
19import net.onrc.onos.core.topology.ITopologyService;
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070020import net.onrc.onos.core.topology.ITopologyListener;
21import net.onrc.onos.core.topology.MutableTopology;
22import net.onrc.onos.core.topology.TopologyEvents;
23import net.onrc.onos.core.topology.Switch;
Srikanth Vavilapallif3bfcf92014-09-19 07:42:10 -070024import net.onrc.onos.core.intent.Path;
Sangho Shin2f263692014-09-15 14:09:41 -070025
26import org.projectfloodlight.openflow.types.IPv4Address;
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070027import org.projectfloodlight.openflow.util.HexString;
Sangho Shin2f263692014-09-15 14:09:41 -070028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070031public class SegmentRoutingManager implements IFloodlightModule, ITopologyListener {
Sangho Shin2f263692014-09-15 14:09:41 -070032
33 private static final Logger log = LoggerFactory
34 .getLogger(SegmentRoutingManager.class);
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070035 private ITopologyService topologyService;
36 private MutableTopology mutableTopology;
Sangho Shin2f263692014-09-15 14:09:41 -070037
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 Vavilapallib7e5c5e2014-09-18 07:38:27 -070073 topologyService = context.getServiceImpl(ITopologyService.class);
74 mutableTopology = topologyService.getTopology();
75 topologyService.addListener(this, false);
Sangho Shin2f263692014-09-15 14:09:41 -070076
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 Vavilapallib7e5c5e2014-09-18 07:38:27 -0700146 /**
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 Vavilapallif3bfcf92014-09-19 07:42:10 -0700163 log.debug("ECMPShortestPathGraph is computed for switch {}",
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -0700164 HexString.toHexString(sw.getDpid().value()));
Srikanth Vavilapallif3bfcf92014-09-19 07:42:10 -0700165 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 Vavilapallib7e5c5e2014-09-18 07:38:27 -0700174 }
175 }
176 }
Sangho Shin2f263692014-09-15 14:09:41 -0700177}