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 | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 38 | import org.onosproject.routingapi.FibListener; |
| 39 | import org.onosproject.routingapi.FibUpdate; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 40 | import org.onosproject.sdnip.config.BgpPeer; |
| 41 | import org.onosproject.sdnip.config.Interface; |
| 42 | import org.onosproject.sdnip.config.SdnIpConfigurationService; |
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 | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 82 | private final SdnIpConfigurationService configService; |
| 83 | private final InterfaceService interfaceService; |
| 84 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Class constructor. |
| 87 | * |
| 88 | * @param appId the Application ID |
| 89 | * @param intentService the intent service |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 90 | * @param configService the SDN-IP configuration service |
| 91 | * @param interfaceService the interface service |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 92 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 93 | IntentSynchronizer(ApplicationId appId, IntentService intentService, |
| 94 | SdnIpConfigurationService configService, |
| 95 | InterfaceService interfaceService) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 96 | this.appId = appId; |
| 97 | this.intentService = intentService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 98 | peerIntents = new ConcurrentHashMap<>(); |
| 99 | routeIntents = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 100 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 101 | this.configService = configService; |
| 102 | this.interfaceService = interfaceService; |
| 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 = |
| 292 | interfaceService.getInterface(peer.connectPoint()); |
| 293 | } else { |
| 294 | // Route to non-peer |
| 295 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 296 | egressInterface = |
| 297 | interfaceService.getMatchingInterface(nextHopIpAddress); |
| 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 | |
| 313 | for (Interface intf : interfaceService.getInterfaces()) { |
| 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(); |
| 322 | if (prefix.version() == Ip4Address.VERSION) { |
| 323 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 324 | } else { |
| 325 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 326 | } |
| 327 | selector.matchIPDst(prefix); |
| 328 | |
| 329 | // Rewrite the destination MAC address |
| 330 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
| 331 | .setEthDst(nextHopMacAddress); |
| 332 | if (!egressInterface.vlan().equals(VlanId.NONE)) { |
| 333 | treatment.setVlanId(egressInterface.vlan()); |
| 334 | // If we set VLAN ID, we have to make sure a VLAN tag exists. |
| 335 | // TODO support no VLAN -> VLAN routing |
| 336 | selector.matchVlanId(VlanId.ANY); |
| 337 | } |
| 338 | |
| 339 | return new MultiPointToSinglePointIntent(appId, selector.build(), |
| 340 | treatment.build(), |
| 341 | ingressPorts, egressPort); |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 346 | // |
| 347 | // NOTE: Semantically, we MUST withdraw existing intents before |
| 348 | // submitting new intents. |
| 349 | // |
| 350 | synchronized (this) { |
| 351 | MultiPointToSinglePointIntent intent; |
| 352 | |
| 353 | log.debug("SDN-IP submitting intents = {} withdrawing = {}", |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 354 | updates.size(), withdraws.size()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 355 | |
| 356 | // |
| 357 | // Prepare the Intent batch operations for the intents to withdraw |
| 358 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 359 | for (FibUpdate withdraw : withdraws) { |
| 360 | checkArgument(withdraw.type() == FibUpdate.Type.DELETE, |
| 361 | "FibUpdate with wrong type in withdraws list"); |
| 362 | |
| 363 | IpPrefix prefix = withdraw.entry().prefix(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 364 | intent = routeIntents.remove(prefix); |
| 365 | if (intent == null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 366 | log.trace("SDN-IP No intent in routeIntents to delete " + |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 367 | "for prefix: {}", prefix); |
| 368 | continue; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 369 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 370 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 371 | log.trace("SDN-IP Withdrawing intent: {}", intent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 372 | intentService.withdraw(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 373 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 376 | // |
| 377 | // Prepare the Intent batch operations for the intents to submit |
| 378 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 379 | for (FibUpdate update : updates) { |
| 380 | checkArgument(update.type() == FibUpdate.Type.UPDATE, |
| 381 | "FibUpdate with wrong type in updates list"); |
| 382 | |
| 383 | IpPrefix prefix = update.entry().prefix(); |
| 384 | intent = generateRouteIntent(prefix, update.entry().nextHopIp(), |
| 385 | update.entry().nextHopMac()); |
| 386 | |
| 387 | if (intent == null) { |
| 388 | // This preserves the old semantics - if an intent can't be |
| 389 | // generated, we don't do anything with that prefix. But |
| 390 | // perhaps we should withdraw the old intent anyway? |
| 391 | continue; |
| 392 | } |
| 393 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 394 | MultiPointToSinglePointIntent oldIntent = |
| 395 | routeIntents.put(prefix, intent); |
| 396 | if (isElectedLeader && isActivatedLeader) { |
| 397 | if (oldIntent != null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 398 | log.trace("SDN-IP Withdrawing old intent: {}", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 399 | oldIntent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 400 | intentService.withdraw(oldIntent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 401 | } |
Jonathan Hart | b3b8a0a | 2015-02-12 17:36:13 -0800 | [diff] [blame] | 402 | log.trace("SDN-IP Submitting intent: {}", intent); |
| 403 | intentService.submit(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 404 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 410 | * Synchronize the in-memory Intents with the Intents in the Intent |
| 411 | * framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 412 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 413 | void synchronizeIntents() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 414 | synchronized (this) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 415 | |
| 416 | Map<IntentKey, Intent> localIntents = new HashMap<>(); |
| 417 | Map<IntentKey, Intent> fetchedIntents = new HashMap<>(); |
| 418 | Collection<Intent> storeInMemoryIntents = new LinkedList<>(); |
| 419 | Collection<Intent> addIntents = new LinkedList<>(); |
| 420 | Collection<Intent> deleteIntents = new LinkedList<>(); |
| 421 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 422 | if (!isElectedLeader) { |
| 423 | return; // Nothing to do: not the leader anymore |
| 424 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 425 | log.debug("SDN-IP synchronizing all intents..."); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 426 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 427 | // Prepare the local intents |
| 428 | for (Intent intent : routeIntents.values()) { |
| 429 | localIntents.put(new IntentKey(intent), intent); |
| 430 | } |
| 431 | for (Intent intent : peerIntents.values()) { |
| 432 | localIntents.put(new IntentKey(intent), intent); |
| 433 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 434 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 435 | // Fetch all intents for this application |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 436 | for (Intent intent : intentService.getIntents()) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 437 | if (!intent.appId().equals(appId)) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 438 | continue; |
| 439 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 440 | fetchedIntents.put(new IntentKey(intent), intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 441 | } |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 442 | if (log.isDebugEnabled()) { |
| 443 | for (Intent intent: fetchedIntents.values()) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 444 | log.trace("SDN-IP Intent Synchronizer: fetched intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 445 | intent); |
| 446 | } |
| 447 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 448 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 449 | computeIntentsDelta(localIntents, fetchedIntents, |
| 450 | storeInMemoryIntents, addIntents, |
| 451 | deleteIntents); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 452 | |
| 453 | // |
| 454 | // Perform the actions: |
| 455 | // 1. Store in memory fetched intents that are same. Can be done |
| 456 | // even if we are not the leader anymore |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 457 | // 2. Delete intents: check if the leader before the operation |
| 458 | // 3. Add intents: check if the leader before the operation |
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 | for (Intent intent : storeInMemoryIntents) { |
| 461 | // Store the intent in memory based on its type |
| 462 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 463 | MultiPointToSinglePointIntent mp2pIntent = |
| 464 | (MultiPointToSinglePointIntent) intent; |
| 465 | // Find the IP prefix |
| 466 | Criterion c = |
| 467 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 468 | if (c == null) { |
| 469 | // Try IPv6 |
| 470 | c = |
| 471 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST); |
| 472 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 473 | if (c != null && c instanceof IPCriterion) { |
| 474 | IPCriterion ipCriterion = (IPCriterion) c; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 475 | IpPrefix ipPrefix = ipCriterion.ip(); |
| 476 | if (ipPrefix == null) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 477 | continue; |
| 478 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 479 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 480 | "in-memory Route Intent for prefix {}", |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 481 | ipPrefix); |
| 482 | routeIntents.put(ipPrefix, mp2pIntent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 483 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 484 | 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] | 485 | mp2pIntent.id()); |
| 486 | } |
| 487 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 488 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 489 | if (intent instanceof PointToPointIntent) { |
| 490 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 491 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 492 | "in-memory Peer Intent {}", p2pIntent); |
| 493 | peerIntents.put(new IntentKey(intent), p2pIntent); |
| 494 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 495 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Withdraw Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 499 | for (Intent intent : deleteIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 500 | intentService.withdraw(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 501 | log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 502 | intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 503 | } |
| 504 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 505 | log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 506 | "not elected leader anymore"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 507 | isActivatedLeader = false; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 508 | return; |
| 509 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 510 | |
| 511 | // Add Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 512 | for (Intent intent : addIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 513 | intentService.submit(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 514 | log.trace("SDN-IP Intent Synchronizer: submitting intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 515 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 516 | } |
| 517 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 518 | log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 519 | "not elected leader anymore"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 520 | isActivatedLeader = false; |
| 521 | return; |
| 522 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 523 | |
| 524 | if (isElectedLeader) { |
| 525 | isActivatedLeader = true; // Allow push of Intents |
| 526 | } else { |
| 527 | isActivatedLeader = false; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 528 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 529 | log.debug("SDN-IP intent synchronization completed"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 534 | * Computes the delta in two sets of Intents: local in-memory Intents, |
| 535 | * and intents fetched from the Intent framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 536 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 537 | * @param localIntents the local in-memory Intents |
| 538 | * @param fetchedIntents the Intents fetched from the Intent framework |
| 539 | * @param storeInMemoryIntents the Intents that should be stored in memory. |
| 540 | * Note: This Collection must be allocated by the caller, and it will |
| 541 | * be populated by this method. |
| 542 | * @param addIntents the Intents that should be added to the Intent |
| 543 | * framework. Note: This Collection must be allocated by the caller, and |
| 544 | * it will be populated by this method. |
| 545 | * @param deleteIntents the Intents that should be deleted from the Intent |
| 546 | * framework. Note: This Collection must be allocated by the caller, and |
| 547 | * it will be populated by this method. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 548 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 549 | private void computeIntentsDelta( |
| 550 | final Map<IntentKey, Intent> localIntents, |
| 551 | final Map<IntentKey, Intent> fetchedIntents, |
| 552 | Collection<Intent> storeInMemoryIntents, |
| 553 | Collection<Intent> addIntents, |
| 554 | Collection<Intent> deleteIntents) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 555 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 556 | // |
| 557 | // Compute the deltas between the LOCAL in-memory Intents and the |
| 558 | // FETCHED Intents: |
| 559 | // - If an Intent is in both the LOCAL and FETCHED sets: |
| 560 | // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then |
| 561 | // the LOCAL Intent should be added/installed; otherwise the |
| 562 | // FETCHED intent should be stored in the local memory |
| 563 | // (i.e., override the LOCAL Intent) to preserve the original |
| 564 | // Intent ID. |
| 565 | // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL |
| 566 | // Intent should be added/installed. |
| 567 | // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED |
| 568 | // Intent should be deleted/withdrawn. |
| 569 | // |
| 570 | for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) { |
| 571 | IntentKey intentKey = entry.getKey(); |
| 572 | Intent localIntent = entry.getValue(); |
| 573 | Intent fetchedIntent = fetchedIntents.get(intentKey); |
| 574 | |
| 575 | if (fetchedIntent == null) { |
| 576 | // |
| 577 | // No FETCHED Intent found: push the LOCAL Intent. |
| 578 | // |
| 579 | addIntents.add(localIntent); |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | IntentState state = |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 584 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 585 | if (state == null || |
| 586 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 587 | state == IntentState.WITHDRAWN) { |
| 588 | // The intent has been withdrawn but according to our route |
| 589 | // table it should be installed. We'll reinstall it. |
| 590 | addIntents.add(localIntent); |
| 591 | continue; |
| 592 | } |
| 593 | storeInMemoryIntents.add(fetchedIntent); |
| 594 | } |
| 595 | |
| 596 | for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) { |
| 597 | IntentKey intentKey = entry.getKey(); |
| 598 | Intent fetchedIntent = entry.getValue(); |
| 599 | Intent localIntent = localIntents.get(intentKey); |
| 600 | |
| 601 | if (localIntent != null) { |
| 602 | continue; |
| 603 | } |
| 604 | |
| 605 | IntentState state = |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 606 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 607 | if (state == null || |
| 608 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 609 | state == IntentState.WITHDRAWN) { |
| 610 | // Nothing to do. The intent has been already withdrawn. |
| 611 | continue; |
| 612 | } |
| 613 | // |
| 614 | // No LOCAL Intent found: delete/withdraw the FETCHED Intent. |
| 615 | // |
| 616 | deleteIntents.add(fetchedIntent); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Helper class that can be used to compute the key for an Intent by |
| 622 | * by excluding the Intent ID. |
| 623 | */ |
| 624 | static final class IntentKey { |
| 625 | private final Intent intent; |
| 626 | |
| 627 | /** |
| 628 | * Constructor. |
| 629 | * |
| 630 | * @param intent the intent to use |
| 631 | */ |
| 632 | IntentKey(Intent intent) { |
| 633 | checkArgument((intent instanceof MultiPointToSinglePointIntent) || |
| 634 | (intent instanceof PointToPointIntent), |
| 635 | "Intent type not recognized", intent); |
| 636 | this.intent = intent; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Compares two Multi-Point to Single-Point Intents whether they |
| 641 | * represent same logical intention. |
| 642 | * |
| 643 | * @param intent1 the first Intent to compare |
| 644 | * @param intent2 the second Intent to compare |
| 645 | * @return true if both Intents represent same logical intention, |
| 646 | * otherwise false |
| 647 | */ |
| 648 | static boolean equalIntents(MultiPointToSinglePointIntent intent1, |
| 649 | MultiPointToSinglePointIntent intent2) { |
| 650 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 651 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 652 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 653 | Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) && |
| 654 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Compares two Point-to-Point Intents whether they represent |
| 659 | * same logical intention. |
| 660 | * |
| 661 | * @param intent1 the first Intent to compare |
| 662 | * @param intent2 the second Intent to compare |
| 663 | * @return true if both Intents represent same logical intention, |
| 664 | * otherwise false |
| 665 | */ |
| 666 | static boolean equalIntents(PointToPointIntent intent1, |
| 667 | PointToPointIntent intent2) { |
| 668 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 669 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 670 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 671 | Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) && |
| 672 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 673 | } |
| 674 | |
| 675 | @Override |
| 676 | public int hashCode() { |
| 677 | if (intent instanceof PointToPointIntent) { |
| 678 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
| 679 | return Objects.hash(p2pIntent.appId(), |
| 680 | p2pIntent.resources(), |
| 681 | p2pIntent.selector(), |
| 682 | p2pIntent.treatment(), |
| 683 | p2pIntent.constraints(), |
| 684 | p2pIntent.ingressPoint(), |
| 685 | p2pIntent.egressPoint()); |
| 686 | } |
| 687 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 688 | MultiPointToSinglePointIntent m2pIntent = |
| 689 | (MultiPointToSinglePointIntent) intent; |
| 690 | return Objects.hash(m2pIntent.appId(), |
| 691 | m2pIntent.resources(), |
| 692 | m2pIntent.selector(), |
| 693 | m2pIntent.treatment(), |
| 694 | m2pIntent.constraints(), |
| 695 | m2pIntent.ingressPoints(), |
| 696 | m2pIntent.egressPoint()); |
| 697 | } |
| 698 | checkArgument(false, "Intent type not recognized", intent); |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | @Override |
| 703 | public boolean equals(Object obj) { |
| 704 | if (this == obj) { |
| 705 | return true; |
| 706 | } |
| 707 | if ((obj == null) || (!(obj instanceof IntentKey))) { |
| 708 | return false; |
| 709 | } |
| 710 | IntentKey other = (IntentKey) obj; |
| 711 | |
| 712 | if (this.intent instanceof PointToPointIntent) { |
| 713 | if (!(other.intent instanceof PointToPointIntent)) { |
| 714 | return false; |
| 715 | } |
| 716 | return equalIntents((PointToPointIntent) this.intent, |
| 717 | (PointToPointIntent) other.intent); |
| 718 | } |
| 719 | if (this.intent instanceof MultiPointToSinglePointIntent) { |
| 720 | if (!(other.intent instanceof MultiPointToSinglePointIntent)) { |
| 721 | return false; |
| 722 | } |
| 723 | return equalIntents( |
| 724 | (MultiPointToSinglePointIntent) this.intent, |
| 725 | (MultiPointToSinglePointIntent) other.intent); |
| 726 | } |
| 727 | checkArgument(false, "Intent type not recognized", intent); |
| 728 | return false; |
| 729 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 730 | } |
| 731 | } |