blob: a3670e6cab050ce369e81028da9a90b365b194f2 [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
Pier Luigi21fffd22018-01-19 10:24:53 +0100807 private List<GroupBucket> createL3MulticastBucket(List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700808 List<GroupBucket> l3McastBuckets = new ArrayList<>();
Pier Luigi21fffd22018-01-19 10:24:53 +0100809 // For each inner group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700810 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 });
Pier Luigi21fffd22018-01-19 10:24:53 +0100820 // Done return the new list of buckets
821 return l3McastBuckets;
822 }
823
824
825 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
826 List<GroupInfo> groupInfos) {
827 // Let's create a new list mcast buckets
828 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700829
830 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700831 int l3MulticastGroupId = L3_MULTICAST_TYPE |
832 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
833 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700834 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700835
836 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800837 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700838 new GroupBuckets(l3McastBuckets),
839 l3MulticastGroupKey,
840 l3MulticastGroupId,
841 nextObj.appId());
842
843 // Put all dependency information into allGroupKeys
844 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
845 groupInfos.forEach(groupInfo -> {
846 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700847 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700848 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700849 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
850 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700851 }
852 gkeyChain.addFirst(l3MulticastGroupKey);
853 allGroupKeys.add(gkeyChain);
854 });
855
856 // Point the next objective to this group
857 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
858 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
859
860 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700861 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700862 groupInfos.forEach(groupInfo -> {
863 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700864 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700865
866 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700867 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
868 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
869 1, false, deviceId);
870 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700871 }
872
873 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700874 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700875 });
Charles Chan188ebf52015-12-23 00:15:11 -0800876 }
877
Charles Chan188ebf52015-12-23 00:15:11 -0800878 /**
879 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
880 * a chain of groups. The hashed Next Objective passed in by the application
881 * has to be broken up into a group chain comprising of an
882 * L3 ECMP group as the top level group. Buckets of this group can point
883 * to a variety of groups in a group chain, depending on the whether
884 * MPLS labels are being pushed or not.
885 * <p>
886 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
887 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
888 * check the nextObjective meta to see what is matching before being
889 * sent to this nextObjective.
890 *
891 * @param nextObj the nextObjective of type HASHED
892 */
Pier Ventre140a8942016-11-02 07:26:38 -0700893 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800894 // storage for all group keys in the chain of groups created
895 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
896 List<GroupInfo> unsentGroups = new ArrayList<>();
897 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
898
899 // now we can create the outermost L3 ECMP group
900 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
901 for (GroupInfo gi : unsentGroups) {
902 // create ECMP bucket to point to the outer group
903 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700904 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800905 GroupBucket sbucket = DefaultGroupBucket
906 .createSelectGroupBucket(ttb.build());
907 l3ecmpGroupBuckets.add(sbucket);
908 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800909 int l3ecmpIndex = getNextAvailableIndex();
910 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
911 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700912 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800913 GroupDescription l3ecmpGroupDesc =
914 new DefaultGroupDescription(
915 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800916 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800917 new GroupBuckets(l3ecmpGroupBuckets),
918 l3ecmpGroupKey,
919 l3ecmpGroupId,
920 nextObj.appId());
921 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
922 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700923 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800924
925 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700926 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800927 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
928
929 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
930 // that depends on it
931 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
932
933 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
934 deviceId, Integer.toHexString(l3ecmpGroupId),
935 l3ecmpGroupKey, nextObj.id());
936 // finally we are ready to send the innermost groups
937 for (GroupInfo gi : unsentGroups) {
938 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700939 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
940 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
941 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800942 }
Charles Chan188ebf52015-12-23 00:15:11 -0800943 }
944
945 /**
946 * Creates group chains for all buckets in a hashed group, and stores the
947 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
948 * should be empty.
949 * <p>
950 * Does not create the top level ECMP group. Does not actually send the
951 * groups to the groupService.
952 *
953 * @param nextObj the Next Objective with buckets that need to be converted
954 * to group chains
955 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
956 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
957 */
Pier Ventre140a8942016-11-02 07:26:38 -0700958 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700959 List<Deque<GroupKey>> allGroupKeys,
960 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800961 // break up hashed next objective to multiple groups
962 Collection<TrafficTreatment> buckets = nextObj.next();
963
964 for (TrafficTreatment bucket : buckets) {
965 //figure out how many labels are pushed in each bucket
966 int labelsPushed = 0;
967 MplsLabel innermostLabel = null;
968 for (Instruction ins : bucket.allInstructions()) {
969 if (ins.type() == Instruction.Type.L2MODIFICATION) {
970 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
971 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
972 labelsPushed++;
973 }
974 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
975 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700976 innermostLabel =
977 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800978 }
979 }
980 }
981 }
982
Yi Tsengef19de12017-04-24 11:33:05 -0700983 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800984 // XXX we only deal with 0 and 1 label push right now
985 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700986 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700987 TrafficSelector metaSelector = nextObj.meta();
988 if (metaSelector != null) {
989 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700990 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
991 nextObj.appId(), true,
992 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700993 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700994 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
995 nextObj.appId(), false,
996 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700997 }
998 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700999 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1000 nextObj.appId(), false,
1001 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001002 }
Yi Tsengef19de12017-04-24 11:33:05 -07001003 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -08001004 log.error("Could not process nextObj={} in dev:{}",
1005 nextObj.id(), deviceId);
1006 return;
1007 }
Yi Tsengef19de12017-04-24 11:33:05 -07001008 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
1009 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -08001010
1011 // we can't send the inner group description yet, as we have to
1012 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -07001013 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -08001014
1015 } else if (labelsPushed == 1) {
1016 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1017 nextObj.appId(), true,
1018 nextObj.meta());
1019 if (onelabelGroupInfo == null) {
1020 log.error("Could not process nextObj={} in dev:{}",
1021 nextObj.id(), deviceId);
1022 return;
1023 }
1024 // we need to add another group to this chain - the L3VPN group
1025 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001026 if (requireVlanPopBeforeMplsPush()) {
1027 l3vpnTtb.popVlan();
1028 }
Charles Chan188ebf52015-12-23 00:15:11 -08001029 l3vpnTtb.pushMpls()
1030 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001031 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001032 if (supportCopyTtl()) {
1033 l3vpnTtb.copyTtlOut();
1034 }
1035 if (supportSetMplsBos()) {
1036 l3vpnTtb.setMplsBos(true);
1037 }
Charles Chan0f43e472017-02-14 14:00:16 -08001038 if (requireVlanPopBeforeMplsPush()) {
1039 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1040 }
Charles Chan40132b32017-01-22 00:19:37 -08001041
Charles Chan188ebf52015-12-23 00:15:11 -08001042 GroupBucket l3vpnGrpBkt =
1043 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001044 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001045 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1046 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1047 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001048 GroupDescription l3vpnGroupDesc =
1049 new DefaultGroupDescription(
1050 deviceId,
1051 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001052 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1053 l3vpnGroupKey,
1054 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001055 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001056 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1057 1,
1058 false,
1059 deviceId);
1060 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001061
Yi Tsengef19de12017-04-24 11:33:05 -07001062 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1063 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1064 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001065
1066 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001067 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001068
1069 // we can't send the innermost group yet, as we have to create
1070 // the dependent ECMP group first. So we store ...
1071 unsentGroups.add(onelabelGroupInfo);
1072
Yi Tsengef19de12017-04-24 11:33:05 -07001073 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1074 deviceId, Integer.toHexString(l3vpnGroupId),
1075 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001076
1077 } else {
1078 log.warn("Driver currently does not handle more than 1 MPLS "
1079 + "labels. Not processing nextObjective {}", nextObj.id());
1080 return;
1081 }
1082
1083 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001084 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001085 }
1086 }
1087
Pier Ventre42287df2016-11-09 14:17:26 -08001088 /**
1089 * Processes the pseudo wire related next objective.
1090 * This procedure try to reuse the mpls label groups,
1091 * the mpls interface group and the l2 interface group.
1092 *
1093 * @param nextObjective the objective to process.
1094 */
1095 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001096 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1097 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001098 }
1099
Saurav Das8be4e3a2016-03-11 17:19:07 -08001100 //////////////////////////////////////
1101 // Group Editing
1102 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001103 /**
1104 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001105 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001106 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001107 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001108 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001109 * @param next the representation of the existing group-chain for this next objective
1110 */
1111 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001112 if (nextObjective.type() != NextObjective.Type.HASHED &&
1113 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001114 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001115 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001116 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001117 return;
1118 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001119
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001120 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001121 // existing bucket. If it is for an existing output port, then its a
1122 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001123 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001124 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1125 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1126 groupService,
1127 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001128 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1129 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001130
1131 nextObjective.next().forEach(trafficTreatment -> {
1132 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001133 if (portNumber == null) {
1134 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001135 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001136 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001137 // its possible that portnumbers are same but labels are different
1138 int label = readLabelFromTreatment(trafficTreatment);
1139 if (label == -1) {
1140 duplicateBuckets.add(trafficTreatment);
1141 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001142 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1143 groupService, deviceId,
1144 portNumber, label);
1145 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001146 duplicateBuckets.add(trafficTreatment);
1147 } else {
1148 nonDuplicateBuckets.add(trafficTreatment);
1149 }
1150 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001151 } else {
1152 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001153 }
1154 });
1155
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001156 if (duplicateBuckets.isEmpty()) {
1157 // use the original objective
1158 objectiveToAdd = nextObjective;
1159 } else if (!nonDuplicateBuckets.isEmpty()) {
1160 // only use the non-duplicate buckets if there are any
1161 log.debug("Some buckets {} already exist in next id {}, duplicate "
1162 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1163 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001164 NextObjective.Builder builder = DefaultNextObjective.builder()
1165 .withType(nextObjective.type())
1166 .withId(nextObjective.id())
1167 .withMeta(nextObjective.meta())
1168 .fromApp(nextObjective.appId());
1169 nonDuplicateBuckets.forEach(builder::addTreatment);
1170
1171 ObjectiveContext context = nextObjective.context().orElse(null);
1172 objectiveToAdd = builder.addToExisting(context);
1173 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001174 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001175 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1176 duplicateBuckets, nextObjective.id());
1177 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001178 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001179 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001180
Saurav Das1a129a02016-11-18 15:21:57 -08001181 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001182 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001183 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001184 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001185 }
1186 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001187
Saurav Das1a129a02016-11-18 15:21:57 -08001188 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001189 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001190 // storage for all group keys in the chain of groups created
1191 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1192 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001193 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001194 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1195
Yi Tseng78f51f42017-02-02 13:54:58 -08001196 // now we can create the buckets to add to the outermost L3 ECMP group
1197 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001198
Saurav Das1a129a02016-11-18 15:21:57 -08001199 // retrieve the original L3 ECMP group
1200 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001201 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001202 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001203 return;
1204 }
Saurav Das1a129a02016-11-18 15:21:57 -08001205 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001206 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001207
1208 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001209 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001210 // processed, the info will be extracted for the bucketAdd call to groupService
1211 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001212 new DefaultGroupDescription(deviceId,
1213 SELECT,
1214 new GroupBuckets(newBuckets),
1215 l3ecmpGroupKey,
1216 l3ecmpGroupId,
1217 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001218 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1219 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001220 true,
1221 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001222
Saurav Dasc88d4662017-05-15 15:34:25 -07001223 // update new bucket-chains
1224 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1225 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1226 newBucketChain.addFirst(l3ecmpGroupKey);
1227 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001228 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001229 updatePendingNextObjective(l3ecmpGroupKey,
1230 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001231 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001232 deviceId, Integer.toHexString(l3ecmpGroupId),
1233 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001234
1235 unsentGroups.forEach(groupInfo -> {
1236 // send the innermost group
1237 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001238 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1239 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001240 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1241 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001242 });
Saurav Das1a129a02016-11-18 15:21:57 -08001243 }
Charles Chan188ebf52015-12-23 00:15:11 -08001244
Saurav Das1a129a02016-11-18 15:21:57 -08001245 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001246 List<Deque<GroupKey>> allActiveKeys) {
1247 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001248 if (assignedVlan == null) {
1249 log.warn("VLAN ID required by broadcast next obj is missing. "
1250 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1251 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001252 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001253 return;
1254 }
Saurav Das1a129a02016-11-18 15:21:57 -08001255 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001256 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001257 if (ipDst != null) {
1258 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001259 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001260 } else {
1261 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001262 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001263 }
1264 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001265 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001266 }
1267 }
1268
1269 private void addBucketToL2FloodGroup(NextObjective nextObj,
1270 List<Deque<GroupKey>> allActiveKeys,
1271 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001272 VlanId assignedVlan) {
1273 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1274
1275 if (l2FloodGroup == null) {
1276 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1277 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001278 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001279 return;
1280 }
Saurav Das1a129a02016-11-18 15:21:57 -08001281
Yi Tseng78f51f42017-02-02 13:54:58 -08001282 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1283 int l2floodGroupId = l2FloodGroup.id().id();
1284 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1285
1286 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001287 new DefaultGroupDescription(deviceId,
1288 ALL,
1289 new GroupBuckets(newBuckets),
1290 l2floodGroupKey,
1291 l2floodGroupId,
1292 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001293
Yi Tseng78f51f42017-02-02 13:54:58 -08001294 GroupChainElem l2FloodGroupChainElement =
1295 new GroupChainElem(l2FloodGroupDescription,
1296 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001297 true,
1298 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001299
Yi Tseng78f51f42017-02-02 13:54:58 -08001300
1301 //ensure assignedVlan applies to the chosen group
1302 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1303
1304 if (!floodGroupVlan.equals(assignedVlan)) {
1305 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1306 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1307 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001308 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001309 return;
1310 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001311 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001312 groupInfos.forEach(groupInfo -> {
1313 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001314 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001315 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001316 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001317 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001318
Yi Tsengef19de12017-04-24 11:33:05 -07001319 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001320 deviceId, Integer.toHexString(l2floodGroupId),
1321 l2floodGroupKey, nextObj.id());
1322 // send the innermost group
1323 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001324 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001325 deviceId);
1326
Yi Tsengef19de12017-04-24 11:33:05 -07001327 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001328
Yi Tsengef19de12017-04-24 11:33:05 -07001329 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1330 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001331 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1332
1333 if (existsL2IGroup != null) {
1334 // group already exist
1335 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1336 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001337 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001338 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001339 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001340
1341 updatePendingNextObjective(l2floodGroupKey,
1342 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001343 }
1344
Saurav Das1a129a02016-11-18 15:21:57 -08001345 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1346 List<Deque<GroupKey>> allActiveKeys,
1347 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001348 VlanId assignedVlan) {
Pier Luigi21fffd22018-01-19 10:24:53 +01001349 // Create the buckets to add to the outermost L3 Multicast group
1350 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001351
1352 // get the group being edited
1353 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1354 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001355 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001356 return;
1357 }
1358 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1359 int l3mcastGroupId = l3mcastGroup.id().id();
1360
1361 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001362 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001363 if (!expectedVlan.equals(assignedVlan)) {
1364 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1365 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1366 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001367 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001368 }
1369 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001370 new DefaultGroupDescription(deviceId,
1371 ALL,
1372 new GroupBuckets(newBuckets),
1373 l3mcastGroupKey,
1374 l3mcastGroupId,
1375 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001376 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001377 groupInfos.size(),
1378 true,
1379 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001380
1381 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001382 groupInfos.forEach(groupInfo -> {
1383 // update original NextGroup with new bucket-chain
1384 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001385 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001386 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001387 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1388 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001389 }
1390 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001391 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001392
Yi Tsengef19de12017-04-24 11:33:05 -07001393 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001394 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001395 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1396 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1397 1,
1398 false,
1399 deviceId);
1400 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001401 }
Yi Tsengef19de12017-04-24 11:33:05 -07001402 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001403 deviceId, Integer.toHexString(l3mcastGroupId),
1404 l3mcastGroupKey, nextObj.id());
1405 // send the innermost group
1406 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001407 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001408 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001409 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001410
1411 });
1412
Saurav Das1a129a02016-11-18 15:21:57 -08001413 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001414 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001415 }
1416
1417 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001418 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001419 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001420 * may be in use (referenced by other groups) elsewhere.
1421 *
Saurav Dasceccf242017-08-03 18:30:35 -07001422 * @param nextObjective a next objective that contains information for the
1423 * buckets to be removed from the group
1424 * @param next the representation of the existing group-chains for this next
1425 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001426 */
1427 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001428 if (nextObjective.type() != NextObjective.Type.HASHED &&
1429 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001430 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1431 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001432 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001433 return;
1434 }
Yi Tsengef19de12017-04-24 11:33:05 -07001435 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001436 List<Integer> indicesToRemove = Lists.newArrayList();
1437 for (TrafficTreatment treatment : nextObjective.next()) {
1438 // find the top-level bucket in the group-chain by matching the
1439 // outport and label from different groups in the chain
1440 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1441 int labelToRemove = readLabelFromTreatment(treatment);
1442 if (portToRemove == null) {
1443 log.warn("treatment {} of next objective {} has no outport.. "
1444 + "cannot remove bucket from group in dev: {}", treatment,
1445 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001446 continue;
1447 }
Saurav Dasceccf242017-08-03 18:30:35 -07001448 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1449 groupService, deviceId,
1450 portToRemove, labelToRemove);
1451 indicesToRemove.addAll(existing);
1452
Charles Chan188ebf52015-12-23 00:15:11 -08001453 }
Saurav Dasceccf242017-08-03 18:30:35 -07001454
1455 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1456 indicesToRemove.forEach(index -> chainsToRemove
1457 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001458 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001459 log.warn("Could not find appropriate group-chain for removing bucket"
1460 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001461 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001462 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001463 }
Saurav Dasceccf242017-08-03 18:30:35 -07001464 removeBucket(chainsToRemove, nextObjective);
1465 }
1466
1467 /**
1468 * Removes top-level buckets from a group that represents the given next objective.
1469 *
1470 * @param chainsToRemove a list of group bucket chains to remove
1471 * @param nextObjective the next objective that contains information for the
1472 * buckets to be removed from the group
1473 */
1474 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1475 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001476 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1477 //first group key is the one we want to modify
1478 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001479 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001480 for (Deque<GroupKey> foundChain : chainsToRemove) {
1481 //second group key is the one we wish to remove the reference to
1482 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001483 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001484 // the chain.
1485 log.warn("Can't find second group key from chain {}",
1486 foundChain);
1487 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001488 }
Saurav Dasceccf242017-08-03 18:30:35 -07001489 GroupKey pointedGroupKey = foundChain.stream()
1490 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001491 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001492 if (pointedGroup == null) {
1493 continue;
1494 }
1495
1496 GroupBucket bucket;
1497 if (nextObjective.type() == NextObjective.Type.HASHED) {
1498 bucket = DefaultGroupBucket.createSelectGroupBucket(
1499 DefaultTrafficTreatment.builder()
1500 .group(pointedGroup.id())
1501 .build());
1502 } else {
1503 bucket = DefaultGroupBucket.createAllGroupBucket(
1504 DefaultTrafficTreatment.builder()
1505 .group(pointedGroup.id())
1506 .build());
1507 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001508 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001509 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001510
1511 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1512 List<String> pointedGroupIds; // for debug log
1513 pointedGroupIds = bucketsToRemove.stream()
1514 .map(GroupBucket::treatment)
1515 .map(TrafficTreatment::allInstructions)
1516 .flatMap(List::stream)
1517 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1518 .map(inst -> (Instructions.GroupInstruction) inst)
1519 .map(Instructions.GroupInstruction::groupId)
1520 .map(GroupId::id)
1521 .map(Integer::toHexString)
1522 .map(id -> HEX_PREFIX + id)
1523 .collect(Collectors.toList());
1524
Yi Tseng78f51f42017-02-02 13:54:58 -08001525 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001526 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001527 pointedGroupIds, nextObjective.id(), deviceId);
1528 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001529 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1530 removeBuckets, modGroupKey,
1531 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001532 // update store - synchronize access as there may be multiple threads
1533 // trying to remove buckets from the same group, each with its own
1534 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001535 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001536 // get a fresh copy of what the store holds
1537 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1538 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001539 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001540 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001541 // Object.equals(), we have to check the deque elems one by one
1542 allActiveKeys
1543 .removeIf(active ->
1544 chainsToRemove.stream().anyMatch(remove ->
1545 Arrays.equals(remove.toArray(new GroupKey[0]),
1546 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001547 // If no buckets in the group, then retain an entry for the
1548 // top level group which still exists.
1549 if (allActiveKeys.isEmpty()) {
1550 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1551 top.add(modGroupKey);
1552 allActiveKeys.add(top);
1553 }
1554 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001555 new OfdpaNextGroup(allActiveKeys,
1556 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001557 }
Charles Chan188ebf52015-12-23 00:15:11 -08001558 }
1559
1560 /**
Saurav Das961beb22017-03-29 19:09:17 -07001561 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001562 *
1563 * @param nextObjective the next objective to remove
1564 * @param next the NextGroup that represents the existing group-chain for
1565 * this next objective
1566 */
1567 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001568
Yi Tsengef19de12017-04-24 11:33:05 -07001569 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001570
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001571 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001572 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001573 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001574
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001575 allActiveKeys
1576 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1577 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001578 flowObjectiveStore.removeNextGroup(nextObjective.id());
1579 }
1580
Saurav Dasceccf242017-08-03 18:30:35 -07001581 /**
1582 * Checks existing buckets in {@link NextGroup} to verify if they match
1583 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1584 * to ensure that the buckets match up.
1585 *
1586 * @param nextObjective the next objective to verify
1587 * @param next the representation of the existing group which has to be
1588 * modified to match the given next objective
1589 */
1590 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
1591 if (nextObjective.type() != NextObjective.Type.HASHED) {
1592 log.warn("verification not supported for {} group", nextObjective.type());
1593 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1594 return;
1595 }
1596 log.debug("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
1597 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1598 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1599 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigiec6ac422018-01-29 10:30:59 +01001600
1601 // Iterating over the treatments of the next objective allows
1602 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07001603 for (TrafficTreatment bkt : nextObjective.next()) {
1604 PortNumber portNumber = readOutPortFromTreatment(bkt);
1605 int label = readLabelFromTreatment(bkt);
1606 if (portNumber == null) {
1607 log.warn("treatment {} of next objective {} has no outport.. "
1608 + "cannot remove bucket from group in dev: {}", bkt,
1609 nextObjective.id(), deviceId);
1610 fail(nextObjective, ObjectiveError.BADPARAMS);
1611 return;
1612 }
1613 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1614 groupService, deviceId,
1615 portNumber, label);
1616 if (existing.isEmpty()) {
1617 // if it doesn't exist, mark this bucket for creation
1618 bucketsToCreate.add(bkt);
1619 }
1620 if (existing.size() > 1) {
1621 // if it exists but there are duplicates, mark the others for removal
1622 existing.remove(0);
1623 indicesToRemove.addAll(existing);
1624 }
1625 }
1626
Pier Luigiec6ac422018-01-29 10:30:59 +01001627 // Detect situation where the next data has more buckets
1628 // (not duplicates) respect to the next objective
1629 if (allActiveKeys.size() > nextObjective.next().size()) {
1630 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
1631 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
1632 deviceId, nextObjective.id(), nextObjective.next().size(), allActiveKeys.size());
1633 List<Integer> otherIndices = indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
1634 groupService, deviceId);
1635 // Filter out the indices not present
1636 otherIndices = otherIndices.stream()
1637 .filter(index -> !indicesToRemove.contains(index))
1638 .collect(Collectors.toList());
1639 // Add all to the final list
1640 indicesToRemove.addAll(otherIndices);
1641 }
1642
Saurav Dasceccf242017-08-03 18:30:35 -07001643 if (!bucketsToCreate.isEmpty()) {
1644 log.info("creating {} buckets as part of nextId: {} verification",
1645 bucketsToCreate.size(), nextObjective.id());
1646 //create a nextObjective only with these buckets
1647 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1648 .withId(nextObjective.id())
1649 .withType(NextObjective.Type.HASHED)
1650 .withMeta(nextObjective.meta())
1651 .fromApp(nextObjective.appId());
1652 bucketsToCreate.forEach(bucket -> nextObjBuilder.addTreatment(bucket));
1653 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1654 }
1655
1656 if (!indicesToRemove.isEmpty()) {
1657 log.info("removing {} buckets as part of nextId: {} verification",
1658 indicesToRemove.size(), nextObjective.id());
1659 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1660 indicesToRemove.forEach(index -> chainsToRemove
1661 .add(allActiveKeys.get(index)));
1662 removeBucket(chainsToRemove, nextObjective);
1663 }
1664
Saurav Das9df5b7c2017-08-14 16:44:43 -07001665 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1666 // flowObjective store record is in-sync with nextObjective passed-in
1667 // Nevertheless groupStore may not be in sync due to bug in the store
1668 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1669 // method will not be required.
1670 GroupKey hashGroupKey = allActiveKeys.get(0).peekFirst();
1671 Group hashGroup = groupService.getGroup(deviceId, hashGroupKey);
1672 int actualGroupSize = hashGroup.buckets().buckets().size();
1673 int objGroupSize = nextObjective.next().size();
1674 if (actualGroupSize != objGroupSize) {
1675 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1676 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1677 objGroupSize, actualGroupSize);
1678 }
1679 if (actualGroupSize > objGroupSize) {
1680 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1681 //check every bucket in the actual group
1682 for (GroupBucket bucket : hashGroup.buckets().buckets()) {
1683 GroupInstruction g = (GroupInstruction) bucket.treatment()
1684 .allInstructions().iterator().next();
1685 GroupId gidToCheck = g.groupId(); // the group pointed to
1686 boolean matches = false;
1687 for (Deque<GroupKey> validChain : allActiveKeys) {
1688 if (validChain.size() < 2) {
1689 continue;
1690 }
1691 GroupKey pointedGroupKey = validChain.stream()
1692 .collect(Collectors.toList()).get(1);
1693 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1694 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1695 matches = true;
1696 break;
1697 }
1698 }
1699 if (!matches) {
1700 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1701 bucketsToRemove.add(bucket);
1702 }
1703 }
1704 // remove buckets for which there was no record in the obj store
1705 if (bucketsToRemove.isEmpty()) {
1706 log.warn("Mismatch detected but could not determine which"
1707 + "buckets to remove");
1708 } else {
1709 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1710 groupService.removeBucketsFromGroup(deviceId, hashGroupKey,
1711 removeBuckets, hashGroupKey,
1712 nextObjective.appId());
1713 }
1714 } else if (actualGroupSize < objGroupSize) {
1715 // should also add buckets not in group-store but in obj-store
1716 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1717 //check every bucket in the obj
1718 for (Deque<GroupKey> validChain : allActiveKeys) {
1719 if (validChain.size() < 2) {
1720 continue;
1721 }
1722 GroupKey pointedGroupKey = validChain.stream()
1723 .collect(Collectors.toList()).get(1);
1724 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1725 if (pointedGroup == null) {
1726 // group should exist, otherwise cannot be added as bucket
1727 continue;
1728 }
1729 boolean matches = false;
1730 for (GroupBucket bucket : hashGroup.buckets().buckets()) {
1731 GroupInstruction g = (GroupInstruction) bucket.treatment()
1732 .allInstructions().iterator().next();
1733 GroupId gidToCheck = g.groupId(); // the group pointed to
1734 if (pointedGroup.id().equals(gidToCheck)) {
1735 matches = true;
1736 break;
1737 }
1738 }
1739 if (!matches) {
1740 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1741 TrafficTreatment t = DefaultTrafficTreatment.builder()
1742 .group(pointedGroup.id())
1743 .build();
1744 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1745 }
1746 }
1747 if (bucketsToAdd.isEmpty()) {
1748 log.warn("Mismatch detected but could not determine which "
1749 + "buckets to add");
1750 } else {
1751 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
1752 groupService.addBucketsToGroup(deviceId, hashGroupKey,
1753 addBuckets, hashGroupKey,
1754 nextObjective.appId());
1755 }
1756 }
1757 }
1758
Saurav Dasceccf242017-08-03 18:30:35 -07001759 pass(nextObjective);
1760 }
1761
1762 //////////////////////////////////////
1763 // Helper methods and classes
1764 //////////////////////////////////////
1765
Yi Tsengef19de12017-04-24 11:33:05 -07001766 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1767 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001768 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001769 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001770 }
Yi Tsengef19de12017-04-24 11:33:05 -07001771 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001772 return val;
1773 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001774 }
1775
Yi Tsengef19de12017-04-24 11:33:05 -07001776 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1777 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001778 if (val == null) {
1779 val = Sets.newConcurrentHashSet();
1780 }
1781 val.add(gce);
1782 return val;
1783 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001784 }
1785
Yi Tsengef19de12017-04-24 11:33:05 -07001786 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1787 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001788 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1789 if (nextObjs != null) {
1790 nextObjs.add(nextObjective);
1791 } else {
1792 nextObjs = Sets.newHashSet(nextObjective);
1793 }
1794 return nextObjs;
1795 });
1796 }
1797
Yi Tsengef19de12017-04-24 11:33:05 -07001798 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1799 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1800 if (nextObjs != null) {
1801
1802 nextObjs.forEach(nextObj -> {
1803 log.debug("Group {} updated, update pending next objective {}.",
1804 groupKey, nextObj);
1805
1806 pass(nextObj);
1807 });
1808 }
1809 return Sets.newHashSet();
1810 });
1811 }
1812
1813 private void processPendingRemoveNextObjs(GroupKey key) {
1814 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1815 if (groupKeys.isEmpty()) {
1816 pendingRemoveNextObjectives.invalidate(nextObjective);
1817 pass(nextObjective);
1818 } else {
1819 groupKeys.remove(key);
1820 }
1821 });
1822 }
1823
1824 protected int getNextAvailableIndex() {
1825 return (int) nextIndex.incrementAndGet();
1826 }
1827
1828 protected Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1829 int nextid) {
1830 GroupKey topLevelGroupKey;
1831 if (!allActiveKeys.isEmpty()) {
1832 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1833 } else {
1834 log.warn("Could not determine top level group while processing"
1835 + "next:{} in dev:{}", nextid, deviceId);
1836 return null;
1837 }
1838 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1839 if (topGroup == null) {
1840 log.warn("Could not find top level group while processing "
1841 + "next:{} in dev:{}", nextid, deviceId);
1842 }
1843 return topGroup;
1844 }
1845
1846 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1847 //first check for group chain
1848 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1849 if (gceSet != null) {
1850 for (GroupChainElem gce : gceSet) {
1851 log.debug("Group service {} group key {} in device {}. "
1852 + "Processing next group in group chain with group id 0x{}",
1853 (added) ? "ADDED" : "processed",
1854 key, deviceId,
1855 Integer.toHexString(gce.groupDescription().givenGroupId()));
1856 processGroupChain(gce);
1857 }
1858 } else {
1859 // otherwise chain complete - check for waiting nextObjectives
1860 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1861 pendingAddNextObjectives.getIfPresent(key);
1862 if (nextGrpList != null) {
1863 pendingAddNextObjectives.invalidate(key);
1864 nextGrpList.forEach(nextGrp -> {
1865 log.debug("Group service {} group key {} in device:{}. "
1866 + "Done implementing next objective: {} <<-->> gid:0x{}",
1867 (added) ? "ADDED" : "processed",
1868 key, deviceId, nextGrp.nextObjective().id(),
1869 Integer.toHexString(groupService.getGroup(deviceId, key)
1870 .givenGroupId()));
1871 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001872 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001873
1874 // check if addBuckets waiting for this completion
1875 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1876 if (pendBkts != null) {
1877 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1878 }
1879 return null;
1880 });
1881 });
1882 }
1883 }
1884 }
1885
Charles Chan188ebf52015-12-23 00:15:11 -08001886 /**
1887 * Processes next element of a group chain. Assumption is that if this
1888 * group points to another group, the latter has already been created
1889 * and this driver has received notification for it. A second assumption is
1890 * that if there is another group waiting for this group then the appropriate
1891 * stores already have the information to act upon the notification for the
1892 * creation of this group.
1893 * <p>
1894 * The processing of the GroupChainElement depends on the number of groups
1895 * this element is waiting on. For all group types other than SIMPLE, a
1896 * GroupChainElement could be waiting on multiple groups.
1897 *
1898 * @param gce the group chain element to be processed next
1899 */
1900 private void processGroupChain(GroupChainElem gce) {
1901 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1902 if (waitOnGroups != 0) {
1903 log.debug("GCE: {} not ready to be processed", gce);
1904 return;
1905 }
1906 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001907 if (gce.addBucketToGroup()) {
1908 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1909 gce.groupDescription().appCookie(),
1910 gce.groupDescription().buckets(),
1911 gce.groupDescription().appCookie(),
1912 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001913 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001914 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001915 }
1916 }
1917
Yi Tsengef19de12017-04-24 11:33:05 -07001918 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1919 List<GroupKey> groupKeys) {
1920 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001921 }
1922
Saurav Dasc88d4662017-05-15 15:34:25 -07001923 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1924 synchronized (flowObjectiveStore) {
1925 // get fresh copy of what the store holds
1926 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1927 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1928 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1929 return;
1930 }
1931 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1932 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001933 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001934 // If active keys shows only the top-level group without a chain of groups,
1935 // then it represents an empty group. Update by replacing empty chain.
1936 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1937 allActiveKeys.clear();
1938 }
1939 allActiveKeys.addAll(nextGrp.allKeys());
1940 flowObjectiveStore.putNextGroup(nextId,
1941 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1942 }
1943 }
1944 }
1945
Saurav Das8be4e3a2016-03-11 17:19:07 -08001946 private class InnerGroupListener implements GroupListener {
1947 @Override
1948 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001949 switch (event.type()) {
1950 case GROUP_ADDED:
1951 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1952 break;
1953 case GROUP_REMOVED:
1954 processPendingRemoveNextObjs(event.subject().appCookie());
1955 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001956 case GROUP_UPDATED:
1957 processPendingUpdateNextObjs(event.subject().appCookie());
1958 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001959 default:
1960 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001961 }
1962 }
1963 }
Charles Chan188ebf52015-12-23 00:15:11 -08001964}