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