Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 1 | /* |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 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 | */ |
| 16 | |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 17 | package org.onosproject.simplefabric.impl; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Sets; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 23 | import org.osgi.service.component.annotations.Activate; |
| 24 | import org.osgi.service.component.annotations.Component; |
| 25 | import org.osgi.service.component.annotations.Deactivate; |
| 26 | import org.osgi.service.component.annotations.Reference; |
| 27 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.EncapsulationType; |
| 34 | import org.onosproject.net.FilteredConnectPoint; |
| 35 | import org.onosproject.net.Host; |
| 36 | import org.onosproject.net.ResourceGroup; |
| 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
| 39 | import org.onosproject.net.host.HostService; |
| 40 | import org.onosproject.net.intent.Constraint; |
| 41 | import org.onosproject.net.intent.Intent; |
| 42 | import org.onosproject.net.intent.IntentService; |
| 43 | import org.onosproject.net.intent.Key; |
| 44 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 45 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
| 46 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
| 47 | import org.onosproject.net.intent.constraint.PartialFailureConstraint; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 48 | import org.onosproject.net.intf.Interface; |
| 49 | import org.onosproject.simplefabric.api.FabricNetwork; |
Jian Li | c7efc1d | 2018-08-23 16:37:34 +0900 | [diff] [blame] | 50 | import org.onosproject.simplefabric.api.SimpleFabricEvent; |
| 51 | import org.onosproject.simplefabric.api.SimpleFabricListener; |
| 52 | import org.onosproject.simplefabric.api.SimpleFabricService; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
| 55 | |
| 56 | import java.io.PrintStream; |
| 57 | import java.util.ArrayList; |
| 58 | import java.util.Collection; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 59 | import java.util.HashSet; |
| 60 | import java.util.List; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 61 | import java.util.Map; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 62 | import java.util.Objects; |
| 63 | import java.util.Set; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 64 | import java.util.stream.Collectors; |
| 65 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 66 | import static org.onosproject.simplefabric.api.Constants.FORWARDING_APP_ID; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 67 | import static org.onosproject.simplefabric.api.Constants.PRI_L2NETWORK_BROADCAST; |
| 68 | import static org.onosproject.simplefabric.api.Constants.PRI_L2NETWORK_UNICAST; |
| 69 | |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * An implementation of L2NetworkOperationService. |
| 73 | * Handles the execution order of the L2 Network operations generated by the |
| 74 | * application. |
| 75 | */ |
| 76 | @Component(immediate = true, enabled = false) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 77 | public class SimpleFabricForwarding { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 78 | |
| 79 | public static final String BROADCAST = "BCAST"; |
| 80 | public static final String UNICAST = "UNI"; |
| 81 | |
| 82 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 83 | protected ApplicationId appId; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 84 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 86 | protected CoreService coreService; |
| 87 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 89 | protected IntentService intentService; |
| 90 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 92 | protected HostService hostService; |
| 93 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 95 | protected SimpleFabricService simpleFabric; |
| 96 | |
| 97 | public static final ImmutableList<Constraint> L2NETWORK_CONSTRAINTS = |
| 98 | ImmutableList.of(new PartialFailureConstraint()); |
| 99 | |
| 100 | private Map<Key, SinglePointToMultiPointIntent> bctIntentsMap = Maps.newConcurrentMap(); |
| 101 | private Map<Key, MultiPointToSinglePointIntent> uniIntentsMap = Maps.newConcurrentMap(); |
| 102 | private Set<Key> toBePurgedIntentKeys = new HashSet<>(); |
| 103 | |
| 104 | private final InternalSimpleFabricListener simpleFabricListener = new InternalSimpleFabricListener(); |
| 105 | |
| 106 | @Activate |
| 107 | public void activate() { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 108 | appId = coreService.registerApplication(FORWARDING_APP_ID); |
| 109 | log.info("simple fabric forwarding starting with l2net app id {}", appId.toString()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 110 | |
| 111 | simpleFabric.addListener(simpleFabricListener); |
| 112 | |
| 113 | refresh(); |
| 114 | checkIntentsPurge(); |
| 115 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 116 | log.info("simple fabric forwarding started"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Deactivate |
| 120 | public void deactivate() { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 121 | log.info("simple fabric forwarding stopping"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 122 | |
| 123 | simpleFabric.removeListener(simpleFabricListener); |
| 124 | |
| 125 | for (Intent intent : bctIntentsMap.values()) { |
| 126 | intentService.withdraw(intent); |
| 127 | toBePurgedIntentKeys.add(intent.key()); |
| 128 | } |
| 129 | for (Intent intent : uniIntentsMap.values()) { |
| 130 | intentService.withdraw(intent); |
| 131 | toBePurgedIntentKeys.add(intent.key()); |
| 132 | } |
| 133 | for (Key key : toBePurgedIntentKeys) { |
| 134 | Intent intentToPurge = intentService.getIntent(key); |
| 135 | if (intentToPurge != null) { |
| 136 | intentService.purge(intentToPurge); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // do not set clear for switch compatibility |
| 141 | //bctIntentsMap.clear(); |
| 142 | //uniIntentsMap.clear(); |
| 143 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 144 | log.info("simple fabric forwarding stopped"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private void refresh() { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 148 | log.debug("simple fabric forwarding refresh"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 149 | |
| 150 | Map<Key, SinglePointToMultiPointIntent> newBctIntentsMap = Maps.newConcurrentMap(); |
| 151 | Map<Key, MultiPointToSinglePointIntent> newUniIntentsMap = Maps.newConcurrentMap(); |
| 152 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 153 | for (FabricNetwork fabricNetwork : simpleFabric.fabricNetworks()) { |
| 154 | // scans all l2network regardless of isDirty flag |
| 155 | // if fabricNetwork.isForward == false or number of interfaces() < 2, no Intents generated |
| 156 | for (SinglePointToMultiPointIntent intent : buildBrcIntents(fabricNetwork)) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 157 | newBctIntentsMap.put(intent.key(), intent); |
| 158 | } |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 159 | for (MultiPointToSinglePointIntent intent : |
| 160 | buildUniIntents(fabricNetwork, hostsFromL2Network(fabricNetwork))) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 161 | newUniIntentsMap.put(intent.key(), intent); |
| 162 | } |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 163 | if (fabricNetwork.isDirty()) { |
| 164 | fabricNetwork.setDirty(false); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | boolean bctUpdated = false; |
| 169 | for (SinglePointToMultiPointIntent intent : bctIntentsMap.values()) { |
| 170 | SinglePointToMultiPointIntent newIntent = newBctIntentsMap.get(intent.key()); |
| 171 | if (newIntent == null) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 172 | log.info("simple fabric forwarding withdraw broadcast intent: {}", |
| 173 | intent.key().toString()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 174 | toBePurgedIntentKeys.add(intent.key()); |
| 175 | intentService.withdraw(intent); |
| 176 | bctUpdated = true; |
| 177 | } |
| 178 | } |
| 179 | for (SinglePointToMultiPointIntent intent : newBctIntentsMap.values()) { |
| 180 | SinglePointToMultiPointIntent oldIntent = bctIntentsMap.get(intent.key()); |
| 181 | if (oldIntent == null || |
| 182 | !oldIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()) || |
| 183 | !oldIntent.filteredIngressPoint().equals(intent.filteredIngressPoint()) || |
| 184 | !oldIntent.selector().equals(intent.selector()) || |
| 185 | !oldIntent.treatment().equals(intent.treatment()) || |
| 186 | !oldIntent.constraints().equals(intent.constraints())) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 187 | log.info("simple fabric forwarding submit broadcast intent: {}", |
| 188 | intent.key().toString()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 189 | toBePurgedIntentKeys.remove(intent.key()); |
| 190 | intentService.submit(intent); |
| 191 | bctUpdated = true; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | boolean uniUpdated = false; |
| 196 | for (MultiPointToSinglePointIntent intent : uniIntentsMap.values()) { |
| 197 | MultiPointToSinglePointIntent newIntent = newUniIntentsMap.get(intent.key()); |
| 198 | if (newIntent == null) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 199 | log.info("simple fabric forwarding withdraw unicast intent: {}", |
| 200 | intent.key().toString()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 201 | toBePurgedIntentKeys.add(intent.key()); |
| 202 | intentService.withdraw(intent); |
| 203 | uniUpdated = true; |
| 204 | } |
| 205 | } |
| 206 | for (MultiPointToSinglePointIntent intent : newUniIntentsMap.values()) { |
| 207 | MultiPointToSinglePointIntent oldIntent = uniIntentsMap.get(intent.key()); |
| 208 | if (oldIntent == null || |
| 209 | !oldIntent.filteredEgressPoint().equals(intent.filteredEgressPoint()) || |
| 210 | !oldIntent.filteredIngressPoints().equals(intent.filteredIngressPoints()) || |
| 211 | !oldIntent.selector().equals(intent.selector()) || |
| 212 | !oldIntent.treatment().equals(intent.treatment()) || |
| 213 | !oldIntent.constraints().equals(intent.constraints())) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 214 | log.info("simple fabric forwarding submit unicast intent: {}", |
| 215 | intent.key().toString()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 216 | toBePurgedIntentKeys.remove(intent.key()); |
| 217 | intentService.submit(intent); |
| 218 | uniUpdated = true; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (bctUpdated) { |
| 223 | bctIntentsMap = newBctIntentsMap; |
| 224 | } |
| 225 | if (uniUpdated) { |
| 226 | uniIntentsMap = newUniIntentsMap; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | private void checkIntentsPurge() { |
| 231 | // check intents to be purge |
| 232 | if (!toBePurgedIntentKeys.isEmpty()) { |
| 233 | Set<Key> purgedKeys = new HashSet<>(); |
| 234 | for (Key key : toBePurgedIntentKeys) { |
| 235 | Intent intentToPurge = intentService.getIntent(key); |
| 236 | if (intentToPurge == null) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 237 | log.info("simple fabric forwarding purged intent: key={}", key.toString()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 238 | purgedKeys.add(key); |
| 239 | } else { |
| 240 | switch (intentService.getIntentState(key)) { |
| 241 | case FAILED: |
| 242 | case WITHDRAWN: |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 243 | log.info("simple fabric forwarding try to purge intent: key={}", |
| 244 | key.toString()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 245 | intentService.purge(intentToPurge); |
| 246 | break; |
| 247 | case INSTALL_REQ: |
| 248 | case INSTALLED: |
| 249 | case INSTALLING: |
| 250 | case RECOMPILING: |
| 251 | case COMPILING: |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 252 | log.warn("simple fabric forwarding withdraw intent to purge: key={}", key); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 253 | intentService.withdraw(intentToPurge); |
| 254 | break; |
| 255 | case WITHDRAW_REQ: |
| 256 | case WITHDRAWING: |
| 257 | case PURGE_REQ: |
| 258 | case CORRUPT: |
| 259 | default: |
| 260 | // no action |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | toBePurgedIntentKeys.removeAll(purgedKeys); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // Generates Unicast Intents and broadcast Intents for the L2 Network. |
| 270 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 271 | private Set<Intent> generateL2NetworkIntents(FabricNetwork fabricNetwork) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 272 | return new ImmutableSet.Builder<Intent>() |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 273 | .addAll(buildBrcIntents(fabricNetwork)) |
| 274 | .addAll(buildUniIntents(fabricNetwork, hostsFromL2Network(fabricNetwork))) |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 275 | .build(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // Build Boadcast Intents for a L2 Network. |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 279 | private Set<SinglePointToMultiPointIntent> buildBrcIntents(FabricNetwork fabricNetwork) { |
| 280 | Set<Interface> interfaces = fabricNetwork.interfaces(); |
| 281 | if (interfaces.size() < 2 || !fabricNetwork.isForward() || !fabricNetwork.isBroadcast()) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 282 | return ImmutableSet.of(); |
| 283 | } |
| 284 | Set<SinglePointToMultiPointIntent> brcIntents = Sets.newHashSet(); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 285 | ResourceGroup resourceGroup = ResourceGroup.of(fabricNetwork.name()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 286 | |
| 287 | // Generates broadcast Intents from any network interface to other |
| 288 | // network interface from the L2 Network. |
| 289 | interfaces |
| 290 | .forEach(src -> { |
| 291 | FilteredConnectPoint srcFcp = buildFilteredConnectedPoint(src); |
| 292 | Set<FilteredConnectPoint> dstFcps = interfaces.stream() |
| 293 | .filter(iface -> !iface.equals(src)) |
| 294 | .map(this::buildFilteredConnectedPoint) |
| 295 | .collect(Collectors.toSet()); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 296 | Key key = buildKey(fabricNetwork.name(), "BCAST", |
| 297 | srcFcp.connectPoint(), MacAddress.BROADCAST); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 298 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 299 | .matchEthDst(MacAddress.BROADCAST) |
| 300 | .build(); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 301 | SinglePointToMultiPointIntent.Builder |
| 302 | intentBuilder = SinglePointToMultiPointIntent.builder() |
| 303 | .appId(appId) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 304 | .key(key) |
| 305 | .selector(selector) |
| 306 | .filteredIngressPoint(srcFcp) |
| 307 | .filteredEgressPoints(dstFcps) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 308 | .constraints(buildConstraints(L2NETWORK_CONSTRAINTS, |
| 309 | fabricNetwork.encapsulation())) |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 310 | .priority(PRI_L2NETWORK_BROADCAST) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 311 | .resourceGroup(resourceGroup); |
| 312 | brcIntents.add(intentBuilder.build()); |
| 313 | }); |
| 314 | return brcIntents; |
| 315 | } |
| 316 | |
| 317 | // Builds unicast Intents for a L2 Network. |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 318 | private Set<MultiPointToSinglePointIntent> buildUniIntents(FabricNetwork fabricNetwork, |
| 319 | Set<Host> hosts) { |
| 320 | Set<Interface> interfaces = fabricNetwork.interfaces(); |
| 321 | if (!fabricNetwork.isForward() || interfaces.size() < 2) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 322 | return ImmutableSet.of(); |
| 323 | } |
| 324 | Set<MultiPointToSinglePointIntent> uniIntents = Sets.newHashSet(); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 325 | ResourceGroup resourceGroup = ResourceGroup.of(fabricNetwork.name()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 326 | hosts.forEach(host -> { |
| 327 | FilteredConnectPoint hostFcp = buildFilteredConnectedPoint(host); |
| 328 | Set<FilteredConnectPoint> srcFcps = interfaces.stream() |
| 329 | .map(this::buildFilteredConnectedPoint) |
| 330 | .filter(fcp -> !fcp.equals(hostFcp)) |
| 331 | .collect(Collectors.toSet()); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 332 | Key key = buildKey(fabricNetwork.name(), "UNI", hostFcp.connectPoint(), host.mac()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 333 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 334 | .matchEthDst(host.mac()).build(); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 335 | MultiPointToSinglePointIntent.Builder |
| 336 | intentBuilder = MultiPointToSinglePointIntent.builder() |
| 337 | .appId(appId) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 338 | .key(key) |
| 339 | .selector(selector) |
| 340 | .filteredIngressPoints(srcFcps) |
| 341 | .filteredEgressPoint(hostFcp) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 342 | .constraints(buildConstraints(L2NETWORK_CONSTRAINTS, |
| 343 | fabricNetwork.encapsulation())) |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 344 | .priority(PRI_L2NETWORK_UNICAST) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 345 | .resourceGroup(resourceGroup); |
| 346 | uniIntents.add(intentBuilder.build()); |
| 347 | }); |
| 348 | |
| 349 | return uniIntents; |
| 350 | } |
| 351 | |
| 352 | // Intent generate utilities |
| 353 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 354 | private Set<Host> hostsFromL2Network(FabricNetwork fabricNetwork) { |
| 355 | Set<Interface> interfaces = fabricNetwork.interfaces(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 356 | return interfaces.stream() |
| 357 | .map(this::hostsFromInterface) |
| 358 | .flatMap(Collection::stream) |
| 359 | .collect(Collectors.toSet()); |
| 360 | } |
| 361 | |
| 362 | private Set<Host> hostsFromInterface(Interface iface) { |
| 363 | return hostService.getConnectedHosts(iface.connectPoint()) |
| 364 | .stream() |
| 365 | .filter(host -> host.vlan().equals(iface.vlan())) |
| 366 | .collect(Collectors.toSet()); |
| 367 | } |
| 368 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 369 | private Key buildKey(String l2NetworkName, String type, |
| 370 | ConnectPoint cPoint, MacAddress dstMac) { |
| 371 | return Key.of(l2NetworkName + "-" + type + "-" + |
| 372 | cPoint.toString() + "-" + dstMac, appId); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 373 | } |
| 374 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 375 | private List<Constraint> buildConstraints(List<Constraint> constraints, |
| 376 | EncapsulationType encapsulation) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 377 | if (!encapsulation.equals(EncapsulationType.NONE)) { |
| 378 | List<Constraint> newConstraints = new ArrayList<>(constraints); |
| 379 | constraints.stream() |
| 380 | .filter(c -> c instanceof EncapsulationConstraint) |
| 381 | .forEach(newConstraints::remove); |
| 382 | newConstraints.add(new EncapsulationConstraint(encapsulation)); |
| 383 | return ImmutableList.copyOf(newConstraints); |
| 384 | } |
| 385 | return constraints; |
| 386 | } |
| 387 | |
| 388 | private FilteredConnectPoint buildFilteredConnectedPoint(Interface iface) { |
| 389 | Objects.requireNonNull(iface); |
| 390 | TrafficSelector.Builder trafficSelector = DefaultTrafficSelector.builder(); |
| 391 | |
| 392 | if (iface.vlan() != null && !iface.vlan().equals(VlanId.NONE)) { |
| 393 | trafficSelector.matchVlanId(iface.vlan()); |
| 394 | } |
| 395 | return new FilteredConnectPoint(iface.connectPoint(), trafficSelector.build()); |
| 396 | } |
| 397 | |
| 398 | protected FilteredConnectPoint buildFilteredConnectedPoint(Host host) { |
| 399 | Objects.requireNonNull(host); |
| 400 | TrafficSelector.Builder trafficSelector = DefaultTrafficSelector.builder(); |
| 401 | |
| 402 | if (host.vlan() != null && !host.vlan().equals(VlanId.NONE)) { |
| 403 | trafficSelector.matchVlanId(host.vlan()); |
| 404 | } |
| 405 | return new FilteredConnectPoint(host.location(), trafficSelector.build()); |
| 406 | } |
| 407 | |
| 408 | // Dump command handler |
| 409 | private void dump(String subject, PrintStream out) { |
Ray Milkey | 2ff6716 | 2018-01-22 10:14:19 -0800 | [diff] [blame] | 410 | if ("intents".equals(subject)) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 411 | out.println("Forwarding Broadcast Intents:\n"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 412 | for (SinglePointToMultiPointIntent intent: bctIntentsMap.values()) { |
| 413 | out.println(" " + intent.key().toString() |
| 414 | + ": " + intent.selector().criteria() |
| 415 | + ", [" + intent.filteredIngressPoint().connectPoint() |
| 416 | + "] -> " + intent.filteredEgressPoints().stream() |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 417 | .map(FilteredConnectPoint::connectPoint) |
| 418 | .collect(Collectors.toSet())); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 419 | } |
| 420 | out.println(""); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 421 | out.println("Forwarding Unicast Intents:\n"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 422 | for (MultiPointToSinglePointIntent intent: uniIntentsMap.values()) { |
| 423 | out.println(" " + intent.key().toString() |
| 424 | + ": " + intent.selector().criteria() |
| 425 | + ", [" + intent.filteredIngressPoints().stream() |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 426 | .map(FilteredConnectPoint::connectPoint) |
| 427 | .collect(Collectors.toSet()) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 428 | + "] -> " + intent.filteredEgressPoint().connectPoint()); |
| 429 | } |
| 430 | out.println(""); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 431 | out.println("Forwarding Intents to Be Purged:\n"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 432 | for (Key key: toBePurgedIntentKeys) { |
| 433 | out.println(" " + key.toString()); |
| 434 | } |
| 435 | out.println(""); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Listener |
| 440 | private class InternalSimpleFabricListener implements SimpleFabricListener { |
| 441 | @Override |
| 442 | public void event(SimpleFabricEvent event) { |
| 443 | switch (event.type()) { |
| 444 | case SIMPLE_FABRIC_UPDATED: |
| 445 | refresh(); |
| 446 | checkIntentsPurge(); |
| 447 | break; |
| 448 | case SIMPLE_FABRIC_IDLE: |
| 449 | refresh(); |
| 450 | checkIntentsPurge(); |
| 451 | break; |
| 452 | case SIMPLE_FABRIC_DUMP: |
| 453 | dump(event.subject(), event.out()); |
| 454 | break; |
| 455 | default: |
| 456 | // NOTE: nothing to do on SIMPLE_FABRIC_FLUSH |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | } |