blob: fdee8d6dea2f620f8afe0e4252e9144a56877082 [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 Pantelopoulos671d7462018-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 Yashchuka3679532017-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 Pantelopoulos671d7462018-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 Pantelopoulos671d7462018-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 Pantelopoulos671d7462018-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 Pantelopoulos671d7462018-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 Pantelopoulos671d7462018-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 Pantelopoulosb524fff2018-02-08 12:42:06 -0800303 if (mplsSwap && !isPw) {
Andreas Pantelopoulos671d7462018-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 Pantelopoulos671d7462018-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 Pantelopoulos671d7462018-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 Luigi7b7a29d2018-01-19 10:24:53 +0100807 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
808 List<GroupInfo> groupInfos) {
809 // Let's create a new list mcast buckets
810 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700811
812 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700813 int l3MulticastGroupId = L3_MULTICAST_TYPE |
814 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
815 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700816 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700817
818 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800819 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700820 new GroupBuckets(l3McastBuckets),
821 l3MulticastGroupKey,
822 l3MulticastGroupId,
823 nextObj.appId());
824
825 // Put all dependency information into allGroupKeys
826 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
827 groupInfos.forEach(groupInfo -> {
828 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700829 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700830 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700831 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
832 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700833 }
834 gkeyChain.addFirst(l3MulticastGroupKey);
835 allGroupKeys.add(gkeyChain);
836 });
837
838 // Point the next objective to this group
839 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
840 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
841
842 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700843 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700844 groupInfos.forEach(groupInfo -> {
845 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700846 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700847
848 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700849 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
850 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
851 1, false, deviceId);
852 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700853 }
854
855 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700856 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700857 });
Charles Chan188ebf52015-12-23 00:15:11 -0800858 }
859
Charles Chan188ebf52015-12-23 00:15:11 -0800860 /**
861 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
862 * a chain of groups. The hashed Next Objective passed in by the application
863 * has to be broken up into a group chain comprising of an
864 * L3 ECMP group as the top level group. Buckets of this group can point
865 * to a variety of groups in a group chain, depending on the whether
866 * MPLS labels are being pushed or not.
867 * <p>
868 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
869 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
870 * check the nextObjective meta to see what is matching before being
871 * sent to this nextObjective.
872 *
873 * @param nextObj the nextObjective of type HASHED
874 */
Pier Ventre140a8942016-11-02 07:26:38 -0700875 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800876 // storage for all group keys in the chain of groups created
877 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
878 List<GroupInfo> unsentGroups = new ArrayList<>();
879 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
880
881 // now we can create the outermost L3 ECMP group
882 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
883 for (GroupInfo gi : unsentGroups) {
884 // create ECMP bucket to point to the outer group
885 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700886 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800887 GroupBucket sbucket = DefaultGroupBucket
888 .createSelectGroupBucket(ttb.build());
889 l3ecmpGroupBuckets.add(sbucket);
890 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800891 int l3ecmpIndex = getNextAvailableIndex();
892 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
893 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700894 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800895 GroupDescription l3ecmpGroupDesc =
896 new DefaultGroupDescription(
897 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800898 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800899 new GroupBuckets(l3ecmpGroupBuckets),
900 l3ecmpGroupKey,
901 l3ecmpGroupId,
902 nextObj.appId());
903 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
904 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700905 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800906
907 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700908 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800909 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
910
911 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
912 // that depends on it
913 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
914
915 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
916 deviceId, Integer.toHexString(l3ecmpGroupId),
917 l3ecmpGroupKey, nextObj.id());
918 // finally we are ready to send the innermost groups
919 for (GroupInfo gi : unsentGroups) {
920 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700921 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
922 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
923 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800924 }
Charles Chan188ebf52015-12-23 00:15:11 -0800925 }
926
927 /**
928 * Creates group chains for all buckets in a hashed group, and stores the
929 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
930 * should be empty.
931 * <p>
932 * Does not create the top level ECMP group. Does not actually send the
933 * groups to the groupService.
934 *
935 * @param nextObj the Next Objective with buckets that need to be converted
936 * to group chains
937 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
938 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
939 */
Pier Ventre140a8942016-11-02 07:26:38 -0700940 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700941 List<Deque<GroupKey>> allGroupKeys,
942 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800943 // break up hashed next objective to multiple groups
944 Collection<TrafficTreatment> buckets = nextObj.next();
945
946 for (TrafficTreatment bucket : buckets) {
947 //figure out how many labels are pushed in each bucket
948 int labelsPushed = 0;
949 MplsLabel innermostLabel = null;
950 for (Instruction ins : bucket.allInstructions()) {
951 if (ins.type() == Instruction.Type.L2MODIFICATION) {
952 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
953 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
954 labelsPushed++;
955 }
956 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
957 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700958 innermostLabel =
959 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800960 }
961 }
962 }
963 }
964
Yi Tsengef19de12017-04-24 11:33:05 -0700965 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800966 // XXX we only deal with 0 and 1 label push right now
967 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700968 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700969 TrafficSelector metaSelector = nextObj.meta();
970 if (metaSelector != null) {
971 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700972 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
973 nextObj.appId(), true,
974 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700975 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700976 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
977 nextObj.appId(), false,
978 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700979 }
980 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700981 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
982 nextObj.appId(), false,
983 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700984 }
Yi Tsengef19de12017-04-24 11:33:05 -0700985 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800986 log.error("Could not process nextObj={} in dev:{}",
987 nextObj.id(), deviceId);
988 return;
989 }
Yi Tsengef19de12017-04-24 11:33:05 -0700990 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
991 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800992
993 // we can't send the inner group description yet, as we have to
994 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -0700995 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -0800996
997 } else if (labelsPushed == 1) {
998 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
999 nextObj.appId(), true,
1000 nextObj.meta());
1001 if (onelabelGroupInfo == null) {
1002 log.error("Could not process nextObj={} in dev:{}",
1003 nextObj.id(), deviceId);
1004 return;
1005 }
1006 // we need to add another group to this chain - the L3VPN group
1007 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001008 if (requireVlanPopBeforeMplsPush()) {
1009 l3vpnTtb.popVlan();
1010 }
Charles Chan188ebf52015-12-23 00:15:11 -08001011 l3vpnTtb.pushMpls()
1012 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001013 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001014 if (supportCopyTtl()) {
1015 l3vpnTtb.copyTtlOut();
1016 }
1017 if (supportSetMplsBos()) {
1018 l3vpnTtb.setMplsBos(true);
1019 }
Charles Chan0f43e472017-02-14 14:00:16 -08001020 if (requireVlanPopBeforeMplsPush()) {
1021 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1022 }
Charles Chan40132b32017-01-22 00:19:37 -08001023
Charles Chan188ebf52015-12-23 00:15:11 -08001024 GroupBucket l3vpnGrpBkt =
1025 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001026 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001027 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1028 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1029 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001030 GroupDescription l3vpnGroupDesc =
1031 new DefaultGroupDescription(
1032 deviceId,
1033 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001034 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1035 l3vpnGroupKey,
1036 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001037 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001038 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1039 1,
1040 false,
1041 deviceId);
1042 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001043
Yi Tsengef19de12017-04-24 11:33:05 -07001044 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1045 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1046 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001047
1048 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001049 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001050
1051 // we can't send the innermost group yet, as we have to create
1052 // the dependent ECMP group first. So we store ...
1053 unsentGroups.add(onelabelGroupInfo);
1054
Yi Tsengef19de12017-04-24 11:33:05 -07001055 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1056 deviceId, Integer.toHexString(l3vpnGroupId),
1057 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001058
1059 } else {
1060 log.warn("Driver currently does not handle more than 1 MPLS "
1061 + "labels. Not processing nextObjective {}", nextObj.id());
1062 return;
1063 }
1064
1065 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001066 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001067 }
1068 }
1069
Pier Ventre42287df2016-11-09 14:17:26 -08001070 /**
1071 * Processes the pseudo wire related next objective.
1072 * This procedure try to reuse the mpls label groups,
1073 * the mpls interface group and the l2 interface group.
1074 *
1075 * @param nextObjective the objective to process.
1076 */
1077 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001078 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1079 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001080 }
1081
Saurav Das8be4e3a2016-03-11 17:19:07 -08001082 //////////////////////////////////////
1083 // Group Editing
1084 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001085 /**
1086 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001087 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001088 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001089 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001090 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001091 * @param next the representation of the existing group-chain for this next objective
1092 */
1093 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001094 if (nextObjective.type() != NextObjective.Type.HASHED &&
1095 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001096 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001097 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001098 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001099 return;
1100 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001101
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001102 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001103 // existing bucket. If it is for an existing output port, then its a
1104 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001105 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001106 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1107 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1108 groupService,
1109 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001110 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1111 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001112
1113 nextObjective.next().forEach(trafficTreatment -> {
1114 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001115 if (portNumber == null) {
1116 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001117 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001118 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001119 // its possible that portnumbers are same but labels are different
1120 int label = readLabelFromTreatment(trafficTreatment);
1121 if (label == -1) {
1122 duplicateBuckets.add(trafficTreatment);
1123 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001124 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1125 groupService, deviceId,
1126 portNumber, label);
1127 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001128 duplicateBuckets.add(trafficTreatment);
1129 } else {
1130 nonDuplicateBuckets.add(trafficTreatment);
1131 }
1132 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001133 } else {
1134 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001135 }
1136 });
1137
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001138 if (duplicateBuckets.isEmpty()) {
1139 // use the original objective
1140 objectiveToAdd = nextObjective;
1141 } else if (!nonDuplicateBuckets.isEmpty()) {
1142 // only use the non-duplicate buckets if there are any
1143 log.debug("Some buckets {} already exist in next id {}, duplicate "
1144 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1145 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001146 NextObjective.Builder builder = DefaultNextObjective.builder()
1147 .withType(nextObjective.type())
1148 .withId(nextObjective.id())
1149 .withMeta(nextObjective.meta())
1150 .fromApp(nextObjective.appId());
1151 nonDuplicateBuckets.forEach(builder::addTreatment);
1152
1153 ObjectiveContext context = nextObjective.context().orElse(null);
1154 objectiveToAdd = builder.addToExisting(context);
1155 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001156 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001157 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1158 duplicateBuckets, nextObjective.id());
1159 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001160 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001161 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001162
Saurav Das1a129a02016-11-18 15:21:57 -08001163 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001164 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001165 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001166 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001167 }
1168 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001169
Saurav Das1a129a02016-11-18 15:21:57 -08001170 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001171 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001172 // storage for all group keys in the chain of groups created
1173 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1174 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001175 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001176 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1177
Yi Tseng78f51f42017-02-02 13:54:58 -08001178 // now we can create the buckets to add to the outermost L3 ECMP group
1179 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001180
Saurav Das1a129a02016-11-18 15:21:57 -08001181 // retrieve the original L3 ECMP group
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001182 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1183 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001184 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001185 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001186 return;
1187 }
Saurav Das1a129a02016-11-18 15:21:57 -08001188 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001189 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001190
1191 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001192 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001193 // processed, the info will be extracted for the bucketAdd call to groupService
1194 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001195 new DefaultGroupDescription(deviceId,
1196 SELECT,
1197 new GroupBuckets(newBuckets),
1198 l3ecmpGroupKey,
1199 l3ecmpGroupId,
1200 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001201 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1202 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001203 true,
1204 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001205
Saurav Dasc88d4662017-05-15 15:34:25 -07001206 // update new bucket-chains
1207 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1208 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1209 newBucketChain.addFirst(l3ecmpGroupKey);
1210 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001211 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001212 updatePendingNextObjective(l3ecmpGroupKey,
1213 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001214 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001215 deviceId, Integer.toHexString(l3ecmpGroupId),
1216 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001217
1218 unsentGroups.forEach(groupInfo -> {
1219 // send the innermost group
1220 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001221 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1222 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001223 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1224 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001225 });
Saurav Das1a129a02016-11-18 15:21:57 -08001226 }
Charles Chan188ebf52015-12-23 00:15:11 -08001227
Saurav Das1a129a02016-11-18 15:21:57 -08001228 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001229 List<Deque<GroupKey>> allActiveKeys) {
1230 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001231 if (assignedVlan == null) {
1232 log.warn("VLAN ID required by broadcast next obj is missing. "
1233 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1234 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001235 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001236 return;
1237 }
Saurav Das1a129a02016-11-18 15:21:57 -08001238 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001239 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001240 if (ipDst != null) {
1241 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001242 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001243 } else {
1244 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001245 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001246 }
1247 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001248 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001249 }
1250 }
1251
1252 private void addBucketToL2FloodGroup(NextObjective nextObj,
1253 List<Deque<GroupKey>> allActiveKeys,
1254 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001255 VlanId assignedVlan) {
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001256 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1257 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001258
1259 if (l2FloodGroup == null) {
1260 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1261 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001262 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001263 return;
1264 }
Saurav Das1a129a02016-11-18 15:21:57 -08001265
Yi Tseng78f51f42017-02-02 13:54:58 -08001266 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1267 int l2floodGroupId = l2FloodGroup.id().id();
1268 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1269
1270 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001271 new DefaultGroupDescription(deviceId,
1272 ALL,
1273 new GroupBuckets(newBuckets),
1274 l2floodGroupKey,
1275 l2floodGroupId,
1276 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001277
Yi Tseng78f51f42017-02-02 13:54:58 -08001278 GroupChainElem l2FloodGroupChainElement =
1279 new GroupChainElem(l2FloodGroupDescription,
1280 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001281 true,
1282 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001283
Yi Tseng78f51f42017-02-02 13:54:58 -08001284
1285 //ensure assignedVlan applies to the chosen group
1286 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1287
1288 if (!floodGroupVlan.equals(assignedVlan)) {
1289 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1290 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1291 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001292 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001293 return;
1294 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001295 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001296 groupInfos.forEach(groupInfo -> {
1297 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001298 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001299 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001300 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001301 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001302
Yi Tsengef19de12017-04-24 11:33:05 -07001303 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001304 deviceId, Integer.toHexString(l2floodGroupId),
1305 l2floodGroupKey, nextObj.id());
1306 // send the innermost group
1307 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001308 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001309 deviceId);
1310
Yi Tsengef19de12017-04-24 11:33:05 -07001311 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001312
Yi Tsengef19de12017-04-24 11:33:05 -07001313 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1314 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001315 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1316
1317 if (existsL2IGroup != null) {
1318 // group already exist
1319 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1320 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001321 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001322 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001323 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001324
1325 updatePendingNextObjective(l2floodGroupKey,
1326 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001327 }
1328
Saurav Das1a129a02016-11-18 15:21:57 -08001329 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1330 List<Deque<GroupKey>> allActiveKeys,
1331 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001332 VlanId assignedVlan) {
Pier Luigi7b7a29d2018-01-19 10:24:53 +01001333 // Create the buckets to add to the outermost L3 Multicast group
1334 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001335
1336 // get the group being edited
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001337 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1338 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001339 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001340 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001341 return;
1342 }
1343 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1344 int l3mcastGroupId = l3mcastGroup.id().id();
1345
1346 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001347 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001348 if (!expectedVlan.equals(assignedVlan)) {
1349 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1350 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1351 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001352 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001353 }
1354 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001355 new DefaultGroupDescription(deviceId,
1356 ALL,
1357 new GroupBuckets(newBuckets),
1358 l3mcastGroupKey,
1359 l3mcastGroupId,
1360 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001361 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001362 groupInfos.size(),
1363 true,
1364 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001365
1366 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001367 groupInfos.forEach(groupInfo -> {
1368 // update original NextGroup with new bucket-chain
1369 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001370 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001371 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001372 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1373 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001374 }
1375 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001376 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001377
Yi Tsengef19de12017-04-24 11:33:05 -07001378 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001379 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001380 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1381 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1382 1,
1383 false,
1384 deviceId);
1385 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001386 }
Yi Tsengef19de12017-04-24 11:33:05 -07001387 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001388 deviceId, Integer.toHexString(l3mcastGroupId),
1389 l3mcastGroupKey, nextObj.id());
1390 // send the innermost group
1391 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001392 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001393 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001394 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001395
1396 });
1397
Saurav Das1a129a02016-11-18 15:21:57 -08001398 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001399 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001400 }
1401
1402 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001403 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001404 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001405 * may be in use (referenced by other groups) elsewhere.
1406 *
Saurav Dasceccf242017-08-03 18:30:35 -07001407 * @param nextObjective a next objective that contains information for the
1408 * buckets to be removed from the group
1409 * @param next the representation of the existing group-chains for this next
1410 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001411 */
1412 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001413 if (nextObjective.type() != NextObjective.Type.HASHED &&
1414 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001415 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1416 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001417 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001418 return;
1419 }
Yi Tsengef19de12017-04-24 11:33:05 -07001420 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001421 List<Integer> indicesToRemove = Lists.newArrayList();
1422 for (TrafficTreatment treatment : nextObjective.next()) {
1423 // find the top-level bucket in the group-chain by matching the
1424 // outport and label from different groups in the chain
1425 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1426 int labelToRemove = readLabelFromTreatment(treatment);
1427 if (portToRemove == null) {
1428 log.warn("treatment {} of next objective {} has no outport.. "
1429 + "cannot remove bucket from group in dev: {}", treatment,
1430 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001431 continue;
1432 }
Saurav Dasceccf242017-08-03 18:30:35 -07001433 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1434 groupService, deviceId,
1435 portToRemove, labelToRemove);
1436 indicesToRemove.addAll(existing);
1437
Charles Chan188ebf52015-12-23 00:15:11 -08001438 }
Saurav Dasceccf242017-08-03 18:30:35 -07001439
1440 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1441 indicesToRemove.forEach(index -> chainsToRemove
1442 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001443 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001444 log.warn("Could not find appropriate group-chain for removing bucket"
1445 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001446 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001447 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001448 }
Saurav Dasceccf242017-08-03 18:30:35 -07001449 removeBucket(chainsToRemove, nextObjective);
1450 }
1451
1452 /**
1453 * Removes top-level buckets from a group that represents the given next objective.
1454 *
1455 * @param chainsToRemove a list of group bucket chains to remove
1456 * @param nextObjective the next objective that contains information for the
1457 * buckets to be removed from the group
1458 */
1459 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1460 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001461 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1462 //first group key is the one we want to modify
1463 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001464 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001465 for (Deque<GroupKey> foundChain : chainsToRemove) {
1466 //second group key is the one we wish to remove the reference to
1467 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001468 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001469 // the chain.
1470 log.warn("Can't find second group key from chain {}",
1471 foundChain);
1472 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001473 }
Saurav Dasceccf242017-08-03 18:30:35 -07001474 GroupKey pointedGroupKey = foundChain.stream()
1475 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001476 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001477 if (pointedGroup == null) {
1478 continue;
1479 }
1480
1481 GroupBucket bucket;
1482 if (nextObjective.type() == NextObjective.Type.HASHED) {
1483 bucket = DefaultGroupBucket.createSelectGroupBucket(
1484 DefaultTrafficTreatment.builder()
1485 .group(pointedGroup.id())
1486 .build());
1487 } else {
1488 bucket = DefaultGroupBucket.createAllGroupBucket(
1489 DefaultTrafficTreatment.builder()
1490 .group(pointedGroup.id())
1491 .build());
1492 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001493 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001494 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001495
1496 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1497 List<String> pointedGroupIds; // for debug log
1498 pointedGroupIds = bucketsToRemove.stream()
1499 .map(GroupBucket::treatment)
1500 .map(TrafficTreatment::allInstructions)
1501 .flatMap(List::stream)
1502 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1503 .map(inst -> (Instructions.GroupInstruction) inst)
1504 .map(Instructions.GroupInstruction::groupId)
1505 .map(GroupId::id)
1506 .map(Integer::toHexString)
1507 .map(id -> HEX_PREFIX + id)
1508 .collect(Collectors.toList());
1509
Yi Tseng78f51f42017-02-02 13:54:58 -08001510 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001511 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001512 pointedGroupIds, nextObjective.id(), deviceId);
1513 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001514 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1515 removeBuckets, modGroupKey,
1516 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001517 // update store - synchronize access as there may be multiple threads
1518 // trying to remove buckets from the same group, each with its own
1519 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001520 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001521 // get a fresh copy of what the store holds
1522 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1523 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigiaeb11a72018-01-15 09:48:49 +01001524 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001525 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001526 // Object.equals(), we have to check the deque elems one by one
1527 allActiveKeys
1528 .removeIf(active ->
1529 chainsToRemove.stream().anyMatch(remove ->
1530 Arrays.equals(remove.toArray(new GroupKey[0]),
1531 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001532 // If no buckets in the group, then retain an entry for the
1533 // top level group which still exists.
1534 if (allActiveKeys.isEmpty()) {
1535 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1536 top.add(modGroupKey);
1537 allActiveKeys.add(top);
1538 }
1539 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001540 new OfdpaNextGroup(allActiveKeys,
1541 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001542 }
Charles Chan188ebf52015-12-23 00:15:11 -08001543 }
1544
1545 /**
Saurav Das961beb22017-03-29 19:09:17 -07001546 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001547 *
1548 * @param nextObjective the next objective to remove
1549 * @param next the NextGroup that represents the existing group-chain for
1550 * this next objective
1551 */
1552 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001553
Yi Tsengef19de12017-04-24 11:33:05 -07001554 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001555
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001556 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001557 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001558 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001559
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001560 allActiveKeys
1561 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1562 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001563 flowObjectiveStore.removeNextGroup(nextObjective.id());
1564 }
1565
Saurav Dasceccf242017-08-03 18:30:35 -07001566 /**
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001567 * Modify buckets in the L2 interface group.
1568 *
1569 * @param nextObjective a next objective that contains information for the
1570 * buckets to be modified in the group
1571 * @param next the representation of the existing group-chains for this next
1572 * objective, from which the innermost group buckets to remove are determined
1573 */
1574 protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup next) {
1575 if (nextObjective.type() != NextObjective.Type.SIMPLE) {
1576 log.warn("ModifyBucketFromGroup cannot be applied to nextType:{} in dev:{} for next:{}",
1577 nextObjective.type(), deviceId, nextObjective.id());
1578 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1579 return;
1580 }
1581
1582 VlanId assignedVlan = readVlanFromSelector(nextObjective.meta());
1583 if (assignedVlan == null) {
1584 log.warn("VLAN ID required by simple next obj is missing. Abort.");
1585 fail(nextObjective, ObjectiveError.BADPARAMS);
1586 return;
1587 }
1588
1589 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan);
1590
1591 // There is only one L2 interface group in this case
1592 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
1593
1594 // Replace group bucket for L2 interface group
1595 groupService.setBucketsForGroup(deviceId,
1596 l2InterfaceGroupDesc.appCookie(),
1597 l2InterfaceGroupDesc.buckets(),
1598 l2InterfaceGroupDesc.appCookie(),
1599 l2InterfaceGroupDesc.appId());
1600
1601 // update store - synchronize access as there may be multiple threads
1602 // trying to remove buckets from the same group, each with its own
1603 // potentially stale copy of allActiveKeys
1604 synchronized (flowObjectiveStore) {
1605 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1606 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1607 top.add(l2InterfaceGroupDesc.appCookie());
1608 modifiedGroupKeys.add(top);
1609
1610 flowObjectiveStore.putNextGroup(nextObjective.id(),
1611 new OfdpaNextGroup(modifiedGroupKeys,
1612 nextObjective));
1613 }
1614 }
1615
1616 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001617 * Checks existing buckets in {@link NextGroup} to verify if they match
1618 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1619 * to ensure that the buckets match up.
1620 *
1621 * @param nextObjective the next objective to verify
1622 * @param next the representation of the existing group which has to be
1623 * modified to match the given next objective
1624 */
1625 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
Pier Luigi3dfd8352018-01-25 16:16:02 +01001626 if (nextObjective.type() == NextObjective.Type.SIMPLE) {
1627 log.warn("verification not supported for indirect group");
Saurav Dasceccf242017-08-03 18:30:35 -07001628 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1629 return;
1630 }
1631 log.debug("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
1632 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1633 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1634 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigi4a7cdfa2018-01-29 10:30:59 +01001635
1636 // Iterating over the treatments of the next objective allows
1637 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07001638 for (TrafficTreatment bkt : nextObjective.next()) {
1639 PortNumber portNumber = readOutPortFromTreatment(bkt);
1640 int label = readLabelFromTreatment(bkt);
1641 if (portNumber == null) {
1642 log.warn("treatment {} of next objective {} has no outport.. "
1643 + "cannot remove bucket from group in dev: {}", bkt,
1644 nextObjective.id(), deviceId);
1645 fail(nextObjective, ObjectiveError.BADPARAMS);
1646 return;
1647 }
1648 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1649 groupService, deviceId,
1650 portNumber, label);
1651 if (existing.isEmpty()) {
1652 // if it doesn't exist, mark this bucket for creation
1653 bucketsToCreate.add(bkt);
1654 }
1655 if (existing.size() > 1) {
1656 // if it exists but there are duplicates, mark the others for removal
1657 existing.remove(0);
1658 indicesToRemove.addAll(existing);
1659 }
1660 }
1661
Pier Luigi4a7cdfa2018-01-29 10:30:59 +01001662 // Detect situation where the next data has more buckets
1663 // (not duplicates) respect to the next objective
1664 if (allActiveKeys.size() > nextObjective.next().size()) {
1665 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
1666 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
1667 deviceId, nextObjective.id(), nextObjective.next().size(), allActiveKeys.size());
1668 List<Integer> otherIndices = indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
1669 groupService, deviceId);
1670 // Filter out the indices not present
1671 otherIndices = otherIndices.stream()
1672 .filter(index -> !indicesToRemove.contains(index))
1673 .collect(Collectors.toList());
1674 // Add all to the final list
1675 indicesToRemove.addAll(otherIndices);
1676 }
1677
Pier Luigi3dfd8352018-01-25 16:16:02 +01001678 log.debug("Buckets to create {}", bucketsToCreate);
1679 log.debug("Indices to remove {}", indicesToRemove);
1680
Saurav Dasceccf242017-08-03 18:30:35 -07001681 if (!bucketsToCreate.isEmpty()) {
1682 log.info("creating {} buckets as part of nextId: {} verification",
1683 bucketsToCreate.size(), nextObjective.id());
1684 //create a nextObjective only with these buckets
1685 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1686 .withId(nextObjective.id())
Pier Luigi3dfd8352018-01-25 16:16:02 +01001687 .withType(nextObjective.type())
Saurav Dasceccf242017-08-03 18:30:35 -07001688 .withMeta(nextObjective.meta())
1689 .fromApp(nextObjective.appId());
Pier Luigi3dfd8352018-01-25 16:16:02 +01001690 bucketsToCreate.forEach(nextObjBuilder::addTreatment);
1691 // According to the next type we call the proper add function
1692 if (nextObjective.type() == NextObjective.Type.HASHED) {
1693 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1694 } else {
1695 addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1696 }
Saurav Dasceccf242017-08-03 18:30:35 -07001697 }
1698
1699 if (!indicesToRemove.isEmpty()) {
1700 log.info("removing {} buckets as part of nextId: {} verification",
1701 indicesToRemove.size(), nextObjective.id());
1702 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1703 indicesToRemove.forEach(index -> chainsToRemove
1704 .add(allActiveKeys.get(index)));
1705 removeBucket(chainsToRemove, nextObjective);
1706 }
1707
Saurav Das9df5b7c2017-08-14 16:44:43 -07001708 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1709 // flowObjective store record is in-sync with nextObjective passed-in
1710 // Nevertheless groupStore may not be in sync due to bug in the store
1711 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1712 // method will not be required.
Pier Luigi3dfd8352018-01-25 16:16:02 +01001713 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
1714 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
1715 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001716 int objGroupSize = nextObjective.next().size();
1717 if (actualGroupSize != objGroupSize) {
1718 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1719 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1720 objGroupSize, actualGroupSize);
1721 }
1722 if (actualGroupSize > objGroupSize) {
Pier Luigi3dfd8352018-01-25 16:16:02 +01001723 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001724 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1725 //check every bucket in the actual group
Pier Luigi3dfd8352018-01-25 16:16:02 +01001726 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001727 GroupInstruction g = (GroupInstruction) bucket.treatment()
1728 .allInstructions().iterator().next();
1729 GroupId gidToCheck = g.groupId(); // the group pointed to
1730 boolean matches = false;
1731 for (Deque<GroupKey> validChain : allActiveKeys) {
1732 if (validChain.size() < 2) {
1733 continue;
1734 }
1735 GroupKey pointedGroupKey = validChain.stream()
1736 .collect(Collectors.toList()).get(1);
1737 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1738 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1739 matches = true;
1740 break;
1741 }
1742 }
1743 if (!matches) {
1744 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1745 bucketsToRemove.add(bucket);
1746 }
1747 }
1748 // remove buckets for which there was no record in the obj store
1749 if (bucketsToRemove.isEmpty()) {
1750 log.warn("Mismatch detected but could not determine which"
1751 + "buckets to remove");
1752 } else {
1753 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigi3dfd8352018-01-25 16:16:02 +01001754 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
1755 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001756 nextObjective.appId());
1757 }
1758 } else if (actualGroupSize < objGroupSize) {
Pier Luigi3dfd8352018-01-25 16:16:02 +01001759 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001760 // should also add buckets not in group-store but in obj-store
1761 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1762 //check every bucket in the obj
1763 for (Deque<GroupKey> validChain : allActiveKeys) {
1764 if (validChain.size() < 2) {
1765 continue;
1766 }
1767 GroupKey pointedGroupKey = validChain.stream()
1768 .collect(Collectors.toList()).get(1);
1769 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1770 if (pointedGroup == null) {
1771 // group should exist, otherwise cannot be added as bucket
1772 continue;
1773 }
1774 boolean matches = false;
Pier Luigi3dfd8352018-01-25 16:16:02 +01001775 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001776 GroupInstruction g = (GroupInstruction) bucket.treatment()
1777 .allInstructions().iterator().next();
1778 GroupId gidToCheck = g.groupId(); // the group pointed to
1779 if (pointedGroup.id().equals(gidToCheck)) {
1780 matches = true;
1781 break;
1782 }
1783 }
1784 if (!matches) {
1785 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1786 TrafficTreatment t = DefaultTrafficTreatment.builder()
1787 .group(pointedGroup.id())
1788 .build();
Pier Luigi3dfd8352018-01-25 16:16:02 +01001789 // Create the proper bucket according to the next type
1790 if (nextObjective.type() == NextObjective.Type.HASHED) {
1791 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1792 } else {
1793 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
1794 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001795 }
1796 }
1797 if (bucketsToAdd.isEmpty()) {
1798 log.warn("Mismatch detected but could not determine which "
1799 + "buckets to add");
1800 } else {
1801 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigi3dfd8352018-01-25 16:16:02 +01001802 groupService.addBucketsToGroup(deviceId, topGroupKey,
1803 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001804 nextObjective.appId());
1805 }
1806 }
1807 }
1808
Saurav Dasceccf242017-08-03 18:30:35 -07001809 pass(nextObjective);
1810 }
1811
1812 //////////////////////////////////////
1813 // Helper methods and classes
1814 //////////////////////////////////////
1815
Yi Tsengef19de12017-04-24 11:33:05 -07001816 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1817 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001818 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001819 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001820 }
Yi Tsengef19de12017-04-24 11:33:05 -07001821 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001822 return val;
1823 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001824 }
1825
Yi Tsengef19de12017-04-24 11:33:05 -07001826 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1827 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001828 if (val == null) {
1829 val = Sets.newConcurrentHashSet();
1830 }
1831 val.add(gce);
1832 return val;
1833 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001834 }
1835
Yi Tsengef19de12017-04-24 11:33:05 -07001836 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1837 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001838 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1839 if (nextObjs != null) {
1840 nextObjs.add(nextObjective);
1841 } else {
1842 nextObjs = Sets.newHashSet(nextObjective);
1843 }
1844 return nextObjs;
1845 });
1846 }
1847
Yi Tsengef19de12017-04-24 11:33:05 -07001848 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1849 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1850 if (nextObjs != null) {
1851
1852 nextObjs.forEach(nextObj -> {
1853 log.debug("Group {} updated, update pending next objective {}.",
1854 groupKey, nextObj);
1855
1856 pass(nextObj);
1857 });
1858 }
1859 return Sets.newHashSet();
1860 });
1861 }
1862
1863 private void processPendingRemoveNextObjs(GroupKey key) {
1864 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1865 if (groupKeys.isEmpty()) {
1866 pendingRemoveNextObjectives.invalidate(nextObjective);
1867 pass(nextObjective);
1868 } else {
1869 groupKeys.remove(key);
1870 }
1871 });
1872 }
1873
1874 protected int getNextAvailableIndex() {
1875 return (int) nextIndex.incrementAndGet();
1876 }
1877
Yi Tsengef19de12017-04-24 11:33:05 -07001878 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1879 //first check for group chain
1880 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1881 if (gceSet != null) {
1882 for (GroupChainElem gce : gceSet) {
1883 log.debug("Group service {} group key {} in device {}. "
1884 + "Processing next group in group chain with group id 0x{}",
1885 (added) ? "ADDED" : "processed",
1886 key, deviceId,
1887 Integer.toHexString(gce.groupDescription().givenGroupId()));
1888 processGroupChain(gce);
1889 }
1890 } else {
1891 // otherwise chain complete - check for waiting nextObjectives
1892 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1893 pendingAddNextObjectives.getIfPresent(key);
1894 if (nextGrpList != null) {
1895 pendingAddNextObjectives.invalidate(key);
1896 nextGrpList.forEach(nextGrp -> {
1897 log.debug("Group service {} group key {} in device:{}. "
1898 + "Done implementing next objective: {} <<-->> gid:0x{}",
1899 (added) ? "ADDED" : "processed",
1900 key, deviceId, nextGrp.nextObjective().id(),
1901 Integer.toHexString(groupService.getGroup(deviceId, key)
1902 .givenGroupId()));
1903 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001904 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001905
1906 // check if addBuckets waiting for this completion
1907 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1908 if (pendBkts != null) {
1909 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1910 }
1911 return null;
1912 });
1913 });
1914 }
1915 }
1916 }
1917
Charles Chan188ebf52015-12-23 00:15:11 -08001918 /**
1919 * Processes next element of a group chain. Assumption is that if this
1920 * group points to another group, the latter has already been created
1921 * and this driver has received notification for it. A second assumption is
1922 * that if there is another group waiting for this group then the appropriate
1923 * stores already have the information to act upon the notification for the
1924 * creation of this group.
1925 * <p>
1926 * The processing of the GroupChainElement depends on the number of groups
1927 * this element is waiting on. For all group types other than SIMPLE, a
1928 * GroupChainElement could be waiting on multiple groups.
1929 *
1930 * @param gce the group chain element to be processed next
1931 */
1932 private void processGroupChain(GroupChainElem gce) {
1933 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1934 if (waitOnGroups != 0) {
1935 log.debug("GCE: {} not ready to be processed", gce);
1936 return;
1937 }
1938 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001939 if (gce.addBucketToGroup()) {
1940 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1941 gce.groupDescription().appCookie(),
1942 gce.groupDescription().buckets(),
1943 gce.groupDescription().appCookie(),
1944 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001945 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001946 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001947 }
1948 }
1949
Yi Tsengef19de12017-04-24 11:33:05 -07001950 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1951 List<GroupKey> groupKeys) {
1952 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001953 }
1954
Saurav Dasc88d4662017-05-15 15:34:25 -07001955 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1956 synchronized (flowObjectiveStore) {
1957 // get fresh copy of what the store holds
1958 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1959 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1960 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1961 return;
1962 }
1963 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1964 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigiaeb11a72018-01-15 09:48:49 +01001965 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001966 // If active keys shows only the top-level group without a chain of groups,
1967 // then it represents an empty group. Update by replacing empty chain.
1968 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1969 allActiveKeys.clear();
1970 }
1971 allActiveKeys.addAll(nextGrp.allKeys());
1972 flowObjectiveStore.putNextGroup(nextId,
1973 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1974 }
1975 }
1976 }
1977
Saurav Das8be4e3a2016-03-11 17:19:07 -08001978 private class InnerGroupListener implements GroupListener {
1979 @Override
1980 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001981 switch (event.type()) {
1982 case GROUP_ADDED:
1983 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1984 break;
1985 case GROUP_REMOVED:
1986 processPendingRemoveNextObjs(event.subject().appCookie());
1987 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001988 case GROUP_UPDATED:
1989 processPendingUpdateNextObjs(event.subject().appCookie());
1990 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001991 default:
1992 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001993 }
1994 }
1995 }
Charles Chan188ebf52015-12-23 00:15:11 -08001996}