blob: 73d9763893f360f80755f7de3a20b9f042745231 [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 //
Pavlin Radoslavov20be3e62014-11-25 18:52:08 -0800108 // NOTE: We don't withdraw the intents during shutdown, because
109 // it creates flux in the data plane during switchover.
110 //
111
112 /*
113 //
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800114 // Build a batch operation to withdraw all intents from this
115 // application.
116 //
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800117 log.debug("SDN-IP Intent Synchronizer shutdown: " +
118 "withdrawing all intents...");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800119 IntentOperations.Builder builder = IntentOperations.builder();
120 for (Intent intent : intentService.getIntents()) {
121 // Skip the intents from other applications
122 if (!intent.appId().equals(appId)) {
123 continue;
124 }
125
126 // Skip the intents that are already withdrawn
127 IntentState intentState =
128 intentService.getIntentState(intent.id());
129 if (intentState.equals(IntentState.WITHDRAWING) ||
130 intentState.equals(IntentState.WITHDRAWN)) {
131 continue;
132 }
133
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800134 log.debug("SDN-IP Intent Synchronizer withdrawing intent: {}",
135 intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800136 builder.addWithdrawOperation(intent.id());
137 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800138 IntentOperations intentOperations = builder.build();
139 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800140 leaderChanged(false);
141
142 peerIntents.clear();
143 routeIntents.clear();
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800144 log.debug("SDN-IP Intent Synchronizer shutdown completed");
Pavlin Radoslavov20be3e62014-11-25 18:52:08 -0800145 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800146 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800147 }
148
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800149 public void leaderChanged(boolean isLeader) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800150 log.debug("SDN-IP Leader changed: {}", isLeader);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800151
152 if (!isLeader) {
153 this.isElectedLeader = false;
154 this.isActivatedLeader = false;
155 return; // Nothing to do
156 }
157 this.isActivatedLeader = false;
158 this.isElectedLeader = true;
159
160 //
161 // Tell the Intents Synchronizer thread to start the synchronization
162 //
163 intentsSynchronizerSemaphore.release();
164 }
165
166 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800167 * Gets the route intents.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800168 *
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800169 * @return the route intents
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800170 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800171 public Collection<MultiPointToSinglePointIntent> getRouteIntents() {
172 List<MultiPointToSinglePointIntent> result = new LinkedList<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800173
174 for (Map.Entry<Ip4Prefix, MultiPointToSinglePointIntent> entry :
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800175 routeIntents.entrySet()) {
176 result.add(entry.getValue());
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800177 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800178 return result;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800179 }
180
181 /**
182 * Thread for Intent Synchronization.
183 */
184 private void doIntentSynchronizationThread() {
185 boolean interrupted = false;
186 try {
187 while (!interrupted) {
188 try {
189 intentsSynchronizerSemaphore.acquire();
190 //
191 // Drain all permits, because a single synchronization is
192 // sufficient.
193 //
194 intentsSynchronizerSemaphore.drainPermits();
195 } catch (InterruptedException e) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800196 interrupted = true;
197 break;
198 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800199 synchronizeIntents();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800200 }
201 } finally {
202 if (interrupted) {
203 Thread.currentThread().interrupt();
204 }
205 }
206 }
207
208 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800209 * Submits a collection of point-to-point intents.
210 *
211 * @param intents the intents to submit
212 */
213 void submitPeerIntents(Collection<PointToPointIntent> intents) {
214 synchronized (this) {
215 // Store the intents in memory
216 for (PointToPointIntent intent : intents) {
217 peerIntents.put(new IntentKey(intent), intent);
218 }
219
220 // Push the intents
221 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800222 log.debug("SDN-IP Submitting all Peer Intents...");
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800223 IntentOperations.Builder builder = IntentOperations.builder();
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800224 for (Intent intent : intents) {
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800225 builder.addSubmitOperation(intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800226 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800227 IntentOperations intentOperations = builder.build();
228 log.debug("SDN-IP Submitting intents: {}",
229 intentOperations.operations());
230 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800231 }
232 }
233 }
234
235 /**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800236 * Submits a multi-point-to-single-point intent.
237 *
238 * @param prefix the IPv4 matching prefix for the intent to submit
239 * @param intent the intent to submit
240 */
241 void submitRouteIntent(Ip4Prefix prefix,
242 MultiPointToSinglePointIntent intent) {
243 synchronized (this) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800244 MultiPointToSinglePointIntent oldIntent =
245 routeIntents.put(prefix, intent);
246
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800247 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800248 if (oldIntent != null) {
249 //
250 // TODO: Short-term solution to explicitly withdraw
251 // instead of using "replace" operation.
252 //
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800253 log.debug("SDN-IP Withdrawing old intent: {}", oldIntent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800254 intentService.withdraw(oldIntent);
255 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800256 log.debug("SDN-IP Submitting intent: {}", intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800257 intentService.submit(intent);
258 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800259 }
260 }
261
262 /**
263 * Withdraws a multi-point-to-single-point intent.
264 *
265 * @param prefix the IPv4 matching prefix for the intent to withdraw.
266 */
267 void withdrawRouteIntent(Ip4Prefix prefix) {
268 synchronized (this) {
269 MultiPointToSinglePointIntent intent =
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800270 routeIntents.remove(prefix);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800271
272 if (intent == null) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800273 log.debug("SDN-IP no intent in routeIntents to delete for " +
274 "prefix: {}", prefix);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800275 return;
276 }
277
278 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800279 log.debug("SDN-IP Withdrawing intent: {}", intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800280 intentService.withdraw(intent);
281 }
282 }
283 }
284
285 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800286 * Synchronize the in-memory Intents with the Intents in the Intent
287 * framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800288 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800289 void synchronizeIntents() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800290 synchronized (this) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800291
292 Map<IntentKey, Intent> localIntents = new HashMap<>();
293 Map<IntentKey, Intent> fetchedIntents = new HashMap<>();
294 Collection<Intent> storeInMemoryIntents = new LinkedList<>();
295 Collection<Intent> addIntents = new LinkedList<>();
296 Collection<Intent> deleteIntents = new LinkedList<>();
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800297 IntentOperations intentOperations;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800298
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800299 if (!isElectedLeader) {
300 return; // Nothing to do: not the leader anymore
301 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800302 log.debug("SDN-IP synchronizing all intents...");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800303
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800304 // Prepare the local intents
305 for (Intent intent : routeIntents.values()) {
306 localIntents.put(new IntentKey(intent), intent);
307 }
308 for (Intent intent : peerIntents.values()) {
309 localIntents.put(new IntentKey(intent), intent);
310 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800311
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800312 // Fetch all intents for this application
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800313 for (Intent intent : intentService.getIntents()) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800314 if (!intent.appId().equals(appId)) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800315 continue;
316 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800317 fetchedIntents.put(new IntentKey(intent), intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800318 }
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800319 if (log.isDebugEnabled()) {
320 for (Intent intent: fetchedIntents.values()) {
321 log.debug("SDN-IP Intent Synchronizer: fetched intent: {}",
322 intent);
323 }
324 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800325
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800326 computeIntentsDelta(localIntents, fetchedIntents,
327 storeInMemoryIntents, addIntents,
328 deleteIntents);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800329
330 //
331 // Perform the actions:
332 // 1. Store in memory fetched intents that are same. Can be done
333 // even if we are not the leader anymore
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800334 // 2. Delete intents: check if the leader before the operation
335 // 3. Add intents: check if the leader before the operation
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800336 //
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800337 for (Intent intent : storeInMemoryIntents) {
338 // Store the intent in memory based on its type
339 if (intent instanceof MultiPointToSinglePointIntent) {
340 MultiPointToSinglePointIntent mp2pIntent =
341 (MultiPointToSinglePointIntent) intent;
342 // Find the IP prefix
343 Criterion c =
344 mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST);
345 if (c != null && c instanceof IPCriterion) {
346 IPCriterion ipCriterion = (IPCriterion) c;
347 Ip4Prefix ip4Prefix = ipCriterion.ip().getIp4Prefix();
348 if (ip4Prefix == null) {
349 // TODO: For now we support only IPv4
350 continue;
351 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800352 log.debug("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800353 "in-memory Route Intent for prefix {}",
354 ip4Prefix);
355 routeIntents.put(ip4Prefix, mp2pIntent);
356 } else {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800357 log.warn("SDN-IP no IPV4_DST criterion found for Intent {}",
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800358 mp2pIntent.id());
359 }
360 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800361 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800362 if (intent instanceof PointToPointIntent) {
363 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800364 log.debug("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800365 "in-memory Peer Intent {}", p2pIntent);
366 peerIntents.put(new IntentKey(intent), p2pIntent);
367 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800368 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800369 }
370
371 // Withdraw Intents
372 IntentOperations.Builder builder = IntentOperations.builder();
373 for (Intent intent : deleteIntents) {
374 builder.addWithdrawOperation(intent.id());
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800375 log.debug("SDN-IP Intent Synchronizer: withdrawing intent: {}",
376 intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800377 }
378 if (!isElectedLeader) {
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800379 log.debug("SDN-IP Intent Synchronizer: cannot withdraw intents: " +
380 "not elected leader anymore");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800381 isActivatedLeader = false;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800382 return;
383 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800384 intentOperations = builder.build();
385 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800386
387 // Add Intents
388 builder = IntentOperations.builder();
389 for (Intent intent : addIntents) {
390 builder.addSubmitOperation(intent);
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800391 log.debug("SDN-IP Intent Synchronizer: submitting intent: {}",
392 intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800393 }
394 if (!isElectedLeader) {
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800395 log.debug("SDN-IP Intent Synchronizer: cannot submit intents: " +
396 "not elected leader anymore");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800397 isActivatedLeader = false;
398 return;
399 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800400 intentOperations = builder.build();
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800401 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800402
403 if (isElectedLeader) {
404 isActivatedLeader = true; // Allow push of Intents
405 } else {
406 isActivatedLeader = false;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800407 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800408 log.debug("SDN-IP intent synchronization completed");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800409 }
410 }
411
412 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800413 * Computes the delta in two sets of Intents: local in-memory Intents,
414 * and intents fetched from the Intent framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800415 *
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800416 * @param localIntents the local in-memory Intents
417 * @param fetchedIntents the Intents fetched from the Intent framework
418 * @param storeInMemoryIntents the Intents that should be stored in memory.
419 * Note: This Collection must be allocated by the caller, and it will
420 * be populated by this method.
421 * @param addIntents the Intents that should be added to the Intent
422 * framework. Note: This Collection must be allocated by the caller, and
423 * it will be populated by this method.
424 * @param deleteIntents the Intents that should be deleted from the Intent
425 * framework. Note: This Collection must be allocated by the caller, and
426 * it will be populated by this method.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800427 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800428 private void computeIntentsDelta(
429 final Map<IntentKey, Intent> localIntents,
430 final Map<IntentKey, Intent> fetchedIntents,
431 Collection<Intent> storeInMemoryIntents,
432 Collection<Intent> addIntents,
433 Collection<Intent> deleteIntents) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800434
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800435 //
436 // Compute the deltas between the LOCAL in-memory Intents and the
437 // FETCHED Intents:
438 // - If an Intent is in both the LOCAL and FETCHED sets:
439 // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then
440 // the LOCAL Intent should be added/installed; otherwise the
441 // FETCHED intent should be stored in the local memory
442 // (i.e., override the LOCAL Intent) to preserve the original
443 // Intent ID.
444 // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL
445 // Intent should be added/installed.
446 // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED
447 // Intent should be deleted/withdrawn.
448 //
449 for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) {
450 IntentKey intentKey = entry.getKey();
451 Intent localIntent = entry.getValue();
452 Intent fetchedIntent = fetchedIntents.get(intentKey);
453
454 if (fetchedIntent == null) {
455 //
456 // No FETCHED Intent found: push the LOCAL Intent.
457 //
458 addIntents.add(localIntent);
459 continue;
460 }
461
462 IntentState state =
463 intentService.getIntentState(fetchedIntent.id());
464 if (state == IntentState.WITHDRAWING ||
465 state == IntentState.WITHDRAWN) {
466 // The intent has been withdrawn but according to our route
467 // table it should be installed. We'll reinstall it.
468 addIntents.add(localIntent);
469 continue;
470 }
471 storeInMemoryIntents.add(fetchedIntent);
472 }
473
474 for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) {
475 IntentKey intentKey = entry.getKey();
476 Intent fetchedIntent = entry.getValue();
477 Intent localIntent = localIntents.get(intentKey);
478
479 if (localIntent != null) {
480 continue;
481 }
482
483 IntentState state =
484 intentService.getIntentState(fetchedIntent.id());
485 if (state == IntentState.WITHDRAWING ||
486 state == IntentState.WITHDRAWN) {
487 // Nothing to do. The intent has been already withdrawn.
488 continue;
489 }
490 //
491 // No LOCAL Intent found: delete/withdraw the FETCHED Intent.
492 //
493 deleteIntents.add(fetchedIntent);
494 }
495 }
496
497 /**
498 * Helper class that can be used to compute the key for an Intent by
499 * by excluding the Intent ID.
500 */
501 static final class IntentKey {
502 private final Intent intent;
503
504 /**
505 * Constructor.
506 *
507 * @param intent the intent to use
508 */
509 IntentKey(Intent intent) {
510 checkArgument((intent instanceof MultiPointToSinglePointIntent) ||
511 (intent instanceof PointToPointIntent),
512 "Intent type not recognized", intent);
513 this.intent = intent;
514 }
515
516 /**
517 * Compares two Multi-Point to Single-Point Intents whether they
518 * represent same logical intention.
519 *
520 * @param intent1 the first Intent to compare
521 * @param intent2 the second Intent to compare
522 * @return true if both Intents represent same logical intention,
523 * otherwise false
524 */
525 static boolean equalIntents(MultiPointToSinglePointIntent intent1,
526 MultiPointToSinglePointIntent intent2) {
527 return Objects.equals(intent1.appId(), intent2.appId()) &&
528 Objects.equals(intent1.selector(), intent2.selector()) &&
529 Objects.equals(intent1.treatment(), intent2.treatment()) &&
530 Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) &&
531 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
532 }
533
534 /**
535 * Compares two Point-to-Point Intents whether they represent
536 * same logical intention.
537 *
538 * @param intent1 the first Intent to compare
539 * @param intent2 the second Intent to compare
540 * @return true if both Intents represent same logical intention,
541 * otherwise false
542 */
543 static boolean equalIntents(PointToPointIntent intent1,
544 PointToPointIntent intent2) {
545 return Objects.equals(intent1.appId(), intent2.appId()) &&
546 Objects.equals(intent1.selector(), intent2.selector()) &&
547 Objects.equals(intent1.treatment(), intent2.treatment()) &&
548 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
549 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
550 }
551
552 @Override
553 public int hashCode() {
554 if (intent instanceof PointToPointIntent) {
555 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
556 return Objects.hash(p2pIntent.appId(),
557 p2pIntent.resources(),
558 p2pIntent.selector(),
559 p2pIntent.treatment(),
560 p2pIntent.constraints(),
561 p2pIntent.ingressPoint(),
562 p2pIntent.egressPoint());
563 }
564 if (intent instanceof MultiPointToSinglePointIntent) {
565 MultiPointToSinglePointIntent m2pIntent =
566 (MultiPointToSinglePointIntent) intent;
567 return Objects.hash(m2pIntent.appId(),
568 m2pIntent.resources(),
569 m2pIntent.selector(),
570 m2pIntent.treatment(),
571 m2pIntent.constraints(),
572 m2pIntent.ingressPoints(),
573 m2pIntent.egressPoint());
574 }
575 checkArgument(false, "Intent type not recognized", intent);
576 return 0;
577 }
578
579 @Override
580 public boolean equals(Object obj) {
581 if (this == obj) {
582 return true;
583 }
584 if ((obj == null) || (!(obj instanceof IntentKey))) {
585 return false;
586 }
587 IntentKey other = (IntentKey) obj;
588
589 if (this.intent instanceof PointToPointIntent) {
590 if (!(other.intent instanceof PointToPointIntent)) {
591 return false;
592 }
593 return equalIntents((PointToPointIntent) this.intent,
594 (PointToPointIntent) other.intent);
595 }
596 if (this.intent instanceof MultiPointToSinglePointIntent) {
597 if (!(other.intent instanceof MultiPointToSinglePointIntent)) {
598 return false;
599 }
600 return equalIntents(
601 (MultiPointToSinglePointIntent) this.intent,
602 (MultiPointToSinglePointIntent) other.intent);
603 }
604 checkArgument(false, "Intent type not recognized", intent);
605 return false;
606 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800607 }
608}