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; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.criteria.Criteria.IPCriterion; |
| 32 | import org.onosproject.net.flow.criteria.Criterion; |
| 33 | import org.onosproject.net.intent.Intent; |
| 34 | import org.onosproject.net.intent.IntentOperations; |
| 35 | import org.onosproject.net.intent.IntentService; |
| 36 | import org.onosproject.net.intent.IntentState; |
| 37 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 38 | import org.onosproject.net.intent.PointToPointIntent; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 39 | import org.onosproject.sdnip.config.BgpPeer; |
| 40 | import org.onosproject.sdnip.config.Interface; |
| 41 | import org.onosproject.sdnip.config.SdnIpConfigurationService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 45 | import java.util.Collection; |
| 46 | import java.util.HashMap; |
| 47 | import java.util.HashSet; |
| 48 | import java.util.LinkedList; |
| 49 | import java.util.List; |
| 50 | import java.util.Map; |
| 51 | import java.util.Objects; |
| 52 | import java.util.Set; |
| 53 | import java.util.concurrent.ConcurrentHashMap; |
| 54 | import java.util.concurrent.ExecutorService; |
| 55 | import java.util.concurrent.Executors; |
| 56 | import java.util.concurrent.Semaphore; |
| 57 | |
| 58 | import static com.google.common.base.Preconditions.checkArgument; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 59 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Synchronizes intents between the in-memory intent store and the |
| 62 | * IntentService. |
| 63 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 64 | public class IntentSynchronizer implements FibListener { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 65 | private static final Logger log = |
| 66 | LoggerFactory.getLogger(IntentSynchronizer.class); |
| 67 | |
| 68 | private final ApplicationId appId; |
| 69 | private final IntentService intentService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 70 | private final Map<IntentKey, PointToPointIntent> peerIntents; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 71 | private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 72 | |
| 73 | // |
| 74 | // State to deal with SDN-IP Leader election and pushing Intents |
| 75 | // |
| 76 | private final ExecutorService bgpIntentsSynchronizerExecutor; |
| 77 | private final Semaphore intentsSynchronizerSemaphore = new Semaphore(0); |
| 78 | private volatile boolean isElectedLeader = false; |
| 79 | private volatile boolean isActivatedLeader = false; |
| 80 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 81 | private final SdnIpConfigurationService configService; |
| 82 | private final InterfaceService interfaceService; |
| 83 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Class constructor. |
| 86 | * |
| 87 | * @param appId the Application ID |
| 88 | * @param intentService the intent service |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 89 | * @param configService the SDN-IP configuration service |
| 90 | * @param interfaceService the interface service |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 91 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 92 | IntentSynchronizer(ApplicationId appId, IntentService intentService, |
| 93 | SdnIpConfigurationService configService, |
| 94 | InterfaceService interfaceService) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 95 | this.appId = appId; |
| 96 | this.intentService = intentService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 97 | peerIntents = new ConcurrentHashMap<>(); |
| 98 | routeIntents = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 99 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 100 | this.configService = configService; |
| 101 | this.interfaceService = interfaceService; |
| 102 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 103 | bgpIntentsSynchronizerExecutor = Executors.newSingleThreadExecutor( |
| 104 | new ThreadFactoryBuilder() |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 105 | .setNameFormat("sdnip-intents-synchronizer-%d").build()); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Starts the synchronizer. |
| 110 | */ |
| 111 | public void start() { |
| 112 | bgpIntentsSynchronizerExecutor.execute(new Runnable() { |
| 113 | @Override |
| 114 | public void run() { |
| 115 | doIntentSynchronizationThread(); |
| 116 | } |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Stops the synchronizer. |
| 122 | */ |
| 123 | public void stop() { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 124 | synchronized (this) { |
| 125 | // Stop the thread(s) |
| 126 | bgpIntentsSynchronizerExecutor.shutdownNow(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 127 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 128 | // |
| 129 | // Withdraw all SDN-IP intents |
| 130 | // |
| 131 | if (!isElectedLeader) { |
| 132 | return; // Nothing to do: not the leader anymore |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 133 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 134 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 135 | // |
Pavlin Radoslavov | 20be3e6 | 2014-11-25 18:52:08 -0800 | [diff] [blame] | 136 | // NOTE: We don't withdraw the intents during shutdown, because |
| 137 | // it creates flux in the data plane during switchover. |
| 138 | // |
| 139 | |
| 140 | /* |
| 141 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 142 | // Build a batch operation to withdraw all intents from this |
| 143 | // application. |
| 144 | // |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 145 | log.debug("SDN-IP Intent Synchronizer shutdown: " + |
| 146 | "withdrawing all intents..."); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 147 | IntentOperations.Builder builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 148 | for (Intent intent : intentService.getIntents()) { |
| 149 | // Skip the intents from other applications |
| 150 | if (!intent.appId().equals(appId)) { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | // Skip the intents that are already withdrawn |
| 155 | IntentState intentState = |
| 156 | intentService.getIntentState(intent.id()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 157 | if ((intentState == null) || |
| 158 | intentState.equals(IntentState.WITHDRAWING) || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 159 | intentState.equals(IntentState.WITHDRAWN)) { |
| 160 | continue; |
| 161 | } |
| 162 | |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 163 | log.trace("SDN-IP Intent Synchronizer withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 164 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 165 | builder.addWithdrawOperation(intent.id()); |
| 166 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 167 | IntentOperations intentOperations = builder.build(); |
| 168 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 169 | leaderChanged(false); |
| 170 | |
| 171 | peerIntents.clear(); |
| 172 | routeIntents.clear(); |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 173 | log.debug("SDN-IP Intent Synchronizer shutdown completed"); |
Pavlin Radoslavov | 20be3e6 | 2014-11-25 18:52:08 -0800 | [diff] [blame] | 174 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 175 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 178 | /** |
| 179 | * Signals the synchronizer that the SDN-IP leadership has changed. |
| 180 | * |
| 181 | * @param isLeader true if this instance is now the leader, otherwise false |
| 182 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 183 | public void leaderChanged(boolean isLeader) { |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 184 | log.debug("SDN-IP Leader changed: {}", isLeader); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 185 | |
| 186 | if (!isLeader) { |
| 187 | this.isElectedLeader = false; |
| 188 | this.isActivatedLeader = false; |
| 189 | return; // Nothing to do |
| 190 | } |
| 191 | this.isActivatedLeader = false; |
| 192 | this.isElectedLeader = true; |
| 193 | |
| 194 | // |
| 195 | // Tell the Intents Synchronizer thread to start the synchronization |
| 196 | // |
| 197 | intentsSynchronizerSemaphore.release(); |
| 198 | } |
| 199 | |
| 200 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 201 | * Gets the route intents. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 202 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 203 | * @return the route intents |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 204 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 205 | public Collection<MultiPointToSinglePointIntent> getRouteIntents() { |
| 206 | List<MultiPointToSinglePointIntent> result = new LinkedList<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 207 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 208 | for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 209 | routeIntents.entrySet()) { |
| 210 | result.add(entry.getValue()); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 211 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 212 | return result; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Thread for Intent Synchronization. |
| 217 | */ |
| 218 | private void doIntentSynchronizationThread() { |
| 219 | boolean interrupted = false; |
| 220 | try { |
| 221 | while (!interrupted) { |
| 222 | try { |
| 223 | intentsSynchronizerSemaphore.acquire(); |
| 224 | // |
| 225 | // Drain all permits, because a single synchronization is |
| 226 | // sufficient. |
| 227 | // |
| 228 | intentsSynchronizerSemaphore.drainPermits(); |
| 229 | } catch (InterruptedException e) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 230 | interrupted = true; |
| 231 | break; |
| 232 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 233 | synchronizeIntents(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 234 | } |
| 235 | } finally { |
| 236 | if (interrupted) { |
| 237 | Thread.currentThread().interrupt(); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 243 | * Submits a collection of point-to-point intents. |
| 244 | * |
| 245 | * @param intents the intents to submit |
| 246 | */ |
| 247 | void submitPeerIntents(Collection<PointToPointIntent> intents) { |
| 248 | synchronized (this) { |
| 249 | // Store the intents in memory |
| 250 | for (PointToPointIntent intent : intents) { |
| 251 | peerIntents.put(new IntentKey(intent), intent); |
| 252 | } |
| 253 | |
| 254 | // Push the intents |
| 255 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 256 | log.debug("SDN-IP Submitting all Peer Intents..."); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 257 | IntentOperations.Builder builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 258 | for (Intent intent : intents) { |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 259 | builder.addSubmitOperation(intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 260 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 261 | IntentOperations intentOperations = builder.build(); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 262 | log.trace("SDN-IP Submitting intents: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 263 | intentOperations.operations()); |
| 264 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 270 | * Generates a route intent for a prefix, the next hop IP address, and |
| 271 | * the next hop MAC address. |
| 272 | * <p/> |
| 273 | * This method will find the egress interface for the intent. |
| 274 | * Intent will match dst IP prefix and rewrite dst MAC address at all other |
| 275 | * border switches, then forward packets according to dst MAC address. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 276 | * |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 277 | * @param prefix IP prefix of the route to add |
| 278 | * @param nextHopIpAddress IP address of the next hop |
| 279 | * @param nextHopMacAddress MAC address of the next hop |
| 280 | * @return the generated intent, or null if no intent should be submitted |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 281 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 282 | private MultiPointToSinglePointIntent generateRouteIntent( |
| 283 | IpPrefix prefix, |
| 284 | IpAddress nextHopIpAddress, |
| 285 | MacAddress nextHopMacAddress) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 286 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 287 | // Find the attachment point (egress interface) of the next hop |
| 288 | Interface egressInterface; |
| 289 | if (configService.getBgpPeers().containsKey(nextHopIpAddress)) { |
| 290 | // Route to a peer |
| 291 | log.debug("Route to peer {}", nextHopIpAddress); |
| 292 | BgpPeer peer = |
| 293 | configService.getBgpPeers().get(nextHopIpAddress); |
| 294 | egressInterface = |
| 295 | interfaceService.getInterface(peer.connectPoint()); |
| 296 | } else { |
| 297 | // Route to non-peer |
| 298 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 299 | egressInterface = |
| 300 | interfaceService.getMatchingInterface(nextHopIpAddress); |
| 301 | if (egressInterface == null) { |
| 302 | log.warn("No outgoing interface found for {}", |
| 303 | nextHopIpAddress); |
| 304 | return null; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // |
| 309 | // Generate the intent itself |
| 310 | // |
| 311 | Set<ConnectPoint> ingressPorts = new HashSet<>(); |
| 312 | ConnectPoint egressPort = egressInterface.connectPoint(); |
| 313 | log.debug("Generating intent for prefix {}, next hop mac {}", |
| 314 | prefix, nextHopMacAddress); |
| 315 | |
| 316 | for (Interface intf : interfaceService.getInterfaces()) { |
| 317 | if (!intf.connectPoint().equals(egressInterface.connectPoint())) { |
| 318 | ConnectPoint srcPort = intf.connectPoint(); |
| 319 | ingressPorts.add(srcPort); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Match the destination IP prefix at the first hop |
| 324 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 325 | if (prefix.version() == Ip4Address.VERSION) { |
| 326 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 327 | } else { |
| 328 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 329 | } |
| 330 | selector.matchIPDst(prefix); |
| 331 | |
| 332 | // Rewrite the destination MAC address |
| 333 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
| 334 | .setEthDst(nextHopMacAddress); |
| 335 | if (!egressInterface.vlan().equals(VlanId.NONE)) { |
| 336 | treatment.setVlanId(egressInterface.vlan()); |
| 337 | // If we set VLAN ID, we have to make sure a VLAN tag exists. |
| 338 | // TODO support no VLAN -> VLAN routing |
| 339 | selector.matchVlanId(VlanId.ANY); |
| 340 | } |
| 341 | |
| 342 | return new MultiPointToSinglePointIntent(appId, selector.build(), |
| 343 | treatment.build(), |
| 344 | ingressPorts, egressPort); |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 349 | // |
| 350 | // NOTE: Semantically, we MUST withdraw existing intents before |
| 351 | // submitting new intents. |
| 352 | // |
| 353 | synchronized (this) { |
| 354 | MultiPointToSinglePointIntent intent; |
| 355 | |
| 356 | log.debug("SDN-IP submitting intents = {} withdrawing = {}", |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 357 | updates.size(), withdraws.size()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 358 | |
| 359 | // |
| 360 | // Prepare the Intent batch operations for the intents to withdraw |
| 361 | // |
| 362 | IntentOperations.Builder withdrawBuilder = |
| 363 | IntentOperations.builder(appId); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 364 | for (FibUpdate withdraw : withdraws) { |
| 365 | checkArgument(withdraw.type() == FibUpdate.Type.DELETE, |
| 366 | "FibUpdate with wrong type in withdraws list"); |
| 367 | |
| 368 | IpPrefix prefix = withdraw.entry().prefix(); |
| 369 | |
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); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 378 | withdrawBuilder.addWithdrawOperation(intent.id()); |
| 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 | // |
| 385 | IntentOperations.Builder submitBuilder = |
| 386 | IntentOperations.builder(appId); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame^] | 387 | for (FibUpdate update : updates) { |
| 388 | checkArgument(update.type() == FibUpdate.Type.UPDATE, |
| 389 | "FibUpdate with wrong type in updates list"); |
| 390 | |
| 391 | IpPrefix prefix = update.entry().prefix(); |
| 392 | intent = generateRouteIntent(prefix, update.entry().nextHopIp(), |
| 393 | update.entry().nextHopMac()); |
| 394 | |
| 395 | if (intent == null) { |
| 396 | // This preserves the old semantics - if an intent can't be |
| 397 | // generated, we don't do anything with that prefix. But |
| 398 | // perhaps we should withdraw the old intent anyway? |
| 399 | continue; |
| 400 | } |
| 401 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 402 | MultiPointToSinglePointIntent oldIntent = |
| 403 | routeIntents.put(prefix, intent); |
| 404 | if (isElectedLeader && isActivatedLeader) { |
| 405 | if (oldIntent != null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 406 | log.trace("SDN-IP Withdrawing old intent: {}", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 407 | oldIntent); |
| 408 | withdrawBuilder.addWithdrawOperation(oldIntent.id()); |
| 409 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 410 | log.trace("SDN-IP Submitting intent: {}", intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 411 | submitBuilder.addSubmitOperation(intent); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // |
| 416 | // Submit the Intent operations |
| 417 | // |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 418 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 419 | IntentOperations intentOperations = withdrawBuilder.build(); |
| 420 | if (!intentOperations.operations().isEmpty()) { |
| 421 | log.debug("SDN-IP Withdrawing intents executed"); |
| 422 | intentService.execute(intentOperations); |
| 423 | } |
| 424 | intentOperations = submitBuilder.build(); |
| 425 | if (!intentOperations.operations().isEmpty()) { |
| 426 | log.debug("SDN-IP Submitting intents executed"); |
| 427 | intentService.execute(intentOperations); |
| 428 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 434 | * Synchronize the in-memory Intents with the Intents in the Intent |
| 435 | * framework. |
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 | void synchronizeIntents() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 438 | synchronized (this) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 439 | |
| 440 | Map<IntentKey, Intent> localIntents = new HashMap<>(); |
| 441 | Map<IntentKey, Intent> fetchedIntents = new HashMap<>(); |
| 442 | Collection<Intent> storeInMemoryIntents = new LinkedList<>(); |
| 443 | Collection<Intent> addIntents = new LinkedList<>(); |
| 444 | Collection<Intent> deleteIntents = new LinkedList<>(); |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 445 | IntentOperations intentOperations; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 446 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 447 | if (!isElectedLeader) { |
| 448 | return; // Nothing to do: not the leader anymore |
| 449 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 450 | log.debug("SDN-IP synchronizing all intents..."); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 451 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 452 | // Prepare the local intents |
| 453 | for (Intent intent : routeIntents.values()) { |
| 454 | localIntents.put(new IntentKey(intent), intent); |
| 455 | } |
| 456 | for (Intent intent : peerIntents.values()) { |
| 457 | localIntents.put(new IntentKey(intent), intent); |
| 458 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 459 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 460 | // Fetch all intents for this application |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 461 | for (Intent intent : intentService.getIntents()) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 462 | if (!intent.appId().equals(appId)) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 463 | continue; |
| 464 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 465 | fetchedIntents.put(new IntentKey(intent), intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 466 | } |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 467 | if (log.isDebugEnabled()) { |
| 468 | for (Intent intent: fetchedIntents.values()) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 469 | log.trace("SDN-IP Intent Synchronizer: fetched intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 470 | intent); |
| 471 | } |
| 472 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 473 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 474 | computeIntentsDelta(localIntents, fetchedIntents, |
| 475 | storeInMemoryIntents, addIntents, |
| 476 | deleteIntents); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 477 | |
| 478 | // |
| 479 | // Perform the actions: |
| 480 | // 1. Store in memory fetched intents that are same. Can be done |
| 481 | // even if we are not the leader anymore |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 482 | // 2. Delete intents: check if the leader before the operation |
| 483 | // 3. Add intents: check if the leader before the operation |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 484 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 485 | for (Intent intent : storeInMemoryIntents) { |
| 486 | // Store the intent in memory based on its type |
| 487 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 488 | MultiPointToSinglePointIntent mp2pIntent = |
| 489 | (MultiPointToSinglePointIntent) intent; |
| 490 | // Find the IP prefix |
| 491 | Criterion c = |
| 492 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 493 | if (c == null) { |
| 494 | // Try IPv6 |
| 495 | c = |
| 496 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST); |
| 497 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 498 | if (c != null && c instanceof IPCriterion) { |
| 499 | IPCriterion ipCriterion = (IPCriterion) c; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 500 | IpPrefix ipPrefix = ipCriterion.ip(); |
| 501 | if (ipPrefix == null) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 502 | continue; |
| 503 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 504 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 505 | "in-memory Route Intent for prefix {}", |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 506 | ipPrefix); |
| 507 | routeIntents.put(ipPrefix, mp2pIntent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 508 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 509 | 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] | 510 | mp2pIntent.id()); |
| 511 | } |
| 512 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 513 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 514 | if (intent instanceof PointToPointIntent) { |
| 515 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 516 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 517 | "in-memory Peer Intent {}", p2pIntent); |
| 518 | peerIntents.put(new IntentKey(intent), p2pIntent); |
| 519 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 520 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // Withdraw Intents |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 524 | IntentOperations.Builder builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 525 | for (Intent intent : deleteIntents) { |
| 526 | builder.addWithdrawOperation(intent.id()); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 527 | log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 528 | intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 529 | } |
| 530 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 531 | log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 532 | "not elected leader anymore"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 533 | isActivatedLeader = false; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 534 | return; |
| 535 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 536 | intentOperations = builder.build(); |
| 537 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 538 | |
| 539 | // Add Intents |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 540 | builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 541 | for (Intent intent : addIntents) { |
| 542 | builder.addSubmitOperation(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 543 | log.trace("SDN-IP Intent Synchronizer: submitting intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 544 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 545 | } |
| 546 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 547 | log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 548 | "not elected leader anymore"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 549 | isActivatedLeader = false; |
| 550 | return; |
| 551 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 552 | intentOperations = builder.build(); |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 553 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 554 | |
| 555 | if (isElectedLeader) { |
| 556 | isActivatedLeader = true; // Allow push of Intents |
| 557 | } else { |
| 558 | isActivatedLeader = false; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 559 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 560 | log.debug("SDN-IP intent synchronization completed"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
| 564 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 565 | * Computes the delta in two sets of Intents: local in-memory Intents, |
| 566 | * and intents fetched from the Intent framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 567 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 568 | * @param localIntents the local in-memory Intents |
| 569 | * @param fetchedIntents the Intents fetched from the Intent framework |
| 570 | * @param storeInMemoryIntents the Intents that should be stored in memory. |
| 571 | * Note: This Collection must be allocated by the caller, and it will |
| 572 | * be populated by this method. |
| 573 | * @param addIntents the Intents that should be added to the Intent |
| 574 | * framework. Note: This Collection must be allocated by the caller, and |
| 575 | * it will be populated by this method. |
| 576 | * @param deleteIntents the Intents that should be deleted from the Intent |
| 577 | * framework. Note: This Collection must be allocated by the caller, and |
| 578 | * it will be populated by this method. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 579 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 580 | private void computeIntentsDelta( |
| 581 | final Map<IntentKey, Intent> localIntents, |
| 582 | final Map<IntentKey, Intent> fetchedIntents, |
| 583 | Collection<Intent> storeInMemoryIntents, |
| 584 | Collection<Intent> addIntents, |
| 585 | Collection<Intent> deleteIntents) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 586 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 587 | // |
| 588 | // Compute the deltas between the LOCAL in-memory Intents and the |
| 589 | // FETCHED Intents: |
| 590 | // - If an Intent is in both the LOCAL and FETCHED sets: |
| 591 | // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then |
| 592 | // the LOCAL Intent should be added/installed; otherwise the |
| 593 | // FETCHED intent should be stored in the local memory |
| 594 | // (i.e., override the LOCAL Intent) to preserve the original |
| 595 | // Intent ID. |
| 596 | // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL |
| 597 | // Intent should be added/installed. |
| 598 | // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED |
| 599 | // Intent should be deleted/withdrawn. |
| 600 | // |
| 601 | for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) { |
| 602 | IntentKey intentKey = entry.getKey(); |
| 603 | Intent localIntent = entry.getValue(); |
| 604 | Intent fetchedIntent = fetchedIntents.get(intentKey); |
| 605 | |
| 606 | if (fetchedIntent == null) { |
| 607 | // |
| 608 | // No FETCHED Intent found: push the LOCAL Intent. |
| 609 | // |
| 610 | addIntents.add(localIntent); |
| 611 | continue; |
| 612 | } |
| 613 | |
| 614 | IntentState state = |
| 615 | intentService.getIntentState(fetchedIntent.id()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 616 | if (state == null || |
| 617 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 618 | state == IntentState.WITHDRAWN) { |
| 619 | // The intent has been withdrawn but according to our route |
| 620 | // table it should be installed. We'll reinstall it. |
| 621 | addIntents.add(localIntent); |
| 622 | continue; |
| 623 | } |
| 624 | storeInMemoryIntents.add(fetchedIntent); |
| 625 | } |
| 626 | |
| 627 | for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) { |
| 628 | IntentKey intentKey = entry.getKey(); |
| 629 | Intent fetchedIntent = entry.getValue(); |
| 630 | Intent localIntent = localIntents.get(intentKey); |
| 631 | |
| 632 | if (localIntent != null) { |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | IntentState state = |
| 637 | intentService.getIntentState(fetchedIntent.id()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 638 | if (state == null || |
| 639 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 640 | state == IntentState.WITHDRAWN) { |
| 641 | // Nothing to do. The intent has been already withdrawn. |
| 642 | continue; |
| 643 | } |
| 644 | // |
| 645 | // No LOCAL Intent found: delete/withdraw the FETCHED Intent. |
| 646 | // |
| 647 | deleteIntents.add(fetchedIntent); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Helper class that can be used to compute the key for an Intent by |
| 653 | * by excluding the Intent ID. |
| 654 | */ |
| 655 | static final class IntentKey { |
| 656 | private final Intent intent; |
| 657 | |
| 658 | /** |
| 659 | * Constructor. |
| 660 | * |
| 661 | * @param intent the intent to use |
| 662 | */ |
| 663 | IntentKey(Intent intent) { |
| 664 | checkArgument((intent instanceof MultiPointToSinglePointIntent) || |
| 665 | (intent instanceof PointToPointIntent), |
| 666 | "Intent type not recognized", intent); |
| 667 | this.intent = intent; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Compares two Multi-Point to Single-Point Intents whether they |
| 672 | * represent same logical intention. |
| 673 | * |
| 674 | * @param intent1 the first Intent to compare |
| 675 | * @param intent2 the second Intent to compare |
| 676 | * @return true if both Intents represent same logical intention, |
| 677 | * otherwise false |
| 678 | */ |
| 679 | static boolean equalIntents(MultiPointToSinglePointIntent intent1, |
| 680 | MultiPointToSinglePointIntent intent2) { |
| 681 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 682 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 683 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 684 | Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) && |
| 685 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Compares two Point-to-Point Intents whether they represent |
| 690 | * same logical intention. |
| 691 | * |
| 692 | * @param intent1 the first Intent to compare |
| 693 | * @param intent2 the second Intent to compare |
| 694 | * @return true if both Intents represent same logical intention, |
| 695 | * otherwise false |
| 696 | */ |
| 697 | static boolean equalIntents(PointToPointIntent intent1, |
| 698 | PointToPointIntent intent2) { |
| 699 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 700 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 701 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 702 | Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) && |
| 703 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 704 | } |
| 705 | |
| 706 | @Override |
| 707 | public int hashCode() { |
| 708 | if (intent instanceof PointToPointIntent) { |
| 709 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
| 710 | return Objects.hash(p2pIntent.appId(), |
| 711 | p2pIntent.resources(), |
| 712 | p2pIntent.selector(), |
| 713 | p2pIntent.treatment(), |
| 714 | p2pIntent.constraints(), |
| 715 | p2pIntent.ingressPoint(), |
| 716 | p2pIntent.egressPoint()); |
| 717 | } |
| 718 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 719 | MultiPointToSinglePointIntent m2pIntent = |
| 720 | (MultiPointToSinglePointIntent) intent; |
| 721 | return Objects.hash(m2pIntent.appId(), |
| 722 | m2pIntent.resources(), |
| 723 | m2pIntent.selector(), |
| 724 | m2pIntent.treatment(), |
| 725 | m2pIntent.constraints(), |
| 726 | m2pIntent.ingressPoints(), |
| 727 | m2pIntent.egressPoint()); |
| 728 | } |
| 729 | checkArgument(false, "Intent type not recognized", intent); |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | @Override |
| 734 | public boolean equals(Object obj) { |
| 735 | if (this == obj) { |
| 736 | return true; |
| 737 | } |
| 738 | if ((obj == null) || (!(obj instanceof IntentKey))) { |
| 739 | return false; |
| 740 | } |
| 741 | IntentKey other = (IntentKey) obj; |
| 742 | |
| 743 | if (this.intent instanceof PointToPointIntent) { |
| 744 | if (!(other.intent instanceof PointToPointIntent)) { |
| 745 | return false; |
| 746 | } |
| 747 | return equalIntents((PointToPointIntent) this.intent, |
| 748 | (PointToPointIntent) other.intent); |
| 749 | } |
| 750 | if (this.intent instanceof MultiPointToSinglePointIntent) { |
| 751 | if (!(other.intent instanceof MultiPointToSinglePointIntent)) { |
| 752 | return false; |
| 753 | } |
| 754 | return equalIntents( |
| 755 | (MultiPointToSinglePointIntent) this.intent, |
| 756 | (MultiPointToSinglePointIntent) other.intent); |
| 757 | } |
| 758 | checkArgument(false, "Intent type not recognized", intent); |
| 759 | return false; |
| 760 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 761 | } |
| 762 | } |