blob: 84678dccd9186e2d518c3db811a1b2bb82705d9b [file] [log] [blame]
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04001/*
Carmelo Casconee44592f2018-09-12 02:24:47 -07002 * Copyright 2018-present Open Networking Foundation
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04003 *
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 */
16
Andrea Campanella0288c872017-08-07 18:32:51 +020017package org.onosproject.drivers.p4runtime;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040018
Carmelo Cascone58136812018-07-19 03:40:16 +020019import com.google.common.collect.ImmutableList;
Carmelo Cascone26600972018-09-10 00:23:20 -070020import com.google.common.collect.Lists;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040021import org.onosproject.net.DeviceId;
Carmelo Casconee44592f2018-09-12 02:24:47 -070022import org.onosproject.net.driver.AbstractHandlerBehaviour;
Yi Tseng82512da2017-08-16 19:46:36 -070023import org.onosproject.net.group.Group;
Carmelo Cascone58136812018-07-19 03:40:16 +020024import org.onosproject.net.group.GroupDescription;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040025import org.onosproject.net.group.GroupOperation;
26import org.onosproject.net.group.GroupOperations;
27import org.onosproject.net.group.GroupProgrammable;
Yi Tseng82512da2017-08-16 19:46:36 -070028import org.slf4j.Logger;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040029
Yi Tseng82512da2017-08-16 19:46:36 -070030import java.util.Collection;
31import java.util.Collections;
Carmelo Cascone26600972018-09-10 00:23:20 -070032import java.util.List;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040033
Carmelo Cascone26600972018-09-10 00:23:20 -070034import static com.google.common.base.Preconditions.checkArgument;
Yi Tseng82512da2017-08-16 19:46:36 -070035import static org.slf4j.LoggerFactory.getLogger;
36
37/**
Carmelo Casconee44592f2018-09-12 02:24:47 -070038 * Implementation of GroupProgrammable for P4Runtime devices that uses two
39 * different implementation of the same behavior to handle both action profile
40 * groups and multicast groups.
Yi Tseng82512da2017-08-16 19:46:36 -070041 */
Carmelo Casconee75b7942017-11-21 17:14:49 -080042public class P4RuntimeGroupProgrammable
Carmelo Casconee44592f2018-09-12 02:24:47 -070043 extends AbstractHandlerBehaviour implements GroupProgrammable {
Carmelo Casconee75b7942017-11-21 17:14:49 -080044
Carmelo Casconee44592f2018-09-12 02:24:47 -070045 private final Logger log = getLogger(this.getClass());
Carmelo Casconee75b7942017-11-21 17:14:49 -080046
Carmelo Casconee44592f2018-09-12 02:24:47 -070047 private void doPerformGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
48 // TODO: fix GroupProgrammable API, passing the device ID is ambiguous
49 checkArgument(deviceId.equals(data().deviceId()),
50 "passed deviceId must be the same assigned to this behavior");
51 final List<GroupOperation> actionGroups = Lists.newArrayList();
52 final List<GroupOperation> multicastGroups = Lists.newArrayList();
53 groupOps.operations().forEach(op -> {
54 if (op.groupType().equals(GroupDescription.Type.ALL)) {
55 multicastGroups.add(op);
56 } else {
57 actionGroups.add(op);
58 }
59 });
60 if (!actionGroups.isEmpty()) {
61 actionProgrammable().performGroupOperation(
62 deviceId, new GroupOperations(actionGroups));
Carmelo Casconee75b7942017-11-21 17:14:49 -080063 }
Carmelo Casconee44592f2018-09-12 02:24:47 -070064 if (!multicastGroups.isEmpty()) {
65 multicastProgrammable().performGroupOperation(
66 deviceId, new GroupOperations(multicastGroups));
67 }
68 }
69
70 private Collection<Group> doGetGroups() {
71 return new ImmutableList.Builder<Group>()
72 .addAll(actionProgrammable().getGroups())
73 .addAll(multicastProgrammable().getGroups())
74 .build();
75 }
76
77 private P4RuntimeActionGroupProgrammable actionProgrammable() {
78 P4RuntimeActionGroupProgrammable prog = new P4RuntimeActionGroupProgrammable();
79 prog.setData(data());
80 prog.setHandler(handler());
81 return prog;
82 }
83
84 private P4RuntimeMulticastGroupProgrammable multicastProgrammable() {
85 P4RuntimeMulticastGroupProgrammable prog = new P4RuntimeMulticastGroupProgrammable();
86 prog.setData(data());
87 prog.setHandler(handler());
88 return prog;
Carmelo Casconee75b7942017-11-21 17:14:49 -080089 }
90
91 @Override
Carmelo Casconee44592f2018-09-12 02:24:47 -070092 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
93 try {
94 doPerformGroupOperation(deviceId, groupOps);
95 } catch (Throwable ex) {
96 log.error("Unhandled exception on performGroupOperation", ex);
Yi Tseng82512da2017-08-16 19:46:36 -070097 }
Yi Tseng82512da2017-08-16 19:46:36 -070098 }
99
Yi Tseng82512da2017-08-16 19:46:36 -0700100 @Override
101 public Collection<Group> getGroups() {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700102 try {
103 return doGetGroups();
104 } catch (Throwable ex) {
105 log.error("Unhandled exception on getGroups", ex);
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200106 return Collections.emptyList();
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400107 }
Carmelo Casconee75b7942017-11-21 17:14:49 -0800108 }
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400109}