Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.compiler; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 17 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Daniele Moro | 3a6e151 | 2017-12-22 12:14:44 +0100 | [diff] [blame] | 19 | import org.apache.commons.lang3.tuple.Pair; |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 20 | import org.onlab.graph.ScalarWeight; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DefaultPath; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.DisjointPath; |
| 25 | import org.onosproject.net.EdgeLink; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.Path; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Port; |
| 29 | import org.onosproject.net.PortNumber; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.DefaultFlowRule; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.FlowRule; |
| 34 | import org.onosproject.net.flow.TrafficSelector; |
| 35 | import org.onosproject.net.flow.TrafficTreatment; |
| 36 | import org.onosproject.net.flow.instructions.Instruction; |
| 37 | import org.onosproject.net.flow.instructions.Instructions; |
| 38 | import org.onosproject.net.group.DefaultGroupBucket; |
| 39 | import org.onosproject.net.group.DefaultGroupDescription; |
| 40 | import org.onosproject.net.group.DefaultGroupKey; |
| 41 | import org.onosproject.net.group.Group; |
| 42 | import org.onosproject.net.group.GroupBucket; |
| 43 | import org.onosproject.net.group.GroupBuckets; |
| 44 | import org.onosproject.net.group.GroupDescription; |
| 45 | import org.onosproject.net.group.GroupKey; |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 46 | import org.onosproject.net.group.GroupListener; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 47 | import org.onosproject.net.group.GroupService; |
| 48 | import org.onosproject.net.intent.FlowRuleIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.intent.Intent; |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 50 | import org.onosproject.net.intent.IntentCompilationException; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 51 | import org.onosproject.net.intent.IntentId; |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 52 | import org.onosproject.net.intent.LinkCollectionIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.net.intent.PathIntent; |
| 54 | import org.onosproject.net.intent.PointToPointIntent; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 55 | import org.onosproject.net.intent.constraint.ProtectionConstraint; |
| 56 | import org.onosproject.net.intent.impl.PathNotFoundException; |
| 57 | import org.onosproject.net.link.LinkService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 58 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 59 | import org.osgi.service.component.annotations.Activate; |
| 60 | import org.osgi.service.component.annotations.Component; |
| 61 | import org.osgi.service.component.annotations.Deactivate; |
| 62 | import org.osgi.service.component.annotations.Reference; |
| 63 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 64 | import org.slf4j.Logger; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 65 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 66 | import java.nio.ByteBuffer; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 67 | import java.util.ArrayList; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 68 | import java.util.Collections; |
| 69 | import java.util.Iterator; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 70 | import java.util.List; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 71 | import java.util.ListIterator; |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 72 | import java.util.Set; |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 73 | import java.util.concurrent.CompletableFuture; |
| 74 | import java.util.concurrent.ExecutionException; |
| 75 | import java.util.concurrent.TimeUnit; |
| 76 | import java.util.concurrent.TimeoutException; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 77 | import java.util.stream.Collectors; |
Daniele Moro | 3a6e151 | 2017-12-22 12:14:44 +0100 | [diff] [blame] | 78 | import java.util.stream.IntStream; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 79 | import java.util.stream.Stream; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 80 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 81 | import static java.util.Arrays.asList; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 82 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 83 | import static org.slf4j.LoggerFactory.getLogger; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 84 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 85 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 86 | * An intent compiler for {@link org.onosproject.net.intent.PointToPointIntent}. |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 87 | */ |
| 88 | @Component(immediate = true) |
| 89 | public class PointToPointIntentCompiler |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 90 | extends ConnectivityIntentCompiler<PointToPointIntent> { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 91 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 92 | // TODO: use off-the-shell core provider ID |
| 93 | private static final ProviderId PID = |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 94 | new ProviderId("core", "org.onosproject.core", true); |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 95 | // TODO: consider whether the default cost is appropriate or not |
| 96 | public static final int DEFAULT_COST = 1; |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 97 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 98 | protected static final int PRIORITY = Intent.DEFAULT_INTENT_PRIORITY; |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 99 | |
| 100 | private static final int GROUP_TIMEOUT = 5; |
| 101 | |
| 102 | private final Logger log = getLogger(getClass()); |
| 103 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 104 | protected boolean erasePrimary = false; |
| 105 | protected boolean eraseBackup = false; |
| 106 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 108 | protected GroupService groupService; |
| 109 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 111 | protected LinkService linkService; |
| 112 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 113 | @Activate |
| 114 | public void activate() { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 115 | intentManager.registerCompiler(PointToPointIntent.class, this); |
| 116 | } |
| 117 | |
| 118 | @Deactivate |
| 119 | public void deactivate() { |
| 120 | intentManager.unregisterCompiler(PointToPointIntent.class); |
| 121 | } |
| 122 | |
| 123 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 124 | public List<Intent> compile(PointToPointIntent intent, List<Intent> installable) { |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 125 | log.trace("compiling {} {}", intent, installable); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 126 | ConnectPoint ingressPoint = intent.filteredIngressPoint().connectPoint(); |
| 127 | ConnectPoint egressPoint = intent.filteredEgressPoint().connectPoint(); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 128 | |
Daniele Moro | 3a6e151 | 2017-12-22 12:14:44 +0100 | [diff] [blame] | 129 | //TODO: handle protected path case with suggested path!! |
| 130 | //Idea: use suggested path as primary and another path from path service as protection |
| 131 | if (intent.suggestedPath() != null && intent.suggestedPath().size() > 0) { |
| 132 | Path path = new DefaultPath(PID, intent.suggestedPath(), new ScalarWeight(1)); |
| 133 | //Check intent constraints against suggested path and suggested path availability |
| 134 | if (checkPath(path, intent.constraints()) && pathAvailable(intent)) { |
| 135 | allocateIntentBandwidth(intent, path); |
| 136 | return asList(createLinkCollectionIntent(ImmutableSet.copyOf(intent.suggestedPath()), |
| 137 | DEFAULT_COST, intent)); |
| 138 | } |
| 139 | } |
| 140 | |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 141 | if (ingressPoint.deviceId().equals(egressPoint.deviceId())) { |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 142 | return createZeroHopLinkCollectionIntent(intent); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 143 | } |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 144 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 145 | // proceed with no protected paths |
| 146 | if (!ProtectionConstraint.requireProtectedPath(intent)) { |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 147 | return createUnprotectedLinkCollectionIntent(intent); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | try { |
| 151 | // attempt to compute and implement backup path |
| 152 | return createProtectedIntent(ingressPoint, egressPoint, intent, installable); |
| 153 | } catch (PathNotFoundException e) { |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 154 | log.warn("Could not find disjoint Path for {}", intent); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 155 | // no disjoint path extant -- maximum one path exists between devices |
| 156 | return createSinglePathIntent(ingressPoint, egressPoint, intent, installable); |
| 157 | } |
| 158 | } |
| 159 | |
Daniele Moro | 3a6e151 | 2017-12-22 12:14:44 +0100 | [diff] [blame] | 160 | private void allocateIntentBandwidth(PointToPointIntent intent, Path path) { |
| 161 | ConnectPoint ingressCP = intent.filteredIngressPoint().connectPoint(); |
| 162 | ConnectPoint egressCP = intent.filteredEgressPoint().connectPoint(); |
| 163 | |
| 164 | List<ConnectPoint> pathCPs = |
| 165 | path.links().stream() |
| 166 | .flatMap(l -> Stream.of(l.src(), l.dst())) |
| 167 | .collect(Collectors.toList()); |
| 168 | |
| 169 | pathCPs.add(ingressCP); |
| 170 | pathCPs.add(egressCP); |
| 171 | |
| 172 | allocateBandwidth(intent, pathCPs); |
| 173 | } |
| 174 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 175 | private List<Intent> createZeroHopIntent(ConnectPoint ingressPoint, |
| 176 | ConnectPoint egressPoint, |
| 177 | PointToPointIntent intent) { |
| 178 | List<Link> links = asList(createEdgeLink(ingressPoint, true), createEdgeLink(egressPoint, false)); |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 179 | return asList(createPathIntent(new DefaultPath(PID, links, ScalarWeight.toWeight(DEFAULT_COST)), |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 180 | intent, PathIntent.ProtectionType.PRIMARY)); |
| 181 | } |
| 182 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 183 | private List<Intent> createZeroHopLinkCollectionIntent(PointToPointIntent intent) { |
| 184 | return asList(createLinkCollectionIntent(ImmutableSet.of(), DEFAULT_COST, |
| 185 | intent)); |
| 186 | } |
| 187 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 188 | private List<Intent> createUnprotectedLinkCollectionIntent(PointToPointIntent intent) { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 189 | Path path = getPathOrException(intent, intent.filteredIngressPoint().connectPoint().deviceId(), |
| 190 | intent.filteredEgressPoint().connectPoint().deviceId()); |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 191 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 192 | // Allocate bandwidth if a bandwidth constraint is set |
Daniele Moro | 3a6e151 | 2017-12-22 12:14:44 +0100 | [diff] [blame] | 193 | allocateIntentBandwidth(intent, path); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 194 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 195 | return asList(createLinkCollectionIntent(ImmutableSet.copyOf(path.links()), |
| 196 | path.cost(), |
| 197 | intent)); |
| 198 | } |
| 199 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 200 | //FIXME: Compatibility with EncapsulationConstraint |
| 201 | private List<Intent> createProtectedIntent(ConnectPoint ingressPoint, |
| 202 | ConnectPoint egressPoint, |
| 203 | PointToPointIntent intent, |
| 204 | List<Intent> installable) { |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 205 | log.trace("createProtectedIntent"); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 206 | DisjointPath path = getDisjointPath(intent, ingressPoint.deviceId(), |
| 207 | egressPoint.deviceId()); |
| 208 | |
| 209 | List<Intent> reusableIntents = null; |
| 210 | if (installable != null) { |
| 211 | reusableIntents = filterInvalidSubIntents(installable, intent); |
| 212 | if (reusableIntents.size() == installable.size()) { |
| 213 | // all old paths are still viable |
| 214 | return installable; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | List<Intent> intentList = new ArrayList<>(); |
| 219 | |
| 220 | // primary path intent |
| 221 | List<Link> links = new ArrayList<>(); |
| 222 | links.addAll(path.links()); |
| 223 | links.add(createEdgeLink(egressPoint, false)); |
| 224 | |
| 225 | // backup path intent |
| 226 | List<Link> backupLinks = new ArrayList<>(); |
| 227 | backupLinks.addAll(path.backup().links()); |
| 228 | backupLinks.add(createEdgeLink(egressPoint, false)); |
| 229 | |
| 230 | /* |
| 231 | * One of the old paths is still entirely intact. This old path has |
| 232 | * already been made primary, so we must add a backup path intent |
| 233 | * and modify the failover group treatment accordingly. |
| 234 | */ |
| 235 | if (reusableIntents != null && reusableIntents.size() > 1) { |
| 236 | /* |
| 237 | * Ensures that the egress port on source device is different than |
| 238 | * that of existing path so that failover group will be useful |
| 239 | * (would not be useful if both output ports in group bucket were |
| 240 | * the same). Does not necessarily ensure that the new backup path |
| 241 | * is entirely disjoint from the old path. |
| 242 | */ |
| 243 | PortNumber primaryPort = getPrimaryPort(intent); |
| 244 | if (primaryPort != null && !links.get(0).src().port().equals(primaryPort)) { |
| 245 | reusableIntents.add(createPathIntent(new DefaultPath(PID, links, |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 246 | path.weight(), path.annotations()), |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 247 | intent, PathIntent.ProtectionType.BACKUP)); |
| 248 | updateFailoverGroup(intent, links); |
| 249 | return reusableIntents; |
| 250 | |
| 251 | } else { |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 252 | reusableIntents.add(createPathIntent(new DefaultPath(PID, backupLinks, |
| 253 | path.backup().weight(), |
| 254 | path.backup().annotations()), intent, PathIntent.ProtectionType.BACKUP)); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 255 | updateFailoverGroup(intent, backupLinks); |
| 256 | return reusableIntents; |
| 257 | } |
| 258 | } |
| 259 | |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 260 | intentList.add(createPathIntent(new DefaultPath(PID, links, path.weight(), |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 261 | path.annotations()), |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 262 | intent, PathIntent.ProtectionType.PRIMARY)); |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 263 | intentList.add(createPathIntent(new DefaultPath(PID, backupLinks, path.backup().weight(), |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 264 | path.backup().annotations()), |
| 265 | intent, PathIntent.ProtectionType.BACKUP)); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 266 | |
| 267 | // Create fast failover flow rule intent or, if it already exists, |
| 268 | // add contents appropriately. |
| 269 | if (groupService.getGroup(ingressPoint.deviceId(), |
| 270 | makeGroupKey(intent.id())) == null) { |
| 271 | // manufactured fast failover flow rule intent |
| 272 | createFailoverTreatmentGroup(path.links(), path.backup().links(), intent); |
| 273 | |
| 274 | FlowRuleIntent frIntent = new FlowRuleIntent(intent.appId(), |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 275 | intent.key(), |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 276 | createFailoverFlowRules(intent), |
| 277 | asList(ingressPoint.deviceId()), |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 278 | PathIntent.ProtectionType.FAILOVER, |
| 279 | intent.resourceGroup()); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 280 | intentList.add(frIntent); |
| 281 | } else { |
| 282 | updateFailoverGroup(intent, links); |
| 283 | updateFailoverGroup(intent, backupLinks); |
| 284 | } |
| 285 | |
| 286 | return intentList; |
| 287 | } |
| 288 | |
| 289 | private List<Intent> createSinglePathIntent(ConnectPoint ingressPoint, |
| 290 | ConnectPoint egressPoint, |
| 291 | PointToPointIntent intent, |
| 292 | List<Intent> installable) { |
| 293 | List<Link> links = new ArrayList<>(); |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 294 | Path onlyPath = getPathOrException(intent, ingressPoint.deviceId(), |
| 295 | egressPoint.deviceId()); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 296 | |
| 297 | List<Intent> reusableIntents = null; |
| 298 | if (installable != null) { |
| 299 | reusableIntents = filterInvalidSubIntents(installable, intent); |
| 300 | if (reusableIntents.size() == installable.size()) { |
| 301 | // all old paths are still viable |
| 302 | return installable; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // If there exists a full path from old installable intents, |
| 307 | // return the intents that comprise it. |
| 308 | if (reusableIntents != null && reusableIntents.size() > 1) { |
| 309 | return reusableIntents; |
| 310 | } else { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 311 | // Allocate bandwidth if a bandwidth constraint is set |
Daniele Moro | 3a6e151 | 2017-12-22 12:14:44 +0100 | [diff] [blame] | 312 | allocateIntentBandwidth(intent, onlyPath); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 313 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 314 | links.add(createEdgeLink(ingressPoint, true)); |
| 315 | links.addAll(onlyPath.links()); |
| 316 | links.add(createEdgeLink(egressPoint, false)); |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 317 | return asList(createPathIntent(new DefaultPath(PID, links, onlyPath.weight(), |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 318 | onlyPath.annotations()), |
| 319 | intent, PathIntent.ProtectionType.PRIMARY)); |
| 320 | } |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Creates a path intent from the specified path and original |
| 325 | * connectivity intent. |
| 326 | * |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 327 | * @param path path to create an intent for |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 328 | * @param intent original intent |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 329 | * @param type primary or backup |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 330 | */ |
| 331 | private Intent createPathIntent(Path path, |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 332 | PointToPointIntent intent, |
| 333 | PathIntent.ProtectionType type) { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 334 | return PathIntent.builder() |
| 335 | .appId(intent.appId()) |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 336 | .key(intent.key()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 337 | .selector(intent.selector()) |
| 338 | .treatment(intent.treatment()) |
| 339 | .path(path) |
| 340 | .constraints(intent.constraints()) |
| 341 | .priority(intent.priority()) |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 342 | .setType(type) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 343 | .resourceGroup(intent.resourceGroup()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 344 | .build(); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 347 | |
| 348 | /** |
| 349 | * Creates a link collection intent from the specified path and original |
| 350 | * point to point intent. |
| 351 | * |
| 352 | * @param links the links of the packets |
| 353 | * @param cost the cost associated to the links |
| 354 | * @param intent the point to point intent we are compiling |
| 355 | * @return the link collection intent |
| 356 | */ |
| 357 | private Intent createLinkCollectionIntent(Set<Link> links, |
| 358 | double cost, |
| 359 | PointToPointIntent intent) { |
| 360 | |
| 361 | return LinkCollectionIntent.builder() |
| 362 | .key(intent.key()) |
| 363 | .appId(intent.appId()) |
| 364 | .selector(intent.selector()) |
| 365 | .treatment(intent.treatment()) |
| 366 | .links(ImmutableSet.copyOf(links)) |
| 367 | .filteredIngressPoints(ImmutableSet.of( |
| 368 | intent.filteredIngressPoint() |
| 369 | )) |
| 370 | .filteredEgressPoints(ImmutableSet.of( |
| 371 | intent.filteredEgressPoint() |
| 372 | )) |
| 373 | .applyTreatmentOnEgress(true) |
| 374 | .constraints(intent.constraints()) |
| 375 | .priority(intent.priority()) |
| 376 | .cost(cost) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 377 | .resourceGroup(intent.resourceGroup()) |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 378 | .build(); |
| 379 | } |
| 380 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 381 | /** |
| 382 | * Gets primary port number through failover group associated |
| 383 | * with this intent. |
| 384 | */ |
| 385 | private PortNumber getPrimaryPort(PointToPointIntent intent) { |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 386 | Group group = groupService.getGroup(intent.filteredIngressPoint().connectPoint().deviceId(), |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 387 | makeGroupKey(intent.id())); |
| 388 | PortNumber primaryPort = null; |
| 389 | if (group != null) { |
| 390 | List<GroupBucket> buckets = group.buckets().buckets(); |
| 391 | Iterator<GroupBucket> iterator = buckets.iterator(); |
| 392 | while (primaryPort == null && iterator.hasNext()) { |
| 393 | GroupBucket bucket = iterator.next(); |
| 394 | Instruction individualInstruction = bucket.treatment().allInstructions().get(0); |
| 395 | if (individualInstruction instanceof Instructions.OutputInstruction) { |
| 396 | Instructions.OutputInstruction outInstruction = |
| 397 | (Instructions.OutputInstruction) individualInstruction; |
| 398 | PortNumber tempPortNum = outInstruction.port(); |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 399 | Port port = deviceService.getPort(intent.filteredIngressPoint().connectPoint().deviceId(), |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 400 | tempPortNum); |
| 401 | if (port != null && port.isEnabled()) { |
| 402 | primaryPort = tempPortNum; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | return primaryPort; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Creates group key unique to each intent. |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 412 | * |
| 413 | * @param intentId identifier of intent to get a key for |
| 414 | * @return unique group key for the intent identifier |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 415 | */ |
| 416 | public static GroupKey makeGroupKey(IntentId intentId) { |
| 417 | ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); |
| 418 | buffer.putLong(intentId.fingerprint()); |
| 419 | return new DefaultGroupKey(buffer.array()); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Creates a new failover group with the initial ports of the links |
| 424 | * from the primary and backup path. |
| 425 | * |
| 426 | * @param links links from the primary path |
| 427 | * @param backupLinks links from the backup path |
| 428 | * @param intent intent from which this call originates |
| 429 | */ |
| 430 | private void createFailoverTreatmentGroup(List<Link> links, |
| 431 | List<Link> backupLinks, |
| 432 | PointToPointIntent intent) { |
| 433 | |
| 434 | List<GroupBucket> buckets = new ArrayList<>(); |
| 435 | |
| 436 | TrafficTreatment.Builder tBuilderIn = DefaultTrafficTreatment.builder(); |
| 437 | ConnectPoint src = links.get(0).src(); |
| 438 | tBuilderIn.setOutput(src.port()); |
| 439 | |
| 440 | TrafficTreatment.Builder tBuilderIn2 = DefaultTrafficTreatment.builder(); |
| 441 | ConnectPoint src2 = backupLinks.get(0).src(); |
| 442 | tBuilderIn2.setOutput(src2.port()); |
| 443 | |
| 444 | buckets.add(DefaultGroupBucket.createFailoverGroupBucket(tBuilderIn.build(), src.port(), null)); |
| 445 | buckets.add(DefaultGroupBucket.createFailoverGroupBucket(tBuilderIn2.build(), src2.port(), null)); |
| 446 | |
| 447 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 448 | |
| 449 | GroupDescription groupDesc = new DefaultGroupDescription(src.deviceId(), Group.Type.FAILOVER, |
| 450 | groupBuckets, makeGroupKey(intent.id()), null, intent.appId()); |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 451 | log.trace("adding failover group {}", groupDesc); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 452 | groupService.addGroup(groupDesc); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Manufactures flow rule with treatment that is defined by failover |
| 457 | * group and traffic selector determined by ingress port of the intent. |
| 458 | * |
| 459 | * @param intent intent which is being compiled (for appId) |
| 460 | * @return a list of a singular flow rule with fast failover |
| 461 | * outport traffic treatment |
| 462 | */ |
| 463 | private List<FlowRule> createFailoverFlowRules(PointToPointIntent intent) { |
| 464 | List<FlowRule> flowRules = new ArrayList<>(); |
| 465 | |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 466 | ConnectPoint ingress = intent.filteredIngressPoint().connectPoint(); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 467 | DeviceId deviceId = ingress.deviceId(); |
| 468 | |
| 469 | // flow rule with failover traffic treatment |
| 470 | TrafficSelector trafficSelector = DefaultTrafficSelector.builder(intent.selector()) |
| 471 | .matchInPort(ingress.port()).build(); |
| 472 | |
| 473 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder(); |
| 474 | flowRules.add(flowRuleBuilder.withSelector(trafficSelector) |
| 475 | .withTreatment(buildFailoverTreatment(deviceId, makeGroupKey(intent.id()))) |
| 476 | .fromApp(intent.appId()) |
| 477 | .makePermanent() |
| 478 | .forDevice(deviceId) |
| 479 | .withPriority(PRIORITY) |
| 480 | .build()); |
| 481 | |
| 482 | return flowRules; |
| 483 | } |
| 484 | |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 485 | |
| 486 | /** |
| 487 | * Waits for specified group to appear maximum of {@value #GROUP_TIMEOUT} seconds. |
| 488 | * |
| 489 | * @param deviceId {@link DeviceId} |
| 490 | * @param groupKey {@link GroupKey} to wait for. |
| 491 | * @return {@link Group} |
| 492 | * @throws IntentCompilationException on any error. |
| 493 | */ |
| 494 | private Group waitForGroup(DeviceId deviceId, GroupKey groupKey) { |
| 495 | return waitForGroup(deviceId, groupKey, GROUP_TIMEOUT, TimeUnit.SECONDS); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Waits for specified group to appear until timeout. |
| 500 | * |
| 501 | * @param deviceId {@link DeviceId} |
| 502 | * @param groupKey {@link GroupKey} to wait for. |
| 503 | * @param timeout timeout |
| 504 | * @param unit unit of timeout |
| 505 | * @return {@link Group} |
| 506 | * @throws IntentCompilationException on any error. |
| 507 | */ |
| 508 | private Group waitForGroup(DeviceId deviceId, GroupKey groupKey, long timeout, TimeUnit unit) { |
| 509 | Group group = groupService.getGroup(deviceId, groupKey); |
| 510 | if (group != null) { |
| 511 | return group; |
| 512 | } |
| 513 | |
| 514 | final CompletableFuture<Group> future = new CompletableFuture<>(); |
| 515 | final GroupListener listener = event -> { |
| 516 | if (event.subject().deviceId() == deviceId && |
| 517 | event.subject().appCookie().equals(groupKey)) { |
| 518 | future.complete(event.subject()); |
| 519 | return; |
| 520 | } |
| 521 | }; |
| 522 | |
| 523 | groupService.addListener(listener); |
| 524 | try { |
| 525 | group = groupService.getGroup(deviceId, groupKey); |
| 526 | if (group != null) { |
| 527 | return group; |
| 528 | } |
| 529 | return future.get(timeout, unit); |
| 530 | } catch (InterruptedException e) { |
| 531 | log.debug("Interrupted", e); |
| 532 | Thread.currentThread().interrupt(); |
| 533 | throw new IntentCompilationException("Interrupted", e); |
| 534 | } catch (ExecutionException e) { |
| 535 | log.debug("ExecutionException", e); |
| 536 | throw new IntentCompilationException("ExecutionException caught", e); |
| 537 | } catch (TimeoutException e) { |
| 538 | // one last try |
| 539 | group = groupService.getGroup(deviceId, groupKey); |
| 540 | if (group != null) { |
| 541 | return group; |
| 542 | } else { |
| 543 | log.debug("Timeout", e); |
| 544 | throw new IntentCompilationException("Timeout", e); |
| 545 | } |
| 546 | } finally { |
| 547 | groupService.removeListener(listener); |
| 548 | } |
| 549 | } |
| 550 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 551 | private TrafficTreatment buildFailoverTreatment(DeviceId srcDevice, |
| 552 | GroupKey groupKey) { |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 553 | Group group = waitForGroup(srcDevice, groupKey); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 554 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 555 | TrafficTreatment trafficTreatment = tBuilder.group(group.id()).build(); |
| 556 | return trafficTreatment; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Deletes intents from the given list if the ports or links the intent |
| 561 | * relies on are no longer viable. The failover flow rule intent is never |
| 562 | * deleted -- only its contents are updated. |
| 563 | * |
| 564 | * @param oldInstallables list of intents to examine |
| 565 | * @return list of reusable installable intents |
| 566 | */ |
| 567 | private List<Intent> filterInvalidSubIntents(List<Intent> oldInstallables, |
| 568 | PointToPointIntent pointIntent) { |
| 569 | List<Intent> intentList = new ArrayList<>(); |
| 570 | intentList.addAll(oldInstallables); |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 571 | |
| 572 | Iterator<Intent> iterator = intentList.iterator(); |
| 573 | while (iterator.hasNext()) { |
| 574 | Intent intent = iterator.next(); |
| 575 | intent.resources().forEach(resource -> { |
| 576 | if (resource instanceof Link) { |
| 577 | Link link = (Link) resource; |
| 578 | if (link.state() == Link.State.INACTIVE) { |
| 579 | setPathsToRemove(intent); |
| 580 | } else if (link instanceof EdgeLink) { |
| 581 | ConnectPoint connectPoint = (link.src().elementId() instanceof DeviceId) |
| 582 | ? link.src() : link.dst(); |
| 583 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 584 | if (port == null || !port.isEnabled()) { |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 585 | setPathsToRemove(intent); |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 586 | } |
| 587 | } else { |
| 588 | Port port1 = deviceService.getPort(link.src().deviceId(), link.src().port()); |
| 589 | Port port2 = deviceService.getPort(link.dst().deviceId(), link.dst().port()); |
| 590 | if (port1 == null || !port1.isEnabled() || port2 == null || !port2.isEnabled()) { |
| 591 | setPathsToRemove(intent); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 594 | } |
| 595 | }); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 596 | } |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 597 | removeAndUpdateIntents(intentList, pointIntent); |
| 598 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 599 | return intentList; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Sets instance variables erasePrimary and eraseBackup. If erasePrimary, |
| 604 | * the primary path is no longer viable and related intents will be deleted. |
| 605 | * If eraseBackup, the backup path is no longer viable and related intents |
| 606 | * will be deleted. |
| 607 | * |
| 608 | * @param intent intent whose resources are found to be disabled/inactive: |
| 609 | * if intent is part of primary path, primary path set for removal; |
| 610 | * if intent is part of backup path, backup path set for removal; |
| 611 | * if bad intent is of type failover, the ingress point is down, |
| 612 | * and both paths are rendered inactive. |
| 613 | * @return true if both primary and backup paths are to be removed |
| 614 | */ |
| 615 | private boolean setPathsToRemove(Intent intent) { |
| 616 | if (intent instanceof FlowRuleIntent) { |
| 617 | FlowRuleIntent frIntent = (FlowRuleIntent) intent; |
| 618 | PathIntent.ProtectionType type = frIntent.type(); |
| 619 | if (type == PathIntent.ProtectionType.PRIMARY || type == PathIntent.ProtectionType.FAILOVER) { |
| 620 | erasePrimary = true; |
| 621 | } |
| 622 | if (type == PathIntent.ProtectionType.BACKUP || type == PathIntent.ProtectionType.FAILOVER) { |
| 623 | eraseBackup = true; |
| 624 | } |
| 625 | } |
| 626 | return erasePrimary && eraseBackup; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Removes intents from installables list, depending on the values |
| 631 | * of instance variables erasePrimary and eraseBackup. Flow rule intents |
| 632 | * that contain the manufactured fast failover flow rules are never deleted. |
| 633 | * The contents are simply modified as necessary. If cleanUpIntents size |
| 634 | * is greater than 1 (failover intent), then one whole path from previous |
| 635 | * installables must be still viable. |
| 636 | * |
| 637 | * @param cleanUpIntents list of installable intents |
| 638 | */ |
| 639 | private void removeAndUpdateIntents(List<Intent> cleanUpIntents, |
| 640 | PointToPointIntent pointIntent) { |
| 641 | ListIterator<Intent> iterator = cleanUpIntents.listIterator(); |
| 642 | while (iterator.hasNext()) { |
| 643 | Intent cIntent = iterator.next(); |
| 644 | if (cIntent instanceof FlowRuleIntent) { |
| 645 | FlowRuleIntent fIntent = (FlowRuleIntent) cIntent; |
| 646 | if (fIntent.type() == PathIntent.ProtectionType.PRIMARY && erasePrimary) { |
| 647 | // remove primary path's flow rule intents |
| 648 | iterator.remove(); |
| 649 | } else if (fIntent.type() == PathIntent.ProtectionType.BACKUP && eraseBackup) { |
| 650 | //remove backup path's flow rule intents |
| 651 | iterator.remove(); |
| 652 | } else if (fIntent.type() == PathIntent.ProtectionType.BACKUP && erasePrimary) { |
| 653 | // promote backup path's flow rule intents to primary |
| 654 | iterator.set(new FlowRuleIntent(fIntent, PathIntent.ProtectionType.PRIMARY)); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | // remove buckets whose watchports are disabled if the failover group exists |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 659 | Group group = groupService.getGroup(pointIntent.filteredIngressPoint().connectPoint().deviceId(), |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 660 | makeGroupKey(pointIntent.id())); |
| 661 | if (group != null) { |
| 662 | updateFailoverGroup(pointIntent); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // Removes buckets whose treatments rely on disabled ports from the |
| 667 | // failover group. |
| 668 | private void updateFailoverGroup(PointToPointIntent pointIntent) { |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 669 | DeviceId deviceId = pointIntent.filteredIngressPoint().connectPoint().deviceId(); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 670 | GroupKey groupKey = makeGroupKey(pointIntent.id()); |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 671 | Group group = waitForGroup(deviceId, groupKey); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 672 | Iterator<GroupBucket> groupIterator = group.buckets().buckets().iterator(); |
| 673 | while (groupIterator.hasNext()) { |
| 674 | GroupBucket bucket = groupIterator.next(); |
| 675 | Instruction individualInstruction = bucket.treatment().allInstructions().get(0); |
| 676 | if (individualInstruction instanceof Instructions.OutputInstruction) { |
| 677 | Instructions.OutputInstruction outInstruction = |
| 678 | (Instructions.OutputInstruction) individualInstruction; |
| 679 | Port port = deviceService.getPort(deviceId, outInstruction.port()); |
| 680 | if (port == null || !port.isEnabled()) { |
| 681 | GroupBuckets removeBuckets = new GroupBuckets(Collections.singletonList(bucket)); |
| 682 | groupService.removeBucketsFromGroup(deviceId, groupKey, |
| 683 | removeBuckets, groupKey, |
| 684 | pointIntent.appId()); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // Adds failover group bucket with treatment outport determined by the |
| 691 | // ingress point of the links. |
| 692 | private void updateFailoverGroup(PointToPointIntent intent, List<Link> links) { |
| 693 | GroupKey groupKey = makeGroupKey(intent.id()); |
| 694 | |
| 695 | TrafficTreatment.Builder tBuilderIn = DefaultTrafficTreatment.builder(); |
| 696 | ConnectPoint src = links.get(0).src(); |
| 697 | tBuilderIn.setOutput(src.port()); |
| 698 | GroupBucket bucket = DefaultGroupBucket.createFailoverGroupBucket(tBuilderIn.build(), src.port(), null); |
| 699 | GroupBuckets addBuckets = new GroupBuckets(Collections.singletonList(bucket)); |
| 700 | |
| 701 | groupService.addBucketsToGroup(src.deviceId(), groupKey, addBuckets, groupKey, intent.appId()); |
| 702 | } |
Daniele Moro | 3a6e151 | 2017-12-22 12:14:44 +0100 | [diff] [blame] | 703 | |
| 704 | /** |
| 705 | * Checks suggested path availability. |
| 706 | * It checks: |
| 707 | * - single links availability; |
| 708 | * - that first and last device of the path are coherent with ingress and egress devices; |
| 709 | * - links contiguity. |
| 710 | * |
| 711 | * @param intent Intent with suggested path to check |
| 712 | * @return true if the suggested path is available |
| 713 | */ |
| 714 | private boolean pathAvailable(PointToPointIntent intent) { |
| 715 | // Check links availability |
| 716 | List<Link> suggestedPath = intent.suggestedPath(); |
| 717 | for (Link link : suggestedPath) { |
| 718 | if (!(link instanceof EdgeLink) && !linkService.getLinks(link.src()).contains(link)) { |
| 719 | return false; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | //Check that first and last device of the path are intent ingress and egress devices |
| 724 | if (!suggestedPath.get(0).src() |
| 725 | .deviceId().equals(intent.filteredIngressPoint().connectPoint().deviceId())) { |
| 726 | return false; |
| 727 | } |
| 728 | if (!suggestedPath.get(suggestedPath.size() - 1).dst() |
| 729 | .deviceId().equals(intent.filteredEgressPoint().connectPoint().deviceId())) { |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | // Check contiguity |
| 734 | List<Pair<Link, Link>> linkPairs = IntStream. |
| 735 | range(0, suggestedPath.size() - 1) |
| 736 | .mapToObj(i -> Pair.of(suggestedPath.get(i), suggestedPath.get(i + 1))) |
| 737 | .collect(Collectors.toList()); |
| 738 | |
| 739 | for (Pair<Link, Link> linkPair : linkPairs) { |
| 740 | if (!linkPair.getKey().dst().deviceId().equals(linkPair.getValue().src().deviceId())) { |
| 741 | return false; |
| 742 | } |
| 743 | } |
| 744 | return true; |
| 745 | } |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 746 | } |