sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onlab.packet.IPv4; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.event.Event; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 28 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 29 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 30 | import org.onosproject.mastership.MastershipService; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Link; |
| 34 | import org.onosproject.net.MastershipRole; |
| 35 | import org.onosproject.net.Port; |
| 36 | import org.onosproject.net.device.DeviceEvent; |
| 37 | import org.onosproject.net.device.DeviceListener; |
| 38 | import org.onosproject.net.device.DeviceService; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 40 | import org.onosproject.net.group.Group; |
| 41 | import org.onosproject.net.group.GroupEvent; |
| 42 | import org.onosproject.net.group.GroupKey; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 43 | import org.onosproject.net.host.HostService; |
| 44 | import org.onosproject.net.intent.IntentService; |
| 45 | import org.onosproject.net.link.LinkEvent; |
| 46 | import org.onosproject.net.link.LinkListener; |
| 47 | import org.onosproject.net.link.LinkService; |
| 48 | import org.onosproject.net.packet.InboundPacket; |
| 49 | import org.onosproject.net.packet.PacketContext; |
| 50 | import org.onosproject.net.packet.PacketProcessor; |
| 51 | import org.onosproject.net.packet.PacketService; |
| 52 | import org.onosproject.net.topology.TopologyService; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 53 | import org.onosproject.segmentrouting.config.NetworkConfigManager; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
| 57 | import java.util.Map; |
| 58 | import java.util.concurrent.ConcurrentHashMap; |
| 59 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 60 | import java.util.concurrent.Executors; |
| 61 | import java.util.concurrent.ScheduledExecutorService; |
| 62 | import java.util.concurrent.ScheduledFuture; |
| 63 | import java.util.concurrent.TimeUnit; |
| 64 | |
| 65 | @SuppressWarnings("ALL") |
| 66 | @Component(immediate = true) |
| 67 | public class SegmentRoutingManager { |
| 68 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 69 | private static Logger log = LoggerFactory |
| 70 | .getLogger(SegmentRoutingManager.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected CoreService coreService; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected TopologyService topologyService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected PacketService packetService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected IntentService intentService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected HostService hostService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected DeviceService deviceService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 91 | protected FlowObjectiveService flowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected LinkService linkService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 97 | protected MastershipService mastershipService; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 98 | protected ArpHandler arpHandler = null; |
| 99 | protected IcmpHandler icmpHandler = null; |
| 100 | protected IpHandler ipHandler = null; |
| 101 | protected RoutingRulePopulator routingRulePopulator = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 102 | protected ApplicationId appId; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 103 | protected DeviceConfiguration deviceConfiguration = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 104 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 105 | private DefaultRoutingHandler defaultRoutingHandler = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 106 | private InternalPacketProcessor processor = new InternalPacketProcessor(); |
| 107 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
| 108 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 109 | private ScheduledExecutorService executorService = Executors |
| 110 | .newScheduledThreadPool(1); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 111 | |
| 112 | private static ScheduledFuture<?> eventHandlerFuture = null; |
| 113 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<Event>(); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 114 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<DeviceId, DefaultGroupHandler>(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 115 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 116 | private NetworkConfigManager networkConfigService = new NetworkConfigManager();; |
| 117 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 118 | private static int numOfEvents = 0; |
| 119 | private static int numOfHandlerExecution = 0; |
| 120 | private static int numOfHandlerScheduled = 0; |
| 121 | |
| 122 | @Activate |
| 123 | protected void activate() { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 124 | appId = coreService |
| 125 | .registerApplication("org.onosproject.segmentrouting"); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 126 | networkConfigService.init(); |
| 127 | deviceConfiguration = new DeviceConfiguration(networkConfigService); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 128 | arpHandler = new ArpHandler(this); |
| 129 | icmpHandler = new IcmpHandler(this); |
| 130 | ipHandler = new IpHandler(this); |
| 131 | routingRulePopulator = new RoutingRulePopulator(this); |
| 132 | defaultRoutingHandler = new DefaultRoutingHandler(this); |
| 133 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 134 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
| 135 | linkService.addListener(new InternalLinkListener()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 136 | deviceService.addListener(new InternalDeviceListener()); |
| 137 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 138 | for (Device device : deviceService.getDevices()) { |
| 139 | if (mastershipService.getLocalRole(device.id()) == MastershipRole.MASTER) { |
| 140 | DefaultGroupHandler groupHandler = DefaultGroupHandler |
| 141 | .createGroupHandler(device.id(), appId, |
| 142 | deviceConfiguration, linkService, |
| 143 | flowObjectiveService); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 144 | groupHandlerMap.put(device.id(), groupHandler); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 145 | defaultRoutingHandler.populateTtpRules(device.id()); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 146 | log.debug("Initiating default group handling for {}", device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 147 | } else { |
| 148 | log.debug("Activate: Local role {} " |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 149 | + "is not MASTER for device {}", |
| 150 | mastershipService.getLocalRole(device.id()), |
| 151 | device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 155 | defaultRoutingHandler.startPopulationProcess(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 156 | log.info("Started"); |
| 157 | } |
| 158 | |
| 159 | @Deactivate |
| 160 | protected void deactivate() { |
| 161 | packetService.removeProcessor(processor); |
| 162 | processor = null; |
| 163 | log.info("Stopped"); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the GrouopKey object for the device and the NighborSet given. |
| 168 | * |
| 169 | * @param ns NeightborSet object for the GroupKey |
| 170 | * @return GroupKey object for the NeighborSet |
| 171 | */ |
| 172 | public GroupKey getGroupKey(NeighborSet ns) { |
| 173 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 174 | for (DefaultGroupHandler groupHandler : groupHandlerMap.values()) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 175 | return groupHandler.getGroupKey(ns); |
| 176 | } |
| 177 | |
| 178 | return null; |
| 179 | } |
| 180 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 181 | public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns) { |
| 182 | |
| 183 | return (groupHandlerMap.get(deviceId) != null) ? groupHandlerMap |
| 184 | .get(deviceId).getNextObjectiveId(ns) : -1; |
| 185 | } |
| 186 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 187 | private class InternalPacketProcessor implements PacketProcessor { |
| 188 | |
| 189 | @Override |
| 190 | public void process(PacketContext context) { |
| 191 | |
| 192 | if (context.isHandled()) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | InboundPacket pkt = context.inPacket(); |
| 197 | Ethernet ethernet = pkt.parsed(); |
| 198 | |
| 199 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 200 | arpHandler.processPacketIn(pkt); |
| 201 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 202 | IPv4 ipPacket = (IPv4) ethernet.getPayload(); |
| 203 | ipHandler.addToPacketBuffer(ipPacket); |
| 204 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 205 | icmpHandler.processPacketIn(pkt); |
| 206 | } else { |
| 207 | ipHandler.processPacketIn(pkt); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | private class InternalLinkListener implements LinkListener { |
| 214 | @Override |
| 215 | public void event(LinkEvent event) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 216 | if (event.type() == LinkEvent.Type.LINK_ADDED |
| 217 | || event.type() == LinkEvent.Type.LINK_REMOVED) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 218 | scheduleEventHandlerIfNotScheduled(event); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private class InternalDeviceListener implements DeviceListener { |
| 224 | |
| 225 | @Override |
| 226 | public void event(DeviceEvent event) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 227 | if (mastershipService.getLocalRole(event.subject().id()) != MastershipRole.MASTER) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 228 | log.debug("Local role {} is not MASTER for device {}", |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 229 | mastershipService.getLocalRole(event.subject().id()), |
| 230 | event.subject().id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | switch (event.type()) { |
| 235 | case DEVICE_ADDED: |
| 236 | case PORT_REMOVED: |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 237 | case DEVICE_UPDATED: |
| 238 | case DEVICE_AVAILABILITY_CHANGED: |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 239 | scheduleEventHandlerIfNotScheduled(event); |
| 240 | break; |
| 241 | default: |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 246 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
| 247 | |
| 248 | eventQueue.add(event); |
| 249 | numOfEvents++; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 250 | if (eventHandlerFuture == null || eventHandlerFuture.isDone()) { |
| 251 | eventHandlerFuture = executorService |
| 252 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 253 | numOfHandlerScheduled++; |
| 254 | } |
| 255 | |
| 256 | log.trace("numOfEvents {}, numOfEventHanlderScheduled {}", numOfEvents, |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 257 | numOfHandlerScheduled); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 258 | |
| 259 | } |
| 260 | |
| 261 | private class InternalEventHandler implements Runnable { |
| 262 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 263 | @Override |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 264 | public void run() { |
| 265 | numOfHandlerExecution++; |
| 266 | while (!eventQueue.isEmpty()) { |
| 267 | Event event = eventQueue.poll(); |
| 268 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 269 | processLinkAdded((Link) event.subject()); |
| 270 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
| 271 | processLinkRemoved((Link) event.subject()); |
| 272 | } else if (event.type() == GroupEvent.Type.GROUP_ADDED) { |
| 273 | processGroupAdded((Group) event.subject()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 274 | } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 275 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 276 | event.type() == DeviceEvent.Type.DEVICE_UPDATED) { |
| 277 | if (deviceService.isAvailable(((Device) event.subject()).id())) { |
| 278 | processDeviceAdded((Device) event.subject()); |
| 279 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 280 | } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) { |
| 281 | processPortRemoved((Device) event.subject(), |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 282 | ((DeviceEvent) event).port()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 283 | } else { |
| 284 | log.warn("Unhandled event type: {}", event.type()); |
| 285 | } |
| 286 | } |
| 287 | log.debug("numOfHandlerExecution {} numOfEventHanlderScheduled {} numOfEvents {}", |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 288 | numOfHandlerExecution, numOfHandlerScheduled, numOfEvents); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 292 | private void processLinkAdded(Link link) { |
| 293 | log.debug("A new link {} was added", link.toString()); |
| 294 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 295 | if (mastershipService.getLocalRole(link.src().deviceId()) == MastershipRole.MASTER) { |
| 296 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src() |
| 297 | .deviceId()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 298 | if (groupHandler != null) { |
| 299 | groupHandler.linkUp(link); |
| 300 | } |
| 301 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 302 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | private void processLinkRemoved(Link link) { |
| 306 | log.debug("A link {} was removed", link.toString()); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 307 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId()); |
| 308 | if (groupHandler != null) { |
| 309 | groupHandler.portDown(link.src().port()); |
| 310 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 311 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 312 | } |
| 313 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 314 | private void processGroupAdded(Group group) { |
| 315 | log.debug("A new group with ID {} was added", group.id()); |
| 316 | defaultRoutingHandler.resumePopulationProcess(); |
| 317 | } |
| 318 | |
| 319 | private void processDeviceAdded(Device device) { |
| 320 | log.debug("A new device with ID {} was added", device.id()); |
| 321 | defaultRoutingHandler.populateTtpRules(device.id()); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 322 | DefaultGroupHandler dgh = DefaultGroupHandler.createGroupHandler(device |
| 323 | .id(), appId, deviceConfiguration, linkService, flowObjectiveService); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 324 | groupHandlerMap.put(device.id(), dgh); |
| 325 | } |
| 326 | |
| 327 | private void processPortRemoved(Device device, Port port) { |
| 328 | log.debug("Port {} was removed", port.toString()); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 329 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 330 | if (groupHandler != null) { |
| 331 | groupHandler.portDown(port.number()); |
| 332 | } |
| 333 | } |
| 334 | } |