blob: 85574b8f1f251e8ea2be83a01b3a54a44d689f72 [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 }
Jonathan Hartb3b8a0a2015-02-12 17:36:13 -0800402 log.trace("SDN-IP Submitting intent: {}", intent);
403 intentService.submit(intent);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800404 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800405 }
406 }
407 }
408
409 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800410 * Synchronize the in-memory Intents with the Intents in the Intent
411 * framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800412 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800413 void synchronizeIntents() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800414 synchronized (this) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800415
416 Map<IntentKey, Intent> localIntents = new HashMap<>();
417 Map<IntentKey, Intent> fetchedIntents = new HashMap<>();
418 Collection<Intent> storeInMemoryIntents = new LinkedList<>();
419 Collection<Intent> addIntents = new LinkedList<>();
420 Collection<Intent> deleteIntents = new LinkedList<>();
421
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800422 if (!isElectedLeader) {
423 return; // Nothing to do: not the leader anymore
424 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800425 log.debug("SDN-IP synchronizing all intents...");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800426
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800427 // Prepare the local intents
428 for (Intent intent : routeIntents.values()) {
429 localIntents.put(new IntentKey(intent), intent);
430 }
431 for (Intent intent : peerIntents.values()) {
432 localIntents.put(new IntentKey(intent), intent);
433 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800434
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800435 // Fetch all intents for this application
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800436 for (Intent intent : intentService.getIntents()) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800437 if (!intent.appId().equals(appId)) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800438 continue;
439 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800440 fetchedIntents.put(new IntentKey(intent), intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800441 }
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800442 if (log.isDebugEnabled()) {
443 for (Intent intent: fetchedIntents.values()) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800444 log.trace("SDN-IP Intent Synchronizer: fetched intent: {}",
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800445 intent);
446 }
447 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800448
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800449 computeIntentsDelta(localIntents, fetchedIntents,
450 storeInMemoryIntents, addIntents,
451 deleteIntents);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800452
453 //
454 // Perform the actions:
455 // 1. Store in memory fetched intents that are same. Can be done
456 // even if we are not the leader anymore
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800457 // 2. Delete intents: check if the leader before the operation
458 // 3. Add intents: check if the leader before the operation
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800459 //
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800460 for (Intent intent : storeInMemoryIntents) {
461 // Store the intent in memory based on its type
462 if (intent instanceof MultiPointToSinglePointIntent) {
463 MultiPointToSinglePointIntent mp2pIntent =
464 (MultiPointToSinglePointIntent) intent;
465 // Find the IP prefix
466 Criterion c =
467 mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800468 if (c == null) {
469 // Try IPv6
470 c =
471 mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST);
472 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800473 if (c != null && c instanceof IPCriterion) {
474 IPCriterion ipCriterion = (IPCriterion) c;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800475 IpPrefix ipPrefix = ipCriterion.ip();
476 if (ipPrefix == null) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800477 continue;
478 }
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800479 log.trace("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800480 "in-memory Route Intent for prefix {}",
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800481 ipPrefix);
482 routeIntents.put(ipPrefix, mp2pIntent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800483 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800484 log.warn("SDN-IP no IPV4_DST or IPV6_DST criterion found for Intent {}",
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800485 mp2pIntent.id());
486 }
487 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800488 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800489 if (intent instanceof PointToPointIntent) {
490 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800491 log.trace("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800492 "in-memory Peer Intent {}", p2pIntent);
493 peerIntents.put(new IntentKey(intent), p2pIntent);
494 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800495 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800496 }
497
498 // Withdraw Intents
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800499 for (Intent intent : deleteIntents) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800500 intentService.withdraw(intent);
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800501 log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}",
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800502 intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800503 }
504 if (!isElectedLeader) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800505 log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " +
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800506 "not elected leader anymore");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800507 isActivatedLeader = false;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800508 return;
509 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800510
511 // Add Intents
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800512 for (Intent intent : addIntents) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800513 intentService.submit(intent);
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800514 log.trace("SDN-IP Intent Synchronizer: submitting intent: {}",
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800515 intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800516 }
517 if (!isElectedLeader) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800518 log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " +
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800519 "not elected leader anymore");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800520 isActivatedLeader = false;
521 return;
522 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800523
524 if (isElectedLeader) {
525 isActivatedLeader = true; // Allow push of Intents
526 } else {
527 isActivatedLeader = false;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800528 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800529 log.debug("SDN-IP intent synchronization completed");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800530 }
531 }
532
533 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800534 * Computes the delta in two sets of Intents: local in-memory Intents,
535 * and intents fetched from the Intent framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800536 *
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800537 * @param localIntents the local in-memory Intents
538 * @param fetchedIntents the Intents fetched from the Intent framework
539 * @param storeInMemoryIntents the Intents that should be stored in memory.
540 * Note: This Collection must be allocated by the caller, and it will
541 * be populated by this method.
542 * @param addIntents the Intents that should be added to the Intent
543 * framework. Note: This Collection must be allocated by the caller, and
544 * it will be populated by this method.
545 * @param deleteIntents the Intents that should be deleted from the Intent
546 * framework. Note: This Collection must be allocated by the caller, and
547 * it will be populated by this method.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800548 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800549 private void computeIntentsDelta(
550 final Map<IntentKey, Intent> localIntents,
551 final Map<IntentKey, Intent> fetchedIntents,
552 Collection<Intent> storeInMemoryIntents,
553 Collection<Intent> addIntents,
554 Collection<Intent> deleteIntents) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800555
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800556 //
557 // Compute the deltas between the LOCAL in-memory Intents and the
558 // FETCHED Intents:
559 // - If an Intent is in both the LOCAL and FETCHED sets:
560 // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then
561 // the LOCAL Intent should be added/installed; otherwise the
562 // FETCHED intent should be stored in the local memory
563 // (i.e., override the LOCAL Intent) to preserve the original
564 // Intent ID.
565 // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL
566 // Intent should be added/installed.
567 // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED
568 // Intent should be deleted/withdrawn.
569 //
570 for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) {
571 IntentKey intentKey = entry.getKey();
572 Intent localIntent = entry.getValue();
573 Intent fetchedIntent = fetchedIntents.get(intentKey);
574
575 if (fetchedIntent == null) {
576 //
577 // No FETCHED Intent found: push the LOCAL Intent.
578 //
579 addIntents.add(localIntent);
580 continue;
581 }
582
583 IntentState state =
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800584 intentService.getIntentState(fetchedIntent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800585 if (state == null ||
586 state == IntentState.WITHDRAWING ||
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800587 state == IntentState.WITHDRAWN) {
588 // The intent has been withdrawn but according to our route
589 // table it should be installed. We'll reinstall it.
590 addIntents.add(localIntent);
591 continue;
592 }
593 storeInMemoryIntents.add(fetchedIntent);
594 }
595
596 for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) {
597 IntentKey intentKey = entry.getKey();
598 Intent fetchedIntent = entry.getValue();
599 Intent localIntent = localIntents.get(intentKey);
600
601 if (localIntent != null) {
602 continue;
603 }
604
605 IntentState state =
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800606 intentService.getIntentState(fetchedIntent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800607 if (state == null ||
608 state == IntentState.WITHDRAWING ||
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800609 state == IntentState.WITHDRAWN) {
610 // Nothing to do. The intent has been already withdrawn.
611 continue;
612 }
613 //
614 // No LOCAL Intent found: delete/withdraw the FETCHED Intent.
615 //
616 deleteIntents.add(fetchedIntent);
617 }
618 }
619
620 /**
621 * Helper class that can be used to compute the key for an Intent by
622 * by excluding the Intent ID.
623 */
624 static final class IntentKey {
625 private final Intent intent;
626
627 /**
628 * Constructor.
629 *
630 * @param intent the intent to use
631 */
632 IntentKey(Intent intent) {
633 checkArgument((intent instanceof MultiPointToSinglePointIntent) ||
634 (intent instanceof PointToPointIntent),
635 "Intent type not recognized", intent);
636 this.intent = intent;
637 }
638
639 /**
640 * Compares two Multi-Point to Single-Point Intents whether they
641 * represent same logical intention.
642 *
643 * @param intent1 the first Intent to compare
644 * @param intent2 the second Intent to compare
645 * @return true if both Intents represent same logical intention,
646 * otherwise false
647 */
648 static boolean equalIntents(MultiPointToSinglePointIntent intent1,
649 MultiPointToSinglePointIntent intent2) {
650 return Objects.equals(intent1.appId(), intent2.appId()) &&
651 Objects.equals(intent1.selector(), intent2.selector()) &&
652 Objects.equals(intent1.treatment(), intent2.treatment()) &&
653 Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) &&
654 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
655 }
656
657 /**
658 * Compares two Point-to-Point Intents whether they represent
659 * same logical intention.
660 *
661 * @param intent1 the first Intent to compare
662 * @param intent2 the second Intent to compare
663 * @return true if both Intents represent same logical intention,
664 * otherwise false
665 */
666 static boolean equalIntents(PointToPointIntent intent1,
667 PointToPointIntent intent2) {
668 return Objects.equals(intent1.appId(), intent2.appId()) &&
669 Objects.equals(intent1.selector(), intent2.selector()) &&
670 Objects.equals(intent1.treatment(), intent2.treatment()) &&
671 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
672 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
673 }
674
675 @Override
676 public int hashCode() {
677 if (intent instanceof PointToPointIntent) {
678 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
679 return Objects.hash(p2pIntent.appId(),
680 p2pIntent.resources(),
681 p2pIntent.selector(),
682 p2pIntent.treatment(),
683 p2pIntent.constraints(),
684 p2pIntent.ingressPoint(),
685 p2pIntent.egressPoint());
686 }
687 if (intent instanceof MultiPointToSinglePointIntent) {
688 MultiPointToSinglePointIntent m2pIntent =
689 (MultiPointToSinglePointIntent) intent;
690 return Objects.hash(m2pIntent.appId(),
691 m2pIntent.resources(),
692 m2pIntent.selector(),
693 m2pIntent.treatment(),
694 m2pIntent.constraints(),
695 m2pIntent.ingressPoints(),
696 m2pIntent.egressPoint());
697 }
698 checkArgument(false, "Intent type not recognized", intent);
699 return 0;
700 }
701
702 @Override
703 public boolean equals(Object obj) {
704 if (this == obj) {
705 return true;
706 }
707 if ((obj == null) || (!(obj instanceof IntentKey))) {
708 return false;
709 }
710 IntentKey other = (IntentKey) obj;
711
712 if (this.intent instanceof PointToPointIntent) {
713 if (!(other.intent instanceof PointToPointIntent)) {
714 return false;
715 }
716 return equalIntents((PointToPointIntent) this.intent,
717 (PointToPointIntent) other.intent);
718 }
719 if (this.intent instanceof MultiPointToSinglePointIntent) {
720 if (!(other.intent instanceof MultiPointToSinglePointIntent)) {
721 return false;
722 }
723 return equalIntents(
724 (MultiPointToSinglePointIntent) this.intent,
725 (MultiPointToSinglePointIntent) other.intent);
726 }
727 checkArgument(false, "Intent type not recognized", intent);
728 return false;
729 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800730 }
731}