Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
| 16 | package org.onlab.onos.sdnip; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 23 | import java.util.Objects; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 24 | import java.util.concurrent.ConcurrentHashMap; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Executors; |
| 27 | import java.util.concurrent.Semaphore; |
| 28 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 29 | import org.apache.commons.lang3.tuple.Pair; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 30 | import org.onlab.onos.core.ApplicationId; |
| 31 | import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion; |
| 32 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 33 | import org.onlab.onos.net.intent.Intent; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 34 | import org.onlab.onos.net.intent.IntentOperations; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 35 | import org.onlab.onos.net.intent.IntentService; |
| 36 | import org.onlab.onos.net.intent.IntentState; |
| 37 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 38 | import org.onlab.onos.net.intent.PointToPointIntent; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 39 | import org.onlab.packet.Ip4Prefix; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 43 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 44 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 45 | import static com.google.common.base.Preconditions.checkArgument; |
| 46 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 47 | public class IntentSynchronizer { |
| 48 | private static final Logger log = |
| 49 | LoggerFactory.getLogger(IntentSynchronizer.class); |
| 50 | |
| 51 | private final ApplicationId appId; |
| 52 | private final IntentService intentService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 53 | private final Map<IntentKey, PointToPointIntent> peerIntents; |
| 54 | private final Map<Ip4Prefix, MultiPointToSinglePointIntent> routeIntents; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 55 | |
| 56 | // |
| 57 | // State to deal with SDN-IP Leader election and pushing Intents |
| 58 | // |
| 59 | private final ExecutorService bgpIntentsSynchronizerExecutor; |
| 60 | private final Semaphore intentsSynchronizerSemaphore = new Semaphore(0); |
| 61 | private volatile boolean isElectedLeader = false; |
| 62 | private volatile boolean isActivatedLeader = false; |
| 63 | |
| 64 | /** |
| 65 | * Class constructor. |
| 66 | * |
| 67 | * @param appId the Application ID |
| 68 | * @param intentService the intent service |
| 69 | */ |
| 70 | IntentSynchronizer(ApplicationId appId, IntentService intentService) { |
| 71 | this.appId = appId; |
| 72 | this.intentService = intentService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 73 | peerIntents = new ConcurrentHashMap<>(); |
| 74 | routeIntents = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 75 | |
| 76 | bgpIntentsSynchronizerExecutor = Executors.newSingleThreadExecutor( |
| 77 | new ThreadFactoryBuilder() |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 78 | .setNameFormat("sdnip-intents-synchronizer-%d").build()); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Starts the synchronizer. |
| 83 | */ |
| 84 | public void start() { |
| 85 | bgpIntentsSynchronizerExecutor.execute(new Runnable() { |
| 86 | @Override |
| 87 | public void run() { |
| 88 | doIntentSynchronizationThread(); |
| 89 | } |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Stops the synchronizer. |
| 95 | */ |
| 96 | public void stop() { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 97 | synchronized (this) { |
| 98 | // Stop the thread(s) |
| 99 | bgpIntentsSynchronizerExecutor.shutdownNow(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 100 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 101 | // |
| 102 | // Withdraw all SDN-IP intents |
| 103 | // |
| 104 | if (!isElectedLeader) { |
| 105 | return; // Nothing to do: not the leader anymore |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 106 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 107 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 108 | // |
Pavlin Radoslavov | 20be3e6 | 2014-11-25 18:52:08 -0800 | [diff] [blame] | 109 | // NOTE: We don't withdraw the intents during shutdown, because |
| 110 | // it creates flux in the data plane during switchover. |
| 111 | // |
| 112 | |
| 113 | /* |
| 114 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 115 | // Build a batch operation to withdraw all intents from this |
| 116 | // application. |
| 117 | // |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 118 | log.debug("SDN-IP Intent Synchronizer shutdown: " + |
| 119 | "withdrawing all intents..."); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 120 | IntentOperations.Builder builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 121 | for (Intent intent : intentService.getIntents()) { |
| 122 | // Skip the intents from other applications |
| 123 | if (!intent.appId().equals(appId)) { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | // Skip the intents that are already withdrawn |
| 128 | IntentState intentState = |
| 129 | intentService.getIntentState(intent.id()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 130 | if ((intentState == null) || |
| 131 | intentState.equals(IntentState.WITHDRAWING) || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 132 | intentState.equals(IntentState.WITHDRAWN)) { |
| 133 | continue; |
| 134 | } |
| 135 | |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 136 | log.trace("SDN-IP Intent Synchronizer withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 137 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 138 | builder.addWithdrawOperation(intent.id()); |
| 139 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 140 | IntentOperations intentOperations = builder.build(); |
| 141 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 142 | leaderChanged(false); |
| 143 | |
| 144 | peerIntents.clear(); |
| 145 | routeIntents.clear(); |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 146 | log.debug("SDN-IP Intent Synchronizer shutdown completed"); |
Pavlin Radoslavov | 20be3e6 | 2014-11-25 18:52:08 -0800 | [diff] [blame] | 147 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 148 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 151 | public void leaderChanged(boolean isLeader) { |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 152 | log.debug("SDN-IP Leader changed: {}", isLeader); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 153 | |
| 154 | if (!isLeader) { |
| 155 | this.isElectedLeader = false; |
| 156 | this.isActivatedLeader = false; |
| 157 | return; // Nothing to do |
| 158 | } |
| 159 | this.isActivatedLeader = false; |
| 160 | this.isElectedLeader = true; |
| 161 | |
| 162 | // |
| 163 | // Tell the Intents Synchronizer thread to start the synchronization |
| 164 | // |
| 165 | intentsSynchronizerSemaphore.release(); |
| 166 | } |
| 167 | |
| 168 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 169 | * Gets the route intents. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 170 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 171 | * @return the route intents |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 172 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 173 | public Collection<MultiPointToSinglePointIntent> getRouteIntents() { |
| 174 | List<MultiPointToSinglePointIntent> result = new LinkedList<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 175 | |
| 176 | for (Map.Entry<Ip4Prefix, MultiPointToSinglePointIntent> entry : |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 177 | routeIntents.entrySet()) { |
| 178 | result.add(entry.getValue()); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 179 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 180 | return result; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Thread for Intent Synchronization. |
| 185 | */ |
| 186 | private void doIntentSynchronizationThread() { |
| 187 | boolean interrupted = false; |
| 188 | try { |
| 189 | while (!interrupted) { |
| 190 | try { |
| 191 | intentsSynchronizerSemaphore.acquire(); |
| 192 | // |
| 193 | // Drain all permits, because a single synchronization is |
| 194 | // sufficient. |
| 195 | // |
| 196 | intentsSynchronizerSemaphore.drainPermits(); |
| 197 | } catch (InterruptedException e) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 198 | interrupted = true; |
| 199 | break; |
| 200 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 201 | synchronizeIntents(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 202 | } |
| 203 | } finally { |
| 204 | if (interrupted) { |
| 205 | Thread.currentThread().interrupt(); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 211 | * Submits a collection of point-to-point intents. |
| 212 | * |
| 213 | * @param intents the intents to submit |
| 214 | */ |
| 215 | void submitPeerIntents(Collection<PointToPointIntent> intents) { |
| 216 | synchronized (this) { |
| 217 | // Store the intents in memory |
| 218 | for (PointToPointIntent intent : intents) { |
| 219 | peerIntents.put(new IntentKey(intent), intent); |
| 220 | } |
| 221 | |
| 222 | // Push the intents |
| 223 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 224 | log.debug("SDN-IP Submitting all Peer Intents..."); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 225 | IntentOperations.Builder builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 226 | for (Intent intent : intents) { |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 227 | builder.addSubmitOperation(intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 228 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 229 | IntentOperations intentOperations = builder.build(); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 230 | log.trace("SDN-IP Submitting intents: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 231 | intentOperations.operations()); |
| 232 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 238 | * Updates multi-point-to-single-point route intents. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 239 | * |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 240 | * @param submitIntents the intents to submit |
| 241 | * @param withdrawPrefixes the IPv4 matching prefixes for the intents |
| 242 | * to withdraw |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 243 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 244 | void updateRouteIntents( |
| 245 | Collection<Pair<Ip4Prefix, MultiPointToSinglePointIntent>> submitIntents, |
| 246 | Collection<Ip4Prefix> withdrawPrefixes) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 247 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 248 | // |
| 249 | // NOTE: Semantically, we MUST withdraw existing intents before |
| 250 | // submitting new intents. |
| 251 | // |
| 252 | synchronized (this) { |
| 253 | MultiPointToSinglePointIntent intent; |
| 254 | |
| 255 | log.debug("SDN-IP submitting intents = {} withdrawing = {}", |
| 256 | submitIntents.size(), withdrawPrefixes.size()); |
| 257 | |
| 258 | // |
| 259 | // Prepare the Intent batch operations for the intents to withdraw |
| 260 | // |
| 261 | IntentOperations.Builder withdrawBuilder = |
| 262 | IntentOperations.builder(appId); |
| 263 | for (Ip4Prefix prefix : withdrawPrefixes) { |
| 264 | intent = routeIntents.remove(prefix); |
| 265 | if (intent == null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 266 | log.trace("SDN-IP No intent in routeIntents to delete " + |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 267 | "for prefix: {}", prefix); |
| 268 | continue; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 269 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 270 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 271 | log.trace("SDN-IP Withdrawing intent: {}", intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 272 | withdrawBuilder.addWithdrawOperation(intent.id()); |
| 273 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 276 | // |
| 277 | // Prepare the Intent batch operations for the intents to submit |
| 278 | // |
| 279 | IntentOperations.Builder submitBuilder = |
| 280 | IntentOperations.builder(appId); |
| 281 | for (Pair<Ip4Prefix, MultiPointToSinglePointIntent> pair : |
| 282 | submitIntents) { |
| 283 | Ip4Prefix prefix = pair.getLeft(); |
| 284 | intent = pair.getRight(); |
| 285 | MultiPointToSinglePointIntent oldIntent = |
| 286 | routeIntents.put(prefix, intent); |
| 287 | if (isElectedLeader && isActivatedLeader) { |
| 288 | if (oldIntent != null) { |
| 289 | // |
| 290 | // TODO: Short-term solution to explicitly withdraw |
| 291 | // instead of using "replace" operation. |
| 292 | // |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 293 | log.trace("SDN-IP Withdrawing old intent: {}", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 294 | oldIntent); |
| 295 | withdrawBuilder.addWithdrawOperation(oldIntent.id()); |
| 296 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 297 | log.trace("SDN-IP Submitting intent: {}", intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 298 | submitBuilder.addSubmitOperation(intent); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // |
| 303 | // Submit the Intent operations |
| 304 | // |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 305 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 306 | IntentOperations intentOperations = withdrawBuilder.build(); |
| 307 | if (!intentOperations.operations().isEmpty()) { |
| 308 | log.debug("SDN-IP Withdrawing intents executed"); |
| 309 | intentService.execute(intentOperations); |
| 310 | } |
| 311 | intentOperations = submitBuilder.build(); |
| 312 | if (!intentOperations.operations().isEmpty()) { |
| 313 | log.debug("SDN-IP Submitting intents executed"); |
| 314 | intentService.execute(intentOperations); |
| 315 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 321 | * Synchronize the in-memory Intents with the Intents in the Intent |
| 322 | * framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 323 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 324 | void synchronizeIntents() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 325 | synchronized (this) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 326 | |
| 327 | Map<IntentKey, Intent> localIntents = new HashMap<>(); |
| 328 | Map<IntentKey, Intent> fetchedIntents = new HashMap<>(); |
| 329 | Collection<Intent> storeInMemoryIntents = new LinkedList<>(); |
| 330 | Collection<Intent> addIntents = new LinkedList<>(); |
| 331 | Collection<Intent> deleteIntents = new LinkedList<>(); |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 332 | IntentOperations intentOperations; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 333 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 334 | if (!isElectedLeader) { |
| 335 | return; // Nothing to do: not the leader anymore |
| 336 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 337 | log.debug("SDN-IP synchronizing all intents..."); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 338 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 339 | // Prepare the local intents |
| 340 | for (Intent intent : routeIntents.values()) { |
| 341 | localIntents.put(new IntentKey(intent), intent); |
| 342 | } |
| 343 | for (Intent intent : peerIntents.values()) { |
| 344 | localIntents.put(new IntentKey(intent), intent); |
| 345 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 346 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 347 | // Fetch all intents for this application |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 348 | for (Intent intent : intentService.getIntents()) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 349 | if (!intent.appId().equals(appId)) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 350 | continue; |
| 351 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 352 | fetchedIntents.put(new IntentKey(intent), intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 353 | } |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 354 | if (log.isDebugEnabled()) { |
| 355 | for (Intent intent: fetchedIntents.values()) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 356 | log.trace("SDN-IP Intent Synchronizer: fetched intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 357 | intent); |
| 358 | } |
| 359 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 360 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 361 | computeIntentsDelta(localIntents, fetchedIntents, |
| 362 | storeInMemoryIntents, addIntents, |
| 363 | deleteIntents); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 364 | |
| 365 | // |
| 366 | // Perform the actions: |
| 367 | // 1. Store in memory fetched intents that are same. Can be done |
| 368 | // even if we are not the leader anymore |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 369 | // 2. Delete intents: check if the leader before the operation |
| 370 | // 3. Add intents: check if the leader before the operation |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 371 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 372 | for (Intent intent : storeInMemoryIntents) { |
| 373 | // Store the intent in memory based on its type |
| 374 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 375 | MultiPointToSinglePointIntent mp2pIntent = |
| 376 | (MultiPointToSinglePointIntent) intent; |
| 377 | // Find the IP prefix |
| 378 | Criterion c = |
| 379 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST); |
| 380 | if (c != null && c instanceof IPCriterion) { |
| 381 | IPCriterion ipCriterion = (IPCriterion) c; |
| 382 | Ip4Prefix ip4Prefix = ipCriterion.ip().getIp4Prefix(); |
| 383 | if (ip4Prefix == null) { |
| 384 | // TODO: For now we support only IPv4 |
| 385 | continue; |
| 386 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 387 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 388 | "in-memory Route Intent for prefix {}", |
| 389 | ip4Prefix); |
| 390 | routeIntents.put(ip4Prefix, mp2pIntent); |
| 391 | } else { |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 392 | log.warn("SDN-IP no IPV4_DST criterion found for Intent {}", |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 393 | mp2pIntent.id()); |
| 394 | } |
| 395 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 396 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 397 | if (intent instanceof PointToPointIntent) { |
| 398 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 399 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 400 | "in-memory Peer Intent {}", p2pIntent); |
| 401 | peerIntents.put(new IntentKey(intent), p2pIntent); |
| 402 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 403 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // Withdraw Intents |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 407 | IntentOperations.Builder builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 408 | for (Intent intent : deleteIntents) { |
| 409 | builder.addWithdrawOperation(intent.id()); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 410 | log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 411 | intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 412 | } |
| 413 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 414 | log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 415 | "not elected leader anymore"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 416 | isActivatedLeader = false; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 417 | return; |
| 418 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 419 | intentOperations = builder.build(); |
| 420 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 421 | |
| 422 | // Add Intents |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 423 | builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 424 | for (Intent intent : addIntents) { |
| 425 | builder.addSubmitOperation(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 426 | log.trace("SDN-IP Intent Synchronizer: submitting intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 427 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 428 | } |
| 429 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame^] | 430 | log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 431 | "not elected leader anymore"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 432 | isActivatedLeader = false; |
| 433 | return; |
| 434 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 435 | intentOperations = builder.build(); |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 436 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 437 | |
| 438 | if (isElectedLeader) { |
| 439 | isActivatedLeader = true; // Allow push of Intents |
| 440 | } else { |
| 441 | isActivatedLeader = false; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 442 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 443 | log.debug("SDN-IP intent synchronization completed"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 448 | * Computes the delta in two sets of Intents: local in-memory Intents, |
| 449 | * and intents fetched from the Intent framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 450 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 451 | * @param localIntents the local in-memory Intents |
| 452 | * @param fetchedIntents the Intents fetched from the Intent framework |
| 453 | * @param storeInMemoryIntents the Intents that should be stored in memory. |
| 454 | * Note: This Collection must be allocated by the caller, and it will |
| 455 | * be populated by this method. |
| 456 | * @param addIntents the Intents that should be added to the Intent |
| 457 | * framework. Note: This Collection must be allocated by the caller, and |
| 458 | * it will be populated by this method. |
| 459 | * @param deleteIntents the Intents that should be deleted from the Intent |
| 460 | * framework. Note: This Collection must be allocated by the caller, and |
| 461 | * it will be populated by this method. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 462 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 463 | private void computeIntentsDelta( |
| 464 | final Map<IntentKey, Intent> localIntents, |
| 465 | final Map<IntentKey, Intent> fetchedIntents, |
| 466 | Collection<Intent> storeInMemoryIntents, |
| 467 | Collection<Intent> addIntents, |
| 468 | Collection<Intent> deleteIntents) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 469 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 470 | // |
| 471 | // Compute the deltas between the LOCAL in-memory Intents and the |
| 472 | // FETCHED Intents: |
| 473 | // - If an Intent is in both the LOCAL and FETCHED sets: |
| 474 | // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then |
| 475 | // the LOCAL Intent should be added/installed; otherwise the |
| 476 | // FETCHED intent should be stored in the local memory |
| 477 | // (i.e., override the LOCAL Intent) to preserve the original |
| 478 | // Intent ID. |
| 479 | // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL |
| 480 | // Intent should be added/installed. |
| 481 | // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED |
| 482 | // Intent should be deleted/withdrawn. |
| 483 | // |
| 484 | for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) { |
| 485 | IntentKey intentKey = entry.getKey(); |
| 486 | Intent localIntent = entry.getValue(); |
| 487 | Intent fetchedIntent = fetchedIntents.get(intentKey); |
| 488 | |
| 489 | if (fetchedIntent == null) { |
| 490 | // |
| 491 | // No FETCHED Intent found: push the LOCAL Intent. |
| 492 | // |
| 493 | addIntents.add(localIntent); |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | IntentState state = |
| 498 | intentService.getIntentState(fetchedIntent.id()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 499 | if (state == null || |
| 500 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 501 | state == IntentState.WITHDRAWN) { |
| 502 | // The intent has been withdrawn but according to our route |
| 503 | // table it should be installed. We'll reinstall it. |
| 504 | addIntents.add(localIntent); |
| 505 | continue; |
| 506 | } |
| 507 | storeInMemoryIntents.add(fetchedIntent); |
| 508 | } |
| 509 | |
| 510 | for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) { |
| 511 | IntentKey intentKey = entry.getKey(); |
| 512 | Intent fetchedIntent = entry.getValue(); |
| 513 | Intent localIntent = localIntents.get(intentKey); |
| 514 | |
| 515 | if (localIntent != null) { |
| 516 | continue; |
| 517 | } |
| 518 | |
| 519 | IntentState state = |
| 520 | intentService.getIntentState(fetchedIntent.id()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 521 | if (state == null || |
| 522 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 523 | state == IntentState.WITHDRAWN) { |
| 524 | // Nothing to do. The intent has been already withdrawn. |
| 525 | continue; |
| 526 | } |
| 527 | // |
| 528 | // No LOCAL Intent found: delete/withdraw the FETCHED Intent. |
| 529 | // |
| 530 | deleteIntents.add(fetchedIntent); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Helper class that can be used to compute the key for an Intent by |
| 536 | * by excluding the Intent ID. |
| 537 | */ |
| 538 | static final class IntentKey { |
| 539 | private final Intent intent; |
| 540 | |
| 541 | /** |
| 542 | * Constructor. |
| 543 | * |
| 544 | * @param intent the intent to use |
| 545 | */ |
| 546 | IntentKey(Intent intent) { |
| 547 | checkArgument((intent instanceof MultiPointToSinglePointIntent) || |
| 548 | (intent instanceof PointToPointIntent), |
| 549 | "Intent type not recognized", intent); |
| 550 | this.intent = intent; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Compares two Multi-Point to Single-Point Intents whether they |
| 555 | * represent same logical intention. |
| 556 | * |
| 557 | * @param intent1 the first Intent to compare |
| 558 | * @param intent2 the second Intent to compare |
| 559 | * @return true if both Intents represent same logical intention, |
| 560 | * otherwise false |
| 561 | */ |
| 562 | static boolean equalIntents(MultiPointToSinglePointIntent intent1, |
| 563 | MultiPointToSinglePointIntent intent2) { |
| 564 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 565 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 566 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 567 | Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) && |
| 568 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Compares two Point-to-Point Intents whether they represent |
| 573 | * same logical intention. |
| 574 | * |
| 575 | * @param intent1 the first Intent to compare |
| 576 | * @param intent2 the second Intent to compare |
| 577 | * @return true if both Intents represent same logical intention, |
| 578 | * otherwise false |
| 579 | */ |
| 580 | static boolean equalIntents(PointToPointIntent intent1, |
| 581 | PointToPointIntent intent2) { |
| 582 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 583 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 584 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 585 | Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) && |
| 586 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public int hashCode() { |
| 591 | if (intent instanceof PointToPointIntent) { |
| 592 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
| 593 | return Objects.hash(p2pIntent.appId(), |
| 594 | p2pIntent.resources(), |
| 595 | p2pIntent.selector(), |
| 596 | p2pIntent.treatment(), |
| 597 | p2pIntent.constraints(), |
| 598 | p2pIntent.ingressPoint(), |
| 599 | p2pIntent.egressPoint()); |
| 600 | } |
| 601 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 602 | MultiPointToSinglePointIntent m2pIntent = |
| 603 | (MultiPointToSinglePointIntent) intent; |
| 604 | return Objects.hash(m2pIntent.appId(), |
| 605 | m2pIntent.resources(), |
| 606 | m2pIntent.selector(), |
| 607 | m2pIntent.treatment(), |
| 608 | m2pIntent.constraints(), |
| 609 | m2pIntent.ingressPoints(), |
| 610 | m2pIntent.egressPoint()); |
| 611 | } |
| 612 | checkArgument(false, "Intent type not recognized", intent); |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | @Override |
| 617 | public boolean equals(Object obj) { |
| 618 | if (this == obj) { |
| 619 | return true; |
| 620 | } |
| 621 | if ((obj == null) || (!(obj instanceof IntentKey))) { |
| 622 | return false; |
| 623 | } |
| 624 | IntentKey other = (IntentKey) obj; |
| 625 | |
| 626 | if (this.intent instanceof PointToPointIntent) { |
| 627 | if (!(other.intent instanceof PointToPointIntent)) { |
| 628 | return false; |
| 629 | } |
| 630 | return equalIntents((PointToPointIntent) this.intent, |
| 631 | (PointToPointIntent) other.intent); |
| 632 | } |
| 633 | if (this.intent instanceof MultiPointToSinglePointIntent) { |
| 634 | if (!(other.intent instanceof MultiPointToSinglePointIntent)) { |
| 635 | return false; |
| 636 | } |
| 637 | return equalIntents( |
| 638 | (MultiPointToSinglePointIntent) this.intent, |
| 639 | (MultiPointToSinglePointIntent) other.intent); |
| 640 | } |
| 641 | checkArgument(false, "Intent type not recognized", intent); |
| 642 | return false; |
| 643 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 644 | } |
| 645 | } |