Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -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; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.LeadershipEvent; |
| 26 | import org.onosproject.cluster.LeadershipEventListener; |
| 27 | import org.onosproject.cluster.LeadershipService; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.net.intent.Intent; |
| 31 | import org.onosproject.net.intent.IntentService; |
| 32 | import org.onosproject.net.intent.IntentState; |
| 33 | import org.onosproject.net.intent.Key; |
| 34 | import org.onosproject.routing.IntentSynchronizationAdminService; |
| 35 | import org.onosproject.routing.IntentSynchronizationService; |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame] | 38 | |
| 39 | import java.util.HashMap; |
| 40 | import java.util.LinkedList; |
| 41 | import java.util.List; |
| 42 | import java.util.Map; |
| 43 | import java.util.Map.Entry; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 44 | import java.util.Objects; |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame] | 45 | import java.util.concurrent.ConcurrentHashMap; |
| 46 | import java.util.concurrent.ExecutorService; |
| 47 | |
Luca Prete | 86ac7d1 | 2015-12-02 23:36:49 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.IntentUtils; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 49 | |
| 50 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 51 | import static org.onlab.util.Tools.groupedThreads; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 52 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 53 | /** |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 54 | * Synchronizes intents between an in-memory intent store and the IntentService. |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 55 | */ |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 56 | @Service |
| 57 | @Component(immediate = true) |
| 58 | public class IntentSynchronizer implements IntentSynchronizationService, |
| 59 | IntentSynchronizationAdminService { |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 60 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 61 | private static final Logger log = LoggerFactory.getLogger(IntentSynchronizer.class); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 62 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 63 | private static final String APP_NAME = "org.onosproject.intentsynchronizer"; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 64 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected CoreService coreService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 67 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected LeadershipService leadershipService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected ClusterService clusterService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected IntentService intentService; |
| 76 | |
| 77 | private ApplicationId appId; |
| 78 | |
| 79 | private final InternalLeadershipListener leadershipEventListener = |
| 80 | new InternalLeadershipListener(); |
| 81 | |
| 82 | private final Map<Key, Intent> intents = new ConcurrentHashMap<>(); |
| 83 | |
| 84 | private ExecutorService intentsSynchronizerExecutor; |
| 85 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 86 | private volatile boolean isElectedLeader = false; |
| 87 | private volatile boolean isActivatedLeader = false; |
| 88 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 89 | @Activate |
| 90 | public void activate() { |
| 91 | intentsSynchronizerExecutor = createExecutor(); |
| 92 | |
| 93 | this.appId = coreService.registerApplication(APP_NAME); |
| 94 | |
| 95 | leadershipService.addListener(leadershipEventListener); |
| 96 | leadershipService.runForLeadership(appId.name()); |
| 97 | |
| 98 | log.info("Started"); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 101 | @Deactivate |
| 102 | public void deactivate() { |
| 103 | leadershipService.withdraw(appId.name()); |
| 104 | leadershipService.removeListener(leadershipEventListener); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 105 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 106 | synchronized (this) { |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 107 | intentsSynchronizerExecutor.shutdownNow(); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 108 | } |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 109 | |
| 110 | log.info("Stopped"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame] | 113 | /** |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 114 | * Creates an executor that will be used for synchronization tasks. |
| 115 | * <p> |
| 116 | * Can be overridden to change the type of executor used. |
| 117 | * </p> |
| 118 | * |
| 119 | * @return executor service |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame] | 120 | */ |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 121 | protected ExecutorService createExecutor() { |
| 122 | return newSingleThreadExecutor(groupedThreads("onos/" + appId, "sync")); |
| 123 | } |
| 124 | |
| 125 | @Override |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame] | 126 | public void removeIntents() { |
| 127 | if (!isElectedLeader) { |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 128 | // Only leader will withdraw intents |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
| 132 | log.debug("Intent Synchronizer shutdown: withdrawing all intents..."); |
| 133 | |
| 134 | for (Entry<Key, Intent> entry : intents.entrySet()) { |
| 135 | intentService.withdraw(entry.getValue()); |
| 136 | log.debug("Intent Synchronizer withdrawing intent: {}", |
| 137 | entry.getValue()); |
| 138 | } |
| 139 | |
| 140 | intents.clear(); |
| 141 | log.info("Tried to clean all intents"); |
| 142 | } |
| 143 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 144 | @Override |
| 145 | public void submit(Intent intent) { |
| 146 | synchronized (this) { |
| 147 | intents.put(intent.key(), intent); |
| 148 | if (isElectedLeader && isActivatedLeader) { |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 149 | log.trace("Submitting intent: {}", intent); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 150 | intentService.submit(intent); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void withdraw(Intent intent) { |
| 157 | synchronized (this) { |
| 158 | intents.remove(intent.key(), intent); |
| 159 | if (isElectedLeader && isActivatedLeader) { |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 160 | log.trace("Withdrawing intent: {}", intent); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 161 | intentService.withdraw(intent); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 166 | /** |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 167 | * Signals the synchronizer that the leadership has changed. |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 168 | * |
| 169 | * @param isLeader true if this instance is now the leader, otherwise false |
| 170 | */ |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 171 | private void leaderChanged(boolean isLeader) { |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 172 | log.debug("Leader changed: {}", isLeader); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 173 | |
| 174 | if (!isLeader) { |
| 175 | this.isElectedLeader = false; |
| 176 | this.isActivatedLeader = false; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 177 | // Nothing to do |
| 178 | return; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 179 | } |
| 180 | this.isActivatedLeader = false; |
| 181 | this.isElectedLeader = true; |
| 182 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 183 | // Run the synchronization task |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 184 | intentsSynchronizerExecutor.execute(this::synchronizeIntents); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 187 | private void synchronizeIntents() { |
| 188 | Map<Key, Intent> serviceIntents = new HashMap<>(); |
| 189 | intentService.getIntents().forEach(i -> { |
| 190 | if (i.appId().equals(appId)) { |
| 191 | serviceIntents.put(i.key(), i); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 192 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 193 | }); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 194 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 195 | List<Intent> intentsToAdd = new LinkedList<>(); |
| 196 | List<Intent> intentsToRemove = new LinkedList<>(); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 197 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 198 | for (Intent localIntent : intents.values()) { |
| 199 | Intent serviceIntent = serviceIntents.remove(localIntent.key()); |
| 200 | if (serviceIntent == null) { |
| 201 | intentsToAdd.add(localIntent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 202 | } else { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 203 | IntentState state = intentService.getIntentState(serviceIntent.key()); |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 204 | if (!IntentUtils.intentsAreEqual(serviceIntent, localIntent) || state == null || |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 205 | state == IntentState.WITHDRAW_REQ || |
| 206 | state == IntentState.WITHDRAWING || |
| 207 | state == IntentState.WITHDRAWN) { |
| 208 | intentsToAdd.add(localIntent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 209 | } |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 212 | |
| 213 | for (Intent serviceIntent : serviceIntents.values()) { |
| 214 | IntentState state = intentService.getIntentState(serviceIntent.key()); |
| 215 | if (state != null && state != IntentState.WITHDRAW_REQ |
| 216 | && state != IntentState.WITHDRAWING |
| 217 | && state != IntentState.WITHDRAWN) { |
| 218 | intentsToRemove.add(serviceIntent); |
| 219 | } |
| 220 | } |
| 221 | |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 222 | log.debug("Intent Synchronizer: submitting {}, withdrawing {}", |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 223 | intentsToAdd.size(), intentsToRemove.size()); |
| 224 | |
| 225 | // Withdraw Intents |
| 226 | for (Intent intent : intentsToRemove) { |
| 227 | intentService.withdraw(intent); |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 228 | log.trace("Intent Synchronizer: withdrawing intent: {}", |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 229 | intent); |
| 230 | } |
| 231 | if (!isElectedLeader) { |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 232 | log.debug("Intent Synchronizer: cannot withdraw intents: " + |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 233 | "not elected leader anymore"); |
| 234 | isActivatedLeader = false; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 238 | // Add Intents |
| 239 | for (Intent intent : intentsToAdd) { |
| 240 | intentService.submit(intent); |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 241 | log.trace("Intent Synchronizer: submitting intent: {}", |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 242 | intent); |
| 243 | } |
| 244 | if (!isElectedLeader) { |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 245 | log.debug("Intent Synchronizer: cannot submit intents: " + |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 246 | "not elected leader anymore"); |
| 247 | isActivatedLeader = false; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 248 | return; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | if (isElectedLeader) { |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 252 | // Allow push of Intents |
| 253 | isActivatedLeader = true; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 254 | } else { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 255 | isActivatedLeader = false; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 256 | } |
Luca Prete | 00043db | 2015-11-03 15:40:40 -0800 | [diff] [blame] | 257 | log.debug("Intent synchronization completed"); |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 258 | } |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 259 | |
| 260 | @Override |
| 261 | public void modifyPrimary(boolean isPrimary) { |
| 262 | leaderChanged(isPrimary); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * A listener for leadership events. |
| 267 | */ |
| 268 | private class InternalLeadershipListener implements LeadershipEventListener { |
| 269 | |
| 270 | @Override |
| 271 | public void event(LeadershipEvent event) { |
| 272 | if (!event.subject().topic().equals(appId.name())) { |
| 273 | // Not our topic: ignore |
| 274 | return; |
| 275 | } |
| 276 | if (!Objects.equals(event.subject().leader(), |
| 277 | clusterService.getLocalNode().id())) { |
| 278 | // The event is not about this instance: ignore |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | switch (event.type()) { |
| 283 | case LEADER_ELECTED: |
| 284 | log.info("IntentSynchronizer gained leadership"); |
| 285 | leaderChanged(true); |
| 286 | break; |
| 287 | case LEADER_BOOTED: |
| 288 | log.info("IntentSynchronizer lost leadership"); |
| 289 | leaderChanged(false); |
| 290 | break; |
| 291 | case LEADER_REELECTED: |
| 292 | default: |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 297 | } |