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