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