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 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 18 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 19 | import org.onlab.packet.Ethernet; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.TrafficSelector; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.criteria.Criteria.IPCriterion; |
| 31 | import org.onosproject.net.flow.criteria.Criterion; |
| 32 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.IntentService; |
| 34 | import org.onosproject.net.intent.IntentState; |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame^] | 35 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 37 | import org.onosproject.net.intent.PointToPointIntent; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 38 | import org.onosproject.routing.FibListener; |
| 39 | import org.onosproject.routing.FibUpdate; |
| 40 | import org.onosproject.routing.config.BgpPeer; |
| 41 | import org.onosproject.routing.config.Interface; |
| 42 | import org.onosproject.routing.config.RoutingConfigurationService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 46 | import java.util.Collection; |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame^] | 47 | import java.util.Collections; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 48 | import java.util.HashMap; |
| 49 | import java.util.HashSet; |
| 50 | import java.util.LinkedList; |
| 51 | import java.util.List; |
| 52 | import java.util.Map; |
| 53 | import java.util.Objects; |
| 54 | import java.util.Set; |
| 55 | import java.util.concurrent.ConcurrentHashMap; |
| 56 | import java.util.concurrent.ExecutorService; |
| 57 | import java.util.concurrent.Executors; |
| 58 | import java.util.concurrent.Semaphore; |
| 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); |
| 343 | return new MultiPointToSinglePointIntent(appId, key, selector.build(), |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 344 | treatment.build(), |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame^] | 345 | ingressPorts, egressPort, |
| 346 | Collections.emptyList(), |
| 347 | priority); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 352 | // |
| 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 Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 360 | updates.size(), withdraws.size()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 361 | |
| 362 | // |
| 363 | // Prepare the Intent batch operations for the intents to withdraw |
| 364 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 365 | 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 Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 370 | intent = routeIntents.remove(prefix); |
| 371 | if (intent == null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 372 | log.trace("SDN-IP No intent in routeIntents to delete " + |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 373 | "for prefix: {}", prefix); |
| 374 | continue; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 375 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 376 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 377 | log.trace("SDN-IP Withdrawing intent: {}", intent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 378 | intentService.withdraw(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 379 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 382 | // |
| 383 | // Prepare the Intent batch operations for the intents to submit |
| 384 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 385 | 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 Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 400 | MultiPointToSinglePointIntent oldIntent = |
| 401 | routeIntents.put(prefix, intent); |
| 402 | if (isElectedLeader && isActivatedLeader) { |
| 403 | if (oldIntent != null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 404 | log.trace("SDN-IP Withdrawing old intent: {}", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 405 | oldIntent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 406 | intentService.withdraw(oldIntent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 407 | } |
Jonathan Hart | b3b8a0a | 2015-02-12 17:36:13 -0800 | [diff] [blame] | 408 | log.trace("SDN-IP Submitting intent: {}", intent); |
| 409 | intentService.submit(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 410 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 416 | * Synchronize the in-memory Intents with the Intents in the Intent |
| 417 | * framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 418 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 419 | void synchronizeIntents() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 420 | synchronized (this) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 421 | |
| 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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 428 | if (!isElectedLeader) { |
| 429 | return; // Nothing to do: not the leader anymore |
| 430 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 431 | log.debug("SDN-IP synchronizing all intents..."); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 432 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 433 | // 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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 440 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 441 | // Fetch all intents for this application |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 442 | for (Intent intent : intentService.getIntents()) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 443 | if (!intent.appId().equals(appId)) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 444 | continue; |
| 445 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 446 | fetchedIntents.put(new IntentKey(intent), intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 447 | } |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 448 | if (log.isDebugEnabled()) { |
| 449 | for (Intent intent: fetchedIntents.values()) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 450 | log.trace("SDN-IP Intent Synchronizer: fetched intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 451 | intent); |
| 452 | } |
| 453 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 454 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 455 | computeIntentsDelta(localIntents, fetchedIntents, |
| 456 | storeInMemoryIntents, addIntents, |
| 457 | deleteIntents); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 458 | |
| 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 Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 463 | // 2. Delete intents: check if the leader before the operation |
| 464 | // 3. Add intents: check if the leader before the operation |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 465 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 466 | 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 Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 474 | if (c == null) { |
| 475 | // Try IPv6 |
| 476 | c = |
| 477 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST); |
| 478 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 479 | if (c != null && c instanceof IPCriterion) { |
| 480 | IPCriterion ipCriterion = (IPCriterion) c; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 481 | IpPrefix ipPrefix = ipCriterion.ip(); |
| 482 | if (ipPrefix == null) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 483 | continue; |
| 484 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 485 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 486 | "in-memory Route Intent for prefix {}", |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 487 | ipPrefix); |
| 488 | routeIntents.put(ipPrefix, mp2pIntent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 489 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 490 | 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] | 491 | mp2pIntent.id()); |
| 492 | } |
| 493 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 494 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 495 | if (intent instanceof PointToPointIntent) { |
| 496 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 497 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 498 | "in-memory Peer Intent {}", p2pIntent); |
| 499 | peerIntents.put(new IntentKey(intent), p2pIntent); |
| 500 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 501 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // Withdraw Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 505 | for (Intent intent : deleteIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 506 | intentService.withdraw(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 507 | log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 508 | intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 509 | } |
| 510 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 511 | log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 512 | "not elected leader anymore"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 513 | isActivatedLeader = false; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 514 | return; |
| 515 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 516 | |
| 517 | // Add Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 518 | for (Intent intent : addIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 519 | intentService.submit(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 520 | log.trace("SDN-IP Intent Synchronizer: submitting intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 521 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 522 | } |
| 523 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 524 | log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 525 | "not elected leader anymore"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 526 | isActivatedLeader = false; |
| 527 | return; |
| 528 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 529 | |
| 530 | if (isElectedLeader) { |
| 531 | isActivatedLeader = true; // Allow push of Intents |
| 532 | } else { |
| 533 | isActivatedLeader = false; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 534 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 535 | log.debug("SDN-IP intent synchronization completed"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 540 | * Computes the delta in two sets of Intents: local in-memory Intents, |
| 541 | * and intents fetched from the Intent framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 542 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 543 | * @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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 554 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 555 | 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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 561 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 562 | // |
| 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 Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 590 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 591 | if (state == null || |
| 592 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 593 | 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 Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 612 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 613 | if (state == null || |
| 614 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 615 | 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 Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 736 | } |
| 737 | } |