Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [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.bgprouter; |
| 17 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 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; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 25 | import org.onosproject.incubator.net.intf.InterfaceService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 27 | import org.onosproject.net.config.NetworkConfigService; |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceEvent; |
| 29 | import org.onosproject.net.device.DeviceListener; |
| 30 | import org.onosproject.net.device.DeviceService; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 32 | import org.onosproject.net.packet.PacketService; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 33 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 34 | import org.onosproject.routing.config.BgpConfig; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 35 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 39 | import java.util.Optional; |
Saurav Das | dfc639e | 2015-04-30 11:48:16 -0700 | [diff] [blame] | 40 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 41 | /** |
| 42 | * BgpRouter component. |
| 43 | */ |
| 44 | @Component(immediate = true) |
| 45 | public class BgpRouter { |
| 46 | |
| 47 | private static final Logger log = LoggerFactory.getLogger(BgpRouter.class); |
| 48 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 49 | public static final String BGP_ROUTER_APP = "org.onosproject.bgprouter"; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected CoreService coreService; |
| 53 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 54 | // We depend on the routing configuration being available before starting |
| 55 | // up. When we have dynamic configuration support this will no longer be |
| 56 | // necessary. |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 58 | protected RoutingConfigurationService routingConfigurationService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 61 | protected InterfaceService interfaceService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected NetworkConfigService networkConfigService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected PacketService packetService; |
| 68 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected FlowObjectiveService flowObjectiveService; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected DeviceService deviceService; |
| 74 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 75 | private ApplicationId appId; |
| 76 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 77 | // Device id of control-plane switch (OVS) connected to BGP Speaker - should be |
| 78 | // learned from config |
| 79 | private DeviceId ctrlDeviceId; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 80 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 81 | // Responsible for handling BGP traffic (encapsulated within OF messages) |
| 82 | // between the data-plane switch and the Quagga VM using a control plane OVS. |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 83 | private TunnellingConnectivityManager connectivityManager; |
| 84 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 85 | private DeviceListener deviceListener; |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 86 | private IcmpHandler icmpHandler; |
| 87 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 88 | @Activate |
| 89 | protected void activate() { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 90 | appId = coreService.registerApplication(BGP_ROUTER_APP); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 91 | |
| 92 | ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 93 | BgpConfig bgpConfig = |
| 94 | networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS); |
| 95 | |
| 96 | if (bgpConfig == null) { |
| 97 | log.error("No BgpConfig found"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | getDeviceConfiguration(bgpConfig); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 102 | |
| 103 | connectivityManager = new TunnellingConnectivityManager(appId, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 104 | bgpConfig, |
| 105 | interfaceService, |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 106 | packetService, |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 107 | flowObjectiveService); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 108 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 109 | icmpHandler = new IcmpHandler(interfaceService, packetService); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 110 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 111 | deviceListener = new InnerDeviceListener(); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 112 | deviceService.addListener(deviceListener); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 113 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 114 | connectivityManager.start(); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 115 | icmpHandler.start(); |
| 116 | |
Jonathan Hart | 49bcae9 | 2015-06-04 15:33:15 -0700 | [diff] [blame] | 117 | if (deviceService.isAvailable(ctrlDeviceId)) { |
| 118 | connectivityManager.notifySwitchAvailable(); |
| 119 | } |
| 120 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 121 | log.info("BgpRouter started"); |
| 122 | } |
| 123 | |
| 124 | @Deactivate |
| 125 | protected void deactivate() { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 126 | connectivityManager.stop(); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 127 | icmpHandler.stop(); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 128 | deviceService.removeListener(deviceListener); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 129 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 130 | log.info("BgpRouter stopped"); |
| 131 | } |
| 132 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 133 | private void getDeviceConfiguration(BgpConfig bgpConfig) { |
| 134 | Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker = |
| 135 | bgpConfig.bgpSpeakers().stream().findAny(); |
| 136 | |
| 137 | if (!bgpSpeaker.isPresent()) { |
| 138 | log.error("BGP speaker configuration not found"); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 139 | return; |
| 140 | } |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 141 | |
| 142 | ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId(); |
| 143 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 144 | log.info("Control Plane OVS dpid: {}", ctrlDeviceId); |
| 145 | } |
| 146 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 147 | // Triggers driver setup when a device is (re)detected. |
| 148 | private class InnerDeviceListener implements DeviceListener { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 149 | @Override |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 150 | public void event(DeviceEvent event) { |
| 151 | switch (event.type()) { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 152 | case DEVICE_ADDED: |
| 153 | case DEVICE_AVAILABILITY_CHANGED: |
| 154 | if (deviceService.isAvailable(event.subject().id())) { |
| 155 | log.info("Device connected {}", event.subject().id()); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 156 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 157 | if (event.subject().id().equals(ctrlDeviceId)) { |
| 158 | connectivityManager.notifySwitchAvailable(); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 159 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 160 | } |
| 161 | break; |
| 162 | // TODO other cases |
| 163 | case DEVICE_UPDATED: |
| 164 | case DEVICE_REMOVED: |
| 165 | case DEVICE_SUSPENDED: |
| 166 | case PORT_ADDED: |
| 167 | case PORT_UPDATED: |
| 168 | case PORT_REMOVED: |
| 169 | default: |
| 170 | break; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 173 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame^] | 174 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 175 | } |