blob: 727c8ee60e4184b1c180a40487ebc8a4c7e6d576 [file] [log] [blame]
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -08001/*
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 */
16package org.onlab.onos.sdnip;
17
18import java.util.Collection;
19import java.util.HashMap;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080023import java.util.Objects;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080024import java.util.concurrent.ConcurrentHashMap;
25import java.util.concurrent.ExecutorService;
26import java.util.concurrent.Executors;
27import java.util.concurrent.Semaphore;
28
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080029import org.onlab.onos.core.ApplicationId;
30import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
31import org.onlab.onos.net.flow.criteria.Criterion;
32import org.onlab.onos.net.intent.Intent;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080033import org.onlab.onos.net.intent.IntentOperations;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080034import org.onlab.onos.net.intent.IntentService;
35import org.onlab.onos.net.intent.IntentState;
36import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080037import org.onlab.onos.net.intent.PointToPointIntent;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080038import org.onlab.packet.Ip4Prefix;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080042import com.google.common.util.concurrent.ThreadFactoryBuilder;
43
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080044import static com.google.common.base.Preconditions.checkArgument;
45
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080046public 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 Radoslavova7243cc2014-11-22 21:38:02 -080052 private final Map<IntentKey, PointToPointIntent> peerIntents;
53 private final Map<Ip4Prefix, MultiPointToSinglePointIntent> routeIntents;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080054
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 Radoslavova7243cc2014-11-22 21:38:02 -080072 peerIntents = new ConcurrentHashMap<>();
73 routeIntents = new ConcurrentHashMap<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080074
75 bgpIntentsSynchronizerExecutor = Executors.newSingleThreadExecutor(
76 new ThreadFactoryBuilder()
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080077 .setNameFormat("sdnip-intents-synchronizer-%d").build());
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080078 }
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 Radoslavova7243cc2014-11-22 21:38:02 -080096 synchronized (this) {
97 // Stop the thread(s)
98 bgpIntentsSynchronizerExecutor.shutdownNow();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080099
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800100 //
101 // Withdraw all SDN-IP intents
102 //
103 if (!isElectedLeader) {
104 return; // Nothing to do: not the leader anymore
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800105 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800106
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800107 //
108 // Build a batch operation to withdraw all intents from this
109 // application.
110 //
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800111 log.debug("SDN-IP Intent Synchronizer shutdown: " +
112 "withdrawing all intents...");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800113 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 Radoslavov93ae8322014-11-24 20:54:36 -0800128 log.debug("SDN-IP Intent Synchronizer withdrawing intent: {}",
129 intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800130 builder.addWithdrawOperation(intent.id());
131 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800132 IntentOperations intentOperations = builder.build();
133 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800134 leaderChanged(false);
135
136 peerIntents.clear();
137 routeIntents.clear();
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800138 log.debug("SDN-IP Intent Synchronizer shutdown completed");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800139 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800140 }
141
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800142 public void leaderChanged(boolean isLeader) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800143 log.debug("SDN-IP Leader changed: {}", isLeader);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800144
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 Radoslavova7243cc2014-11-22 21:38:02 -0800160 * Gets the route intents.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800161 *
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800162 * @return the route intents
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800163 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800164 public Collection<MultiPointToSinglePointIntent> getRouteIntents() {
165 List<MultiPointToSinglePointIntent> result = new LinkedList<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800166
167 for (Map.Entry<Ip4Prefix, MultiPointToSinglePointIntent> entry :
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800168 routeIntents.entrySet()) {
169 result.add(entry.getValue());
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800170 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800171 return result;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800172 }
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 Radoslavova071b1e2014-11-17 13:37:57 -0800189 interrupted = true;
190 break;
191 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800192 synchronizeIntents();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800193 }
194 } finally {
195 if (interrupted) {
196 Thread.currentThread().interrupt();
197 }
198 }
199 }
200
201 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800202 * Submits a collection of point-to-point intents.
203 *
204 * @param intents the intents to submit
205 */
206 void submitPeerIntents(Collection<PointToPointIntent> intents) {
207 synchronized (this) {
208 // Store the intents in memory
209 for (PointToPointIntent intent : intents) {
210 peerIntents.put(new IntentKey(intent), intent);
211 }
212
213 // Push the intents
214 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800215 log.debug("SDN-IP Submitting all Peer Intents...");
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800216 IntentOperations.Builder builder = IntentOperations.builder();
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800217 for (Intent intent : intents) {
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800218 builder.addSubmitOperation(intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800219 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800220 IntentOperations intentOperations = builder.build();
221 log.debug("SDN-IP Submitting intents: {}",
222 intentOperations.operations());
223 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800224 }
225 }
226 }
227
228 /**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800229 * Submits a multi-point-to-single-point intent.
230 *
231 * @param prefix the IPv4 matching prefix for the intent to submit
232 * @param intent the intent to submit
233 */
234 void submitRouteIntent(Ip4Prefix prefix,
235 MultiPointToSinglePointIntent intent) {
236 synchronized (this) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800237 MultiPointToSinglePointIntent oldIntent =
238 routeIntents.put(prefix, intent);
239
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800240 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800241 if (oldIntent != null) {
242 //
243 // TODO: Short-term solution to explicitly withdraw
244 // instead of using "replace" operation.
245 //
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800246 log.debug("SDN-IP Withdrawing old intent: {}", oldIntent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800247 intentService.withdraw(oldIntent);
248 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800249 log.debug("SDN-IP Submitting intent: {}", intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800250 intentService.submit(intent);
251 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800252 }
253 }
254
255 /**
256 * Withdraws a multi-point-to-single-point intent.
257 *
258 * @param prefix the IPv4 matching prefix for the intent to withdraw.
259 */
260 void withdrawRouteIntent(Ip4Prefix prefix) {
261 synchronized (this) {
262 MultiPointToSinglePointIntent intent =
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800263 routeIntents.remove(prefix);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800264
265 if (intent == null) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800266 log.debug("SDN-IP no intent in routeIntents to delete for " +
267 "prefix: {}", prefix);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800268 return;
269 }
270
271 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800272 log.debug("SDN-IP Withdrawing intent: {}", intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800273 intentService.withdraw(intent);
274 }
275 }
276 }
277
278 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800279 * Synchronize the in-memory Intents with the Intents in the Intent
280 * framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800281 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800282 void synchronizeIntents() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800283 synchronized (this) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800284
285 Map<IntentKey, Intent> localIntents = new HashMap<>();
286 Map<IntentKey, Intent> fetchedIntents = new HashMap<>();
287 Collection<Intent> storeInMemoryIntents = new LinkedList<>();
288 Collection<Intent> addIntents = new LinkedList<>();
289 Collection<Intent> deleteIntents = new LinkedList<>();
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800290 IntentOperations intentOperations;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800291
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800292 if (!isElectedLeader) {
293 return; // Nothing to do: not the leader anymore
294 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800295 log.debug("SDN-IP synchronizing all intents...");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800296
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800297 // Prepare the local intents
298 for (Intent intent : routeIntents.values()) {
299 localIntents.put(new IntentKey(intent), intent);
300 }
301 for (Intent intent : peerIntents.values()) {
302 localIntents.put(new IntentKey(intent), intent);
303 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800304
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800305 // Fetch all intents for this application
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800306 for (Intent intent : intentService.getIntents()) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800307 if (!intent.appId().equals(appId)) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800308 continue;
309 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800310 fetchedIntents.put(new IntentKey(intent), intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800311 }
312
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800313 computeIntentsDelta(localIntents, fetchedIntents,
314 storeInMemoryIntents, addIntents,
315 deleteIntents);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800316
317 //
318 // Perform the actions:
319 // 1. Store in memory fetched intents that are same. Can be done
320 // even if we are not the leader anymore
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800321 // 2. Delete intents: check if the leader before the operation
322 // 3. Add intents: check if the leader before the operation
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800323 //
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800324 for (Intent intent : storeInMemoryIntents) {
325 // Store the intent in memory based on its type
326 if (intent instanceof MultiPointToSinglePointIntent) {
327 MultiPointToSinglePointIntent mp2pIntent =
328 (MultiPointToSinglePointIntent) intent;
329 // Find the IP prefix
330 Criterion c =
331 mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST);
332 if (c != null && c instanceof IPCriterion) {
333 IPCriterion ipCriterion = (IPCriterion) c;
334 Ip4Prefix ip4Prefix = ipCriterion.ip().getIp4Prefix();
335 if (ip4Prefix == null) {
336 // TODO: For now we support only IPv4
337 continue;
338 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800339 log.debug("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800340 "in-memory Route Intent for prefix {}",
341 ip4Prefix);
342 routeIntents.put(ip4Prefix, mp2pIntent);
343 } else {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800344 log.warn("SDN-IP no IPV4_DST criterion found for Intent {}",
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800345 mp2pIntent.id());
346 }
347 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800348 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800349 if (intent instanceof PointToPointIntent) {
350 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800351 log.debug("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800352 "in-memory Peer Intent {}", p2pIntent);
353 peerIntents.put(new IntentKey(intent), p2pIntent);
354 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800355 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800356 }
357
358 // Withdraw Intents
359 IntentOperations.Builder builder = IntentOperations.builder();
360 for (Intent intent : deleteIntents) {
361 builder.addWithdrawOperation(intent.id());
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800362 log.debug("SDN-IP Intent Synchronizer: withdrawing intent: {}",
363 intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800364 }
365 if (!isElectedLeader) {
366 isActivatedLeader = false;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800367 return;
368 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800369 intentOperations = builder.build();
370 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800371
372 // Add Intents
373 builder = IntentOperations.builder();
374 for (Intent intent : addIntents) {
375 builder.addSubmitOperation(intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800376 }
377 if (!isElectedLeader) {
378 isActivatedLeader = false;
379 return;
380 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800381 intentOperations = builder.build();
382 log.debug("SDN-IP Intent Synchronizer: submitting intents: {}",
383 intentOperations);
384 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800385
386 if (isElectedLeader) {
387 isActivatedLeader = true; // Allow push of Intents
388 } else {
389 isActivatedLeader = false;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800390 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800391 log.debug("SDN-IP intent synchronization completed");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800392 }
393 }
394
395 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800396 * Computes the delta in two sets of Intents: local in-memory Intents,
397 * and intents fetched from the Intent framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800398 *
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800399 * @param localIntents the local in-memory Intents
400 * @param fetchedIntents the Intents fetched from the Intent framework
401 * @param storeInMemoryIntents the Intents that should be stored in memory.
402 * Note: This Collection must be allocated by the caller, and it will
403 * be populated by this method.
404 * @param addIntents the Intents that should be added to the Intent
405 * framework. Note: This Collection must be allocated by the caller, and
406 * it will be populated by this method.
407 * @param deleteIntents the Intents that should be deleted from the Intent
408 * framework. Note: This Collection must be allocated by the caller, and
409 * it will be populated by this method.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800410 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800411 private void computeIntentsDelta(
412 final Map<IntentKey, Intent> localIntents,
413 final Map<IntentKey, Intent> fetchedIntents,
414 Collection<Intent> storeInMemoryIntents,
415 Collection<Intent> addIntents,
416 Collection<Intent> deleteIntents) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800417
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800418 //
419 // Compute the deltas between the LOCAL in-memory Intents and the
420 // FETCHED Intents:
421 // - If an Intent is in both the LOCAL and FETCHED sets:
422 // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then
423 // the LOCAL Intent should be added/installed; otherwise the
424 // FETCHED intent should be stored in the local memory
425 // (i.e., override the LOCAL Intent) to preserve the original
426 // Intent ID.
427 // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL
428 // Intent should be added/installed.
429 // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED
430 // Intent should be deleted/withdrawn.
431 //
432 for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) {
433 IntentKey intentKey = entry.getKey();
434 Intent localIntent = entry.getValue();
435 Intent fetchedIntent = fetchedIntents.get(intentKey);
436
437 if (fetchedIntent == null) {
438 //
439 // No FETCHED Intent found: push the LOCAL Intent.
440 //
441 addIntents.add(localIntent);
442 continue;
443 }
444
445 IntentState state =
446 intentService.getIntentState(fetchedIntent.id());
447 if (state == IntentState.WITHDRAWING ||
448 state == IntentState.WITHDRAWN) {
449 // The intent has been withdrawn but according to our route
450 // table it should be installed. We'll reinstall it.
451 addIntents.add(localIntent);
452 continue;
453 }
454 storeInMemoryIntents.add(fetchedIntent);
455 }
456
457 for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) {
458 IntentKey intentKey = entry.getKey();
459 Intent fetchedIntent = entry.getValue();
460 Intent localIntent = localIntents.get(intentKey);
461
462 if (localIntent != null) {
463 continue;
464 }
465
466 IntentState state =
467 intentService.getIntentState(fetchedIntent.id());
468 if (state == IntentState.WITHDRAWING ||
469 state == IntentState.WITHDRAWN) {
470 // Nothing to do. The intent has been already withdrawn.
471 continue;
472 }
473 //
474 // No LOCAL Intent found: delete/withdraw the FETCHED Intent.
475 //
476 deleteIntents.add(fetchedIntent);
477 }
478 }
479
480 /**
481 * Helper class that can be used to compute the key for an Intent by
482 * by excluding the Intent ID.
483 */
484 static final class IntentKey {
485 private final Intent intent;
486
487 /**
488 * Constructor.
489 *
490 * @param intent the intent to use
491 */
492 IntentKey(Intent intent) {
493 checkArgument((intent instanceof MultiPointToSinglePointIntent) ||
494 (intent instanceof PointToPointIntent),
495 "Intent type not recognized", intent);
496 this.intent = intent;
497 }
498
499 /**
500 * Compares two Multi-Point to Single-Point Intents whether they
501 * represent same logical intention.
502 *
503 * @param intent1 the first Intent to compare
504 * @param intent2 the second Intent to compare
505 * @return true if both Intents represent same logical intention,
506 * otherwise false
507 */
508 static boolean equalIntents(MultiPointToSinglePointIntent intent1,
509 MultiPointToSinglePointIntent intent2) {
510 return Objects.equals(intent1.appId(), intent2.appId()) &&
511 Objects.equals(intent1.selector(), intent2.selector()) &&
512 Objects.equals(intent1.treatment(), intent2.treatment()) &&
513 Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) &&
514 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
515 }
516
517 /**
518 * Compares two Point-to-Point Intents whether they represent
519 * same logical intention.
520 *
521 * @param intent1 the first Intent to compare
522 * @param intent2 the second Intent to compare
523 * @return true if both Intents represent same logical intention,
524 * otherwise false
525 */
526 static boolean equalIntents(PointToPointIntent intent1,
527 PointToPointIntent intent2) {
528 return Objects.equals(intent1.appId(), intent2.appId()) &&
529 Objects.equals(intent1.selector(), intent2.selector()) &&
530 Objects.equals(intent1.treatment(), intent2.treatment()) &&
531 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
532 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
533 }
534
535 @Override
536 public int hashCode() {
537 if (intent instanceof PointToPointIntent) {
538 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
539 return Objects.hash(p2pIntent.appId(),
540 p2pIntent.resources(),
541 p2pIntent.selector(),
542 p2pIntent.treatment(),
543 p2pIntent.constraints(),
544 p2pIntent.ingressPoint(),
545 p2pIntent.egressPoint());
546 }
547 if (intent instanceof MultiPointToSinglePointIntent) {
548 MultiPointToSinglePointIntent m2pIntent =
549 (MultiPointToSinglePointIntent) intent;
550 return Objects.hash(m2pIntent.appId(),
551 m2pIntent.resources(),
552 m2pIntent.selector(),
553 m2pIntent.treatment(),
554 m2pIntent.constraints(),
555 m2pIntent.ingressPoints(),
556 m2pIntent.egressPoint());
557 }
558 checkArgument(false, "Intent type not recognized", intent);
559 return 0;
560 }
561
562 @Override
563 public boolean equals(Object obj) {
564 if (this == obj) {
565 return true;
566 }
567 if ((obj == null) || (!(obj instanceof IntentKey))) {
568 return false;
569 }
570 IntentKey other = (IntentKey) obj;
571
572 if (this.intent instanceof PointToPointIntent) {
573 if (!(other.intent instanceof PointToPointIntent)) {
574 return false;
575 }
576 return equalIntents((PointToPointIntent) this.intent,
577 (PointToPointIntent) other.intent);
578 }
579 if (this.intent instanceof MultiPointToSinglePointIntent) {
580 if (!(other.intent instanceof MultiPointToSinglePointIntent)) {
581 return false;
582 }
583 return equalIntents(
584 (MultiPointToSinglePointIntent) this.intent,
585 (MultiPointToSinglePointIntent) other.intent);
586 }
587 checkArgument(false, "Intent type not recognized", intent);
588 return false;
589 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800590 }
591}