Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Ray Milkey | e56c34d | 2017-08-03 13:39:30 -0700 | [diff] [blame] | 25 | import org.onosproject.component.ComponentService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 26 | import org.onosproject.net.intf.InterfaceService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Jonathan Hart | 60e7f51 | 2017-02-10 10:24:24 -0800 | [diff] [blame] | 28 | import org.onosproject.net.config.NetworkConfigRegistry; |
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 | 60e7f51 | 2017-02-10 10:24:24 -0800 | [diff] [blame] | 36 | import org.onosproject.routing.config.RoutingConfiguration; |
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 | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 58 | protected InterfaceService interfaceService; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 60e7f51 | 2017-02-10 10:24:24 -0800 | [diff] [blame] | 61 | protected NetworkConfigRegistry networkConfigService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected PacketService packetService; |
| 65 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected FlowObjectiveService flowObjectiveService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected DeviceService deviceService; |
| 71 | |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected ComponentService componentService; |
| 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 | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 88 | private static List<String> components = new ArrayList<>(); |
| 89 | static { |
| 90 | components.add("org.onosproject.routing.bgp.BgpSessionManager"); |
Jonathan Hart | a9e2955 | 2016-09-09 07:17:41 -0700 | [diff] [blame] | 91 | components.add("org.onosproject.routing.impl.BgpSpeakerNeighbourHandler"); |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 94 | @Activate |
| 95 | protected void activate() { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 96 | appId = coreService.registerApplication(BGP_ROUTER_APP); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 97 | |
Jonathan Hart | 60e7f51 | 2017-02-10 10:24:24 -0800 | [diff] [blame] | 98 | RoutingConfiguration.register(networkConfigService); |
| 99 | |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 100 | components.forEach(name -> componentService.activate(appId, name)); |
| 101 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 102 | ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 103 | BgpConfig bgpConfig = |
| 104 | networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS); |
| 105 | |
| 106 | if (bgpConfig == null) { |
| 107 | log.error("No BgpConfig found"); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | getDeviceConfiguration(bgpConfig); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 112 | |
| 113 | connectivityManager = new TunnellingConnectivityManager(appId, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 114 | bgpConfig, |
| 115 | interfaceService, |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 116 | packetService, |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 117 | flowObjectiveService); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 118 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 119 | icmpHandler = new IcmpHandler(interfaceService, packetService); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 120 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 121 | deviceListener = new InnerDeviceListener(); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 122 | deviceService.addListener(deviceListener); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 123 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 124 | connectivityManager.start(); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 125 | icmpHandler.start(); |
| 126 | |
Jonathan Hart | 49bcae9 | 2015-06-04 15:33:15 -0700 | [diff] [blame] | 127 | if (deviceService.isAvailable(ctrlDeviceId)) { |
| 128 | connectivityManager.notifySwitchAvailable(); |
| 129 | } |
| 130 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 131 | log.info("BgpRouter started"); |
| 132 | } |
| 133 | |
| 134 | @Deactivate |
| 135 | protected void deactivate() { |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 136 | components.forEach(name -> componentService.deactivate(appId, name)); |
| 137 | |
Jonathan Hart | 60e7f51 | 2017-02-10 10:24:24 -0800 | [diff] [blame] | 138 | RoutingConfiguration.unregister(networkConfigService); |
| 139 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 140 | connectivityManager.stop(); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 141 | icmpHandler.stop(); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 142 | deviceService.removeListener(deviceListener); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 143 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 144 | log.info("BgpRouter stopped"); |
| 145 | } |
| 146 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 147 | private void getDeviceConfiguration(BgpConfig bgpConfig) { |
| 148 | Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker = |
| 149 | bgpConfig.bgpSpeakers().stream().findAny(); |
| 150 | |
| 151 | if (!bgpSpeaker.isPresent()) { |
| 152 | log.error("BGP speaker configuration not found"); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 153 | return; |
| 154 | } |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 155 | |
| 156 | ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId(); |
| 157 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 158 | log.info("Control Plane OVS dpid: {}", ctrlDeviceId); |
| 159 | } |
| 160 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 161 | // Triggers driver setup when a device is (re)detected. |
| 162 | private class InnerDeviceListener implements DeviceListener { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 163 | @Override |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 164 | public void event(DeviceEvent event) { |
| 165 | switch (event.type()) { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 166 | case DEVICE_ADDED: |
| 167 | case DEVICE_AVAILABILITY_CHANGED: |
| 168 | if (deviceService.isAvailable(event.subject().id())) { |
| 169 | log.info("Device connected {}", event.subject().id()); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 170 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 171 | if (event.subject().id().equals(ctrlDeviceId)) { |
| 172 | connectivityManager.notifySwitchAvailable(); |
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 | } |
| 175 | break; |
| 176 | // TODO other cases |
| 177 | case DEVICE_UPDATED: |
| 178 | case DEVICE_REMOVED: |
| 179 | case DEVICE_SUSPENDED: |
| 180 | case PORT_ADDED: |
| 181 | case PORT_UPDATED: |
| 182 | case PORT_REMOVED: |
| 183 | default: |
| 184 | break; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 187 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 188 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 189 | } |