blob: 37804e11728ee8163cdf4ceb972db2b001a7c1b4 [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;
Sangho Shin2f263692014-09-15 14:09:41 -070024
25import org.projectfloodlight.openflow.types.IPv4Address;
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070026import org.projectfloodlight.openflow.util.HexString;
Sangho Shin2f263692014-09-15 14:09:41 -070027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070030public class SegmentRoutingManager implements IFloodlightModule, ITopologyListener {
Sangho Shin2f263692014-09-15 14:09:41 -070031
32 private static final Logger log = LoggerFactory
33 .getLogger(SegmentRoutingManager.class);
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070034 private ITopologyService topologyService;
35 private MutableTopology mutableTopology;
Sangho Shin2f263692014-09-15 14:09:41 -070036
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 Vavilapallib7e5c5e2014-09-18 07:38:27 -070072 topologyService = context.getServiceImpl(ITopologyService.class);
73 mutableTopology = topologyService.getTopology();
74 topologyService.addListener(this, false);
Sangho Shin2f263692014-09-15 14:09:41 -070075
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 Vavilapallib7e5c5e2014-09-18 07:38:27 -0700145 /**
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 Shin2f263692014-09-15 14:09:41 -0700167}