Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 17 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.ConcurrentHashMap; |
| 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Executors; |
| 29 | import java.util.concurrent.Semaphore; |
| 30 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 31 | import org.onlab.packet.Ethernet; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 33 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 34 | import org.onlab.packet.MacAddress; |
| 35 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 37 | import org.onosproject.net.ConnectPoint; |
| 38 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 39 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 40 | import org.onosproject.net.flow.TrafficSelector; |
| 41 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.criteria.Criteria.IPCriterion; |
| 43 | import org.onosproject.net.flow.criteria.Criterion; |
| 44 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.IntentService; |
| 46 | import org.onosproject.net.intent.IntentState; |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 47 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 49 | import org.onosproject.net.intent.PointToPointIntent; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 50 | import org.onosproject.routing.FibListener; |
| 51 | import org.onosproject.routing.FibUpdate; |
| 52 | import org.onosproject.routing.config.BgpPeer; |
| 53 | import org.onosproject.routing.config.Interface; |
| 54 | import org.onosproject.routing.config.RoutingConfigurationService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | import org.slf4j.LoggerFactory; |
| 57 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 58 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 59 | |
| 60 | import static com.google.common.base.Preconditions.checkArgument; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 61 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Synchronizes intents between the in-memory intent store and the |
| 64 | * IntentService. |
| 65 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 66 | public class IntentSynchronizer implements FibListener { |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 67 | private static final int PRIORITY_OFFSET = 100; |
| 68 | private static final int PRIORITY_MULTIPLIER = 5; |
| 69 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 70 | private static final Logger log = |
| 71 | LoggerFactory.getLogger(IntentSynchronizer.class); |
| 72 | |
| 73 | private final ApplicationId appId; |
| 74 | private final IntentService intentService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 75 | private final Map<IntentKey, PointToPointIntent> peerIntents; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 76 | private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 77 | |
| 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 Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 86 | private final RoutingConfigurationService configService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 87 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Class constructor. |
| 90 | * |
| 91 | * @param appId the Application ID |
| 92 | * @param intentService the intent service |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 93 | * @param configService the SDN-IP configuration service |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 94 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 95 | IntentSynchronizer(ApplicationId appId, IntentService intentService, |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 96 | RoutingConfigurationService configService) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 97 | this.appId = appId; |
| 98 | this.intentService = intentService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 99 | peerIntents = new ConcurrentHashMap<>(); |
| 100 | routeIntents = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 101 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 102 | this.configService = configService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 103 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 104 | bgpIntentsSynchronizerExecutor = Executors.newSingleThreadExecutor( |
| 105 | new ThreadFactoryBuilder() |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 106 | .setNameFormat("sdnip-intents-synchronizer-%d").build()); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 107 | } |
| 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 Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 125 | synchronized (this) { |
| 126 | // Stop the thread(s) |
| 127 | bgpIntentsSynchronizerExecutor.shutdownNow(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 128 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 129 | // |
| 130 | // Withdraw all SDN-IP intents |
| 131 | // |
| 132 | if (!isElectedLeader) { |
| 133 | return; // Nothing to do: not the leader anymore |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 134 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 135 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 136 | // |
Pavlin Radoslavov | 20be3e6 | 2014-11-25 18:52:08 -0800 | [diff] [blame] | 137 | // 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 Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 143 | // Build a batch operation to withdraw all intents from this |
| 144 | // application. |
| 145 | // |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 146 | log.debug("SDN-IP Intent Synchronizer shutdown: " + |
| 147 | "withdrawing all intents..."); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 148 | IntentOperations.Builder builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 149 | 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 Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 158 | if ((intentState == null) || |
| 159 | intentState.equals(IntentState.WITHDRAWING) || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 160 | intentState.equals(IntentState.WITHDRAWN)) { |
| 161 | continue; |
| 162 | } |
| 163 | |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 164 | log.trace("SDN-IP Intent Synchronizer withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 165 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 166 | builder.addWithdrawOperation(intent.id()); |
| 167 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 168 | IntentOperations intentOperations = builder.build(); |
| 169 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 170 | leaderChanged(false); |
| 171 | |
| 172 | peerIntents.clear(); |
| 173 | routeIntents.clear(); |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 174 | log.debug("SDN-IP Intent Synchronizer shutdown completed"); |
Pavlin Radoslavov | 20be3e6 | 2014-11-25 18:52:08 -0800 | [diff] [blame] | 175 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 176 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 179 | /** |
| 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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 184 | public void leaderChanged(boolean isLeader) { |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 185 | log.debug("SDN-IP Leader changed: {}", isLeader); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 186 | |
| 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 Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 202 | * Gets the route intents. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 203 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 204 | * @return the route intents |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 205 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 206 | public Collection<MultiPointToSinglePointIntent> getRouteIntents() { |
| 207 | List<MultiPointToSinglePointIntent> result = new LinkedList<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 208 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 209 | for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 210 | routeIntents.entrySet()) { |
| 211 | result.add(entry.getValue()); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 212 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 213 | return result; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 214 | } |
| 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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 231 | interrupted = true; |
| 232 | break; |
| 233 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 234 | synchronizeIntents(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 235 | } |
| 236 | } finally { |
| 237 | if (interrupted) { |
| 238 | Thread.currentThread().interrupt(); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 244 | * 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 Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 257 | log.debug("SDN-IP Submitting all Peer Intents..."); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 258 | for (Intent intent : intents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 259 | log.trace("SDN-IP Submitting intents: {}", intent); |
| 260 | intentService.submit(intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 267 | * 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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 273 | * |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 274 | * @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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 278 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 279 | private MultiPointToSinglePointIntent generateRouteIntent( |
| 280 | IpPrefix prefix, |
| 281 | IpAddress nextHopIpAddress, |
| 282 | MacAddress nextHopMacAddress) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 283 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 284 | // 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 Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 292 | configService.getInterface(peer.connectPoint()); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 293 | } else { |
| 294 | // Route to non-peer |
| 295 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 296 | egressInterface = |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 297 | configService.getMatchingInterface(nextHopIpAddress); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 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 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 313 | for (Interface intf : configService.getInterfaces()) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 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(); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 322 | if (prefix.isIp4()) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 323 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Pavlin Radoslavov | a853709 | 2015-02-23 10:15:20 -0800 | [diff] [blame] | 324 | selector.matchIPDst(prefix); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 325 | } else { |
| 326 | selector.matchEthType(Ethernet.TYPE_IPV6); |
Pavlin Radoslavov | a853709 | 2015-02-23 10:15:20 -0800 | [diff] [blame] | 327 | selector.matchIPv6Dst(prefix); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 328 | } |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 329 | |
| 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 Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 340 | int priority = |
| 341 | prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 342 | Key key = Key.of(prefix.toString(), appId); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 343 | return MultiPointToSinglePointIntent.builder() |
| 344 | .appId(appId) |
| 345 | .key(key) |
| 346 | .selector(selector.build()) |
| 347 | .treatment(treatment.build()) |
| 348 | .ingressPoints(ingressPorts) |
| 349 | .egressPoint(egressPort) |
| 350 | .priority(priority) |
| 351 | .build(); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 356 | // |
| 357 | // NOTE: Semantically, we MUST withdraw existing intents before |
| 358 | // submitting new intents. |
| 359 | // |
| 360 | synchronized (this) { |
| 361 | MultiPointToSinglePointIntent intent; |
| 362 | |
| 363 | log.debug("SDN-IP submitting intents = {} withdrawing = {}", |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 364 | updates.size(), withdraws.size()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 365 | |
| 366 | // |
| 367 | // Prepare the Intent batch operations for the intents to withdraw |
| 368 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 369 | for (FibUpdate withdraw : withdraws) { |
| 370 | checkArgument(withdraw.type() == FibUpdate.Type.DELETE, |
| 371 | "FibUpdate with wrong type in withdraws list"); |
| 372 | |
| 373 | IpPrefix prefix = withdraw.entry().prefix(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 374 | intent = routeIntents.remove(prefix); |
| 375 | if (intent == null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 376 | log.trace("SDN-IP No intent in routeIntents to delete " + |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 377 | "for prefix: {}", prefix); |
| 378 | continue; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 379 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 380 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 381 | log.trace("SDN-IP Withdrawing intent: {}", intent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 382 | intentService.withdraw(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 383 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 386 | // |
| 387 | // Prepare the Intent batch operations for the intents to submit |
| 388 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 389 | for (FibUpdate update : updates) { |
| 390 | checkArgument(update.type() == FibUpdate.Type.UPDATE, |
| 391 | "FibUpdate with wrong type in updates list"); |
| 392 | |
| 393 | IpPrefix prefix = update.entry().prefix(); |
| 394 | intent = generateRouteIntent(prefix, update.entry().nextHopIp(), |
| 395 | update.entry().nextHopMac()); |
| 396 | |
| 397 | if (intent == null) { |
| 398 | // This preserves the old semantics - if an intent can't be |
| 399 | // generated, we don't do anything with that prefix. But |
| 400 | // perhaps we should withdraw the old intent anyway? |
| 401 | continue; |
| 402 | } |
| 403 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 404 | MultiPointToSinglePointIntent oldIntent = |
| 405 | routeIntents.put(prefix, intent); |
| 406 | if (isElectedLeader && isActivatedLeader) { |
| 407 | if (oldIntent != null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 408 | log.trace("SDN-IP Withdrawing old intent: {}", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 409 | oldIntent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 410 | intentService.withdraw(oldIntent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 411 | } |
Jonathan Hart | b3b8a0a | 2015-02-12 17:36:13 -0800 | [diff] [blame] | 412 | log.trace("SDN-IP Submitting intent: {}", intent); |
| 413 | intentService.submit(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 414 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 420 | * Synchronize the in-memory Intents with the Intents in the Intent |
| 421 | * framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 422 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 423 | void synchronizeIntents() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 424 | synchronized (this) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 425 | |
| 426 | Map<IntentKey, Intent> localIntents = new HashMap<>(); |
| 427 | Map<IntentKey, Intent> fetchedIntents = new HashMap<>(); |
| 428 | Collection<Intent> storeInMemoryIntents = new LinkedList<>(); |
| 429 | Collection<Intent> addIntents = new LinkedList<>(); |
| 430 | Collection<Intent> deleteIntents = new LinkedList<>(); |
| 431 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 432 | if (!isElectedLeader) { |
| 433 | return; // Nothing to do: not the leader anymore |
| 434 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 435 | log.debug("SDN-IP synchronizing all intents..."); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 436 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 437 | // Prepare the local intents |
| 438 | for (Intent intent : routeIntents.values()) { |
| 439 | localIntents.put(new IntentKey(intent), intent); |
| 440 | } |
| 441 | for (Intent intent : peerIntents.values()) { |
| 442 | localIntents.put(new IntentKey(intent), intent); |
| 443 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 444 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 445 | // Fetch all intents for this application |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 446 | for (Intent intent : intentService.getIntents()) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 447 | if (!intent.appId().equals(appId)) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 448 | continue; |
| 449 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 450 | fetchedIntents.put(new IntentKey(intent), intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 451 | } |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 452 | if (log.isDebugEnabled()) { |
| 453 | for (Intent intent: fetchedIntents.values()) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 454 | log.trace("SDN-IP Intent Synchronizer: fetched intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 455 | intent); |
| 456 | } |
| 457 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 458 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 459 | computeIntentsDelta(localIntents, fetchedIntents, |
| 460 | storeInMemoryIntents, addIntents, |
| 461 | deleteIntents); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 462 | |
| 463 | // |
| 464 | // Perform the actions: |
| 465 | // 1. Store in memory fetched intents that are same. Can be done |
| 466 | // even if we are not the leader anymore |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 467 | // 2. Delete intents: check if the leader before the operation |
| 468 | // 3. Add intents: check if the leader before the operation |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 469 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 470 | for (Intent intent : storeInMemoryIntents) { |
| 471 | // Store the intent in memory based on its type |
| 472 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 473 | MultiPointToSinglePointIntent mp2pIntent = |
| 474 | (MultiPointToSinglePointIntent) intent; |
| 475 | // Find the IP prefix |
| 476 | Criterion c = |
| 477 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 478 | if (c == null) { |
| 479 | // Try IPv6 |
| 480 | c = |
| 481 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST); |
| 482 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 483 | if (c != null && c instanceof IPCriterion) { |
| 484 | IPCriterion ipCriterion = (IPCriterion) c; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 485 | IpPrefix ipPrefix = ipCriterion.ip(); |
| 486 | if (ipPrefix == null) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 487 | continue; |
| 488 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 489 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 490 | "in-memory Route Intent for prefix {}", |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 491 | ipPrefix); |
| 492 | routeIntents.put(ipPrefix, mp2pIntent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 493 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 494 | log.warn("SDN-IP no IPV4_DST or IPV6_DST criterion found for Intent {}", |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 495 | mp2pIntent.id()); |
| 496 | } |
| 497 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 498 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 499 | if (intent instanceof PointToPointIntent) { |
| 500 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 501 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 502 | "in-memory Peer Intent {}", p2pIntent); |
| 503 | peerIntents.put(new IntentKey(intent), p2pIntent); |
| 504 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 505 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // Withdraw Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 509 | for (Intent intent : deleteIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 510 | intentService.withdraw(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 511 | log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 512 | intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 513 | } |
| 514 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 515 | log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 516 | "not elected leader anymore"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 517 | isActivatedLeader = false; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 518 | return; |
| 519 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 520 | |
| 521 | // Add Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 522 | for (Intent intent : addIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 523 | intentService.submit(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 524 | log.trace("SDN-IP Intent Synchronizer: submitting intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 525 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 526 | } |
| 527 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 528 | log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 529 | "not elected leader anymore"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 530 | isActivatedLeader = false; |
| 531 | return; |
| 532 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 533 | |
| 534 | if (isElectedLeader) { |
| 535 | isActivatedLeader = true; // Allow push of Intents |
| 536 | } else { |
| 537 | isActivatedLeader = false; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 538 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 539 | log.debug("SDN-IP intent synchronization completed"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 544 | * Computes the delta in two sets of Intents: local in-memory Intents, |
| 545 | * and intents fetched from the Intent framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 546 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 547 | * @param localIntents the local in-memory Intents |
| 548 | * @param fetchedIntents the Intents fetched from the Intent framework |
| 549 | * @param storeInMemoryIntents the Intents that should be stored in memory. |
| 550 | * Note: This Collection must be allocated by the caller, and it will |
| 551 | * be populated by this method. |
| 552 | * @param addIntents the Intents that should be added to the Intent |
| 553 | * framework. Note: This Collection must be allocated by the caller, and |
| 554 | * it will be populated by this method. |
| 555 | * @param deleteIntents the Intents that should be deleted from the Intent |
| 556 | * framework. Note: This Collection must be allocated by the caller, and |
| 557 | * it will be populated by this method. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 558 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 559 | private void computeIntentsDelta( |
| 560 | final Map<IntentKey, Intent> localIntents, |
| 561 | final Map<IntentKey, Intent> fetchedIntents, |
| 562 | Collection<Intent> storeInMemoryIntents, |
| 563 | Collection<Intent> addIntents, |
| 564 | Collection<Intent> deleteIntents) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 565 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 566 | // |
| 567 | // Compute the deltas between the LOCAL in-memory Intents and the |
| 568 | // FETCHED Intents: |
| 569 | // - If an Intent is in both the LOCAL and FETCHED sets: |
| 570 | // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then |
| 571 | // the LOCAL Intent should be added/installed; otherwise the |
| 572 | // FETCHED intent should be stored in the local memory |
| 573 | // (i.e., override the LOCAL Intent) to preserve the original |
| 574 | // Intent ID. |
| 575 | // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL |
| 576 | // Intent should be added/installed. |
| 577 | // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED |
| 578 | // Intent should be deleted/withdrawn. |
| 579 | // |
| 580 | for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) { |
| 581 | IntentKey intentKey = entry.getKey(); |
| 582 | Intent localIntent = entry.getValue(); |
| 583 | Intent fetchedIntent = fetchedIntents.get(intentKey); |
| 584 | |
| 585 | if (fetchedIntent == null) { |
| 586 | // |
| 587 | // No FETCHED Intent found: push the LOCAL Intent. |
| 588 | // |
| 589 | addIntents.add(localIntent); |
| 590 | continue; |
| 591 | } |
| 592 | |
| 593 | IntentState state = |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 594 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 595 | if (state == null || |
| 596 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 597 | state == IntentState.WITHDRAWN) { |
| 598 | // The intent has been withdrawn but according to our route |
| 599 | // table it should be installed. We'll reinstall it. |
| 600 | addIntents.add(localIntent); |
| 601 | continue; |
| 602 | } |
| 603 | storeInMemoryIntents.add(fetchedIntent); |
| 604 | } |
| 605 | |
| 606 | for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) { |
| 607 | IntentKey intentKey = entry.getKey(); |
| 608 | Intent fetchedIntent = entry.getValue(); |
| 609 | Intent localIntent = localIntents.get(intentKey); |
| 610 | |
| 611 | if (localIntent != null) { |
| 612 | continue; |
| 613 | } |
| 614 | |
| 615 | IntentState state = |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 616 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 617 | if (state == null || |
| 618 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 619 | state == IntentState.WITHDRAWN) { |
| 620 | // Nothing to do. The intent has been already withdrawn. |
| 621 | continue; |
| 622 | } |
| 623 | // |
| 624 | // No LOCAL Intent found: delete/withdraw the FETCHED Intent. |
| 625 | // |
| 626 | deleteIntents.add(fetchedIntent); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Helper class that can be used to compute the key for an Intent by |
| 632 | * by excluding the Intent ID. |
| 633 | */ |
| 634 | static final class IntentKey { |
| 635 | private final Intent intent; |
| 636 | |
| 637 | /** |
| 638 | * Constructor. |
| 639 | * |
| 640 | * @param intent the intent to use |
| 641 | */ |
| 642 | IntentKey(Intent intent) { |
| 643 | checkArgument((intent instanceof MultiPointToSinglePointIntent) || |
| 644 | (intent instanceof PointToPointIntent), |
| 645 | "Intent type not recognized", intent); |
| 646 | this.intent = intent; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Compares two Multi-Point to Single-Point Intents whether they |
| 651 | * represent same logical intention. |
| 652 | * |
| 653 | * @param intent1 the first Intent to compare |
| 654 | * @param intent2 the second Intent to compare |
| 655 | * @return true if both Intents represent same logical intention, |
| 656 | * otherwise false |
| 657 | */ |
| 658 | static boolean equalIntents(MultiPointToSinglePointIntent intent1, |
| 659 | MultiPointToSinglePointIntent intent2) { |
| 660 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 661 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 662 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 663 | Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) && |
| 664 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Compares two Point-to-Point Intents whether they represent |
| 669 | * same logical intention. |
| 670 | * |
| 671 | * @param intent1 the first Intent to compare |
| 672 | * @param intent2 the second Intent to compare |
| 673 | * @return true if both Intents represent same logical intention, |
| 674 | * otherwise false |
| 675 | */ |
| 676 | static boolean equalIntents(PointToPointIntent intent1, |
| 677 | PointToPointIntent intent2) { |
| 678 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 679 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 680 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 681 | Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) && |
| 682 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 683 | } |
| 684 | |
| 685 | @Override |
| 686 | public int hashCode() { |
| 687 | if (intent instanceof PointToPointIntent) { |
| 688 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
| 689 | return Objects.hash(p2pIntent.appId(), |
| 690 | p2pIntent.resources(), |
| 691 | p2pIntent.selector(), |
| 692 | p2pIntent.treatment(), |
| 693 | p2pIntent.constraints(), |
| 694 | p2pIntent.ingressPoint(), |
| 695 | p2pIntent.egressPoint()); |
| 696 | } |
| 697 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 698 | MultiPointToSinglePointIntent m2pIntent = |
| 699 | (MultiPointToSinglePointIntent) intent; |
| 700 | return Objects.hash(m2pIntent.appId(), |
| 701 | m2pIntent.resources(), |
| 702 | m2pIntent.selector(), |
| 703 | m2pIntent.treatment(), |
| 704 | m2pIntent.constraints(), |
| 705 | m2pIntent.ingressPoints(), |
| 706 | m2pIntent.egressPoint()); |
| 707 | } |
| 708 | checkArgument(false, "Intent type not recognized", intent); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | @Override |
| 713 | public boolean equals(Object obj) { |
| 714 | if (this == obj) { |
| 715 | return true; |
| 716 | } |
| 717 | if ((obj == null) || (!(obj instanceof IntentKey))) { |
| 718 | return false; |
| 719 | } |
| 720 | IntentKey other = (IntentKey) obj; |
| 721 | |
| 722 | if (this.intent instanceof PointToPointIntent) { |
| 723 | if (!(other.intent instanceof PointToPointIntent)) { |
| 724 | return false; |
| 725 | } |
| 726 | return equalIntents((PointToPointIntent) this.intent, |
| 727 | (PointToPointIntent) other.intent); |
| 728 | } |
| 729 | if (this.intent instanceof MultiPointToSinglePointIntent) { |
| 730 | if (!(other.intent instanceof MultiPointToSinglePointIntent)) { |
| 731 | return false; |
| 732 | } |
| 733 | return equalIntents( |
| 734 | (MultiPointToSinglePointIntent) this.intent, |
| 735 | (MultiPointToSinglePointIntent) other.intent); |
| 736 | } |
| 737 | checkArgument(false, "Intent type not recognized", intent); |
| 738 | return false; |
| 739 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 740 | } |
| 741 | } |