Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-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 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 20 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeGroupMirror; |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 21 | import org.onosproject.drivers.p4runtime.mirror.TimedEntry; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 23 | import org.onosproject.net.group.DefaultGroup; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 24 | import org.onosproject.net.group.Group; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 25 | import org.onosproject.net.group.GroupOperation; |
| 26 | import org.onosproject.net.group.GroupOperations; |
| 27 | import org.onosproject.net.group.GroupProgrammable; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 28 | import org.onosproject.net.group.GroupStore; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 29 | import org.onosproject.net.pi.model.PiActionProfileId; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiActionProfileModel; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 31 | import org.onosproject.net.pi.runtime.PiActionGroup; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 32 | import org.onosproject.net.pi.runtime.PiActionGroupHandle; |
| 33 | import org.onosproject.net.pi.service.PiGroupTranslator; |
| 34 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 35 | import org.onosproject.net.pi.service.PiTranslationException; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 37 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 38 | import java.util.Collection; |
| 39 | import java.util.Collections; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 40 | import java.util.Map; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 41 | import java.util.Objects; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 42 | import java.util.concurrent.CompletableFuture; |
| 43 | import java.util.concurrent.ExecutionException; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 44 | import java.util.concurrent.locks.Lock; |
| 45 | import java.util.concurrent.locks.ReentrantLock; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 46 | import java.util.stream.Collectors; |
| 47 | import java.util.stream.Stream; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 48 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 49 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE; |
| 50 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
| 53 | /** |
| 54 | * Implementation of the group programmable behaviour for P4Runtime. |
| 55 | */ |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 56 | public class P4RuntimeGroupProgrammable |
| 57 | extends AbstractP4RuntimeHandlerBehaviour |
| 58 | implements GroupProgrammable { |
| 59 | |
| 60 | private enum Operation { |
| 61 | APPLY, REMOVE |
| 62 | } |
| 63 | |
| 64 | private static final String ACT_GRP_MEMS_STR = "action group members"; |
| 65 | private static final String DELETE_STR = "delete"; |
| 66 | private static final String ACT_GRP_STR = "action group"; |
| 67 | private static final String INSERT_STR = "insert"; |
| 68 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 69 | private static final Logger log = getLogger(P4RuntimeGroupProgrammable.class); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 70 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 71 | // If true, we ignore re-installing groups that are already known in the |
| 72 | // device mirror. |
| 73 | private boolean checkMirrorBeforeUpdate = true; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 74 | |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 75 | // If true, we avoid querying the device and return what's already known by |
| 76 | // the ONOS store. |
| 77 | private boolean ignoreDeviceWhenGet = true; |
| 78 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 79 | private GroupStore groupStore; |
| 80 | private P4RuntimeGroupMirror groupMirror; |
| 81 | private PiGroupTranslator translator; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 82 | |
| 83 | // Needed to synchronize operations over the same group. |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 84 | private static final Map<PiActionGroupHandle, Lock> GROUP_LOCKS = |
| 85 | Maps.newConcurrentMap(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 86 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 87 | @Override |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 88 | protected boolean setupBehaviour() { |
| 89 | if (!super.setupBehaviour()) { |
| 90 | return false; |
| 91 | } |
| 92 | groupMirror = this.handler().get(P4RuntimeGroupMirror.class); |
| 93 | groupStore = handler().get(GroupStore.class); |
| 94 | translator = piTranslationService.groupTranslator(); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void performGroupOperation(DeviceId deviceId, |
| 100 | GroupOperations groupOps) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 101 | if (!setupBehaviour()) { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 102 | return; |
| 103 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 104 | groupOps.operations().forEach(op -> processGroupOp(deviceId, op)); |
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 | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 109 | if (!setupBehaviour()) { |
| 110 | return Collections.emptyList(); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 111 | } |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 112 | if (!ignoreDeviceWhenGet) { |
| 113 | return pipeconf.pipelineModel().actionProfiles().stream() |
| 114 | .map(PiActionProfileModel::id) |
| 115 | .flatMap(this::streamGroupsFromDevice) |
| 116 | .collect(Collectors.toList()); |
| 117 | } else { |
| 118 | return groupMirror.getAll(deviceId).stream() |
| 119 | .map(TimedEntry::entry) |
| 120 | .map(this::forgeGroupEntry) |
| 121 | .collect(Collectors.toList()); |
| 122 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 123 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 124 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 125 | private void processGroupOp(DeviceId deviceId, GroupOperation groupOp) { |
| 126 | final Group pdGroup = groupStore.getGroup(deviceId, groupOp.groupId()); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 127 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 128 | final PiActionGroup piGroup; |
| 129 | try { |
| 130 | piGroup = translator.translate(pdGroup, pipeconf); |
| 131 | } catch (PiTranslationException e) { |
| 132 | log.warn("Unable translate group, aborting {} operation: {}", |
| 133 | groupOp.opType(), e.getMessage()); |
| 134 | return; |
| 135 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 136 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 137 | final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup); |
| 138 | |
| 139 | final PiActionGroup groupOnDevice = groupMirror.get(handle) == null |
| 140 | ? null |
| 141 | : groupMirror.get(handle).entry(); |
| 142 | |
| 143 | final Lock lock = GROUP_LOCKS.computeIfAbsent(handle, k -> new ReentrantLock()); |
| 144 | lock.lock(); |
| 145 | try { |
| 146 | final Operation operation; |
| 147 | switch (groupOp.opType()) { |
| 148 | case ADD: |
| 149 | case MODIFY: |
| 150 | operation = Operation.APPLY; |
| 151 | break; |
| 152 | case DELETE: |
| 153 | operation = Operation.REMOVE; |
| 154 | break; |
| 155 | default: |
| 156 | log.warn("Group operation {} not supported", groupOp.opType()); |
| 157 | return; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 158 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 159 | processPiGroup(handle, piGroup, |
| 160 | groupOnDevice, pdGroup, operation); |
| 161 | } finally { |
| 162 | lock.unlock(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 166 | private void processPiGroup(PiActionGroupHandle handle, |
| 167 | PiActionGroup groupToApply, |
| 168 | PiActionGroup groupOnDevice, |
| 169 | Group pdGroup, Operation operation) { |
| 170 | if (operation == Operation.APPLY) { |
| 171 | if (groupOnDevice != null) { |
| 172 | if (checkMirrorBeforeUpdate |
| 173 | && groupOnDevice.equals(groupToApply)) { |
| 174 | // Group on device has the same members, ignore operation. |
| 175 | return; |
| 176 | } |
| 177 | // Remove before adding it. |
| 178 | processPiGroup(handle, groupToApply, groupOnDevice, |
| 179 | pdGroup, Operation.REMOVE); |
| 180 | } |
| 181 | if (writeGroupToDevice(groupToApply)) { |
| 182 | groupMirror.put(handle, groupToApply); |
| 183 | translator.learn(handle, new PiTranslatedEntity<>( |
| 184 | pdGroup, groupToApply, handle)); |
| 185 | } |
| 186 | } else { |
| 187 | if (deleteGroupFromDevice(groupToApply)) { |
| 188 | groupMirror.remove(handle); |
| 189 | translator.forget(handle); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private boolean writeGroupToDevice(PiActionGroup groupToApply) { |
| 195 | // First insert members, then group. |
| 196 | // The operation is deemed successful if both operations are successful. |
| 197 | // FIXME: add transactional semantics, i.e. remove members if group fails. |
| 198 | final boolean membersSuccess = completeFuture( |
| 199 | client.writeActionGroupMembers(groupToApply, INSERT, pipeconf), |
| 200 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 201 | return membersSuccess && completeFuture( |
| 202 | client.writeActionGroup(groupToApply, INSERT, pipeconf), |
| 203 | ACT_GRP_STR, INSERT_STR); |
| 204 | } |
| 205 | |
| 206 | private boolean deleteGroupFromDevice(PiActionGroup piActionGroup) { |
| 207 | // First delete group, then members. |
| 208 | // The operation is deemed successful if both operations are successful. |
| 209 | final boolean groupSuccess = completeFuture( |
| 210 | client.writeActionGroup(piActionGroup, DELETE, pipeconf), |
| 211 | ACT_GRP_STR, DELETE_STR); |
| 212 | return groupSuccess && completeFuture( |
| 213 | client.writeActionGroupMembers(piActionGroup, DELETE, pipeconf), |
| 214 | ACT_GRP_MEMS_STR, DELETE_STR); |
| 215 | } |
| 216 | |
| 217 | private boolean completeFuture(CompletableFuture<Boolean> completableFuture, |
| 218 | String topic, String action) { |
| 219 | try { |
| 220 | if (completableFuture.get()) { |
| 221 | return true; |
| 222 | } else { |
| 223 | log.warn("Unable to {} {}", action, topic); |
| 224 | return false; |
| 225 | } |
| 226 | } catch (InterruptedException | ExecutionException e) { |
| 227 | log.warn("Exception while performing {} {}: {}", action, topic, e.getMessage()); |
| 228 | log.debug("Exception", e); |
| 229 | return false; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private Stream<Group> streamGroupsFromDevice(PiActionProfileId actProfId) { |
| 234 | try { |
| 235 | // Read PI groups and return original PD one. |
| 236 | return client.dumpGroups(actProfId, pipeconf).get().stream() |
| 237 | .map(this::forgeGroupEntry) |
| 238 | .filter(Objects::nonNull); |
| 239 | } catch (ExecutionException | InterruptedException e) { |
| 240 | log.error("Exception while dumping groups from action profile '{}' on {}: {}", |
| 241 | actProfId.id(), deviceId, e); |
| 242 | return Stream.empty(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | private Group forgeGroupEntry(PiActionGroup piGroup) { |
| 247 | final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup); |
| 248 | if (!translator.lookup(handle).isPresent()) { |
| 249 | log.warn("Missing PI group from translation store: {} - {}:{}", |
| 250 | pipeconf.id(), piGroup.actionProfileId(), |
| 251 | piGroup.id()); |
| 252 | return null; |
| 253 | } |
| 254 | final long life = groupMirror.get(handle) != null |
| 255 | ? groupMirror.get(handle).lifeSec() : 0; |
| 256 | final Group original = translator.lookup(handle).get().original(); |
| 257 | final DefaultGroup forgedGroup = new DefaultGroup(original.id(), original); |
| 258 | forgedGroup.setState(Group.GroupState.ADDED); |
| 259 | forgedGroup.setLife(life); |
| 260 | return forgedGroup; |
| 261 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 262 | } |