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