blob: c51d7d3ca0467d81104f8d3eb674b71522b9efbd [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080017
Jonathan Hart552e31f2015-02-06 11:11:59 -080018import com.google.common.util.concurrent.ThreadFactoryBuilder;
19import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.IpAddress;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080022import org.onlab.packet.IpPrefix;
Jonathan Hart552e31f2015-02-06 11:11:59 -080023import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.ApplicationId;
Jonathan Hart552e31f2015-02-06 11:11:59 -080026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.flow.DefaultTrafficSelector;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.TrafficSelector;
30import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.criteria.Criteria.IPCriterion;
32import org.onosproject.net.flow.criteria.Criterion;
33import org.onosproject.net.intent.Intent;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.intent.IntentService;
35import org.onosproject.net.intent.IntentState;
36import org.onosproject.net.intent.MultiPointToSinglePointIntent;
37import org.onosproject.net.intent.PointToPointIntent;
Jonathan Hart41349e92015-02-09 14:14:02 -080038import org.onosproject.routingapi.FibListener;
39import org.onosproject.routingapi.FibUpdate;
Jonathan Hart552e31f2015-02-06 11:11:59 -080040import org.onosproject.sdnip.config.BgpPeer;
41import org.onosproject.sdnip.config.Interface;
42import org.onosproject.sdnip.config.SdnIpConfigurationService;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
Jonathan Hart552e31f2015-02-06 11:11:59 -080046import java.util.Collection;
47import java.util.HashMap;
48import java.util.HashSet;
49import java.util.LinkedList;
50import java.util.List;
51import java.util.Map;
52import java.util.Objects;
53import java.util.Set;
54import java.util.concurrent.ConcurrentHashMap;
55import java.util.concurrent.ExecutorService;
56import java.util.concurrent.Executors;
57import java.util.concurrent.Semaphore;
58
59import static com.google.common.base.Preconditions.checkArgument;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080060
Jonathan Hart51372182014-12-03 21:32:34 -080061/**
62 * Synchronizes intents between the in-memory intent store and the
63 * IntentService.
64 */
Jonathan Hart552e31f2015-02-06 11:11:59 -080065public class IntentSynchronizer implements FibListener {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080066 private static final Logger log =
67 LoggerFactory.getLogger(IntentSynchronizer.class);
68
69 private final ApplicationId appId;
70 private final IntentService intentService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080071 private final Map<IntentKey, PointToPointIntent> peerIntents;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080072 private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080073
74 //
75 // State to deal with SDN-IP Leader election and pushing Intents
76 //
77 private final ExecutorService bgpIntentsSynchronizerExecutor;
78 private final Semaphore intentsSynchronizerSemaphore = new Semaphore(0);
79 private volatile boolean isElectedLeader = false;
80 private volatile boolean isActivatedLeader = false;
81
Jonathan Hart552e31f2015-02-06 11:11:59 -080082 private final SdnIpConfigurationService configService;
83 private final InterfaceService interfaceService;
84
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080085 /**
86 * Class constructor.
87 *
88 * @param appId the Application ID
89 * @param intentService the intent service
Jonathan Hart552e31f2015-02-06 11:11:59 -080090 * @param configService the SDN-IP configuration service
91 * @param interfaceService the interface service
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080092 */
Jonathan Hart552e31f2015-02-06 11:11:59 -080093 IntentSynchronizer(ApplicationId appId, IntentService intentService,
94 SdnIpConfigurationService configService,
95 InterfaceService interfaceService) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080096 this.appId = appId;
97 this.intentService = intentService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080098 peerIntents = new ConcurrentHashMap<>();
99 routeIntents = new ConcurrentHashMap<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800100
Jonathan Hart552e31f2015-02-06 11:11:59 -0800101 this.configService = configService;
102 this.interfaceService = interfaceService;
103
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800104 bgpIntentsSynchronizerExecutor = Executors.newSingleThreadExecutor(
105 new ThreadFactoryBuilder()
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800106 .setNameFormat("sdnip-intents-synchronizer-%d").build());
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800107 }
108
109 /**
110 * Starts the synchronizer.
111 */
112 public void start() {
113 bgpIntentsSynchronizerExecutor.execute(new Runnable() {
114 @Override
115 public void run() {
116 doIntentSynchronizationThread();
117 }
118 });
119 }
120
121 /**
122 * Stops the synchronizer.
123 */
124 public void stop() {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800125 synchronized (this) {
126 // Stop the thread(s)
127 bgpIntentsSynchronizerExecutor.shutdownNow();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800128
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800129 //
130 // Withdraw all SDN-IP intents
131 //
132 if (!isElectedLeader) {
133 return; // Nothing to do: not the leader anymore
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800134 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800135
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800136 //
Pavlin Radoslavov20be3e62014-11-25 18:52:08 -0800137 // NOTE: We don't withdraw the intents during shutdown, because
138 // it creates flux in the data plane during switchover.
139 //
140
141 /*
142 //
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800143 // Build a batch operation to withdraw all intents from this
144 // application.
145 //
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800146 log.debug("SDN-IP Intent Synchronizer shutdown: " +
147 "withdrawing all intents...");
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800148 IntentOperations.Builder builder = IntentOperations.builder(appId);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800149 for (Intent intent : intentService.getIntents()) {
150 // Skip the intents from other applications
151 if (!intent.appId().equals(appId)) {
152 continue;
153 }
154
155 // Skip the intents that are already withdrawn
156 IntentState intentState =
157 intentService.getIntentState(intent.id());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800158 if ((intentState == null) ||
159 intentState.equals(IntentState.WITHDRAWING) ||
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800160 intentState.equals(IntentState.WITHDRAWN)) {
161 continue;
162 }
163
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800164 log.trace("SDN-IP Intent Synchronizer withdrawing intent: {}",
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800165 intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800166 builder.addWithdrawOperation(intent.id());
167 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800168 IntentOperations intentOperations = builder.build();
169 intentService.execute(intentOperations);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800170 leaderChanged(false);
171
172 peerIntents.clear();
173 routeIntents.clear();
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800174 log.debug("SDN-IP Intent Synchronizer shutdown completed");
Pavlin Radoslavov20be3e62014-11-25 18:52:08 -0800175 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800176 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800177 }
178
Jonathan Hart51372182014-12-03 21:32:34 -0800179 /**
180 * Signals the synchronizer that the SDN-IP leadership has changed.
181 *
182 * @param isLeader true if this instance is now the leader, otherwise false
183 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800184 public void leaderChanged(boolean isLeader) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800185 log.debug("SDN-IP Leader changed: {}", isLeader);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800186
187 if (!isLeader) {
188 this.isElectedLeader = false;
189 this.isActivatedLeader = false;
190 return; // Nothing to do
191 }
192 this.isActivatedLeader = false;
193 this.isElectedLeader = true;
194
195 //
196 // Tell the Intents Synchronizer thread to start the synchronization
197 //
198 intentsSynchronizerSemaphore.release();
199 }
200
201 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800202 * Gets the route intents.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800203 *
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800204 * @return the route intents
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800205 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800206 public Collection<MultiPointToSinglePointIntent> getRouteIntents() {
207 List<MultiPointToSinglePointIntent> result = new LinkedList<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800208
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800209 for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry :
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800210 routeIntents.entrySet()) {
211 result.add(entry.getValue());
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800212 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800213 return result;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800214 }
215
216 /**
217 * Thread for Intent Synchronization.
218 */
219 private void doIntentSynchronizationThread() {
220 boolean interrupted = false;
221 try {
222 while (!interrupted) {
223 try {
224 intentsSynchronizerSemaphore.acquire();
225 //
226 // Drain all permits, because a single synchronization is
227 // sufficient.
228 //
229 intentsSynchronizerSemaphore.drainPermits();
230 } catch (InterruptedException e) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800231 interrupted = true;
232 break;
233 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800234 synchronizeIntents();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800235 }
236 } finally {
237 if (interrupted) {
238 Thread.currentThread().interrupt();
239 }
240 }
241 }
242
243 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800244 * Submits a collection of point-to-point intents.
245 *
246 * @param intents the intents to submit
247 */
248 void submitPeerIntents(Collection<PointToPointIntent> intents) {
249 synchronized (this) {
250 // Store the intents in memory
251 for (PointToPointIntent intent : intents) {
252 peerIntents.put(new IntentKey(intent), intent);
253 }
254
255 // Push the intents
256 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800257 log.debug("SDN-IP Submitting all Peer Intents...");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800258 for (Intent intent : intents) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800259 log.trace("SDN-IP Submitting intents: {}", intent);
260 intentService.submit(intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800261 }
262 }
263 }
264 }
265
266 /**
Jonathan Hart552e31f2015-02-06 11:11:59 -0800267 * Generates a route intent for a prefix, the next hop IP address, and
268 * the next hop MAC address.
269 * <p/>
270 * This method will find the egress interface for the intent.
271 * Intent will match dst IP prefix and rewrite dst MAC address at all other
272 * border switches, then forward packets according to dst MAC address.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800273 *
Jonathan Hart552e31f2015-02-06 11:11:59 -0800274 * @param prefix IP prefix of the route to add
275 * @param nextHopIpAddress IP address of the next hop
276 * @param nextHopMacAddress MAC address of the next hop
277 * @return the generated intent, or null if no intent should be submitted
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800278 */
Jonathan Hart552e31f2015-02-06 11:11:59 -0800279 private MultiPointToSinglePointIntent generateRouteIntent(
280 IpPrefix prefix,
281 IpAddress nextHopIpAddress,
282 MacAddress nextHopMacAddress) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800283
Jonathan Hart552e31f2015-02-06 11:11:59 -0800284 // Find the attachment point (egress interface) of the next hop
285 Interface egressInterface;
286 if (configService.getBgpPeers().containsKey(nextHopIpAddress)) {
287 // Route to a peer
288 log.debug("Route to peer {}", nextHopIpAddress);
289 BgpPeer peer =
290 configService.getBgpPeers().get(nextHopIpAddress);
291 egressInterface =
292 interfaceService.getInterface(peer.connectPoint());
293 } else {
294 // Route to non-peer
295 log.debug("Route to non-peer {}", nextHopIpAddress);
296 egressInterface =
297 interfaceService.getMatchingInterface(nextHopIpAddress);
298 if (egressInterface == null) {
299 log.warn("No outgoing interface found for {}",
300 nextHopIpAddress);
301 return null;
302 }
303 }
304
305 //
306 // Generate the intent itself
307 //
308 Set<ConnectPoint> ingressPorts = new HashSet<>();
309 ConnectPoint egressPort = egressInterface.connectPoint();
310 log.debug("Generating intent for prefix {}, next hop mac {}",
311 prefix, nextHopMacAddress);
312
313 for (Interface intf : interfaceService.getInterfaces()) {
314 if (!intf.connectPoint().equals(egressInterface.connectPoint())) {
315 ConnectPoint srcPort = intf.connectPoint();
316 ingressPorts.add(srcPort);
317 }
318 }
319
320 // Match the destination IP prefix at the first hop
321 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
322 if (prefix.version() == Ip4Address.VERSION) {
323 selector.matchEthType(Ethernet.TYPE_IPV4);
324 } else {
325 selector.matchEthType(Ethernet.TYPE_IPV6);
326 }
327 selector.matchIPDst(prefix);
328
329 // Rewrite the destination MAC address
330 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
331 .setEthDst(nextHopMacAddress);
332 if (!egressInterface.vlan().equals(VlanId.NONE)) {
333 treatment.setVlanId(egressInterface.vlan());
334 // If we set VLAN ID, we have to make sure a VLAN tag exists.
335 // TODO support no VLAN -> VLAN routing
336 selector.matchVlanId(VlanId.ANY);
337 }
338
339 return new MultiPointToSinglePointIntent(appId, selector.build(),
340 treatment.build(),
341 ingressPorts, egressPort);
342 }
343
344 @Override
345 public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800346 //
347 // NOTE: Semantically, we MUST withdraw existing intents before
348 // submitting new intents.
349 //
350 synchronized (this) {
351 MultiPointToSinglePointIntent intent;
352
353 log.debug("SDN-IP submitting intents = {} withdrawing = {}",
Jonathan Hart552e31f2015-02-06 11:11:59 -0800354 updates.size(), withdraws.size());
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800355
356 //
357 // Prepare the Intent batch operations for the intents to withdraw
358 //
Jonathan Hart552e31f2015-02-06 11:11:59 -0800359 for (FibUpdate withdraw : withdraws) {
360 checkArgument(withdraw.type() == FibUpdate.Type.DELETE,
361 "FibUpdate with wrong type in withdraws list");
362
363 IpPrefix prefix = withdraw.entry().prefix();
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800364 intent = routeIntents.remove(prefix);
365 if (intent == null) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800366 log.trace("SDN-IP No intent in routeIntents to delete " +
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800367 "for prefix: {}", prefix);
368 continue;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800369 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800370 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800371 log.trace("SDN-IP Withdrawing intent: {}", intent);
Brian O'Connor03406a42015-02-03 17:28:57 -0800372 intentService.withdraw(intent);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800373 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800374 }
375
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800376 //
377 // Prepare the Intent batch operations for the intents to submit
378 //
Jonathan Hart552e31f2015-02-06 11:11:59 -0800379 for (FibUpdate update : updates) {
380 checkArgument(update.type() == FibUpdate.Type.UPDATE,
381 "FibUpdate with wrong type in updates list");
382
383 IpPrefix prefix = update.entry().prefix();
384 intent = generateRouteIntent(prefix, update.entry().nextHopIp(),
385 update.entry().nextHopMac());
386
387 if (intent == null) {
388 // This preserves the old semantics - if an intent can't be
389 // generated, we don't do anything with that prefix. But
390 // perhaps we should withdraw the old intent anyway?
391 continue;
392 }
393
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800394 MultiPointToSinglePointIntent oldIntent =
395 routeIntents.put(prefix, intent);
396 if (isElectedLeader && isActivatedLeader) {
397 if (oldIntent != null) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800398 log.trace("SDN-IP Withdrawing old intent: {}",
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800399 oldIntent);
Brian O'Connor03406a42015-02-03 17:28:57 -0800400 intentService.withdraw(oldIntent);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800401 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800402 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800403 }
404 }
405 }
406
407 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800408 * Synchronize the in-memory Intents with the Intents in the Intent
409 * framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800410 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800411 void synchronizeIntents() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800412 synchronized (this) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800413
414 Map<IntentKey, Intent> localIntents = new HashMap<>();
415 Map<IntentKey, Intent> fetchedIntents = new HashMap<>();
416 Collection<Intent> storeInMemoryIntents = new LinkedList<>();
417 Collection<Intent> addIntents = new LinkedList<>();
418 Collection<Intent> deleteIntents = new LinkedList<>();
419
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800420 if (!isElectedLeader) {
421 return; // Nothing to do: not the leader anymore
422 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800423 log.debug("SDN-IP synchronizing all intents...");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800424
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800425 // Prepare the local intents
426 for (Intent intent : routeIntents.values()) {
427 localIntents.put(new IntentKey(intent), intent);
428 }
429 for (Intent intent : peerIntents.values()) {
430 localIntents.put(new IntentKey(intent), intent);
431 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800432
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800433 // Fetch all intents for this application
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800434 for (Intent intent : intentService.getIntents()) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800435 if (!intent.appId().equals(appId)) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800436 continue;
437 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800438 fetchedIntents.put(new IntentKey(intent), intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800439 }
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800440 if (log.isDebugEnabled()) {
441 for (Intent intent: fetchedIntents.values()) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800442 log.trace("SDN-IP Intent Synchronizer: fetched intent: {}",
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800443 intent);
444 }
445 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800446
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800447 computeIntentsDelta(localIntents, fetchedIntents,
448 storeInMemoryIntents, addIntents,
449 deleteIntents);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800450
451 //
452 // Perform the actions:
453 // 1. Store in memory fetched intents that are same. Can be done
454 // even if we are not the leader anymore
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800455 // 2. Delete intents: check if the leader before the operation
456 // 3. Add intents: check if the leader before the operation
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800457 //
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800458 for (Intent intent : storeInMemoryIntents) {
459 // Store the intent in memory based on its type
460 if (intent instanceof MultiPointToSinglePointIntent) {
461 MultiPointToSinglePointIntent mp2pIntent =
462 (MultiPointToSinglePointIntent) intent;
463 // Find the IP prefix
464 Criterion c =
465 mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800466 if (c == null) {
467 // Try IPv6
468 c =
469 mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST);
470 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800471 if (c != null && c instanceof IPCriterion) {
472 IPCriterion ipCriterion = (IPCriterion) c;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800473 IpPrefix ipPrefix = ipCriterion.ip();
474 if (ipPrefix == null) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800475 continue;
476 }
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800477 log.trace("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800478 "in-memory Route Intent for prefix {}",
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800479 ipPrefix);
480 routeIntents.put(ipPrefix, mp2pIntent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800481 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800482 log.warn("SDN-IP no IPV4_DST or IPV6_DST criterion found for Intent {}",
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800483 mp2pIntent.id());
484 }
485 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800486 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800487 if (intent instanceof PointToPointIntent) {
488 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800489 log.trace("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800490 "in-memory Peer Intent {}", p2pIntent);
491 peerIntents.put(new IntentKey(intent), p2pIntent);
492 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800493 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800494 }
495
496 // Withdraw Intents
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800497 for (Intent intent : deleteIntents) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800498 intentService.withdraw(intent);
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800499 log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}",
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800500 intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800501 }
502 if (!isElectedLeader) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800503 log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " +
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800504 "not elected leader anymore");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800505 isActivatedLeader = false;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800506 return;
507 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800508
509 // Add Intents
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800510 for (Intent intent : addIntents) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800511 intentService.submit(intent);
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800512 log.trace("SDN-IP Intent Synchronizer: submitting intent: {}",
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800513 intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800514 }
515 if (!isElectedLeader) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800516 log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " +
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800517 "not elected leader anymore");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800518 isActivatedLeader = false;
519 return;
520 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800521
522 if (isElectedLeader) {
523 isActivatedLeader = true; // Allow push of Intents
524 } else {
525 isActivatedLeader = false;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800526 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800527 log.debug("SDN-IP intent synchronization completed");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800528 }
529 }
530
531 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800532 * Computes the delta in two sets of Intents: local in-memory Intents,
533 * and intents fetched from the Intent framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800534 *
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800535 * @param localIntents the local in-memory Intents
536 * @param fetchedIntents the Intents fetched from the Intent framework
537 * @param storeInMemoryIntents the Intents that should be stored in memory.
538 * Note: This Collection must be allocated by the caller, and it will
539 * be populated by this method.
540 * @param addIntents the Intents that should be added to the Intent
541 * framework. Note: This Collection must be allocated by the caller, and
542 * it will be populated by this method.
543 * @param deleteIntents the Intents that should be deleted from the Intent
544 * framework. Note: This Collection must be allocated by the caller, and
545 * it will be populated by this method.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800546 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800547 private void computeIntentsDelta(
548 final Map<IntentKey, Intent> localIntents,
549 final Map<IntentKey, Intent> fetchedIntents,
550 Collection<Intent> storeInMemoryIntents,
551 Collection<Intent> addIntents,
552 Collection<Intent> deleteIntents) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800553
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800554 //
555 // Compute the deltas between the LOCAL in-memory Intents and the
556 // FETCHED Intents:
557 // - If an Intent is in both the LOCAL and FETCHED sets:
558 // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then
559 // the LOCAL Intent should be added/installed; otherwise the
560 // FETCHED intent should be stored in the local memory
561 // (i.e., override the LOCAL Intent) to preserve the original
562 // Intent ID.
563 // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL
564 // Intent should be added/installed.
565 // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED
566 // Intent should be deleted/withdrawn.
567 //
568 for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) {
569 IntentKey intentKey = entry.getKey();
570 Intent localIntent = entry.getValue();
571 Intent fetchedIntent = fetchedIntents.get(intentKey);
572
573 if (fetchedIntent == null) {
574 //
575 // No FETCHED Intent found: push the LOCAL Intent.
576 //
577 addIntents.add(localIntent);
578 continue;
579 }
580
581 IntentState state =
582 intentService.getIntentState(fetchedIntent.id());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800583 if (state == null ||
584 state == IntentState.WITHDRAWING ||
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800585 state == IntentState.WITHDRAWN) {
586 // The intent has been withdrawn but according to our route
587 // table it should be installed. We'll reinstall it.
588 addIntents.add(localIntent);
589 continue;
590 }
591 storeInMemoryIntents.add(fetchedIntent);
592 }
593
594 for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) {
595 IntentKey intentKey = entry.getKey();
596 Intent fetchedIntent = entry.getValue();
597 Intent localIntent = localIntents.get(intentKey);
598
599 if (localIntent != null) {
600 continue;
601 }
602
603 IntentState state =
604 intentService.getIntentState(fetchedIntent.id());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800605 if (state == null ||
606 state == IntentState.WITHDRAWING ||
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800607 state == IntentState.WITHDRAWN) {
608 // Nothing to do. The intent has been already withdrawn.
609 continue;
610 }
611 //
612 // No LOCAL Intent found: delete/withdraw the FETCHED Intent.
613 //
614 deleteIntents.add(fetchedIntent);
615 }
616 }
617
618 /**
619 * Helper class that can be used to compute the key for an Intent by
620 * by excluding the Intent ID.
621 */
622 static final class IntentKey {
623 private final Intent intent;
624
625 /**
626 * Constructor.
627 *
628 * @param intent the intent to use
629 */
630 IntentKey(Intent intent) {
631 checkArgument((intent instanceof MultiPointToSinglePointIntent) ||
632 (intent instanceof PointToPointIntent),
633 "Intent type not recognized", intent);
634 this.intent = intent;
635 }
636
637 /**
638 * Compares two Multi-Point to Single-Point Intents whether they
639 * represent same logical intention.
640 *
641 * @param intent1 the first Intent to compare
642 * @param intent2 the second Intent to compare
643 * @return true if both Intents represent same logical intention,
644 * otherwise false
645 */
646 static boolean equalIntents(MultiPointToSinglePointIntent intent1,
647 MultiPointToSinglePointIntent intent2) {
648 return Objects.equals(intent1.appId(), intent2.appId()) &&
649 Objects.equals(intent1.selector(), intent2.selector()) &&
650 Objects.equals(intent1.treatment(), intent2.treatment()) &&
651 Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) &&
652 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
653 }
654
655 /**
656 * Compares two Point-to-Point Intents whether they represent
657 * same logical intention.
658 *
659 * @param intent1 the first Intent to compare
660 * @param intent2 the second Intent to compare
661 * @return true if both Intents represent same logical intention,
662 * otherwise false
663 */
664 static boolean equalIntents(PointToPointIntent intent1,
665 PointToPointIntent intent2) {
666 return Objects.equals(intent1.appId(), intent2.appId()) &&
667 Objects.equals(intent1.selector(), intent2.selector()) &&
668 Objects.equals(intent1.treatment(), intent2.treatment()) &&
669 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
670 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
671 }
672
673 @Override
674 public int hashCode() {
675 if (intent instanceof PointToPointIntent) {
676 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
677 return Objects.hash(p2pIntent.appId(),
678 p2pIntent.resources(),
679 p2pIntent.selector(),
680 p2pIntent.treatment(),
681 p2pIntent.constraints(),
682 p2pIntent.ingressPoint(),
683 p2pIntent.egressPoint());
684 }
685 if (intent instanceof MultiPointToSinglePointIntent) {
686 MultiPointToSinglePointIntent m2pIntent =
687 (MultiPointToSinglePointIntent) intent;
688 return Objects.hash(m2pIntent.appId(),
689 m2pIntent.resources(),
690 m2pIntent.selector(),
691 m2pIntent.treatment(),
692 m2pIntent.constraints(),
693 m2pIntent.ingressPoints(),
694 m2pIntent.egressPoint());
695 }
696 checkArgument(false, "Intent type not recognized", intent);
697 return 0;
698 }
699
700 @Override
701 public boolean equals(Object obj) {
702 if (this == obj) {
703 return true;
704 }
705 if ((obj == null) || (!(obj instanceof IntentKey))) {
706 return false;
707 }
708 IntentKey other = (IntentKey) obj;
709
710 if (this.intent instanceof PointToPointIntent) {
711 if (!(other.intent instanceof PointToPointIntent)) {
712 return false;
713 }
714 return equalIntents((PointToPointIntent) this.intent,
715 (PointToPointIntent) other.intent);
716 }
717 if (this.intent instanceof MultiPointToSinglePointIntent) {
718 if (!(other.intent instanceof MultiPointToSinglePointIntent)) {
719 return false;
720 }
721 return equalIntents(
722 (MultiPointToSinglePointIntent) this.intent,
723 (MultiPointToSinglePointIntent) other.intent);
724 }
725 checkArgument(false, "Intent type not recognized", intent);
726 return false;
727 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800728 }
729}