blob: 93730c77b06a565d19f8eba6b50896d3d62f9634 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07003 *
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 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Charles Chan188ebf52015-12-23 00:15:11 -080017
18import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
Charles Chan5b9df8d2016-03-28 22:21:40 -070022import com.google.common.collect.ImmutableList;
23import com.google.common.collect.Lists;
Yi Tseng78f51f42017-02-02 13:54:58 -080024import com.google.common.collect.Sets;
Charles Chan188ebf52015-12-23 00:15:11 -080025import org.onlab.osgi.ServiceDirectory;
Charles Chan5b9df8d2016-03-28 22:21:40 -070026import org.onlab.packet.IpPrefix;
Charles Chan5270ed02016-01-30 23:22:37 -080027import org.onlab.packet.MacAddress;
Charles Chan188ebf52015-12-23 00:15:11 -080028import org.onlab.packet.MplsLabel;
29import org.onlab.packet.VlanId;
30import org.onosproject.core.ApplicationId;
Yi Tseng78f51f42017-02-02 13:54:58 -080031import org.onosproject.core.GroupId;
Charles Chan32562522016-04-07 14:37:14 -070032import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080033import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criterion;
Pier Ventre42287df2016-11-09 14:17:26 -080041import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080042import org.onosproject.net.flow.criteria.VlanIdCriterion;
43import org.onosproject.net.flow.instructions.Instruction;
44import org.onosproject.net.flow.instructions.Instructions;
Saurav Das9df5b7c2017-08-14 16:44:43 -070045import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
Charles Chan188ebf52015-12-23 00:15:11 -080046import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080047import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan188ebf52015-12-23 00:15:11 -080048import org.onosproject.net.flowobjective.FlowObjectiveStore;
49import org.onosproject.net.flowobjective.NextObjective;
Saurav Dasc88d4662017-05-15 15:34:25 -070050import org.onosproject.net.flowobjective.Objective.Operation;
Yi Tseng47f82dc2017-03-05 22:48:39 -080051import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080052import org.onosproject.net.flowobjective.ObjectiveError;
53import org.onosproject.net.group.DefaultGroupBucket;
54import org.onosproject.net.group.DefaultGroupDescription;
55import org.onosproject.net.group.DefaultGroupKey;
56import org.onosproject.net.group.Group;
57import org.onosproject.net.group.GroupBucket;
58import org.onosproject.net.group.GroupBuckets;
59import org.onosproject.net.group.GroupDescription;
60import org.onosproject.net.group.GroupEvent;
61import org.onosproject.net.group.GroupKey;
62import org.onosproject.net.group.GroupListener;
63import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080064import org.onosproject.store.service.AtomicCounter;
65import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080066import org.slf4j.Logger;
67
68import java.util.ArrayDeque;
69import java.util.ArrayList;
Saurav Dasceccf242017-08-03 18:30:35 -070070import java.util.Arrays;
Charles Chan188ebf52015-12-23 00:15:11 -080071import java.util.Collection;
72import java.util.Collections;
73import java.util.Deque;
74import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080075import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080076import java.util.Set;
77import java.util.concurrent.ConcurrentHashMap;
78import java.util.concurrent.CopyOnWriteArrayList;
79import java.util.concurrent.Executors;
80import java.util.concurrent.ScheduledExecutorService;
81import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080082import java.util.stream.Collectors;
83
84import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070085import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
86import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080087import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070088import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080089import static org.onosproject.net.group.GroupDescription.Type.ALL;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -080090import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
Yi Tseng78f51f42017-02-02 13:54:58 -080091import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080092import static org.slf4j.LoggerFactory.getLogger;
93
94/**
Saurav Das961beb22017-03-29 19:09:17 -070095 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080096 */
Charles Chan361154b2016-03-24 10:23:39 -070097public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -070098 protected final Logger log = getLogger(getClass());
Charles Chane849c192016-01-11 18:28:54 -080099
Yi Tsengef19de12017-04-24 11:33:05 -0700100 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -0800101 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800102 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700103 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800104
105 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800106 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800107
Yi Tsengef19de12017-04-24 11:33:05 -0700108 protected DeviceId deviceId;
109 private Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
110 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
111 private Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
112 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
113
Yi Tseng47f82dc2017-03-05 22:48:39 -0800114 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800115 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800116 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700117 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800118
Yi Tsengef19de12017-04-24 11:33:05 -0700119 private ScheduledExecutorService groupCheckerExecutor =
120 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
121
122 public Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives() {
123 return pendingAddNextObjectives;
124 }
125
126 public Cache<GroupKey, Set<GroupChainElem>> pendingGroups() {
127 return pendingGroups;
128 }
129
Charles Chan40132b32017-01-22 00:19:37 -0800130 /**
131 * Determines whether this pipeline support copy ttl instructions or not.
132 *
133 * @return true if copy ttl instructions are supported
134 */
135 protected boolean supportCopyTtl() {
136 return true;
137 }
138
139 /**
140 * Determines whether this pipeline support set mpls bos instruction or not.
141 *
142 * @return true if set mpls bos instruction is supported
143 */
144 protected boolean supportSetMplsBos() {
145 return true;
146 }
147
Charles Chan0f43e472017-02-14 14:00:16 -0800148 /**
149 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
150 * <p>
151 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
152 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
153 *
154 * @return true if this pipeline requires popping VLAN before pushing MPLS
155 */
156 protected boolean requireVlanPopBeforeMplsPush() {
157 return false;
158 }
159
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200160 public void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700161 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800162 this.deviceId = deviceId;
163 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800164 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800165 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700166 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800167
Charles Chanfc5c7802016-05-17 13:13:55 -0700168 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800169 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700170 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
171 if (notification.getCause() == RemovalCause.EXPIRED &&
172 Objects.nonNull(notification.getValue())) {
173 notification.getValue()
174 .forEach(ofdpaNextGrp ->
175 fail(ofdpaNextGrp.nextObjective(),
176 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700177 }
178 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800179
Charles Chanfc5c7802016-05-17 13:13:55 -0700180 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
181 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700182 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700183 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700184 fail(notification.getKey(),
185 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800186 }
187 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700188 pendingGroups = CacheBuilder.newBuilder()
189 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700190 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700191 if (notification.getCause() == RemovalCause.EXPIRED) {
192 log.error("Unable to install group with key {} and pending GCEs: {}",
193 notification.getKey(), notification.getValue());
194 }
195 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800196 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700197 GroupChecker groupChecker = new GroupChecker(this);
198 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800199 groupService.addListener(new InnerGroupListener());
200 }
201
Saurav Das8be4e3a2016-03-11 17:19:07 -0800202 //////////////////////////////////////
203 // Group Creation
204 //////////////////////////////////////
205
Yi Tsengef19de12017-04-24 11:33:05 -0700206 /**
207 * Adds a list of group chain by given NextObjective.
208 *
209 * @param nextObjective the NextObjective
210 */
Charles Chan188ebf52015-12-23 00:15:11 -0800211 protected void addGroup(NextObjective nextObjective) {
212 switch (nextObjective.type()) {
213 case SIMPLE:
214 Collection<TrafficTreatment> treatments = nextObjective.next();
215 if (treatments.size() != 1) {
216 log.error("Next Objectives of type Simple should only have a "
217 + "single Traffic Treatment. Next Objective Id:{}",
218 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700219 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800220 return;
221 }
222 processSimpleNextObjective(nextObjective);
223 break;
224 case BROADCAST:
225 processBroadcastNextObjective(nextObjective);
226 break;
227 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700228 if (!verifyHashedNextObjective(nextObjective)) {
229 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
230 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700231 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700232 return;
233 }
Charles Chan188ebf52015-12-23 00:15:11 -0800234 processHashedNextObjective(nextObjective);
235 break;
236 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700237 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800238 log.warn("Unsupported next objective type {}", nextObjective.type());
239 break;
240 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700241 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800242 log.warn("Unknown next objective type {}", nextObjective.type());
243 }
244 }
245
246 /**
247 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
248 * a chain of groups. The simple Next Objective passed
249 * in by the application has to be broken up into a group chain
250 * comprising of an L3 Unicast Group that points to an L2 Interface
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800251 * Group which in-turn points to an output port or an MPLS Interface Group
252 * that points to an L2 Interface Group. In some cases, the simple
Charles Chan188ebf52015-12-23 00:15:11 -0800253 * next Objective can just be an L2 interface without the need for chaining.
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800254 * Further, if the label is set to the Next objective then the Group chain
255 * MPLS Swap - MPLS Interface - L2 Interface is created
Charles Chan188ebf52015-12-23 00:15:11 -0800256 *
257 * @param nextObj the nextObjective of type SIMPLE
258 */
259 private void processSimpleNextObjective(NextObjective nextObj) {
260 TrafficTreatment treatment = nextObj.next().iterator().next();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800261 // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2
Charles Chan188ebf52015-12-23 00:15:11 -0800262 boolean plainL2 = true;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800263 boolean mplsSwap = false;
264 MplsLabel mplsLabel = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800265 for (Instruction ins : treatment.allInstructions()) {
266 if (ins.type() == Instruction.Type.L2MODIFICATION) {
267 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
268 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
269 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
270 plainL2 = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800271 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800272 // mpls label in simple next objectives is used only to indicate
273 // a MPLS Swap group before the MPLS Interface Group
274 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
275 mplsSwap = true;
276 mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
277 }
278
Charles Chan188ebf52015-12-23 00:15:11 -0800279 }
280 }
281
282 if (plainL2) {
283 createL2InterfaceGroup(nextObj);
284 return;
285 }
286
Pier Ventre140a8942016-11-02 07:26:38 -0700287 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800288 // In order to understand if it is a pseudo wire related
289 // next objective we look for the tunnel id in the meta.
290 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700291 if (nextObj.meta() != null) {
292 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800293
294 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
295 .meta()
296 .getCriterion(TUNNEL_ID);
297 if (tunnelIdCriterion != null) {
298 isPw = true;
299 }
300
Pier Ventre140a8942016-11-02 07:26:38 -0700301 }
302
Andreas Pantelopoulos25db42f2018-02-08 12:42:06 -0800303 if (mplsSwap && !isPw) {
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800304 log.debug("Creating a MPLS Swap - MPLS Interface - L2 Interface group chain.");
305
306 // break up simple next objective to GroupChain objects
307 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
308 nextObj.appId(), true,
309 nextObj.meta());
310 if (groupInfo == null) {
311 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
312 fail(nextObj, ObjectiveError.BADPARAMS);
313 return;
314 }
315
316 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
317 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
318 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
319
320 // creating the mpls swap group and adding it to the chain
321 GroupChainElem groupChainElem;
322 GroupKey groupKey;
323 GroupDescription groupDescription;
324 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
325 int index = getNextAvailableIndex();
326
327 groupDescription = createMplsSwap(
328 nextGid,
329 OfdpaMplsGroupSubType.MPLS_SWAP_LABEL,
330 index,
331 mplsLabel,
332 nextObj.appId()
333 );
334
335 groupKey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(index));
336 groupChainElem = new GroupChainElem(groupDescription, 1, false, deviceId);
337 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), groupChainElem);
338 gkeyChain.addFirst(groupKey);
339
340 // create a new List from singletonList that is mutable
341 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
342 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
343
344 // now we are ready to send the l2 groupDescription (inner), as all the stores
345 // that will get async replies have been updated. By waiting to update
346 // the stores, we prevent nasty race conditions.
347 groupService.addGroup(groupInfo.innerMostGroupDesc());
348 } else if (!isPw) {
Pier Ventre42287df2016-11-09 14:17:26 -0800349 // break up simple next objective to GroupChain objects
350 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
351 nextObj.appId(), isMpls,
352 nextObj.meta());
353 if (groupInfo == null) {
354 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800355 fail(nextObj, ObjectiveError.BADPARAMS);
Pier Ventre42287df2016-11-09 14:17:26 -0800356 return;
357 }
358 // create object for local and distributed storage
359 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700360 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
361 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Pier Ventre42287df2016-11-09 14:17:26 -0800362 OfdpaNextGroup ofdpaGrp =
363 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
364
365 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700366 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800367
368 // now we are ready to send the l2 groupDescription (inner), as all the stores
369 // that will get async replies have been updated. By waiting to update
370 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700371 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800372 } else {
373 // We handle the pseudo wire with a different a procedure.
374 // This procedure is meant to handle both initiation and
375 // termination of the pseudo wire.
376 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800377 }
Charles Chan188ebf52015-12-23 00:15:11 -0800378 }
379
Charles Chan188ebf52015-12-23 00:15:11 -0800380 /**
381 * Creates a simple L2 Interface Group.
382 *
383 * @param nextObj the next Objective
384 */
385 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700386 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700387 if (assignedVlan == null) {
388 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700389 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800390 return;
391 }
392
Charles Chan5b9df8d2016-03-28 22:21:40 -0700393 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800394
Charles Chan5b9df8d2016-03-28 22:21:40 -0700395 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700396 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan188ebf52015-12-23 00:15:11 -0800397
Charles Chan5b9df8d2016-03-28 22:21:40 -0700398 // Put all dependency information into allGroupKeys
399 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
400 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
401 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
402 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800403
Charles Chan5b9df8d2016-03-28 22:21:40 -0700404 // Point the next objective to this group
405 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
406 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
407
408 // Start installing the inner-most group
409 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800410 }
411
412 /**
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800413 * Creates an Mpls group of type swap.
414 *
415 * @param nextGroupId the next group in the chain
416 * @param subtype the mpls swap label group subtype
417 * @param index the index of the group
418 * @param mplsLabel the mpls label to swap
419 * @param applicationId the application id
420 * @return the group description
421 */
422 protected GroupDescription createMplsSwap(int nextGroupId,
423 OfdpaMplsGroupSubType subtype,
424 int index,
425 MplsLabel mplsLabel,
426 ApplicationId applicationId) {
427
428 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
429
430 treatment.setMpls(mplsLabel);
431
432 // We point the group to the next group.
433 treatment.group(new GroupId(nextGroupId));
434 GroupBucket groupBucket = DefaultGroupBucket
435 .createIndirectGroupBucket(treatment.build());
436 // Finally we build the group description.
437 int groupId = makeMplsLabelGroupId(subtype, index);
438 GroupKey groupKey = new DefaultGroupKey(
439 Ofdpa2Pipeline.appKryo.serialize(index)
440 );
441 return new DefaultGroupDescription(
442 deviceId,
443 INDIRECT,
444 new GroupBuckets(Collections.singletonList(groupBucket)),
445 groupKey,
446 groupId,
447 applicationId
448 );
449 }
450
451 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800452 * Creates one of two possible group-chains from the treatment
453 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700454 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
455 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800456 * The returned 'inner' group description is always the L2 Interface group.
457 *
458 * @param treatment that needs to be broken up to create the group chain
459 * @param nextId of the next objective that needs this group chain
460 * @param appId of the application that sent this next objective
461 * @param mpls determines if L3Unicast or MPLSInterface group is created
462 * @param meta metadata passed in by the application as part of the nextObjective
463 * @return GroupInfo containing the GroupDescription of the
464 * L2Interface group(inner) and the GroupDescription of the (outer)
465 * L3Unicast/MPLSInterface group. May return null if there is an
466 * error in processing the chain
467 */
Charles Chan425854b2016-04-11 15:32:12 -0700468 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700469 ApplicationId appId, boolean mpls,
470 TrafficSelector meta) {
471 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
472 }
473
474 /**
475 * Internal implementation of createL2L3Chain.
476 * <p>
477 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
478 * Since it is non-OF spec, we need an extension treatment for that.
479 * The useSetVlanExtension must be set to false for OFDPA i12.
480 * </p>
481 *
482 * @param treatment that needs to be broken up to create the group chain
483 * @param nextId of the next objective that needs this group chain
484 * @param appId of the application that sent this next objective
485 * @param mpls determines if L3Unicast or MPLSInterface group is created
486 * @param meta metadata passed in by the application as part of the nextObjective
487 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
488 * @return GroupInfo containing the GroupDescription of the
489 * L2Interface group(inner) and the GroupDescription of the (outer)
490 * L3Unicast/MPLSInterface group. May return null if there is an
491 * error in processing the chain
492 */
493 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800494 ApplicationId appId, boolean mpls,
495 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800496 // for the l2interface group, get vlan and port info
497 // for the outer group, get the src/dst mac, and vlan info
498 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
499 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
500 VlanId vlanid = null;
501 long portNum = 0;
502 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800503 MacAddress srcMac;
504 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800505 for (Instruction ins : treatment.allInstructions()) {
506 if (ins.type() == Instruction.Type.L2MODIFICATION) {
507 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
508 switch (l2ins.subtype()) {
509 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800510 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
511 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800512 break;
513 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800514 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
515 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800516 break;
517 case VLAN_ID:
518 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700519 if (useSetVlanExtension) {
520 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
521 outerTtb.extension(ofdpaSetVlanVid, deviceId);
522 } else {
523 outerTtb.setVlanId(vlanid);
524 }
Charles Chan188ebf52015-12-23 00:15:11 -0800525 setVlan = true;
526 break;
527 case VLAN_POP:
528 innerTtb.popVlan();
529 popVlan = true;
530 break;
531 case DEC_MPLS_TTL:
532 case MPLS_LABEL:
533 case MPLS_POP:
534 case MPLS_PUSH:
535 case VLAN_PCP:
536 case VLAN_PUSH:
537 default:
538 break;
539 }
540 } else if (ins.type() == Instruction.Type.OUTPUT) {
541 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
542 innerTtb.add(ins);
543 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700544 log.debug("Driver does not handle this type of TrafficTreatment"
Charles Chan188ebf52015-12-23 00:15:11 -0800545 + " instruction in nextObjectives: {}", ins.type());
546 }
547 }
548
549 if (vlanid == null && meta != null) {
550 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700551 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800552 if (vidCriterion != null) {
553 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
554 }
555 // if vlan is not set, use the vlan in metadata for outerTtb
556 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700557 if (useSetVlanExtension) {
558 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
559 outerTtb.extension(ofdpaSetVlanVid, deviceId);
560 } else {
561 outerTtb.setVlanId(vlanid);
562 }
Charles Chan188ebf52015-12-23 00:15:11 -0800563 }
564 }
565
566 if (vlanid == null) {
567 log.error("Driver cannot process an L2/L3 group chain without "
568 + "egress vlan information for dev: {} port:{}",
569 deviceId, portNum);
570 return null;
571 }
572
573 if (!setVlan && !popVlan) {
574 // untagged outgoing port
575 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
576 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700577 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800578 innerTtb = temp;
579 }
580
581 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700582 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800583 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800584 // but different for the same portnumber on different devices. Also different
585 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800586 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700587 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800588
589 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700590 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800591 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700592 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800593 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700594 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
595 final GroupKey mplsGroupKey = new DefaultGroupKey(
596 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800597 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800598 // create the mpls-interface group description to wait for the
599 // l2 interface group to be processed
600 GroupBucket mplsinterfaceGroupBucket =
601 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
602 outerGrpDesc = new DefaultGroupDescription(
603 deviceId,
604 GroupDescription.Type.INDIRECT,
605 new GroupBuckets(Collections.singletonList(
606 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700607 mplsGroupKey,
608 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800609 appId);
610 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700611 deviceId, Integer.toHexString(mplsGroupId),
612 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800613 } else {
614 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800615 int l3unicastIndex = getNextAvailableIndex();
616 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
617 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700618 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800619 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800620 // create the l3unicast group description to wait for the
621 // l2 interface group to be processed
622 GroupBucket l3unicastGroupBucket =
623 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
624 outerGrpDesc = new DefaultGroupDescription(
625 deviceId,
626 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700627 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800628 l3groupkey,
629 l3groupId,
630 appId);
631 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
632 deviceId, Integer.toHexString(l3groupId),
633 l3groupkey, nextId);
634 }
635
636 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700637 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800638 updatePendingGroups(l2groupkey, gce);
639
Yi Tsengef19de12017-04-24 11:33:05 -0700640 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700641 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800642 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
643 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700644 new DefaultGroupDescription(deviceId,
645 GroupDescription.Type.INDIRECT,
646 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
647 l2groupkey,
648 l2groupId,
649 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800650 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
651 deviceId, Integer.toHexString(l2groupId),
652 l2groupkey, nextId);
653 return new GroupInfo(l2groupDescription, outerGrpDesc);
654
655 }
656
657 /**
658 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
659 * a chain of groups. The broadcast Next Objective passed in by the application
660 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800661 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800662 *
663 * @param nextObj the nextObjective of type BROADCAST
664 */
665 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700666 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700667 if (assignedVlan == null) {
668 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700669 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800670 return;
671 }
Charles Chan188ebf52015-12-23 00:15:11 -0800672
Charles Chan5b9df8d2016-03-28 22:21:40 -0700673 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
674
Yi Tsengef19de12017-04-24 11:33:05 -0700675 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700676 if (ipDst != null) {
677 if (ipDst.isMulticast()) {
678 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
679 } else {
680 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700681 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700682 }
683 } else {
684 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
685 }
686 }
687
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700688 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
689 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700690 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
691
692 // break up broadcast next objective to multiple groups
693 Collection<TrafficTreatment> buckets = nextObj.next();
694
Charles Chan188ebf52015-12-23 00:15:11 -0800695 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800696 for (TrafficTreatment treatment : buckets) {
697 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
698 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700699 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800700 // ensure that the only allowed treatments are pop-vlan and output
701 for (Instruction ins : treatment.allInstructions()) {
702 if (ins.type() == Instruction.Type.L2MODIFICATION) {
703 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
704 switch (l2ins.subtype()) {
705 case VLAN_POP:
706 newTreatment.add(l2ins);
707 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700708 case VLAN_ID:
709 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
710 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800711 default:
712 log.debug("action {} not permitted for broadcast nextObj",
713 l2ins.subtype());
714 break;
715 }
716 } else if (ins.type() == Instruction.Type.OUTPUT) {
717 portNum = ((Instructions.OutputInstruction) ins).port();
718 newTreatment.add(ins);
719 } else {
Charles Chane849c192016-01-11 18:28:54 -0800720 log.debug("TrafficTreatment of type {} not permitted in " +
721 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800722 }
723 }
724
Yi Tsengef19de12017-04-24 11:33:05 -0700725 if (portNum == null) {
726 log.warn("Can't find output port for the bucket {}.", treatment);
727 continue;
728 }
729
Charles Chan188ebf52015-12-23 00:15:11 -0800730 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700731 VlanId l2InterfaceGroupVlan =
732 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
733 egressVlan : assignedVlan;
734 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
735 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700736 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800737 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
738 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
739 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700740 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800741 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700742 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700743 new DefaultGroupDescription(deviceId,
744 GroupDescription.Type.INDIRECT,
745 new GroupBuckets(Collections.singletonList(
746 l2InterfaceGroupBucket)),
747 l2InterfaceGroupKey,
748 l2InterfaceGroupId,
749 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800750 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700751 deviceId, Integer.toHexString(l2InterfaceGroupId),
752 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800753
Charles Chan5b9df8d2016-03-28 22:21:40 -0700754 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
755 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800756 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700757 return groupInfoBuilder.build();
758 }
Charles Chan188ebf52015-12-23 00:15:11 -0800759
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700760 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
761 List<GroupInfo> groupInfos) {
762 // assemble info for l2 flood group. Since there can be only one flood
763 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700764 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
765 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700766
Charles Chan188ebf52015-12-23 00:15:11 -0800767 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700768 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800769 // create the l2flood group-description to wait for all the
770 // l2interface groups to be processed
771 GroupDescription l2floodGroupDescription =
772 new DefaultGroupDescription(
773 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800774 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800775 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700776 l2FloodGroupKey,
777 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800778 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800779 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700780 deviceId, Integer.toHexString(l2FloodGroupId),
781 l2FloodGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800782
Charles Chan5b9df8d2016-03-28 22:21:40 -0700783 // Put all dependency information into allGroupKeys
784 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
785 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700786 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700787 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700788 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
789 groupKeyChain.addFirst(l2FloodGroupKey);
790 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700791 });
Charles Chan188ebf52015-12-23 00:15:11 -0800792
Charles Chan5b9df8d2016-03-28 22:21:40 -0700793 // Point the next objective to this group
794 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700795 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800796
Charles Chan5b9df8d2016-03-28 22:21:40 -0700797 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700798 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700799 groupInfos.forEach(groupInfo -> {
800 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700801 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700802 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700803 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700804 });
805 }
806
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700807 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
808 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700809 List<GroupBucket> l3McastBuckets = new ArrayList<>();
810 groupInfos.forEach(groupInfo -> {
811 // Points to L3 interface group if there is one.
812 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -0700813 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
814 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700815 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800816 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700817 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
818 l3McastBuckets.add(abucket);
819 });
820
821 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700822 int l3MulticastGroupId = L3_MULTICAST_TYPE |
823 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
824 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700825 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700826
827 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800828 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700829 new GroupBuckets(l3McastBuckets),
830 l3MulticastGroupKey,
831 l3MulticastGroupId,
832 nextObj.appId());
833
834 // Put all dependency information into allGroupKeys
835 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
836 groupInfos.forEach(groupInfo -> {
837 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700838 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700839 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700840 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
841 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700842 }
843 gkeyChain.addFirst(l3MulticastGroupKey);
844 allGroupKeys.add(gkeyChain);
845 });
846
847 // Point the next objective to this group
848 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
849 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
850
851 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700852 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700853 groupInfos.forEach(groupInfo -> {
854 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700855 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700856
857 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700858 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
859 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
860 1, false, deviceId);
861 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700862 }
863
864 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700865 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700866 });
Charles Chan188ebf52015-12-23 00:15:11 -0800867 }
868
Charles Chan188ebf52015-12-23 00:15:11 -0800869 /**
870 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
871 * a chain of groups. The hashed Next Objective passed in by the application
872 * has to be broken up into a group chain comprising of an
873 * L3 ECMP group as the top level group. Buckets of this group can point
874 * to a variety of groups in a group chain, depending on the whether
875 * MPLS labels are being pushed or not.
876 * <p>
877 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
878 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
879 * check the nextObjective meta to see what is matching before being
880 * sent to this nextObjective.
881 *
882 * @param nextObj the nextObjective of type HASHED
883 */
Pier Ventre140a8942016-11-02 07:26:38 -0700884 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800885 // storage for all group keys in the chain of groups created
886 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
887 List<GroupInfo> unsentGroups = new ArrayList<>();
888 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
889
890 // now we can create the outermost L3 ECMP group
891 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
892 for (GroupInfo gi : unsentGroups) {
893 // create ECMP bucket to point to the outer group
894 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700895 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800896 GroupBucket sbucket = DefaultGroupBucket
897 .createSelectGroupBucket(ttb.build());
898 l3ecmpGroupBuckets.add(sbucket);
899 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800900 int l3ecmpIndex = getNextAvailableIndex();
901 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
902 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700903 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800904 GroupDescription l3ecmpGroupDesc =
905 new DefaultGroupDescription(
906 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800907 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800908 new GroupBuckets(l3ecmpGroupBuckets),
909 l3ecmpGroupKey,
910 l3ecmpGroupId,
911 nextObj.appId());
912 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
913 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700914 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800915
916 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700917 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800918 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
919
920 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
921 // that depends on it
922 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
923
924 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
925 deviceId, Integer.toHexString(l3ecmpGroupId),
926 l3ecmpGroupKey, nextObj.id());
927 // finally we are ready to send the innermost groups
928 for (GroupInfo gi : unsentGroups) {
929 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700930 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
931 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
932 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800933 }
Charles Chan188ebf52015-12-23 00:15:11 -0800934 }
935
936 /**
937 * Creates group chains for all buckets in a hashed group, and stores the
938 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
939 * should be empty.
940 * <p>
941 * Does not create the top level ECMP group. Does not actually send the
942 * groups to the groupService.
943 *
944 * @param nextObj the Next Objective with buckets that need to be converted
945 * to group chains
946 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
947 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
948 */
Pier Ventre140a8942016-11-02 07:26:38 -0700949 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700950 List<Deque<GroupKey>> allGroupKeys,
951 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800952 // break up hashed next objective to multiple groups
953 Collection<TrafficTreatment> buckets = nextObj.next();
954
955 for (TrafficTreatment bucket : buckets) {
956 //figure out how many labels are pushed in each bucket
957 int labelsPushed = 0;
958 MplsLabel innermostLabel = null;
959 for (Instruction ins : bucket.allInstructions()) {
960 if (ins.type() == Instruction.Type.L2MODIFICATION) {
961 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
962 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
963 labelsPushed++;
964 }
965 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
966 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700967 innermostLabel =
968 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800969 }
970 }
971 }
972 }
973
Yi Tsengef19de12017-04-24 11:33:05 -0700974 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800975 // XXX we only deal with 0 and 1 label push right now
976 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700977 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700978 TrafficSelector metaSelector = nextObj.meta();
979 if (metaSelector != null) {
980 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700981 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
982 nextObj.appId(), true,
983 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700984 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700985 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
986 nextObj.appId(), false,
987 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700988 }
989 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700990 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
991 nextObj.appId(), false,
992 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700993 }
Yi Tsengef19de12017-04-24 11:33:05 -0700994 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800995 log.error("Could not process nextObj={} in dev:{}",
996 nextObj.id(), deviceId);
997 return;
998 }
Yi Tsengef19de12017-04-24 11:33:05 -0700999 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
1000 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -08001001
1002 // we can't send the inner group description yet, as we have to
1003 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -07001004 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -08001005
1006 } else if (labelsPushed == 1) {
1007 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1008 nextObj.appId(), true,
1009 nextObj.meta());
1010 if (onelabelGroupInfo == null) {
1011 log.error("Could not process nextObj={} in dev:{}",
1012 nextObj.id(), deviceId);
1013 return;
1014 }
1015 // we need to add another group to this chain - the L3VPN group
1016 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001017 if (requireVlanPopBeforeMplsPush()) {
1018 l3vpnTtb.popVlan();
1019 }
Charles Chan188ebf52015-12-23 00:15:11 -08001020 l3vpnTtb.pushMpls()
1021 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001022 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001023 if (supportCopyTtl()) {
1024 l3vpnTtb.copyTtlOut();
1025 }
1026 if (supportSetMplsBos()) {
1027 l3vpnTtb.setMplsBos(true);
1028 }
Charles Chan0f43e472017-02-14 14:00:16 -08001029 if (requireVlanPopBeforeMplsPush()) {
1030 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1031 }
Charles Chan40132b32017-01-22 00:19:37 -08001032
Charles Chan188ebf52015-12-23 00:15:11 -08001033 GroupBucket l3vpnGrpBkt =
1034 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001035 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001036 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1037 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1038 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001039 GroupDescription l3vpnGroupDesc =
1040 new DefaultGroupDescription(
1041 deviceId,
1042 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001043 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1044 l3vpnGroupKey,
1045 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001046 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001047 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1048 1,
1049 false,
1050 deviceId);
1051 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001052
Yi Tsengef19de12017-04-24 11:33:05 -07001053 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1054 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1055 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001056
1057 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001058 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001059
1060 // we can't send the innermost group yet, as we have to create
1061 // the dependent ECMP group first. So we store ...
1062 unsentGroups.add(onelabelGroupInfo);
1063
Yi Tsengef19de12017-04-24 11:33:05 -07001064 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1065 deviceId, Integer.toHexString(l3vpnGroupId),
1066 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001067
1068 } else {
1069 log.warn("Driver currently does not handle more than 1 MPLS "
1070 + "labels. Not processing nextObjective {}", nextObj.id());
1071 return;
1072 }
1073
1074 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001075 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001076 }
1077 }
1078
Pier Ventre42287df2016-11-09 14:17:26 -08001079 /**
1080 * Processes the pseudo wire related next objective.
1081 * This procedure try to reuse the mpls label groups,
1082 * the mpls interface group and the l2 interface group.
1083 *
1084 * @param nextObjective the objective to process.
1085 */
1086 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001087 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1088 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001089 }
1090
Saurav Das8be4e3a2016-03-11 17:19:07 -08001091 //////////////////////////////////////
1092 // Group Editing
1093 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001094 /**
1095 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001096 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001097 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001098 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001099 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001100 * @param next the representation of the existing group-chain for this next objective
1101 */
1102 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001103 if (nextObjective.type() != NextObjective.Type.HASHED &&
1104 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001105 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001106 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001107 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001108 return;
1109 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001110
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001111 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001112 // existing bucket. If it is for an existing output port, then its a
1113 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001114 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001115 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1116 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1117 groupService,
1118 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001119 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1120 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001121
1122 nextObjective.next().forEach(trafficTreatment -> {
1123 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001124 if (portNumber == null) {
1125 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001126 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001127 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001128 // its possible that portnumbers are same but labels are different
1129 int label = readLabelFromTreatment(trafficTreatment);
1130 if (label == -1) {
1131 duplicateBuckets.add(trafficTreatment);
1132 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001133 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1134 groupService, deviceId,
1135 portNumber, label);
1136 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001137 duplicateBuckets.add(trafficTreatment);
1138 } else {
1139 nonDuplicateBuckets.add(trafficTreatment);
1140 }
1141 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001142 } else {
1143 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001144 }
1145 });
1146
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001147 if (duplicateBuckets.isEmpty()) {
1148 // use the original objective
1149 objectiveToAdd = nextObjective;
1150 } else if (!nonDuplicateBuckets.isEmpty()) {
1151 // only use the non-duplicate buckets if there are any
1152 log.debug("Some buckets {} already exist in next id {}, duplicate "
1153 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1154 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001155 NextObjective.Builder builder = DefaultNextObjective.builder()
1156 .withType(nextObjective.type())
1157 .withId(nextObjective.id())
1158 .withMeta(nextObjective.meta())
1159 .fromApp(nextObjective.appId());
1160 nonDuplicateBuckets.forEach(builder::addTreatment);
1161
1162 ObjectiveContext context = nextObjective.context().orElse(null);
1163 objectiveToAdd = builder.addToExisting(context);
1164 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001165 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001166 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1167 duplicateBuckets, nextObjective.id());
1168 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001169 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001170 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001171
Saurav Das1a129a02016-11-18 15:21:57 -08001172 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001173 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001174 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001175 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001176 }
1177 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001178
Saurav Das1a129a02016-11-18 15:21:57 -08001179 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001180 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001181 // storage for all group keys in the chain of groups created
1182 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1183 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001184 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001185 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1186
Yi Tseng78f51f42017-02-02 13:54:58 -08001187 // now we can create the buckets to add to the outermost L3 ECMP group
1188 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001189
Saurav Das1a129a02016-11-18 15:21:57 -08001190 // retrieve the original L3 ECMP group
1191 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001192 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001193 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001194 return;
1195 }
Saurav Das1a129a02016-11-18 15:21:57 -08001196 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001197 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001198
1199 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001200 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001201 // processed, the info will be extracted for the bucketAdd call to groupService
1202 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001203 new DefaultGroupDescription(deviceId,
1204 SELECT,
1205 new GroupBuckets(newBuckets),
1206 l3ecmpGroupKey,
1207 l3ecmpGroupId,
1208 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001209 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1210 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001211 true,
1212 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001213
Saurav Dasc88d4662017-05-15 15:34:25 -07001214 // update new bucket-chains
1215 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1216 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1217 newBucketChain.addFirst(l3ecmpGroupKey);
1218 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001219 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001220 updatePendingNextObjective(l3ecmpGroupKey,
1221 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001222 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001223 deviceId, Integer.toHexString(l3ecmpGroupId),
1224 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001225
1226 unsentGroups.forEach(groupInfo -> {
1227 // send the innermost group
1228 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001229 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1230 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001231 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1232 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001233 });
Saurav Das1a129a02016-11-18 15:21:57 -08001234 }
Charles Chan188ebf52015-12-23 00:15:11 -08001235
Saurav Das1a129a02016-11-18 15:21:57 -08001236 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001237 List<Deque<GroupKey>> allActiveKeys) {
1238 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001239 if (assignedVlan == null) {
1240 log.warn("VLAN ID required by broadcast next obj is missing. "
1241 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1242 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001243 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001244 return;
1245 }
Saurav Das1a129a02016-11-18 15:21:57 -08001246 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001247 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001248 if (ipDst != null) {
1249 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001250 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001251 } else {
1252 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001253 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001254 }
1255 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001256 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001257 }
1258 }
1259
1260 private void addBucketToL2FloodGroup(NextObjective nextObj,
1261 List<Deque<GroupKey>> allActiveKeys,
1262 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001263 VlanId assignedVlan) {
1264 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1265
1266 if (l2FloodGroup == null) {
1267 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1268 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001269 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001270 return;
1271 }
Saurav Das1a129a02016-11-18 15:21:57 -08001272
Yi Tseng78f51f42017-02-02 13:54:58 -08001273 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1274 int l2floodGroupId = l2FloodGroup.id().id();
1275 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1276
1277 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001278 new DefaultGroupDescription(deviceId,
1279 ALL,
1280 new GroupBuckets(newBuckets),
1281 l2floodGroupKey,
1282 l2floodGroupId,
1283 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001284
Yi Tseng78f51f42017-02-02 13:54:58 -08001285 GroupChainElem l2FloodGroupChainElement =
1286 new GroupChainElem(l2FloodGroupDescription,
1287 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001288 true,
1289 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001290
Yi Tseng78f51f42017-02-02 13:54:58 -08001291
1292 //ensure assignedVlan applies to the chosen group
1293 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1294
1295 if (!floodGroupVlan.equals(assignedVlan)) {
1296 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1297 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1298 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001299 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001300 return;
1301 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001302 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001303 groupInfos.forEach(groupInfo -> {
1304 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001305 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001306 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001307 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001308 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001309
Yi Tsengef19de12017-04-24 11:33:05 -07001310 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001311 deviceId, Integer.toHexString(l2floodGroupId),
1312 l2floodGroupKey, nextObj.id());
1313 // send the innermost group
1314 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001315 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001316 deviceId);
1317
Yi Tsengef19de12017-04-24 11:33:05 -07001318 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001319
Yi Tsengef19de12017-04-24 11:33:05 -07001320 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1321 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001322 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1323
1324 if (existsL2IGroup != null) {
1325 // group already exist
1326 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1327 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001328 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001329 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001330 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001331
1332 updatePendingNextObjective(l2floodGroupKey,
1333 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001334 }
1335
Saurav Das1a129a02016-11-18 15:21:57 -08001336 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1337 List<Deque<GroupKey>> allActiveKeys,
1338 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001339 VlanId assignedVlan) {
1340 // create the buckets to add to the outermost L3 Multicast group
1341 List<GroupBucket> newBuckets = Lists.newArrayList();
1342 groupInfos.forEach(groupInfo -> {
1343 // Points to L3 interface group if there is one.
1344 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -07001345 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
1346 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
1347 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1348 treatmentBuilder.group(new GroupId(nextGroupDesc.givenGroupId()));
1349 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
Yi Tseng78f51f42017-02-02 13:54:58 -08001350 newBuckets.add(newBucket);
1351 });
Saurav Das1a129a02016-11-18 15:21:57 -08001352
1353 // get the group being edited
1354 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1355 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001356 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001357 return;
1358 }
1359 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1360 int l3mcastGroupId = l3mcastGroup.id().id();
1361
1362 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001363 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001364 if (!expectedVlan.equals(assignedVlan)) {
1365 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1366 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1367 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001368 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001369 }
1370 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001371 new DefaultGroupDescription(deviceId,
1372 ALL,
1373 new GroupBuckets(newBuckets),
1374 l3mcastGroupKey,
1375 l3mcastGroupId,
1376 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001377 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001378 groupInfos.size(),
1379 true,
1380 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001381
1382 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001383 groupInfos.forEach(groupInfo -> {
1384 // update original NextGroup with new bucket-chain
1385 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001386 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001387 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001388 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1389 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001390 }
1391 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001392 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001393
Yi Tsengef19de12017-04-24 11:33:05 -07001394 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001395 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001396 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1397 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1398 1,
1399 false,
1400 deviceId);
1401 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001402 }
Yi Tsengef19de12017-04-24 11:33:05 -07001403 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001404 deviceId, Integer.toHexString(l3mcastGroupId),
1405 l3mcastGroupKey, nextObj.id());
1406 // send the innermost group
1407 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001408 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001409 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001410 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001411
1412 });
1413
Saurav Das1a129a02016-11-18 15:21:57 -08001414 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001415 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001416 }
1417
1418 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001419 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001420 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001421 * may be in use (referenced by other groups) elsewhere.
1422 *
Saurav Dasceccf242017-08-03 18:30:35 -07001423 * @param nextObjective a next objective that contains information for the
1424 * buckets to be removed from the group
1425 * @param next the representation of the existing group-chains for this next
1426 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001427 */
1428 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001429 if (nextObjective.type() != NextObjective.Type.HASHED &&
1430 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001431 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1432 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001433 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001434 return;
1435 }
Yi Tsengef19de12017-04-24 11:33:05 -07001436 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001437 List<Integer> indicesToRemove = Lists.newArrayList();
1438 for (TrafficTreatment treatment : nextObjective.next()) {
1439 // find the top-level bucket in the group-chain by matching the
1440 // outport and label from different groups in the chain
1441 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1442 int labelToRemove = readLabelFromTreatment(treatment);
1443 if (portToRemove == null) {
1444 log.warn("treatment {} of next objective {} has no outport.. "
1445 + "cannot remove bucket from group in dev: {}", treatment,
1446 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001447 continue;
1448 }
Saurav Dasceccf242017-08-03 18:30:35 -07001449 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1450 groupService, deviceId,
1451 portToRemove, labelToRemove);
1452 indicesToRemove.addAll(existing);
1453
Charles Chan188ebf52015-12-23 00:15:11 -08001454 }
Saurav Dasceccf242017-08-03 18:30:35 -07001455
1456 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1457 indicesToRemove.forEach(index -> chainsToRemove
1458 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001459 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001460 log.warn("Could not find appropriate group-chain for removing bucket"
1461 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001462 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001463 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001464 }
Saurav Dasceccf242017-08-03 18:30:35 -07001465 removeBucket(chainsToRemove, nextObjective);
1466 }
1467
1468 /**
1469 * Removes top-level buckets from a group that represents the given next objective.
1470 *
1471 * @param chainsToRemove a list of group bucket chains to remove
1472 * @param nextObjective the next objective that contains information for the
1473 * buckets to be removed from the group
1474 */
1475 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1476 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001477 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1478 //first group key is the one we want to modify
1479 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001480 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001481 for (Deque<GroupKey> foundChain : chainsToRemove) {
1482 //second group key is the one we wish to remove the reference to
1483 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001484 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001485 // the chain.
1486 log.warn("Can't find second group key from chain {}",
1487 foundChain);
1488 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001489 }
Saurav Dasceccf242017-08-03 18:30:35 -07001490 GroupKey pointedGroupKey = foundChain.stream()
1491 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001492 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001493 if (pointedGroup == null) {
1494 continue;
1495 }
1496
1497 GroupBucket bucket;
1498 if (nextObjective.type() == NextObjective.Type.HASHED) {
1499 bucket = DefaultGroupBucket.createSelectGroupBucket(
1500 DefaultTrafficTreatment.builder()
1501 .group(pointedGroup.id())
1502 .build());
1503 } else {
1504 bucket = DefaultGroupBucket.createAllGroupBucket(
1505 DefaultTrafficTreatment.builder()
1506 .group(pointedGroup.id())
1507 .build());
1508 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001509 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001510 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001511
1512 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1513 List<String> pointedGroupIds; // for debug log
1514 pointedGroupIds = bucketsToRemove.stream()
1515 .map(GroupBucket::treatment)
1516 .map(TrafficTreatment::allInstructions)
1517 .flatMap(List::stream)
1518 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1519 .map(inst -> (Instructions.GroupInstruction) inst)
1520 .map(Instructions.GroupInstruction::groupId)
1521 .map(GroupId::id)
1522 .map(Integer::toHexString)
1523 .map(id -> HEX_PREFIX + id)
1524 .collect(Collectors.toList());
1525
Yi Tseng78f51f42017-02-02 13:54:58 -08001526 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001527 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001528 pointedGroupIds, nextObjective.id(), deviceId);
1529 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001530 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1531 removeBuckets, modGroupKey,
1532 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001533 // update store - synchronize access as there may be multiple threads
1534 // trying to remove buckets from the same group, each with its own
1535 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001536 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001537 // get a fresh copy of what the store holds
1538 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1539 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001540 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001541 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001542 // Object.equals(), we have to check the deque elems one by one
1543 allActiveKeys
1544 .removeIf(active ->
1545 chainsToRemove.stream().anyMatch(remove ->
1546 Arrays.equals(remove.toArray(new GroupKey[0]),
1547 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001548 // If no buckets in the group, then retain an entry for the
1549 // top level group which still exists.
1550 if (allActiveKeys.isEmpty()) {
1551 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1552 top.add(modGroupKey);
1553 allActiveKeys.add(top);
1554 }
1555 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001556 new OfdpaNextGroup(allActiveKeys,
1557 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001558 }
Charles Chan188ebf52015-12-23 00:15:11 -08001559 }
1560
1561 /**
Saurav Das961beb22017-03-29 19:09:17 -07001562 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001563 *
1564 * @param nextObjective the next objective to remove
1565 * @param next the NextGroup that represents the existing group-chain for
1566 * this next objective
1567 */
1568 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001569
Yi Tsengef19de12017-04-24 11:33:05 -07001570 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001571
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001572 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001573 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001574 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001575
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001576 allActiveKeys
1577 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1578 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001579 flowObjectiveStore.removeNextGroup(nextObjective.id());
1580 }
1581
Saurav Dasceccf242017-08-03 18:30:35 -07001582 /**
1583 * Checks existing buckets in {@link NextGroup} to verify if they match
1584 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1585 * to ensure that the buckets match up.
1586 *
1587 * @param nextObjective the next objective to verify
1588 * @param next the representation of the existing group which has to be
1589 * modified to match the given next objective
1590 */
1591 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
1592 if (nextObjective.type() != NextObjective.Type.HASHED) {
1593 log.warn("verification not supported for {} group", nextObjective.type());
1594 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1595 return;
1596 }
1597 log.debug("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
1598 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1599 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1600 List<Integer> indicesToRemove = Lists.newArrayList();
Saurav Dasceccf242017-08-03 18:30:35 -07001601 for (TrafficTreatment bkt : nextObjective.next()) {
1602 PortNumber portNumber = readOutPortFromTreatment(bkt);
1603 int label = readLabelFromTreatment(bkt);
1604 if (portNumber == null) {
1605 log.warn("treatment {} of next objective {} has no outport.. "
1606 + "cannot remove bucket from group in dev: {}", bkt,
1607 nextObjective.id(), deviceId);
1608 fail(nextObjective, ObjectiveError.BADPARAMS);
1609 return;
1610 }
1611 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1612 groupService, deviceId,
1613 portNumber, label);
1614 if (existing.isEmpty()) {
1615 // if it doesn't exist, mark this bucket for creation
1616 bucketsToCreate.add(bkt);
1617 }
1618 if (existing.size() > 1) {
1619 // if it exists but there are duplicates, mark the others for removal
1620 existing.remove(0);
1621 indicesToRemove.addAll(existing);
1622 }
1623 }
1624
1625 if (!bucketsToCreate.isEmpty()) {
1626 log.info("creating {} buckets as part of nextId: {} verification",
1627 bucketsToCreate.size(), nextObjective.id());
1628 //create a nextObjective only with these buckets
1629 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1630 .withId(nextObjective.id())
1631 .withType(NextObjective.Type.HASHED)
1632 .withMeta(nextObjective.meta())
1633 .fromApp(nextObjective.appId());
1634 bucketsToCreate.forEach(bucket -> nextObjBuilder.addTreatment(bucket));
1635 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1636 }
1637
1638 if (!indicesToRemove.isEmpty()) {
1639 log.info("removing {} buckets as part of nextId: {} verification",
1640 indicesToRemove.size(), nextObjective.id());
1641 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1642 indicesToRemove.forEach(index -> chainsToRemove
1643 .add(allActiveKeys.get(index)));
1644 removeBucket(chainsToRemove, nextObjective);
1645 }
1646
Saurav Das9df5b7c2017-08-14 16:44:43 -07001647 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1648 // flowObjective store record is in-sync with nextObjective passed-in
1649 // Nevertheless groupStore may not be in sync due to bug in the store
1650 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1651 // method will not be required.
1652 GroupKey hashGroupKey = allActiveKeys.get(0).peekFirst();
1653 Group hashGroup = groupService.getGroup(deviceId, hashGroupKey);
1654 int actualGroupSize = hashGroup.buckets().buckets().size();
1655 int objGroupSize = nextObjective.next().size();
1656 if (actualGroupSize != objGroupSize) {
1657 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1658 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1659 objGroupSize, actualGroupSize);
1660 }
1661 if (actualGroupSize > objGroupSize) {
1662 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1663 //check every bucket in the actual group
1664 for (GroupBucket bucket : hashGroup.buckets().buckets()) {
1665 GroupInstruction g = (GroupInstruction) bucket.treatment()
1666 .allInstructions().iterator().next();
1667 GroupId gidToCheck = g.groupId(); // the group pointed to
1668 boolean matches = false;
1669 for (Deque<GroupKey> validChain : allActiveKeys) {
1670 if (validChain.size() < 2) {
1671 continue;
1672 }
1673 GroupKey pointedGroupKey = validChain.stream()
1674 .collect(Collectors.toList()).get(1);
1675 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1676 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1677 matches = true;
1678 break;
1679 }
1680 }
1681 if (!matches) {
1682 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1683 bucketsToRemove.add(bucket);
1684 }
1685 }
1686 // remove buckets for which there was no record in the obj store
1687 if (bucketsToRemove.isEmpty()) {
1688 log.warn("Mismatch detected but could not determine which"
1689 + "buckets to remove");
1690 } else {
1691 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1692 groupService.removeBucketsFromGroup(deviceId, hashGroupKey,
1693 removeBuckets, hashGroupKey,
1694 nextObjective.appId());
1695 }
1696 } else if (actualGroupSize < objGroupSize) {
1697 // should also add buckets not in group-store but in obj-store
1698 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1699 //check every bucket in the obj
1700 for (Deque<GroupKey> validChain : allActiveKeys) {
1701 if (validChain.size() < 2) {
1702 continue;
1703 }
1704 GroupKey pointedGroupKey = validChain.stream()
1705 .collect(Collectors.toList()).get(1);
1706 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1707 if (pointedGroup == null) {
1708 // group should exist, otherwise cannot be added as bucket
1709 continue;
1710 }
1711 boolean matches = false;
1712 for (GroupBucket bucket : hashGroup.buckets().buckets()) {
1713 GroupInstruction g = (GroupInstruction) bucket.treatment()
1714 .allInstructions().iterator().next();
1715 GroupId gidToCheck = g.groupId(); // the group pointed to
1716 if (pointedGroup.id().equals(gidToCheck)) {
1717 matches = true;
1718 break;
1719 }
1720 }
1721 if (!matches) {
1722 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1723 TrafficTreatment t = DefaultTrafficTreatment.builder()
1724 .group(pointedGroup.id())
1725 .build();
1726 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1727 }
1728 }
1729 if (bucketsToAdd.isEmpty()) {
1730 log.warn("Mismatch detected but could not determine which "
1731 + "buckets to add");
1732 } else {
1733 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
1734 groupService.addBucketsToGroup(deviceId, hashGroupKey,
1735 addBuckets, hashGroupKey,
1736 nextObjective.appId());
1737 }
1738 }
1739 }
1740
Saurav Dasceccf242017-08-03 18:30:35 -07001741 pass(nextObjective);
1742 }
1743
1744 //////////////////////////////////////
1745 // Helper methods and classes
1746 //////////////////////////////////////
1747
Yi Tsengef19de12017-04-24 11:33:05 -07001748 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1749 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001750 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001751 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001752 }
Yi Tsengef19de12017-04-24 11:33:05 -07001753 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001754 return val;
1755 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001756 }
1757
Yi Tsengef19de12017-04-24 11:33:05 -07001758 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1759 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001760 if (val == null) {
1761 val = Sets.newConcurrentHashSet();
1762 }
1763 val.add(gce);
1764 return val;
1765 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001766 }
1767
Yi Tsengef19de12017-04-24 11:33:05 -07001768 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1769 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001770 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1771 if (nextObjs != null) {
1772 nextObjs.add(nextObjective);
1773 } else {
1774 nextObjs = Sets.newHashSet(nextObjective);
1775 }
1776 return nextObjs;
1777 });
1778 }
1779
Yi Tsengef19de12017-04-24 11:33:05 -07001780 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1781 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1782 if (nextObjs != null) {
1783
1784 nextObjs.forEach(nextObj -> {
1785 log.debug("Group {} updated, update pending next objective {}.",
1786 groupKey, nextObj);
1787
1788 pass(nextObj);
1789 });
1790 }
1791 return Sets.newHashSet();
1792 });
1793 }
1794
1795 private void processPendingRemoveNextObjs(GroupKey key) {
1796 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1797 if (groupKeys.isEmpty()) {
1798 pendingRemoveNextObjectives.invalidate(nextObjective);
1799 pass(nextObjective);
1800 } else {
1801 groupKeys.remove(key);
1802 }
1803 });
1804 }
1805
1806 protected int getNextAvailableIndex() {
1807 return (int) nextIndex.incrementAndGet();
1808 }
1809
1810 protected Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1811 int nextid) {
1812 GroupKey topLevelGroupKey;
1813 if (!allActiveKeys.isEmpty()) {
1814 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1815 } else {
1816 log.warn("Could not determine top level group while processing"
1817 + "next:{} in dev:{}", nextid, deviceId);
1818 return null;
1819 }
1820 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1821 if (topGroup == null) {
1822 log.warn("Could not find top level group while processing "
1823 + "next:{} in dev:{}", nextid, deviceId);
1824 }
1825 return topGroup;
1826 }
1827
1828 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1829 //first check for group chain
1830 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1831 if (gceSet != null) {
1832 for (GroupChainElem gce : gceSet) {
1833 log.debug("Group service {} group key {} in device {}. "
1834 + "Processing next group in group chain with group id 0x{}",
1835 (added) ? "ADDED" : "processed",
1836 key, deviceId,
1837 Integer.toHexString(gce.groupDescription().givenGroupId()));
1838 processGroupChain(gce);
1839 }
1840 } else {
1841 // otherwise chain complete - check for waiting nextObjectives
1842 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1843 pendingAddNextObjectives.getIfPresent(key);
1844 if (nextGrpList != null) {
1845 pendingAddNextObjectives.invalidate(key);
1846 nextGrpList.forEach(nextGrp -> {
1847 log.debug("Group service {} group key {} in device:{}. "
1848 + "Done implementing next objective: {} <<-->> gid:0x{}",
1849 (added) ? "ADDED" : "processed",
1850 key, deviceId, nextGrp.nextObjective().id(),
1851 Integer.toHexString(groupService.getGroup(deviceId, key)
1852 .givenGroupId()));
1853 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001854 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001855
1856 // check if addBuckets waiting for this completion
1857 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1858 if (pendBkts != null) {
1859 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1860 }
1861 return null;
1862 });
1863 });
1864 }
1865 }
1866 }
1867
Charles Chan188ebf52015-12-23 00:15:11 -08001868 /**
1869 * Processes next element of a group chain. Assumption is that if this
1870 * group points to another group, the latter has already been created
1871 * and this driver has received notification for it. A second assumption is
1872 * that if there is another group waiting for this group then the appropriate
1873 * stores already have the information to act upon the notification for the
1874 * creation of this group.
1875 * <p>
1876 * The processing of the GroupChainElement depends on the number of groups
1877 * this element is waiting on. For all group types other than SIMPLE, a
1878 * GroupChainElement could be waiting on multiple groups.
1879 *
1880 * @param gce the group chain element to be processed next
1881 */
1882 private void processGroupChain(GroupChainElem gce) {
1883 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1884 if (waitOnGroups != 0) {
1885 log.debug("GCE: {} not ready to be processed", gce);
1886 return;
1887 }
1888 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001889 if (gce.addBucketToGroup()) {
1890 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1891 gce.groupDescription().appCookie(),
1892 gce.groupDescription().buckets(),
1893 gce.groupDescription().appCookie(),
1894 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001895 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001896 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001897 }
1898 }
1899
Yi Tsengef19de12017-04-24 11:33:05 -07001900 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1901 List<GroupKey> groupKeys) {
1902 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001903 }
1904
Saurav Dasc88d4662017-05-15 15:34:25 -07001905 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1906 synchronized (flowObjectiveStore) {
1907 // get fresh copy of what the store holds
1908 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1909 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1910 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1911 return;
1912 }
1913 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1914 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001915 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001916 // If active keys shows only the top-level group without a chain of groups,
1917 // then it represents an empty group. Update by replacing empty chain.
1918 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1919 allActiveKeys.clear();
1920 }
1921 allActiveKeys.addAll(nextGrp.allKeys());
1922 flowObjectiveStore.putNextGroup(nextId,
1923 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1924 }
1925 }
1926 }
1927
Saurav Das8be4e3a2016-03-11 17:19:07 -08001928 private class InnerGroupListener implements GroupListener {
1929 @Override
1930 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001931 switch (event.type()) {
1932 case GROUP_ADDED:
1933 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1934 break;
1935 case GROUP_REMOVED:
1936 processPendingRemoveNextObjs(event.subject().appCookie());
1937 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001938 case GROUP_UPDATED:
1939 processPendingUpdateNextObjs(event.subject().appCookie());
1940 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001941 default:
1942 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001943 }
1944 }
1945 }
Charles Chan188ebf52015-12-23 00:15:11 -08001946}