blob: 6a03ee6b193e09310d548edba5914a2f81a66292 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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;
45import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080046import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan188ebf52015-12-23 00:15:11 -080047import org.onosproject.net.flowobjective.FlowObjectiveStore;
48import org.onosproject.net.flowobjective.NextObjective;
Yi Tseng47f82dc2017-03-05 22:48:39 -080049import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080050import org.onosproject.net.flowobjective.ObjectiveError;
51import org.onosproject.net.group.DefaultGroupBucket;
52import org.onosproject.net.group.DefaultGroupDescription;
53import org.onosproject.net.group.DefaultGroupKey;
54import org.onosproject.net.group.Group;
55import org.onosproject.net.group.GroupBucket;
56import org.onosproject.net.group.GroupBuckets;
57import org.onosproject.net.group.GroupDescription;
58import org.onosproject.net.group.GroupEvent;
59import org.onosproject.net.group.GroupKey;
60import org.onosproject.net.group.GroupListener;
61import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080062import org.onosproject.store.service.AtomicCounter;
63import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080064import org.slf4j.Logger;
65
66import java.util.ArrayDeque;
67import java.util.ArrayList;
68import java.util.Collection;
69import java.util.Collections;
70import java.util.Deque;
71import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080072import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080073import java.util.Set;
74import java.util.concurrent.ConcurrentHashMap;
75import java.util.concurrent.CopyOnWriteArrayList;
76import java.util.concurrent.Executors;
77import java.util.concurrent.ScheduledExecutorService;
78import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080079import java.util.stream.Collectors;
80
81import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070082import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
83import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080084import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070085import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080086import static org.onosproject.net.group.GroupDescription.Type.ALL;
87import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080088import static org.slf4j.LoggerFactory.getLogger;
89
90/**
Saurav Das961beb22017-03-29 19:09:17 -070091 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080092 */
Charles Chan361154b2016-03-24 10:23:39 -070093public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -070094 protected final Logger log = getLogger(getClass());
Charles Chane849c192016-01-11 18:28:54 -080095
Yi Tsengef19de12017-04-24 11:33:05 -070096 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -080097 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080098 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -070099 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800100
101 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800102 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800103
Yi Tsengef19de12017-04-24 11:33:05 -0700104 protected DeviceId deviceId;
105 private Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
106 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
107 private Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
108 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
109
Yi Tseng47f82dc2017-03-05 22:48:39 -0800110 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800111 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800112 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700113 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800114
Yi Tsengef19de12017-04-24 11:33:05 -0700115 private ScheduledExecutorService groupCheckerExecutor =
116 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
117
118 public Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives() {
119 return pendingAddNextObjectives;
120 }
121
122 public Cache<GroupKey, Set<GroupChainElem>> pendingGroups() {
123 return pendingGroups;
124 }
125
Charles Chan40132b32017-01-22 00:19:37 -0800126 /**
127 * Determines whether this pipeline support copy ttl instructions or not.
128 *
129 * @return true if copy ttl instructions are supported
130 */
131 protected boolean supportCopyTtl() {
132 return true;
133 }
134
135 /**
136 * Determines whether this pipeline support set mpls bos instruction or not.
137 *
138 * @return true if set mpls bos instruction is supported
139 */
140 protected boolean supportSetMplsBos() {
141 return true;
142 }
143
Charles Chan0f43e472017-02-14 14:00:16 -0800144 /**
145 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
146 * <p>
147 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
148 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
149 *
150 * @return true if this pipeline requires popping VLAN before pushing MPLS
151 */
152 protected boolean requireVlanPopBeforeMplsPush() {
153 return false;
154 }
155
Charles Chan188ebf52015-12-23 00:15:11 -0800156 protected void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700157 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800158 this.deviceId = deviceId;
159 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800160 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800161 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700162 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800163
Charles Chanfc5c7802016-05-17 13:13:55 -0700164 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800165 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700166 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
167 if (notification.getCause() == RemovalCause.EXPIRED &&
168 Objects.nonNull(notification.getValue())) {
169 notification.getValue()
170 .forEach(ofdpaNextGrp ->
171 fail(ofdpaNextGrp.nextObjective(),
172 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700173 }
174 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800175
Charles Chanfc5c7802016-05-17 13:13:55 -0700176 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
177 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700178 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700179 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700180 fail(notification.getKey(),
181 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800182 }
183 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700184 pendingGroups = CacheBuilder.newBuilder()
185 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700186 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700187 if (notification.getCause() == RemovalCause.EXPIRED) {
188 log.error("Unable to install group with key {} and pending GCEs: {}",
189 notification.getKey(), notification.getValue());
190 }
191 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800192 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700193 GroupChecker groupChecker = new GroupChecker(this);
194 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800195 groupService.addListener(new InnerGroupListener());
196 }
197
Saurav Das8be4e3a2016-03-11 17:19:07 -0800198 //////////////////////////////////////
199 // Group Creation
200 //////////////////////////////////////
201
Yi Tsengef19de12017-04-24 11:33:05 -0700202 /**
203 * Adds a list of group chain by given NextObjective.
204 *
205 * @param nextObjective the NextObjective
206 */
Charles Chan188ebf52015-12-23 00:15:11 -0800207 protected void addGroup(NextObjective nextObjective) {
208 switch (nextObjective.type()) {
209 case SIMPLE:
210 Collection<TrafficTreatment> treatments = nextObjective.next();
211 if (treatments.size() != 1) {
212 log.error("Next Objectives of type Simple should only have a "
213 + "single Traffic Treatment. Next Objective Id:{}",
214 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700215 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800216 return;
217 }
218 processSimpleNextObjective(nextObjective);
219 break;
220 case BROADCAST:
221 processBroadcastNextObjective(nextObjective);
222 break;
223 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700224 if (!verifyHashedNextObjective(nextObjective)) {
225 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
226 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700227 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700228 return;
229 }
Charles Chan188ebf52015-12-23 00:15:11 -0800230 processHashedNextObjective(nextObjective);
231 break;
232 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700233 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800234 log.warn("Unsupported next objective type {}", nextObjective.type());
235 break;
236 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700237 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800238 log.warn("Unknown next objective type {}", nextObjective.type());
239 }
240 }
241
242 /**
243 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
244 * a chain of groups. The simple Next Objective passed
245 * in by the application has to be broken up into a group chain
246 * comprising of an L3 Unicast Group that points to an L2 Interface
247 * Group which in-turn points to an output port. In some cases, the simple
248 * next Objective can just be an L2 interface without the need for chaining.
249 *
250 * @param nextObj the nextObjective of type SIMPLE
251 */
252 private void processSimpleNextObjective(NextObjective nextObj) {
253 TrafficTreatment treatment = nextObj.next().iterator().next();
254 // determine if plain L2 or L3->L2
255 boolean plainL2 = true;
256 for (Instruction ins : treatment.allInstructions()) {
257 if (ins.type() == Instruction.Type.L2MODIFICATION) {
258 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
259 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
260 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
261 plainL2 = false;
262 break;
263 }
264 }
265 }
266
267 if (plainL2) {
268 createL2InterfaceGroup(nextObj);
269 return;
270 }
271
Pier Ventre140a8942016-11-02 07:26:38 -0700272 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800273 // In order to understand if it is a pseudo wire related
274 // next objective we look for the tunnel id in the meta.
275 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700276 if (nextObj.meta() != null) {
277 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800278
279 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
280 .meta()
281 .getCriterion(TUNNEL_ID);
282 if (tunnelIdCriterion != null) {
283 isPw = true;
284 }
285
Pier Ventre140a8942016-11-02 07:26:38 -0700286 }
287
Pier Ventre42287df2016-11-09 14:17:26 -0800288 if (!isPw) {
289 // break up simple next objective to GroupChain objects
290 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
291 nextObj.appId(), isMpls,
292 nextObj.meta());
293 if (groupInfo == null) {
294 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
295 return;
296 }
297 // create object for local and distributed storage
298 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700299 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
300 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Pier Ventre42287df2016-11-09 14:17:26 -0800301 OfdpaNextGroup ofdpaGrp =
302 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
303
304 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700305 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800306
307 // now we are ready to send the l2 groupDescription (inner), as all the stores
308 // that will get async replies have been updated. By waiting to update
309 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700310 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800311 } else {
312 // We handle the pseudo wire with a different a procedure.
313 // This procedure is meant to handle both initiation and
314 // termination of the pseudo wire.
315 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800316 }
Charles Chan188ebf52015-12-23 00:15:11 -0800317 }
318
Charles Chan188ebf52015-12-23 00:15:11 -0800319 /**
320 * Creates a simple L2 Interface Group.
321 *
322 * @param nextObj the next Objective
323 */
324 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700325 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700326 if (assignedVlan == null) {
327 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700328 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800329 return;
330 }
331
Charles Chan5b9df8d2016-03-28 22:21:40 -0700332 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800333
Charles Chan5b9df8d2016-03-28 22:21:40 -0700334 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700335 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan188ebf52015-12-23 00:15:11 -0800336
Charles Chan5b9df8d2016-03-28 22:21:40 -0700337 // Put all dependency information into allGroupKeys
338 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
339 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
340 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
341 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800342
Charles Chan5b9df8d2016-03-28 22:21:40 -0700343 // Point the next objective to this group
344 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
345 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
346
347 // Start installing the inner-most group
348 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800349 }
350
351 /**
352 * Creates one of two possible group-chains from the treatment
353 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700354 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
355 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800356 * The returned 'inner' group description is always the L2 Interface group.
357 *
358 * @param treatment that needs to be broken up to create the group chain
359 * @param nextId of the next objective that needs this group chain
360 * @param appId of the application that sent this next objective
361 * @param mpls determines if L3Unicast or MPLSInterface group is created
362 * @param meta metadata passed in by the application as part of the nextObjective
363 * @return GroupInfo containing the GroupDescription of the
364 * L2Interface group(inner) and the GroupDescription of the (outer)
365 * L3Unicast/MPLSInterface group. May return null if there is an
366 * error in processing the chain
367 */
Charles Chan425854b2016-04-11 15:32:12 -0700368 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700369 ApplicationId appId, boolean mpls,
370 TrafficSelector meta) {
371 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
372 }
373
374 /**
375 * Internal implementation of createL2L3Chain.
376 * <p>
377 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
378 * Since it is non-OF spec, we need an extension treatment for that.
379 * The useSetVlanExtension must be set to false for OFDPA i12.
380 * </p>
381 *
382 * @param treatment that needs to be broken up to create the group chain
383 * @param nextId of the next objective that needs this group chain
384 * @param appId of the application that sent this next objective
385 * @param mpls determines if L3Unicast or MPLSInterface group is created
386 * @param meta metadata passed in by the application as part of the nextObjective
387 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
388 * @return GroupInfo containing the GroupDescription of the
389 * L2Interface group(inner) and the GroupDescription of the (outer)
390 * L3Unicast/MPLSInterface group. May return null if there is an
391 * error in processing the chain
392 */
393 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800394 ApplicationId appId, boolean mpls,
395 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800396 // for the l2interface group, get vlan and port info
397 // for the outer group, get the src/dst mac, and vlan info
398 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
399 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
400 VlanId vlanid = null;
401 long portNum = 0;
402 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800403 MacAddress srcMac;
404 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800405 for (Instruction ins : treatment.allInstructions()) {
406 if (ins.type() == Instruction.Type.L2MODIFICATION) {
407 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
408 switch (l2ins.subtype()) {
409 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800410 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
411 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800412 break;
413 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800414 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
415 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800416 break;
417 case VLAN_ID:
418 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700419 if (useSetVlanExtension) {
420 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
421 outerTtb.extension(ofdpaSetVlanVid, deviceId);
422 } else {
423 outerTtb.setVlanId(vlanid);
424 }
Charles Chan188ebf52015-12-23 00:15:11 -0800425 setVlan = true;
426 break;
427 case VLAN_POP:
428 innerTtb.popVlan();
429 popVlan = true;
430 break;
431 case DEC_MPLS_TTL:
432 case MPLS_LABEL:
433 case MPLS_POP:
434 case MPLS_PUSH:
435 case VLAN_PCP:
436 case VLAN_PUSH:
437 default:
438 break;
439 }
440 } else if (ins.type() == Instruction.Type.OUTPUT) {
441 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
442 innerTtb.add(ins);
443 } else {
444 log.warn("Driver does not handle this type of TrafficTreatment"
445 + " instruction in nextObjectives: {}", ins.type());
446 }
447 }
448
449 if (vlanid == null && meta != null) {
450 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700451 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800452 if (vidCriterion != null) {
453 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
454 }
455 // if vlan is not set, use the vlan in metadata for outerTtb
456 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700457 if (useSetVlanExtension) {
458 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
459 outerTtb.extension(ofdpaSetVlanVid, deviceId);
460 } else {
461 outerTtb.setVlanId(vlanid);
462 }
Charles Chan188ebf52015-12-23 00:15:11 -0800463 }
464 }
465
466 if (vlanid == null) {
467 log.error("Driver cannot process an L2/L3 group chain without "
468 + "egress vlan information for dev: {} port:{}",
469 deviceId, portNum);
470 return null;
471 }
472
473 if (!setVlan && !popVlan) {
474 // untagged outgoing port
475 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
476 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700477 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800478 innerTtb = temp;
479 }
480
481 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700482 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800483 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800484 // but different for the same portnumber on different devices. Also different
485 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800486 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700487 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800488
489 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700490 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800491 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700492 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800493 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700494 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
495 final GroupKey mplsGroupKey = new DefaultGroupKey(
496 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800497 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800498 // create the mpls-interface group description to wait for the
499 // l2 interface group to be processed
500 GroupBucket mplsinterfaceGroupBucket =
501 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
502 outerGrpDesc = new DefaultGroupDescription(
503 deviceId,
504 GroupDescription.Type.INDIRECT,
505 new GroupBuckets(Collections.singletonList(
506 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700507 mplsGroupKey,
508 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800509 appId);
510 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700511 deviceId, Integer.toHexString(mplsGroupId),
512 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800513 } else {
514 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800515 int l3unicastIndex = getNextAvailableIndex();
516 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
517 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700518 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800519 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800520 // create the l3unicast group description to wait for the
521 // l2 interface group to be processed
522 GroupBucket l3unicastGroupBucket =
523 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
524 outerGrpDesc = new DefaultGroupDescription(
525 deviceId,
526 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700527 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800528 l3groupkey,
529 l3groupId,
530 appId);
531 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
532 deviceId, Integer.toHexString(l3groupId),
533 l3groupkey, nextId);
534 }
535
536 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700537 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800538 updatePendingGroups(l2groupkey, gce);
539
Yi Tsengef19de12017-04-24 11:33:05 -0700540 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700541 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800542 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
543 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700544 new DefaultGroupDescription(deviceId,
545 GroupDescription.Type.INDIRECT,
546 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
547 l2groupkey,
548 l2groupId,
549 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800550 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
551 deviceId, Integer.toHexString(l2groupId),
552 l2groupkey, nextId);
553 return new GroupInfo(l2groupDescription, outerGrpDesc);
554
555 }
556
557 /**
558 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
559 * a chain of groups. The broadcast Next Objective passed in by the application
560 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800561 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800562 *
563 * @param nextObj the nextObjective of type BROADCAST
564 */
565 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700566 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700567 if (assignedVlan == null) {
568 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700569 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800570 return;
571 }
Charles Chan188ebf52015-12-23 00:15:11 -0800572
Charles Chan5b9df8d2016-03-28 22:21:40 -0700573 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
574
Yi Tsengef19de12017-04-24 11:33:05 -0700575 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700576 if (ipDst != null) {
577 if (ipDst.isMulticast()) {
578 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
579 } else {
580 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700581 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700582 }
583 } else {
584 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
585 }
586 }
587
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700588 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
589 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700590 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
591
592 // break up broadcast next objective to multiple groups
593 Collection<TrafficTreatment> buckets = nextObj.next();
594
Charles Chan188ebf52015-12-23 00:15:11 -0800595 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800596 for (TrafficTreatment treatment : buckets) {
597 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
598 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700599 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800600 // ensure that the only allowed treatments are pop-vlan and output
601 for (Instruction ins : treatment.allInstructions()) {
602 if (ins.type() == Instruction.Type.L2MODIFICATION) {
603 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
604 switch (l2ins.subtype()) {
605 case VLAN_POP:
606 newTreatment.add(l2ins);
607 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700608 case VLAN_ID:
609 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
610 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800611 default:
612 log.debug("action {} not permitted for broadcast nextObj",
613 l2ins.subtype());
614 break;
615 }
616 } else if (ins.type() == Instruction.Type.OUTPUT) {
617 portNum = ((Instructions.OutputInstruction) ins).port();
618 newTreatment.add(ins);
619 } else {
Charles Chane849c192016-01-11 18:28:54 -0800620 log.debug("TrafficTreatment of type {} not permitted in " +
621 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800622 }
623 }
624
Yi Tsengef19de12017-04-24 11:33:05 -0700625 if (portNum == null) {
626 log.warn("Can't find output port for the bucket {}.", treatment);
627 continue;
628 }
629
Charles Chan188ebf52015-12-23 00:15:11 -0800630 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700631 VlanId l2InterfaceGroupVlan =
632 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
633 egressVlan : assignedVlan;
634 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
635 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700636 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800637 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
638 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
639 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700640 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800641 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700642 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700643 new DefaultGroupDescription(deviceId,
644 GroupDescription.Type.INDIRECT,
645 new GroupBuckets(Collections.singletonList(
646 l2InterfaceGroupBucket)),
647 l2InterfaceGroupKey,
648 l2InterfaceGroupId,
649 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800650 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700651 deviceId, Integer.toHexString(l2InterfaceGroupId),
652 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800653
Charles Chan5b9df8d2016-03-28 22:21:40 -0700654 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
655 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800656 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700657 return groupInfoBuilder.build();
658 }
Charles Chan188ebf52015-12-23 00:15:11 -0800659
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700660 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
661 List<GroupInfo> groupInfos) {
662 // assemble info for l2 flood group. Since there can be only one flood
663 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700664 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
665 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700666
Charles Chan188ebf52015-12-23 00:15:11 -0800667 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700668 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800669 // create the l2flood group-description to wait for all the
670 // l2interface groups to be processed
671 GroupDescription l2floodGroupDescription =
672 new DefaultGroupDescription(
673 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800674 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800675 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700676 l2FloodGroupKey,
677 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800678 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800679 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700680 deviceId, Integer.toHexString(l2FloodGroupId),
681 l2FloodGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800682
Charles Chan5b9df8d2016-03-28 22:21:40 -0700683 // Put all dependency information into allGroupKeys
684 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
685 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700686 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700687 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700688 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
689 groupKeyChain.addFirst(l2FloodGroupKey);
690 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700691 });
Charles Chan188ebf52015-12-23 00:15:11 -0800692
Charles Chan5b9df8d2016-03-28 22:21:40 -0700693 // Point the next objective to this group
694 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700695 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800696
Charles Chan5b9df8d2016-03-28 22:21:40 -0700697 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700698 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700699 groupInfos.forEach(groupInfo -> {
700 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700701 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700702 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700703 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700704 });
705 }
706
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700707 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
708 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700709 List<GroupBucket> l3McastBuckets = new ArrayList<>();
710 groupInfos.forEach(groupInfo -> {
711 // Points to L3 interface group if there is one.
712 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -0700713 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
714 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700715 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800716 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700717 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
718 l3McastBuckets.add(abucket);
719 });
720
721 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700722 int l3MulticastGroupId = L3_MULTICAST_TYPE |
723 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
724 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700725 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700726
727 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800728 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700729 new GroupBuckets(l3McastBuckets),
730 l3MulticastGroupKey,
731 l3MulticastGroupId,
732 nextObj.appId());
733
734 // Put all dependency information into allGroupKeys
735 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
736 groupInfos.forEach(groupInfo -> {
737 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700738 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700739 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700740 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
741 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700742 }
743 gkeyChain.addFirst(l3MulticastGroupKey);
744 allGroupKeys.add(gkeyChain);
745 });
746
747 // Point the next objective to this group
748 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
749 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
750
751 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700752 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700753 groupInfos.forEach(groupInfo -> {
754 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700755 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700756
757 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700758 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
759 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
760 1, false, deviceId);
761 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700762 }
763
764 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700765 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700766 });
Charles Chan188ebf52015-12-23 00:15:11 -0800767 }
768
Charles Chan188ebf52015-12-23 00:15:11 -0800769 /**
770 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
771 * a chain of groups. The hashed Next Objective passed in by the application
772 * has to be broken up into a group chain comprising of an
773 * L3 ECMP group as the top level group. Buckets of this group can point
774 * to a variety of groups in a group chain, depending on the whether
775 * MPLS labels are being pushed or not.
776 * <p>
777 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
778 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
779 * check the nextObjective meta to see what is matching before being
780 * sent to this nextObjective.
781 *
782 * @param nextObj the nextObjective of type HASHED
783 */
Pier Ventre140a8942016-11-02 07:26:38 -0700784 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800785 // storage for all group keys in the chain of groups created
786 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
787 List<GroupInfo> unsentGroups = new ArrayList<>();
788 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
789
790 // now we can create the outermost L3 ECMP group
791 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
792 for (GroupInfo gi : unsentGroups) {
793 // create ECMP bucket to point to the outer group
794 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700795 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800796 GroupBucket sbucket = DefaultGroupBucket
797 .createSelectGroupBucket(ttb.build());
798 l3ecmpGroupBuckets.add(sbucket);
799 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800800 int l3ecmpIndex = getNextAvailableIndex();
801 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
802 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700803 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800804 GroupDescription l3ecmpGroupDesc =
805 new DefaultGroupDescription(
806 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800807 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800808 new GroupBuckets(l3ecmpGroupBuckets),
809 l3ecmpGroupKey,
810 l3ecmpGroupId,
811 nextObj.appId());
812 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
813 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700814 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800815
816 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700817 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800818 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
819
820 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
821 // that depends on it
822 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
823
824 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
825 deviceId, Integer.toHexString(l3ecmpGroupId),
826 l3ecmpGroupKey, nextObj.id());
827 // finally we are ready to send the innermost groups
828 for (GroupInfo gi : unsentGroups) {
829 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700830 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
831 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
832 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800833 }
Charles Chan188ebf52015-12-23 00:15:11 -0800834 }
835
836 /**
837 * Creates group chains for all buckets in a hashed group, and stores the
838 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
839 * should be empty.
840 * <p>
841 * Does not create the top level ECMP group. Does not actually send the
842 * groups to the groupService.
843 *
844 * @param nextObj the Next Objective with buckets that need to be converted
845 * to group chains
846 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
847 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
848 */
Pier Ventre140a8942016-11-02 07:26:38 -0700849 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700850 List<Deque<GroupKey>> allGroupKeys,
851 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800852 // break up hashed next objective to multiple groups
853 Collection<TrafficTreatment> buckets = nextObj.next();
854
855 for (TrafficTreatment bucket : buckets) {
856 //figure out how many labels are pushed in each bucket
857 int labelsPushed = 0;
858 MplsLabel innermostLabel = null;
859 for (Instruction ins : bucket.allInstructions()) {
860 if (ins.type() == Instruction.Type.L2MODIFICATION) {
861 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
862 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
863 labelsPushed++;
864 }
865 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
866 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700867 innermostLabel =
868 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800869 }
870 }
871 }
872 }
873
Yi Tsengef19de12017-04-24 11:33:05 -0700874 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800875 // XXX we only deal with 0 and 1 label push right now
876 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700877 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700878 TrafficSelector metaSelector = nextObj.meta();
879 if (metaSelector != null) {
880 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700881 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
882 nextObj.appId(), true,
883 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700884 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700885 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
886 nextObj.appId(), false,
887 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700888 }
889 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700890 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
891 nextObj.appId(), false,
892 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700893 }
Yi Tsengef19de12017-04-24 11:33:05 -0700894 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800895 log.error("Could not process nextObj={} in dev:{}",
896 nextObj.id(), deviceId);
897 return;
898 }
Yi Tsengef19de12017-04-24 11:33:05 -0700899 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
900 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800901
902 // we can't send the inner group description yet, as we have to
903 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -0700904 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -0800905
906 } else if (labelsPushed == 1) {
907 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
908 nextObj.appId(), true,
909 nextObj.meta());
910 if (onelabelGroupInfo == null) {
911 log.error("Could not process nextObj={} in dev:{}",
912 nextObj.id(), deviceId);
913 return;
914 }
915 // we need to add another group to this chain - the L3VPN group
916 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -0800917 if (requireVlanPopBeforeMplsPush()) {
918 l3vpnTtb.popVlan();
919 }
Charles Chan188ebf52015-12-23 00:15:11 -0800920 l3vpnTtb.pushMpls()
921 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -0700922 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -0800923 if (supportCopyTtl()) {
924 l3vpnTtb.copyTtlOut();
925 }
926 if (supportSetMplsBos()) {
927 l3vpnTtb.setMplsBos(true);
928 }
Charles Chan0f43e472017-02-14 14:00:16 -0800929 if (requireVlanPopBeforeMplsPush()) {
930 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
931 }
Charles Chan40132b32017-01-22 00:19:37 -0800932
Charles Chan188ebf52015-12-23 00:15:11 -0800933 GroupBucket l3vpnGrpBkt =
934 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800935 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700936 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
937 GroupKey l3vpnGroupKey = new DefaultGroupKey(
938 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800939 GroupDescription l3vpnGroupDesc =
940 new DefaultGroupDescription(
941 deviceId,
942 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700943 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
944 l3vpnGroupKey,
945 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800946 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -0700947 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
948 1,
949 false,
950 deviceId);
951 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800952
Yi Tsengef19de12017-04-24 11:33:05 -0700953 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
954 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
955 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -0800956
957 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -0700958 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800959
960 // we can't send the innermost group yet, as we have to create
961 // the dependent ECMP group first. So we store ...
962 unsentGroups.add(onelabelGroupInfo);
963
Yi Tsengef19de12017-04-24 11:33:05 -0700964 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
965 deviceId, Integer.toHexString(l3vpnGroupId),
966 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800967
968 } else {
969 log.warn("Driver currently does not handle more than 1 MPLS "
970 + "labels. Not processing nextObjective {}", nextObj.id());
971 return;
972 }
973
974 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -0700975 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800976 }
977 }
978
Pier Ventre42287df2016-11-09 14:17:26 -0800979 /**
980 * Processes the pseudo wire related next objective.
981 * This procedure try to reuse the mpls label groups,
982 * the mpls interface group and the l2 interface group.
983 *
984 * @param nextObjective the objective to process.
985 */
986 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700987 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
988 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -0800989 }
990
Saurav Das8be4e3a2016-03-11 17:19:07 -0800991 //////////////////////////////////////
992 // Group Editing
993 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -0800994 /**
995 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700996 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -0700997 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -0800998 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700999 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001000 * @param next the representation of the existing group-chain for this next objective
1001 */
1002 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001003 if (nextObjective.type() != NextObjective.Type.HASHED &&
1004 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001005 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001006 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001007 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001008 return;
1009 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001010
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001011 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001012 // existing bucket. If it is for an existing output port, then its a
1013 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001014 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001015 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1016 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1017 groupService,
1018 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001019 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1020 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001021
1022 nextObjective.next().forEach(trafficTreatment -> {
1023 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001024 if (portNumber == null) {
1025 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001026 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001027 if (existingPorts.contains(portNumber)) {
1028 duplicateBuckets.add(trafficTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001029 } else {
1030 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001031 }
1032 });
1033
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001034 if (duplicateBuckets.isEmpty()) {
1035 // use the original objective
1036 objectiveToAdd = nextObjective;
1037 } else if (!nonDuplicateBuckets.isEmpty()) {
1038 // only use the non-duplicate buckets if there are any
1039 log.debug("Some buckets {} already exist in next id {}, duplicate "
1040 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1041 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001042 NextObjective.Builder builder = DefaultNextObjective.builder()
1043 .withType(nextObjective.type())
1044 .withId(nextObjective.id())
1045 .withMeta(nextObjective.meta())
1046 .fromApp(nextObjective.appId());
1047 nonDuplicateBuckets.forEach(builder::addTreatment);
1048
1049 ObjectiveContext context = nextObjective.context().orElse(null);
1050 objectiveToAdd = builder.addToExisting(context);
1051 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001052 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001053 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1054 duplicateBuckets, nextObjective.id());
1055 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001056 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001057 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001058
Saurav Das1a129a02016-11-18 15:21:57 -08001059 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001060 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001061 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001062 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001063 }
1064 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001065
Saurav Das1a129a02016-11-18 15:21:57 -08001066 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001067 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001068 // storage for all group keys in the chain of groups created
1069 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1070 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001071 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001072 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1073
Yi Tseng78f51f42017-02-02 13:54:58 -08001074 // now we can create the buckets to add to the outermost L3 ECMP group
1075 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001076
Saurav Das1a129a02016-11-18 15:21:57 -08001077 // retrieve the original L3 ECMP group
1078 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001079 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001080 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001081 return;
1082 }
Saurav Das1a129a02016-11-18 15:21:57 -08001083 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001084 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001085
1086 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001087 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001088 // processed, the info will be extracted for the bucketAdd call to groupService
1089 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001090 new DefaultGroupDescription(deviceId,
1091 SELECT,
1092 new GroupBuckets(newBuckets),
1093 l3ecmpGroupKey,
1094 l3ecmpGroupId,
1095 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001096 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1097 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001098 true,
1099 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001100
1101 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001102 // If active keys shows only the top-level group without a chain of groups,
1103 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -08001104 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
1105 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001106 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1107 allActiveKeys.clear();
1108 }
1109 allActiveKeys.add(newBucketChain);
Yi Tsengef19de12017-04-24 11:33:05 -07001110 updatePendingNextObjective(l3ecmpGroupKey, new OfdpaNextGroup(allActiveKeys, nextObjective));
Yi Tseng78f51f42017-02-02 13:54:58 -08001111
Yi Tsengef19de12017-04-24 11:33:05 -07001112 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001113 deviceId, Integer.toHexString(l3ecmpGroupId),
1114 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001115
1116 unsentGroups.forEach(groupInfo -> {
1117 // send the innermost group
1118 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001119 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), deviceId);
1120 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1121 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001122 });
Saurav Das1a129a02016-11-18 15:21:57 -08001123 }
Charles Chan188ebf52015-12-23 00:15:11 -08001124
Saurav Das1a129a02016-11-18 15:21:57 -08001125 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001126 List<Deque<GroupKey>> allActiveKeys) {
1127 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001128 if (assignedVlan == null) {
1129 log.warn("VLAN ID required by broadcast next obj is missing. "
1130 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1131 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001132 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001133 return;
1134 }
Saurav Das1a129a02016-11-18 15:21:57 -08001135 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001136 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001137 if (ipDst != null) {
1138 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001139 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001140 } else {
1141 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001142 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001143 }
1144 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001145 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001146 }
1147 }
1148
1149 private void addBucketToL2FloodGroup(NextObjective nextObj,
1150 List<Deque<GroupKey>> allActiveKeys,
1151 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001152 VlanId assignedVlan) {
1153 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1154
1155 if (l2FloodGroup == null) {
1156 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1157 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001158 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001159 return;
1160 }
Saurav Das1a129a02016-11-18 15:21:57 -08001161
Yi Tseng78f51f42017-02-02 13:54:58 -08001162 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1163 int l2floodGroupId = l2FloodGroup.id().id();
1164 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1165
1166 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001167 new DefaultGroupDescription(deviceId,
1168 ALL,
1169 new GroupBuckets(newBuckets),
1170 l2floodGroupKey,
1171 l2floodGroupId,
1172 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001173
Yi Tseng78f51f42017-02-02 13:54:58 -08001174 GroupChainElem l2FloodGroupChainElement =
1175 new GroupChainElem(l2FloodGroupDescription,
1176 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001177 true,
1178 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001179
Saurav Das1a129a02016-11-18 15:21:57 -08001180 updatePendingNextObjective(l2floodGroupKey,
1181 new OfdpaNextGroup(allActiveKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001182
1183 //ensure assignedVlan applies to the chosen group
1184 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1185
1186 if (!floodGroupVlan.equals(assignedVlan)) {
1187 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1188 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1189 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001190 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001191 return;
1192 }
1193
1194 groupInfos.forEach(groupInfo -> {
1195 // update original NextGroup with new bucket-chain
1196 // If active keys shows only the top-level group without a chain of groups,
1197 // then it represents an empty group. Update by replacing empty chain.
1198 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001199 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001200 newBucketChain.addFirst(l2floodGroupKey);
1201 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1202 allActiveKeys.clear();
1203 }
1204 allActiveKeys.add(newBucketChain);
1205
Yi Tsengef19de12017-04-24 11:33:05 -07001206 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001207 deviceId, Integer.toHexString(l2floodGroupId),
1208 l2floodGroupKey, nextObj.id());
1209 // send the innermost group
1210 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001211 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001212 deviceId);
1213
Yi Tsengef19de12017-04-24 11:33:05 -07001214 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001215
Yi Tsengef19de12017-04-24 11:33:05 -07001216 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1217 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001218 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1219
1220 if (existsL2IGroup != null) {
1221 // group already exist
1222 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1223 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001224 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001225 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001226 });
1227 }
1228
Saurav Das1a129a02016-11-18 15:21:57 -08001229 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1230 List<Deque<GroupKey>> allActiveKeys,
1231 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001232 VlanId assignedVlan) {
1233 // create the buckets to add to the outermost L3 Multicast group
1234 List<GroupBucket> newBuckets = Lists.newArrayList();
1235 groupInfos.forEach(groupInfo -> {
1236 // Points to L3 interface group if there is one.
1237 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -07001238 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
1239 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
1240 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1241 treatmentBuilder.group(new GroupId(nextGroupDesc.givenGroupId()));
1242 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
Yi Tseng78f51f42017-02-02 13:54:58 -08001243 newBuckets.add(newBucket);
1244 });
Saurav Das1a129a02016-11-18 15:21:57 -08001245
1246 // get the group being edited
1247 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1248 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001249 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001250 return;
1251 }
1252 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1253 int l3mcastGroupId = l3mcastGroup.id().id();
1254
1255 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001256 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001257 if (!expectedVlan.equals(assignedVlan)) {
1258 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1259 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1260 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001261 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001262 }
1263 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001264 new DefaultGroupDescription(deviceId,
1265 ALL,
1266 new GroupBuckets(newBuckets),
1267 l3mcastGroupKey,
1268 l3mcastGroupId,
1269 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001270 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001271 groupInfos.size(),
1272 true,
1273 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001274 groupInfos.forEach(groupInfo -> {
1275 // update original NextGroup with new bucket-chain
1276 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001277 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001278 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001279 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1280 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001281 }
1282 newBucketChain.addFirst(l3mcastGroupKey);
1283 // If active keys shows only the top-level group without a chain of groups,
1284 // then it represents an empty group. Update by replacing empty chain.
1285 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1286 allActiveKeys.clear();
1287 }
1288 allActiveKeys.add(newBucketChain);
1289
Yi Tsengef19de12017-04-24 11:33:05 -07001290 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001291 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001292 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1293 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1294 1,
1295 false,
1296 deviceId);
1297 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001298 }
Yi Tsengef19de12017-04-24 11:33:05 -07001299 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001300 deviceId, Integer.toHexString(l3mcastGroupId),
1301 l3mcastGroupKey, nextObj.id());
1302 // send the innermost group
1303 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001304 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001305 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001306 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001307
1308 });
1309
Saurav Das1a129a02016-11-18 15:21:57 -08001310 updatePendingNextObjective(l3mcastGroupKey,
1311 new OfdpaNextGroup(allActiveKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001312 }
1313
1314 /**
1315 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001316 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001317 * may be in use (referenced by other groups) elsewhere.
1318 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001319 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001320 * @param next the representation of the existing group-chain for this next objective
1321 */
1322 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001323 if (nextObjective.type() != NextObjective.Type.HASHED &&
1324 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001325 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1326 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001327 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001328 return;
1329 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001330 Set<PortNumber> portsToRemove = Sets.newHashSet();
Charles Chan188ebf52015-12-23 00:15:11 -08001331 Collection<TrafficTreatment> treatments = nextObjective.next();
Yi Tseng78f51f42017-02-02 13:54:58 -08001332 for (TrafficTreatment treatment : treatments) {
1333 // find the bucket to remove by noting the outport, and figuring out the
1334 // top-level group in the group-chain that indirectly references the port
1335 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1336 if (portToRemove == null) {
1337 log.warn("treatment {} of next objective {} has no outport.. cannot remove bucket"
1338 + "from group in dev: {}", treatment, nextObjective.id(), deviceId);
1339 } else {
1340 portsToRemove.add(portToRemove);
1341 }
1342 }
1343
1344 if (portsToRemove.isEmpty()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001345 log.warn("next objective {} has no outport.. cannot remove bucket"
Yi Tseng78f51f42017-02-02 13:54:58 -08001346 + "from group in dev: {}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001347 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001348 }
1349
Yi Tsengef19de12017-04-24 11:33:05 -07001350 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001351 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001352 for (Deque<GroupKey> gkeys : allActiveKeys) {
1353 // last group in group chain should have a single bucket pointing to port
Yi Tseng78f51f42017-02-02 13:54:58 -08001354
Charles Chan188ebf52015-12-23 00:15:11 -08001355 GroupKey groupWithPort = gkeys.peekLast();
1356 Group group = groupService.getGroup(deviceId, groupWithPort);
1357 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001358 log.warn("Inconsistent group chain found when removing bucket"
1359 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001360 continue;
1361 }
Yi Tseng91cf2d42017-04-10 17:05:37 -07001362 if (group.buckets().buckets().isEmpty()) {
1363 log.warn("Can't get output port information from group {} " +
1364 "because there is no bucket in the group.",
1365 group.id().toString());
1366 continue;
1367 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001368 PortNumber pout = readOutPortFromTreatment(
1369 group.buckets().buckets().get(0).treatment());
Yi Tseng78f51f42017-02-02 13:54:58 -08001370 if (portsToRemove.contains(pout)) {
1371 chainsToRemove.add(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001372 }
Charles Chan188ebf52015-12-23 00:15:11 -08001373 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001374 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001375 log.warn("Could not find appropriate group-chain for removing bucket"
1376 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001377 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001378 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001379 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001380 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1381 //first group key is the one we want to modify
1382 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001383 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001384 for (Deque<GroupKey> foundChain : chainsToRemove) {
1385 //second group key is the one we wish to remove the reference to
1386 if (foundChain.size() < 2) {
1387 // additional check to make sure second group key exist in
1388 // the chain.
1389 log.warn("Can't find second group key from chain {}",
1390 foundChain);
1391 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001392 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001393 GroupKey pointedGroupKey = foundChain.stream().collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001394 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1395
1396 if (pointedGroup == null) {
1397 continue;
1398 }
1399
1400 GroupBucket bucket;
1401 if (nextObjective.type() == NextObjective.Type.HASHED) {
1402 bucket = DefaultGroupBucket.createSelectGroupBucket(
1403 DefaultTrafficTreatment.builder()
1404 .group(pointedGroup.id())
1405 .build());
1406 } else {
1407 bucket = DefaultGroupBucket.createAllGroupBucket(
1408 DefaultTrafficTreatment.builder()
1409 .group(pointedGroup.id())
1410 .build());
1411 }
1412
1413 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001414 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001415
1416 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1417 List<String> pointedGroupIds; // for debug log
1418 pointedGroupIds = bucketsToRemove.stream()
1419 .map(GroupBucket::treatment)
1420 .map(TrafficTreatment::allInstructions)
1421 .flatMap(List::stream)
1422 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1423 .map(inst -> (Instructions.GroupInstruction) inst)
1424 .map(Instructions.GroupInstruction::groupId)
1425 .map(GroupId::id)
1426 .map(Integer::toHexString)
1427 .map(id -> HEX_PREFIX + id)
1428 .collect(Collectors.toList());
1429
Yi Tseng78f51f42017-02-02 13:54:58 -08001430 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001431 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001432 pointedGroupIds, nextObjective.id(), deviceId);
1433 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001434 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1435 removeBuckets, modGroupKey,
1436 nextObjective.appId());
1437 // update store
Yi Tsengeeb3dc12017-03-15 10:55:50 -07001438 allActiveKeys.removeAll(chainsToRemove);
1439 // If no buckets in the group, then retain an entry for the
1440 // top level group which still exists.
1441 if (allActiveKeys.isEmpty()) {
Saurav Das1a129a02016-11-18 15:21:57 -08001442 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1443 top.add(modGroupKey);
1444 allActiveKeys.add(top);
1445 }
Saurav Das1a129a02016-11-18 15:21:57 -08001446 flowObjectiveStore.putNextGroup(nextObjective.id(),
Yi Tsengef19de12017-04-24 11:33:05 -07001447 new OfdpaNextGroup(allActiveKeys, nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001448 }
1449
1450 /**
Saurav Das961beb22017-03-29 19:09:17 -07001451 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001452 *
1453 * @param nextObjective the next objective to remove
1454 * @param next the NextGroup that represents the existing group-chain for
1455 * this next objective
1456 */
1457 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Yi Tsengef19de12017-04-24 11:33:05 -07001458 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001459
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001460 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001461 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001462 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001463
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001464 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001465 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1466 flowObjectiveStore.removeNextGroup(nextObjective.id());
1467 }
1468
Yi Tsengef19de12017-04-24 11:33:05 -07001469 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1470 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001471 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001472 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001473 }
Yi Tsengef19de12017-04-24 11:33:05 -07001474 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001475 return val;
1476 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001477 }
1478
Yi Tsengef19de12017-04-24 11:33:05 -07001479 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1480 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001481 if (val == null) {
1482 val = Sets.newConcurrentHashSet();
1483 }
1484 val.add(gce);
1485 return val;
1486 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001487 }
1488
Yi Tsengef19de12017-04-24 11:33:05 -07001489 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1490 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001491 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1492 if (nextObjs != null) {
1493 nextObjs.add(nextObjective);
1494 } else {
1495 nextObjs = Sets.newHashSet(nextObjective);
1496 }
1497 return nextObjs;
1498 });
1499 }
1500
Yi Tsengef19de12017-04-24 11:33:05 -07001501 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1502 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1503 if (nextObjs != null) {
1504
1505 nextObjs.forEach(nextObj -> {
1506 log.debug("Group {} updated, update pending next objective {}.",
1507 groupKey, nextObj);
1508
1509 pass(nextObj);
1510 });
1511 }
1512 return Sets.newHashSet();
1513 });
1514 }
1515
1516 private void processPendingRemoveNextObjs(GroupKey key) {
1517 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1518 if (groupKeys.isEmpty()) {
1519 pendingRemoveNextObjectives.invalidate(nextObjective);
1520 pass(nextObjective);
1521 } else {
1522 groupKeys.remove(key);
1523 }
1524 });
1525 }
1526
1527 protected int getNextAvailableIndex() {
1528 return (int) nextIndex.incrementAndGet();
1529 }
1530
1531 protected Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1532 int nextid) {
1533 GroupKey topLevelGroupKey;
1534 if (!allActiveKeys.isEmpty()) {
1535 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1536 } else {
1537 log.warn("Could not determine top level group while processing"
1538 + "next:{} in dev:{}", nextid, deviceId);
1539 return null;
1540 }
1541 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1542 if (topGroup == null) {
1543 log.warn("Could not find top level group while processing "
1544 + "next:{} in dev:{}", nextid, deviceId);
1545 }
1546 return topGroup;
1547 }
1548
1549 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1550 //first check for group chain
1551 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1552 if (gceSet != null) {
1553 for (GroupChainElem gce : gceSet) {
1554 log.debug("Group service {} group key {} in device {}. "
1555 + "Processing next group in group chain with group id 0x{}",
1556 (added) ? "ADDED" : "processed",
1557 key, deviceId,
1558 Integer.toHexString(gce.groupDescription().givenGroupId()));
1559 processGroupChain(gce);
1560 }
1561 } else {
1562 // otherwise chain complete - check for waiting nextObjectives
1563 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1564 pendingAddNextObjectives.getIfPresent(key);
1565 if (nextGrpList != null) {
1566 pendingAddNextObjectives.invalidate(key);
1567 nextGrpList.forEach(nextGrp -> {
1568 log.debug("Group service {} group key {} in device:{}. "
1569 + "Done implementing next objective: {} <<-->> gid:0x{}",
1570 (added) ? "ADDED" : "processed",
1571 key, deviceId, nextGrp.nextObjective().id(),
1572 Integer.toHexString(groupService.getGroup(deviceId, key)
1573 .givenGroupId()));
1574 pass(nextGrp.nextObjective());
1575 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
1576
1577 // check if addBuckets waiting for this completion
1578 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1579 if (pendBkts != null) {
1580 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1581 }
1582 return null;
1583 });
1584 });
1585 }
1586 }
1587 }
1588
Charles Chan188ebf52015-12-23 00:15:11 -08001589 /**
1590 * Processes next element of a group chain. Assumption is that if this
1591 * group points to another group, the latter has already been created
1592 * and this driver has received notification for it. A second assumption is
1593 * that if there is another group waiting for this group then the appropriate
1594 * stores already have the information to act upon the notification for the
1595 * creation of this group.
1596 * <p>
1597 * The processing of the GroupChainElement depends on the number of groups
1598 * this element is waiting on. For all group types other than SIMPLE, a
1599 * GroupChainElement could be waiting on multiple groups.
1600 *
1601 * @param gce the group chain element to be processed next
1602 */
1603 private void processGroupChain(GroupChainElem gce) {
1604 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1605 if (waitOnGroups != 0) {
1606 log.debug("GCE: {} not ready to be processed", gce);
1607 return;
1608 }
1609 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001610 if (gce.addBucketToGroup()) {
1611 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1612 gce.groupDescription().appCookie(),
1613 gce.groupDescription().buckets(),
1614 gce.groupDescription().appCookie(),
1615 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001616 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001617 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001618 }
1619 }
1620
Yi Tsengef19de12017-04-24 11:33:05 -07001621 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1622 List<GroupKey> groupKeys) {
1623 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001624 }
1625
Saurav Das8be4e3a2016-03-11 17:19:07 -08001626 private class InnerGroupListener implements GroupListener {
1627 @Override
1628 public void event(GroupEvent event) {
1629 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001630 switch (event.type()) {
1631 case GROUP_ADDED:
1632 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1633 break;
1634 case GROUP_REMOVED:
1635 processPendingRemoveNextObjs(event.subject().appCookie());
1636 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001637 case GROUP_UPDATED:
1638 processPendingUpdateNextObjs(event.subject().appCookie());
1639 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001640 default:
1641 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001642 }
1643 }
1644 }
Charles Chan188ebf52015-12-23 00:15:11 -08001645}