Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 20 | import java.util.Collection; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 21 | import java.util.concurrent.ExecutorService; |
| 22 | import java.util.concurrent.Executors; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 23 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 30 | import org.onlab.onos.core.ApplicationId; |
| 31 | import org.onlab.onos.core.CoreService; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.host.HostService; |
| 33 | import org.onlab.onos.net.intent.IntentService; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 34 | import org.onlab.onos.sdnip.bgp.BgpRouteEntry; |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 35 | import org.onlab.onos.sdnip.bgp.BgpSession; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 36 | import org.onlab.onos.sdnip.bgp.BgpSessionManager; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 37 | import org.onlab.onos.sdnip.config.SdnIpConfigReader; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 38 | import org.onlab.onos.store.service.Lock; |
| 39 | import org.onlab.onos.store.service.LockService; |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 42 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 43 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 44 | /** |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 45 | * Component for the SDN-IP peering application. |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 46 | */ |
| 47 | @Component(immediate = true) |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 48 | @Service |
| 49 | public class SdnIp implements SdnIpService { |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 50 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 51 | private static final String SDN_IP_APP = "org.onlab.onos.sdnip"; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 52 | // NOTE: Must be 5s for now |
| 53 | private static final int LEASE_DURATION_MS = 5 * 1000; |
| 54 | private static final int LEASE_EXTEND_RETRY_MAX = 3; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 55 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 59 | protected CoreService coreService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 62 | protected IntentService intentService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected HostService hostService; |
| 66 | |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected LockService lockService; |
| 69 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 70 | private IntentSynchronizer intentSynchronizer; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 71 | private SdnIpConfigReader config; |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 72 | private PeerConnectivityManager peerConnectivity; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 73 | private Router router; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 74 | private BgpSessionManager bgpSessionManager; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 75 | |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 76 | private ExecutorService leaderElectionExecutor; |
| 77 | private Lock leaderLock; |
| 78 | private volatile boolean isShutdown = true; |
| 79 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 80 | @Activate |
| 81 | protected void activate() { |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 82 | log.info("SDN-IP started"); |
| 83 | isShutdown = false; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 84 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 85 | ApplicationId appId = coreService.registerApplication(SDN_IP_APP); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 86 | config = new SdnIpConfigReader(); |
| 87 | config.init(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 88 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 89 | InterfaceService interfaceService = |
| 90 | new HostToInterfaceAdaptor(hostService); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 91 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 92 | intentSynchronizer = new IntentSynchronizer(appId, intentService); |
| 93 | intentSynchronizer.start(); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 94 | |
| 95 | peerConnectivity = new PeerConnectivityManager(appId, config, |
| 96 | interfaceService, intentService); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 97 | peerConnectivity.start(); |
| 98 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 99 | router = new Router(appId, intentSynchronizer, hostService, config, |
| 100 | interfaceService); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 101 | router.start(); |
| 102 | |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 103 | leaderLock = lockService.create(SDN_IP_APP + "/sdnIpLeaderLock"); |
| 104 | leaderElectionExecutor = Executors.newSingleThreadExecutor( |
| 105 | new ThreadFactoryBuilder() |
| 106 | .setNameFormat("sdnip-leader-election-%d").build()); |
| 107 | leaderElectionExecutor.execute(new Runnable() { |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame^] | 108 | @Override |
| 109 | public void run() { |
| 110 | doLeaderElectionThread(); |
| 111 | } |
| 112 | }); |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 113 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 114 | // Manually set the instance as the leader to allow testing |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 115 | // TODO change this when we get a leader election |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 116 | // intentSynchronizer.leaderChanged(true); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 117 | |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 118 | bgpSessionManager = new BgpSessionManager(router); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 119 | // TODO: the local BGP listen port number should be configurable |
| 120 | bgpSessionManager.start(2000); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 121 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 122 | // TODO need to disable link discovery on external ports |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Deactivate |
| 126 | protected void deactivate() { |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 127 | isShutdown = true; |
| 128 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 129 | bgpSessionManager.stop(); |
| 130 | router.stop(); |
| 131 | peerConnectivity.stop(); |
| 132 | intentSynchronizer.stop(); |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 133 | |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 134 | // Stop the thread(s) |
| 135 | leaderElectionExecutor.shutdownNow(); |
| 136 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 137 | log.info("Stopped"); |
| 138 | } |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 139 | |
| 140 | @Override |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 141 | public Collection<BgpSession> getBgpSessions() { |
| 142 | return bgpSessionManager.getBgpSessions(); |
| 143 | } |
| 144 | |
| 145 | @Override |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 146 | public Collection<BgpRouteEntry> getBgpRoutes() { |
| 147 | return bgpSessionManager.getBgpRoutes(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Collection<RouteEntry> getRoutes() { |
| 152 | return router.getRoutes(); |
| 153 | } |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 154 | |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public void modifyPrimary(boolean isPrimary) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 157 | intentSynchronizer.leaderChanged(isPrimary); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 160 | static String dpidToUri(String dpid) { |
| 161 | return "of:" + dpid.replace(":", ""); |
| 162 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * Performs the leader election. |
| 166 | */ |
| 167 | private void doLeaderElectionThread() { |
| 168 | |
| 169 | // |
| 170 | // Try to acquire the lock and keep extending it until the instance |
| 171 | // is shutdown. |
| 172 | // |
| 173 | while (!isShutdown) { |
| 174 | log.debug("SDN-IP Leader Election begin"); |
| 175 | |
| 176 | // Block until it becomes the leader |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame^] | 177 | try { |
| 178 | leaderLock.lock(LEASE_DURATION_MS); |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 179 | |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame^] | 180 | // This instance is the leader |
| 181 | log.info("SDN-IP Leader Elected"); |
| 182 | intentSynchronizer.leaderChanged(true); |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 183 | |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame^] | 184 | // Keep extending the expiration until shutdown |
| 185 | int extensionFailedCountdown = LEASE_EXTEND_RETRY_MAX - 1; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 186 | |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame^] | 187 | // |
| 188 | // Keep periodically extending the lock expiration. |
| 189 | // If there are multiple back-to-back failures to extend (with |
| 190 | // extra sleep time between retrials), then release the lock. |
| 191 | // |
| 192 | while (!isShutdown) { |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 193 | Thread.sleep(LEASE_DURATION_MS / LEASE_EXTEND_RETRY_MAX); |
| 194 | if (leaderLock.extendExpiration(LEASE_DURATION_MS)) { |
| 195 | log.trace("SDN-IP Leader Extended"); |
| 196 | extensionFailedCountdown = LEASE_EXTEND_RETRY_MAX; |
| 197 | } else { |
| 198 | log.debug("SDN-IP Leader Cannot Extend Election"); |
| 199 | if (!leaderLock.isLocked()) { |
| 200 | log.debug("SDN-IP Leader Lock Lost"); |
| 201 | intentSynchronizer.leaderChanged(false); |
| 202 | break; // Try again to get the lock |
| 203 | } |
| 204 | extensionFailedCountdown--; |
| 205 | if (extensionFailedCountdown <= 0) { |
| 206 | // Failed too many times to extend. |
| 207 | // Release the lock. |
| 208 | log.debug("SDN-IP Leader Lock Released"); |
| 209 | intentSynchronizer.leaderChanged(false); |
| 210 | leaderLock.unlock(); |
| 211 | break; // Try again to get the lock |
| 212 | } |
| 213 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 214 | } |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame^] | 215 | } catch (InterruptedException e) { |
| 216 | // Thread interrupted. Time to shutdown |
| 217 | log.debug("SDN-IP Leader Interrupted"); |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 220 | // If we reach here, the instance was shutdown |
| 221 | intentSynchronizer.leaderChanged(false); |
| 222 | leaderLock.unlock(); |
| 223 | } |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 224 | } |