blob: badad7fefdf406a0332f8fb2f7ce017887ab7e6f [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;
Jonathan Hart552e31f2015-02-06 11:11:59 -080020import org.onlab.packet.IpAddress;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080021import org.onlab.packet.IpPrefix;
Jonathan Hart552e31f2015-02-06 11:11:59 -080022import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.core.ApplicationId;
Jonathan Hart552e31f2015-02-06 11:11:59 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.flow.DefaultTrafficSelector;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.flow.criteria.Criteria.IPCriterion;
31import org.onosproject.net.flow.criteria.Criterion;
32import org.onosproject.net.intent.Intent;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.intent.IntentService;
34import org.onosproject.net.intent.IntentState;
Pavlin Radoslavov2aa1f322015-03-11 17:59:44 -070035import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.intent.MultiPointToSinglePointIntent;
37import org.onosproject.net.intent.PointToPointIntent;
Jonathan Hart2da1e602015-02-18 19:09:24 -080038import org.onosproject.routing.FibListener;
39import org.onosproject.routing.FibUpdate;
40import org.onosproject.routing.config.BgpPeer;
41import org.onosproject.routing.config.Interface;
42import org.onosproject.routing.config.RoutingConfigurationService;
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;
Pavlin Radoslavov2aa1f322015-03-11 17:59:44 -070047import java.util.Collections;
Jonathan Hart552e31f2015-02-06 11:11:59 -080048import java.util.HashMap;
49import java.util.HashSet;
50import java.util.LinkedList;
51import java.util.List;
52import java.util.Map;
53import java.util.Objects;
54import java.util.Set;
55import java.util.concurrent.ConcurrentHashMap;
56import java.util.concurrent.ExecutorService;
57import java.util.concurrent.Executors;
58import java.util.concurrent.Semaphore;
59
60import static com.google.common.base.Preconditions.checkArgument;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080061
Jonathan Hart51372182014-12-03 21:32:34 -080062/**
63 * Synchronizes intents between the in-memory intent store and the
64 * IntentService.
65 */
Jonathan Hart552e31f2015-02-06 11:11:59 -080066public class IntentSynchronizer implements FibListener {
Pavlin Radoslavov2aa1f322015-03-11 17:59:44 -070067 private static final int PRIORITY_OFFSET = 100;
68 private static final int PRIORITY_MULTIPLIER = 5;
69
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080070 private static final Logger log =
71 LoggerFactory.getLogger(IntentSynchronizer.class);
72
73 private final ApplicationId appId;
74 private final IntentService intentService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080075 private final Map<IntentKey, PointToPointIntent> peerIntents;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080076 private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080077
78 //
79 // State to deal with SDN-IP Leader election and pushing Intents
80 //
81 private final ExecutorService bgpIntentsSynchronizerExecutor;
82 private final Semaphore intentsSynchronizerSemaphore = new Semaphore(0);
83 private volatile boolean isElectedLeader = false;
84 private volatile boolean isActivatedLeader = false;
85
Jonathan Hart90a02c22015-02-13 11:52:07 -080086 private final RoutingConfigurationService configService;
Jonathan Hart552e31f2015-02-06 11:11:59 -080087
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080088 /**
89 * Class constructor.
90 *
91 * @param appId the Application ID
92 * @param intentService the intent service
Jonathan Hart552e31f2015-02-06 11:11:59 -080093 * @param configService the SDN-IP configuration service
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080094 */
Jonathan Hart552e31f2015-02-06 11:11:59 -080095 IntentSynchronizer(ApplicationId appId, IntentService intentService,
Jonathan Hart90a02c22015-02-13 11:52:07 -080096 RoutingConfigurationService configService) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080097 this.appId = appId;
98 this.intentService = intentService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080099 peerIntents = new ConcurrentHashMap<>();
100 routeIntents = new ConcurrentHashMap<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800101
Jonathan Hart552e31f2015-02-06 11:11:59 -0800102 this.configService = configService;
Jonathan Hart552e31f2015-02-06 11:11:59 -0800103
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 =
Jonathan Hart90a02c22015-02-13 11:52:07 -0800292 configService.getInterface(peer.connectPoint());
Jonathan Hart552e31f2015-02-06 11:11:59 -0800293 } else {
294 // Route to non-peer
295 log.debug("Route to non-peer {}", nextHopIpAddress);
296 egressInterface =
Jonathan Hart90a02c22015-02-13 11:52:07 -0800297 configService.getMatchingInterface(nextHopIpAddress);
Jonathan Hart552e31f2015-02-06 11:11:59 -0800298 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
Jonathan Hart90a02c22015-02-13 11:52:07 -0800313 for (Interface intf : configService.getInterfaces()) {
Jonathan Hart552e31f2015-02-06 11:11:59 -0800314 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();
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700322 if (prefix.isIp4()) {
Jonathan Hart552e31f2015-02-06 11:11:59 -0800323 selector.matchEthType(Ethernet.TYPE_IPV4);
Pavlin Radoslavova8537092015-02-23 10:15:20 -0800324 selector.matchIPDst(prefix);
Jonathan Hart552e31f2015-02-06 11:11:59 -0800325 } else {
326 selector.matchEthType(Ethernet.TYPE_IPV6);
Pavlin Radoslavova8537092015-02-23 10:15:20 -0800327 selector.matchIPv6Dst(prefix);
Jonathan Hart552e31f2015-02-06 11:11:59 -0800328 }
Jonathan Hart552e31f2015-02-06 11:11:59 -0800329
330 // Rewrite the destination MAC address
331 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
332 .setEthDst(nextHopMacAddress);
333 if (!egressInterface.vlan().equals(VlanId.NONE)) {
334 treatment.setVlanId(egressInterface.vlan());
335 // If we set VLAN ID, we have to make sure a VLAN tag exists.
336 // TODO support no VLAN -> VLAN routing
337 selector.matchVlanId(VlanId.ANY);
338 }
339
Pavlin Radoslavov2aa1f322015-03-11 17:59:44 -0700340 int priority =
341 prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
342 Key key = Key.of(prefix.toString(), appId);
343 return new MultiPointToSinglePointIntent(appId, key, selector.build(),
Jonathan Hart552e31f2015-02-06 11:11:59 -0800344 treatment.build(),
Pavlin Radoslavov2aa1f322015-03-11 17:59:44 -0700345 ingressPorts, egressPort,
346 Collections.emptyList(),
347 priority);
Jonathan Hart552e31f2015-02-06 11:11:59 -0800348 }
349
350 @Override
351 public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800352 //
353 // NOTE: Semantically, we MUST withdraw existing intents before
354 // submitting new intents.
355 //
356 synchronized (this) {
357 MultiPointToSinglePointIntent intent;
358
359 log.debug("SDN-IP submitting intents = {} withdrawing = {}",
Jonathan Hart552e31f2015-02-06 11:11:59 -0800360 updates.size(), withdraws.size());
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800361
362 //
363 // Prepare the Intent batch operations for the intents to withdraw
364 //
Jonathan Hart552e31f2015-02-06 11:11:59 -0800365 for (FibUpdate withdraw : withdraws) {
366 checkArgument(withdraw.type() == FibUpdate.Type.DELETE,
367 "FibUpdate with wrong type in withdraws list");
368
369 IpPrefix prefix = withdraw.entry().prefix();
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800370 intent = routeIntents.remove(prefix);
371 if (intent == null) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800372 log.trace("SDN-IP No intent in routeIntents to delete " +
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800373 "for prefix: {}", prefix);
374 continue;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800375 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800376 if (isElectedLeader && isActivatedLeader) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800377 log.trace("SDN-IP Withdrawing intent: {}", intent);
Brian O'Connor03406a42015-02-03 17:28:57 -0800378 intentService.withdraw(intent);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800379 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800380 }
381
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800382 //
383 // Prepare the Intent batch operations for the intents to submit
384 //
Jonathan Hart552e31f2015-02-06 11:11:59 -0800385 for (FibUpdate update : updates) {
386 checkArgument(update.type() == FibUpdate.Type.UPDATE,
387 "FibUpdate with wrong type in updates list");
388
389 IpPrefix prefix = update.entry().prefix();
390 intent = generateRouteIntent(prefix, update.entry().nextHopIp(),
391 update.entry().nextHopMac());
392
393 if (intent == null) {
394 // This preserves the old semantics - if an intent can't be
395 // generated, we don't do anything with that prefix. But
396 // perhaps we should withdraw the old intent anyway?
397 continue;
398 }
399
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800400 MultiPointToSinglePointIntent oldIntent =
401 routeIntents.put(prefix, intent);
402 if (isElectedLeader && isActivatedLeader) {
403 if (oldIntent != null) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800404 log.trace("SDN-IP Withdrawing old intent: {}",
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800405 oldIntent);
Brian O'Connor03406a42015-02-03 17:28:57 -0800406 intentService.withdraw(oldIntent);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800407 }
Jonathan Hartb3b8a0a2015-02-12 17:36:13 -0800408 log.trace("SDN-IP Submitting intent: {}", intent);
409 intentService.submit(intent);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800410 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800411 }
412 }
413 }
414
415 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800416 * Synchronize the in-memory Intents with the Intents in the Intent
417 * framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800418 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800419 void synchronizeIntents() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800420 synchronized (this) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800421
422 Map<IntentKey, Intent> localIntents = new HashMap<>();
423 Map<IntentKey, Intent> fetchedIntents = new HashMap<>();
424 Collection<Intent> storeInMemoryIntents = new LinkedList<>();
425 Collection<Intent> addIntents = new LinkedList<>();
426 Collection<Intent> deleteIntents = new LinkedList<>();
427
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800428 if (!isElectedLeader) {
429 return; // Nothing to do: not the leader anymore
430 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800431 log.debug("SDN-IP synchronizing all intents...");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800432
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800433 // Prepare the local intents
434 for (Intent intent : routeIntents.values()) {
435 localIntents.put(new IntentKey(intent), intent);
436 }
437 for (Intent intent : peerIntents.values()) {
438 localIntents.put(new IntentKey(intent), intent);
439 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800440
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800441 // Fetch all intents for this application
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800442 for (Intent intent : intentService.getIntents()) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800443 if (!intent.appId().equals(appId)) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800444 continue;
445 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800446 fetchedIntents.put(new IntentKey(intent), intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800447 }
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800448 if (log.isDebugEnabled()) {
449 for (Intent intent: fetchedIntents.values()) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800450 log.trace("SDN-IP Intent Synchronizer: fetched intent: {}",
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800451 intent);
452 }
453 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800454
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800455 computeIntentsDelta(localIntents, fetchedIntents,
456 storeInMemoryIntents, addIntents,
457 deleteIntents);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800458
459 //
460 // Perform the actions:
461 // 1. Store in memory fetched intents that are same. Can be done
462 // even if we are not the leader anymore
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800463 // 2. Delete intents: check if the leader before the operation
464 // 3. Add intents: check if the leader before the operation
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800465 //
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800466 for (Intent intent : storeInMemoryIntents) {
467 // Store the intent in memory based on its type
468 if (intent instanceof MultiPointToSinglePointIntent) {
469 MultiPointToSinglePointIntent mp2pIntent =
470 (MultiPointToSinglePointIntent) intent;
471 // Find the IP prefix
472 Criterion c =
473 mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800474 if (c == null) {
475 // Try IPv6
476 c =
477 mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST);
478 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800479 if (c != null && c instanceof IPCriterion) {
480 IPCriterion ipCriterion = (IPCriterion) c;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800481 IpPrefix ipPrefix = ipCriterion.ip();
482 if (ipPrefix == null) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800483 continue;
484 }
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800485 log.trace("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800486 "in-memory Route Intent for prefix {}",
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800487 ipPrefix);
488 routeIntents.put(ipPrefix, mp2pIntent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800489 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800490 log.warn("SDN-IP no IPV4_DST or IPV6_DST criterion found for Intent {}",
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800491 mp2pIntent.id());
492 }
493 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800494 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800495 if (intent instanceof PointToPointIntent) {
496 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800497 log.trace("SDN-IP Intent Synchronizer: updating " +
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800498 "in-memory Peer Intent {}", p2pIntent);
499 peerIntents.put(new IntentKey(intent), p2pIntent);
500 continue;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800501 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800502 }
503
504 // Withdraw Intents
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800505 for (Intent intent : deleteIntents) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800506 intentService.withdraw(intent);
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800507 log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}",
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800508 intent);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800509 }
510 if (!isElectedLeader) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800511 log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " +
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800512 "not elected leader anymore");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800513 isActivatedLeader = false;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800514 return;
515 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800516
517 // Add Intents
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800518 for (Intent intent : addIntents) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800519 intentService.submit(intent);
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800520 log.trace("SDN-IP Intent Synchronizer: submitting intent: {}",
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800521 intent);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800522 }
523 if (!isElectedLeader) {
Pavlin Radoslavov8049bb82014-12-02 13:58:35 -0800524 log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " +
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -0800525 "not elected leader anymore");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800526 isActivatedLeader = false;
527 return;
528 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800529
530 if (isElectedLeader) {
531 isActivatedLeader = true; // Allow push of Intents
532 } else {
533 isActivatedLeader = false;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800534 }
Pavlin Radoslavov93ae8322014-11-24 20:54:36 -0800535 log.debug("SDN-IP intent synchronization completed");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800536 }
537 }
538
539 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800540 * Computes the delta in two sets of Intents: local in-memory Intents,
541 * and intents fetched from the Intent framework.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800542 *
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800543 * @param localIntents the local in-memory Intents
544 * @param fetchedIntents the Intents fetched from the Intent framework
545 * @param storeInMemoryIntents the Intents that should be stored in memory.
546 * Note: This Collection must be allocated by the caller, and it will
547 * be populated by this method.
548 * @param addIntents the Intents that should be added to the Intent
549 * framework. Note: This Collection must be allocated by the caller, and
550 * it will be populated by this method.
551 * @param deleteIntents the Intents that should be deleted from the Intent
552 * framework. Note: This Collection must be allocated by the caller, and
553 * it will be populated by this method.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800554 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800555 private void computeIntentsDelta(
556 final Map<IntentKey, Intent> localIntents,
557 final Map<IntentKey, Intent> fetchedIntents,
558 Collection<Intent> storeInMemoryIntents,
559 Collection<Intent> addIntents,
560 Collection<Intent> deleteIntents) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800561
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800562 //
563 // Compute the deltas between the LOCAL in-memory Intents and the
564 // FETCHED Intents:
565 // - If an Intent is in both the LOCAL and FETCHED sets:
566 // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then
567 // the LOCAL Intent should be added/installed; otherwise the
568 // FETCHED intent should be stored in the local memory
569 // (i.e., override the LOCAL Intent) to preserve the original
570 // Intent ID.
571 // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL
572 // Intent should be added/installed.
573 // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED
574 // Intent should be deleted/withdrawn.
575 //
576 for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) {
577 IntentKey intentKey = entry.getKey();
578 Intent localIntent = entry.getValue();
579 Intent fetchedIntent = fetchedIntents.get(intentKey);
580
581 if (fetchedIntent == null) {
582 //
583 // No FETCHED Intent found: push the LOCAL Intent.
584 //
585 addIntents.add(localIntent);
586 continue;
587 }
588
589 IntentState state =
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800590 intentService.getIntentState(fetchedIntent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800591 if (state == null ||
592 state == IntentState.WITHDRAWING ||
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800593 state == IntentState.WITHDRAWN) {
594 // The intent has been withdrawn but according to our route
595 // table it should be installed. We'll reinstall it.
596 addIntents.add(localIntent);
597 continue;
598 }
599 storeInMemoryIntents.add(fetchedIntent);
600 }
601
602 for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) {
603 IntentKey intentKey = entry.getKey();
604 Intent fetchedIntent = entry.getValue();
605 Intent localIntent = localIntents.get(intentKey);
606
607 if (localIntent != null) {
608 continue;
609 }
610
611 IntentState state =
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800612 intentService.getIntentState(fetchedIntent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800613 if (state == null ||
614 state == IntentState.WITHDRAWING ||
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800615 state == IntentState.WITHDRAWN) {
616 // Nothing to do. The intent has been already withdrawn.
617 continue;
618 }
619 //
620 // No LOCAL Intent found: delete/withdraw the FETCHED Intent.
621 //
622 deleteIntents.add(fetchedIntent);
623 }
624 }
625
626 /**
627 * Helper class that can be used to compute the key for an Intent by
628 * by excluding the Intent ID.
629 */
630 static final class IntentKey {
631 private final Intent intent;
632
633 /**
634 * Constructor.
635 *
636 * @param intent the intent to use
637 */
638 IntentKey(Intent intent) {
639 checkArgument((intent instanceof MultiPointToSinglePointIntent) ||
640 (intent instanceof PointToPointIntent),
641 "Intent type not recognized", intent);
642 this.intent = intent;
643 }
644
645 /**
646 * Compares two Multi-Point to Single-Point Intents whether they
647 * represent same logical intention.
648 *
649 * @param intent1 the first Intent to compare
650 * @param intent2 the second Intent to compare
651 * @return true if both Intents represent same logical intention,
652 * otherwise false
653 */
654 static boolean equalIntents(MultiPointToSinglePointIntent intent1,
655 MultiPointToSinglePointIntent intent2) {
656 return Objects.equals(intent1.appId(), intent2.appId()) &&
657 Objects.equals(intent1.selector(), intent2.selector()) &&
658 Objects.equals(intent1.treatment(), intent2.treatment()) &&
659 Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) &&
660 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
661 }
662
663 /**
664 * Compares two Point-to-Point Intents whether they represent
665 * same logical intention.
666 *
667 * @param intent1 the first Intent to compare
668 * @param intent2 the second Intent to compare
669 * @return true if both Intents represent same logical intention,
670 * otherwise false
671 */
672 static boolean equalIntents(PointToPointIntent intent1,
673 PointToPointIntent intent2) {
674 return Objects.equals(intent1.appId(), intent2.appId()) &&
675 Objects.equals(intent1.selector(), intent2.selector()) &&
676 Objects.equals(intent1.treatment(), intent2.treatment()) &&
677 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
678 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
679 }
680
681 @Override
682 public int hashCode() {
683 if (intent instanceof PointToPointIntent) {
684 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
685 return Objects.hash(p2pIntent.appId(),
686 p2pIntent.resources(),
687 p2pIntent.selector(),
688 p2pIntent.treatment(),
689 p2pIntent.constraints(),
690 p2pIntent.ingressPoint(),
691 p2pIntent.egressPoint());
692 }
693 if (intent instanceof MultiPointToSinglePointIntent) {
694 MultiPointToSinglePointIntent m2pIntent =
695 (MultiPointToSinglePointIntent) intent;
696 return Objects.hash(m2pIntent.appId(),
697 m2pIntent.resources(),
698 m2pIntent.selector(),
699 m2pIntent.treatment(),
700 m2pIntent.constraints(),
701 m2pIntent.ingressPoints(),
702 m2pIntent.egressPoint());
703 }
704 checkArgument(false, "Intent type not recognized", intent);
705 return 0;
706 }
707
708 @Override
709 public boolean equals(Object obj) {
710 if (this == obj) {
711 return true;
712 }
713 if ((obj == null) || (!(obj instanceof IntentKey))) {
714 return false;
715 }
716 IntentKey other = (IntentKey) obj;
717
718 if (this.intent instanceof PointToPointIntent) {
719 if (!(other.intent instanceof PointToPointIntent)) {
720 return false;
721 }
722 return equalIntents((PointToPointIntent) this.intent,
723 (PointToPointIntent) other.intent);
724 }
725 if (this.intent instanceof MultiPointToSinglePointIntent) {
726 if (!(other.intent instanceof MultiPointToSinglePointIntent)) {
727 return false;
728 }
729 return equalIntents(
730 (MultiPointToSinglePointIntent) this.intent,
731 (MultiPointToSinglePointIntent) other.intent);
732 }
733 checkArgument(false, "Intent type not recognized", intent);
734 return false;
735 }
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800736 }
737}