blob: 8acf9a78f74bed40de629bb444acb62b1cae87b4 [file] [log] [blame]
Jonathan Hartf5829202015-02-12 09:37:02 -08001/*
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 */
16package org.onosproject.bgprouter;
17
Jonathan Hartf5829202015-02-12 09:37:02 -080018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hartf5829202015-02-12 09:37:02 -080023import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040025import org.onosproject.incubator.net.intf.InterfaceService;
Jonathan Hartf5829202015-02-12 09:37:02 -080026import org.onosproject.net.DeviceId;
Jonathan Hartdf207092015-12-10 11:19:25 -080027import org.onosproject.net.config.NetworkConfigService;
Saurav Dasbd7f7422015-04-23 16:31:47 -070028import org.onosproject.net.device.DeviceEvent;
29import org.onosproject.net.device.DeviceListener;
30import org.onosproject.net.device.DeviceService;
alshabib910aff12015-04-09 16:55:57 -070031import org.onosproject.net.flowobjective.FlowObjectiveService;
Jonathan Hartf5829202015-02-12 09:37:02 -080032import org.onosproject.net.packet.PacketService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080033import org.onosproject.routing.RoutingService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040034import org.onosproject.routing.config.BgpConfig;
Jonathan Hartdf207092015-12-10 11:19:25 -080035import org.onosproject.routing.config.RoutingConfigurationService;
Jonathan Hartf5829202015-02-12 09:37:02 -080036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
Jonathan Hart4cb39882015-08-12 23:50:55 -040039import java.util.Optional;
Saurav Dasdfc639e2015-04-30 11:48:16 -070040
Jonathan Hartf5829202015-02-12 09:37:02 -080041/**
42 * BgpRouter component.
43 */
44@Component(immediate = true)
45public class BgpRouter {
46
47 private static final Logger log = LoggerFactory.getLogger(BgpRouter.class);
48
Jonathan Hartdf207092015-12-10 11:19:25 -080049 public static final String BGP_ROUTER_APP = "org.onosproject.bgprouter";
Jonathan Hartf5829202015-02-12 09:37:02 -080050
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected CoreService coreService;
53
Jonathan Hartdf207092015-12-10 11:19:25 -080054 // 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 Hartf5829202015-02-12 09:37:02 -080057 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdf207092015-12-10 11:19:25 -080058 protected RoutingConfigurationService routingConfigurationService;
Jonathan Hartf5829202015-02-12 09:37:02 -080059
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart4cb39882015-08-12 23:50:55 -040061 protected InterfaceService interfaceService;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected NetworkConfigService networkConfigService;
Jonathan Hartf5829202015-02-12 09:37:02 -080065
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected PacketService packetService;
68
Saurav Dasbd7f7422015-04-23 16:31:47 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected FlowObjectiveService flowObjectiveService;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected DeviceService deviceService;
74
Jonathan Hartf5829202015-02-12 09:37:02 -080075 private ApplicationId appId;
76
Saurav Dasfbe25c52015-03-04 11:12:00 -080077 // Device id of control-plane switch (OVS) connected to BGP Speaker - should be
78 // learned from config
79 private DeviceId ctrlDeviceId;
Jonathan Hartf5829202015-02-12 09:37:02 -080080
Saurav Dasbd7f7422015-04-23 16:31:47 -070081 // 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 Hartf5829202015-02-12 09:37:02 -080083 private TunnellingConnectivityManager connectivityManager;
84
Saurav Dasbd7f7422015-04-23 16:31:47 -070085 private DeviceListener deviceListener;
sangho5eaf0332015-03-09 15:08:12 -070086 private IcmpHandler icmpHandler;
87
Jonathan Hartf5829202015-02-12 09:37:02 -080088 @Activate
89 protected void activate() {
Jonathan Hartf5829202015-02-12 09:37:02 -080090 appId = coreService.registerApplication(BGP_ROUTER_APP);
Jonathan Hart4cb39882015-08-12 23:50:55 -040091
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 Hartf5829202015-02-12 09:37:02 -0800102
103 connectivityManager = new TunnellingConnectivityManager(appId,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400104 bgpConfig,
105 interfaceService,
Jonathan Hart936a7292015-03-06 18:02:57 -0800106 packetService,
Saurav Das3d038262015-04-23 12:36:58 -0700107 flowObjectiveService);
Jonathan Hartf5829202015-02-12 09:37:02 -0800108
Jonathan Hart4cb39882015-08-12 23:50:55 -0400109 icmpHandler = new IcmpHandler(interfaceService, packetService);
Jonathan Hartdf207092015-12-10 11:19:25 -0800110
Saurav Dasbd7f7422015-04-23 16:31:47 -0700111 deviceListener = new InnerDeviceListener();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700112 deviceService.addListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800113
Jonathan Hartf5829202015-02-12 09:37:02 -0800114 connectivityManager.start();
sangho5eaf0332015-03-09 15:08:12 -0700115 icmpHandler.start();
116
Jonathan Hart49bcae92015-06-04 15:33:15 -0700117 if (deviceService.isAvailable(ctrlDeviceId)) {
118 connectivityManager.notifySwitchAvailable();
119 }
120
Jonathan Hartf5829202015-02-12 09:37:02 -0800121 log.info("BgpRouter started");
122 }
123
124 @Deactivate
125 protected void deactivate() {
Jonathan Hartf5829202015-02-12 09:37:02 -0800126 connectivityManager.stop();
sangho5eaf0332015-03-09 15:08:12 -0700127 icmpHandler.stop();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700128 deviceService.removeListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800129
Jonathan Hartf5829202015-02-12 09:37:02 -0800130 log.info("BgpRouter stopped");
131 }
132
Jonathan Hart4cb39882015-08-12 23:50:55 -0400133 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 Dasfbe25c52015-03-04 11:12:00 -0800139 return;
140 }
Jonathan Hart4cb39882015-08-12 23:50:55 -0400141
142 ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId();
143
Saurav Dasfbe25c52015-03-04 11:12:00 -0800144 log.info("Control Plane OVS dpid: {}", ctrlDeviceId);
145 }
146
Saurav Dasbd7f7422015-04-23 16:31:47 -0700147 // Triggers driver setup when a device is (re)detected.
148 private class InnerDeviceListener implements DeviceListener {
Jonathan Hart7baba072015-02-23 14:27:59 -0800149 @Override
Saurav Dasbd7f7422015-04-23 16:31:47 -0700150 public void event(DeviceEvent event) {
151 switch (event.type()) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800152 case DEVICE_ADDED:
153 case DEVICE_AVAILABILITY_CHANGED:
154 if (deviceService.isAvailable(event.subject().id())) {
155 log.info("Device connected {}", event.subject().id());
Jonathan Hart7baba072015-02-23 14:27:59 -0800156
Jonathan Hartdf207092015-12-10 11:19:25 -0800157 if (event.subject().id().equals(ctrlDeviceId)) {
158 connectivityManager.notifySwitchAvailable();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700159 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800160 }
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 Hart7baba072015-02-23 14:27:59 -0800171 }
172 }
Saurav Dasbd7f7422015-04-23 16:31:47 -0700173 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800174
Jonathan Hartf5829202015-02-12 09:37:02 -0800175}