Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 1 | /* |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 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 | */ |
| 16 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 17 | package org.onosproject.drivers.p4runtime; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 18 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 23 | import org.onosproject.net.group.Group; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 24 | import org.onosproject.net.group.GroupOperation; |
| 25 | import org.onosproject.net.group.GroupOperations; |
| 26 | import org.onosproject.net.group.GroupProgrammable; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 27 | import org.slf4j.Logger; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 28 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 29 | import java.util.Collection; |
| 30 | import java.util.Collections; |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 31 | import java.util.List; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 32 | |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkArgument; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 37 | * Implementation of GroupProgrammable for P4Runtime devices that uses two |
| 38 | * different implementation of the same behavior to handle both action profile |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 39 | * groups and PRE entries. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 40 | */ |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 41 | public class P4RuntimeGroupProgrammable |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 42 | extends AbstractHandlerBehaviour implements GroupProgrammable { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 43 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 44 | private final Logger log = getLogger(this.getClass()); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 45 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 46 | private void doPerformGroupOperation(DeviceId deviceId, GroupOperations groupOps) { |
| 47 | // TODO: fix GroupProgrammable API, passing the device ID is ambiguous |
| 48 | checkArgument(deviceId.equals(data().deviceId()), |
| 49 | "passed deviceId must be the same assigned to this behavior"); |
| 50 | final List<GroupOperation> actionGroups = Lists.newArrayList(); |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 51 | final List<GroupOperation> preGroups = Lists.newArrayList(); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 52 | groupOps.operations().forEach(op -> { |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 53 | switch (op.groupType()) { |
| 54 | case SELECT: |
| 55 | actionGroups.add(op); |
| 56 | break; |
| 57 | case ALL: |
| 58 | case CLONE: |
| 59 | preGroups.add(op); |
| 60 | break; |
| 61 | case FAILOVER: |
| 62 | case INDIRECT: |
| 63 | default: |
| 64 | log.warn("{} group type not supported [{}]", op.groupType(), op); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 65 | } |
| 66 | }); |
| 67 | if (!actionGroups.isEmpty()) { |
| 68 | actionProgrammable().performGroupOperation( |
| 69 | deviceId, new GroupOperations(actionGroups)); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 70 | } |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 71 | if (!preGroups.isEmpty()) { |
| 72 | replicationProgrammable().performGroupOperation( |
| 73 | deviceId, new GroupOperations(preGroups)); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | private Collection<Group> doGetGroups() { |
| 78 | return new ImmutableList.Builder<Group>() |
| 79 | .addAll(actionProgrammable().getGroups()) |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 80 | .addAll(replicationProgrammable().getGroups()) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 81 | .build(); |
| 82 | } |
| 83 | |
| 84 | private P4RuntimeActionGroupProgrammable actionProgrammable() { |
| 85 | P4RuntimeActionGroupProgrammable prog = new P4RuntimeActionGroupProgrammable(); |
| 86 | prog.setData(data()); |
| 87 | prog.setHandler(handler()); |
| 88 | return prog; |
| 89 | } |
| 90 | |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 91 | private P4RuntimeReplicationGroupProgrammable replicationProgrammable() { |
| 92 | P4RuntimeReplicationGroupProgrammable prog = new P4RuntimeReplicationGroupProgrammable(); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 93 | prog.setData(data()); |
| 94 | prog.setHandler(handler()); |
| 95 | return prog; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 99 | public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) { |
| 100 | try { |
| 101 | doPerformGroupOperation(deviceId, groupOps); |
| 102 | } catch (Throwable ex) { |
| 103 | log.error("Unhandled exception on performGroupOperation", ex); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 104 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public Collection<Group> getGroups() { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 109 | try { |
| 110 | return doGetGroups(); |
| 111 | } catch (Throwable ex) { |
| 112 | log.error("Unhandled exception on getGroups", ex); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 113 | return Collections.emptyList(); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 114 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 115 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 116 | } |