Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.drivers.p4runtime; |
| 18 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 21 | import com.google.common.util.concurrent.Striped; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 22 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeActionProfileGroupMirror; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 23 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeActionProfileMemberMirror; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 24 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeMirror; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 25 | import org.onosproject.drivers.p4runtime.mirror.TimedEntry; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.group.DefaultGroup; |
| 28 | import org.onosproject.net.group.DefaultGroupDescription; |
| 29 | import org.onosproject.net.group.Group; |
| 30 | import org.onosproject.net.group.GroupDescription; |
| 31 | import org.onosproject.net.group.GroupOperation; |
| 32 | import org.onosproject.net.group.GroupOperations; |
| 33 | import org.onosproject.net.group.GroupProgrammable; |
| 34 | import org.onosproject.net.group.GroupStore; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 35 | import org.onosproject.net.pi.model.PiActionProfileModel; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 36 | import org.onosproject.net.pi.runtime.PiActionProfileGroup; |
| 37 | import org.onosproject.net.pi.runtime.PiActionProfileGroupHandle; |
| 38 | import org.onosproject.net.pi.runtime.PiActionProfileMember; |
| 39 | import org.onosproject.net.pi.runtime.PiActionProfileMemberHandle; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 40 | import org.onosproject.net.pi.runtime.PiEntity; |
| 41 | import org.onosproject.net.pi.runtime.PiHandle; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 42 | import org.onosproject.net.pi.service.PiGroupTranslator; |
| 43 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
| 44 | import org.onosproject.net.pi.service.PiTranslationException; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 45 | import org.onosproject.p4runtime.api.P4RuntimeReadClient; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 46 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 47 | |
| 48 | import java.util.Collection; |
| 49 | import java.util.Collections; |
| 50 | import java.util.List; |
| 51 | import java.util.Map; |
| 52 | import java.util.Objects; |
| 53 | import java.util.Optional; |
| 54 | import java.util.Set; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 55 | import java.util.concurrent.locks.Lock; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 56 | import java.util.stream.Collectors; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 57 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 58 | import static java.util.stream.Collectors.toMap; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 59 | import static java.util.stream.Collectors.toSet; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Implementation of GroupProgrammable to handle action profile groups in |
| 63 | * P4Runtime. |
| 64 | */ |
| 65 | public class P4RuntimeActionGroupProgrammable |
| 66 | extends AbstractP4RuntimeHandlerBehaviour |
| 67 | implements GroupProgrammable { |
| 68 | |
| 69 | // If true, we avoid querying the device and return what's already known by |
| 70 | // the ONOS store. |
| 71 | private static final String READ_ACTION_GROUPS_FROM_MIRROR = "actionGroupReadFromMirror"; |
| 72 | private static final boolean DEFAULT_READ_ACTION_GROUPS_FROM_MIRROR = false; |
| 73 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 74 | // Used to make sure concurrent calls to write groups are serialized so |
| 75 | // that each request gets consistent access to mirror state. |
| 76 | private static final Striped<Lock> WRITE_LOCKS = Striped.lock(30); |
| 77 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 78 | protected GroupStore groupStore; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 79 | private P4RuntimeActionProfileGroupMirror groupMirror; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 80 | private P4RuntimeActionProfileMemberMirror memberMirror; |
| 81 | private PiGroupTranslator groupTranslator; |
| 82 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 83 | @Override |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 84 | protected boolean setupBehaviour(String opName) { |
| 85 | if (!super.setupBehaviour(opName)) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 86 | return false; |
| 87 | } |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 88 | groupMirror = this.handler().get(P4RuntimeActionProfileGroupMirror.class); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 89 | memberMirror = this.handler().get(P4RuntimeActionProfileMemberMirror.class); |
| 90 | groupStore = handler().get(GroupStore.class); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 91 | groupTranslator = translationService.groupTranslator(); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void performGroupOperation(DeviceId deviceId, |
| 97 | GroupOperations groupOps) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 98 | if (!setupBehaviour("performGroupOperation()")) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
Carmelo Cascone | 5505a6d | 2019-04-17 20:03:24 -0700 | [diff] [blame] | 102 | groupOps.operations().forEach(op -> { |
| 103 | // ONOS-7785 We need the group app cookie (which includes |
| 104 | // the action profile ID) but this is not part of the |
| 105 | // GroupDescription. |
| 106 | Group groupOnStore = groupStore.getGroup(deviceId, op.groupId()); |
| 107 | if (groupOnStore == null) { |
| 108 | log.warn("Unable to find group {} in store, aborting {} operation [{}]", |
| 109 | op.groupId(), op.opType(), op); |
| 110 | return; |
| 111 | } |
| 112 | GroupDescription groupDesc = new DefaultGroupDescription( |
| 113 | deviceId, groupOnStore.type(), groupOnStore.buckets(), groupOnStore.appCookie(), |
| 114 | groupOnStore.id().id(), groupOnStore.appId()); |
| 115 | DefaultGroup groupToApply = new DefaultGroup(op.groupId(), groupDesc); |
| 116 | processPdGroup(groupToApply, op.opType()); |
| 117 | }); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Collection<Group> getGroups() { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 122 | if (!setupBehaviour("getGroups()")) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 123 | return Collections.emptyList(); |
| 124 | } |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 125 | |
| 126 | if (driverBoolProperty(READ_ACTION_GROUPS_FROM_MIRROR, |
| 127 | DEFAULT_READ_ACTION_GROUPS_FROM_MIRROR)) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 128 | return getGroupsFromMirror(); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 131 | // Dump groups and members from device for all action profiles. |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 132 | final P4RuntimeReadClient.ReadRequest request = client.read( |
| 133 | p4DeviceId, pipeconf); |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame] | 134 | |
| 135 | pipeconf.pipelineModel().actionProfiles().stream() |
| 136 | // Do not issue groups and members reads for one-shot tables. |
| 137 | // Those tables won't use separate groups and members, but the |
| 138 | // action profile elements are embedded in the table entry via |
| 139 | // action sets and weighted actions. |
| 140 | .filter(piActionProfileModel -> piActionProfileModel.tables().stream() |
| 141 | .map(tableId -> pipeconf.pipelineModel().table(tableId)) |
| 142 | .allMatch(piTableModel -> piTableModel.isPresent() && |
| 143 | !piTableModel.get().oneShotOnly())) |
| 144 | .map(PiActionProfileModel::id) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 145 | .forEach(id -> request.actionProfileGroups(id) |
| 146 | .actionProfileMembers(id)); |
| 147 | final P4RuntimeReadClient.ReadResponse response = request.submitSync(); |
| 148 | |
| 149 | if (!response.isSuccess()) { |
| 150 | // Error at client level. |
| 151 | return Collections.emptyList(); |
| 152 | } |
| 153 | |
| 154 | final Collection<PiActionProfileGroup> groupsOnDevice = response.all( |
| 155 | PiActionProfileGroup.class); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 156 | final Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice = |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 157 | response.all(PiActionProfileMember.class).stream() |
| 158 | .collect(toMap(m -> m.handle(deviceId), m -> m)); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 159 | |
| 160 | // Sync mirrors. |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 161 | groupMirror.sync(deviceId, groupsOnDevice); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 162 | memberMirror.sync(deviceId, membersOnDevice.values()); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 163 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 164 | // Retrieve the original PD group before translation. |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 165 | final List<Group> result = Lists.newArrayList(); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 166 | final List<PiActionProfileGroup> groupsToRemove = Lists.newArrayList(); |
| 167 | final Set<PiActionProfileMemberHandle> memberHandlesToKeep = Sets.newHashSet(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 168 | for (PiActionProfileGroup piGroup : groupsOnDevice) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 169 | final Group pdGroup = checkAndForgeGroupEntry(piGroup, membersOnDevice); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 170 | if (pdGroup == null) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 171 | // Entry is on device but is inconsistent with controller state. |
| 172 | // Mark for removal. |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 173 | groupsToRemove.add(piGroup); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 174 | } else { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 175 | result.add(pdGroup); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 176 | // Keep track of member handles used in groups. |
| 177 | piGroup.members().stream() |
| 178 | .map(m -> PiActionProfileMemberHandle.of( |
| 179 | deviceId, piGroup.actionProfile(), m.id())) |
| 180 | .forEach(memberHandlesToKeep::add); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 184 | // Trigger clean up of inconsistent groups and members (if any). Also |
| 185 | // take care of removing any orphan member, e.g. from a |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 186 | // partial/unsuccessful group insertion. |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 187 | final Set<PiActionProfileMemberHandle> memberHandlesToRemove = Sets.difference( |
| 188 | membersOnDevice.keySet(), memberHandlesToKeep); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 189 | final Set<PiActionProfileGroupHandle> groupHandlesToRemove = groupsToRemove |
| 190 | .stream().map(g -> g.handle(deviceId)).collect(toSet()); |
| 191 | if (groupHandlesToRemove.size() + memberHandlesToRemove.size() > 0) { |
| 192 | log.warn("Cleaning up {} action profile groups and " + |
| 193 | "{} members on {}...", |
| 194 | groupHandlesToRemove.size(), memberHandlesToRemove.size(), deviceId); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 195 | client.write(p4DeviceId, pipeconf) |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 196 | .delete(groupHandlesToRemove) |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 197 | .delete(memberHandlesToRemove) |
| 198 | .submit().whenComplete((r, ex) -> { |
| 199 | if (ex != null) { |
| 200 | log.error("Exception removing inconsistent group/members", ex); |
| 201 | } else { |
| 202 | log.debug("Completed removal of inconsistent " + |
| 203 | "groups/members ({} of {} updates succeeded)", |
| 204 | r.success().size(), r.all().size()); |
| 205 | groupMirror.applyWriteResponse(r); |
| 206 | memberMirror.applyWriteResponse(r); |
| 207 | } |
| 208 | }); |
| 209 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 210 | } |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 211 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 212 | // Done. |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 213 | return result; |
| 214 | } |
| 215 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 216 | private Collection<Group> getGroupsFromMirror() { |
| 217 | final Map<PiActionProfileMemberHandle, PiActionProfileMember> members = |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 218 | memberMirror.getAll(deviceId).stream() |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 219 | .map(TimedEntry::entry) |
| 220 | .collect(toMap(e -> e.handle(deviceId), e -> e)); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 221 | return groupMirror.getAll(deviceId).stream() |
| 222 | .map(TimedEntry::entry) |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 223 | .map(g -> checkAndForgeGroupEntry( |
| 224 | g, members)) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 225 | .filter(Objects::nonNull) |
| 226 | .collect(Collectors.toList()); |
| 227 | } |
| 228 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 229 | private Group checkAndForgeGroupEntry( |
| 230 | PiActionProfileGroup piGroupOnDevice, |
| 231 | Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice) { |
| 232 | final PiActionProfileGroupHandle handle = PiActionProfileGroupHandle.of( |
| 233 | deviceId, piGroupOnDevice); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 234 | final Optional<PiTranslatedEntity<Group, PiActionProfileGroup>> |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 235 | translatedEntity = groupTranslator.lookup(handle); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 236 | final TimedEntry<PiActionProfileGroup> mirrorEntry = groupMirror.get(handle); |
| 237 | // Check that entry obtained from device is consistent with what is known |
| 238 | // by the translation store. |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 239 | if (!translatedEntity.isPresent()) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 240 | log.warn("Group not found in translation store: {}", handle); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 241 | return null; |
| 242 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 243 | final PiActionProfileGroup piGroupFromStore = translatedEntity.get().translated(); |
| 244 | if (!piGroupFromStore.equals(piGroupOnDevice)) { |
| 245 | log.warn("Group on device {} is different from the one in " + |
| 246 | "translation store: {} [device={}, store={}]", |
| 247 | deviceId, handle, piGroupOnDevice, piGroupFromStore); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 248 | return null; |
| 249 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 250 | // Groups in P4Runtime contains only a reference to members. Check that |
| 251 | // the actual member instances in the translation store are the same |
| 252 | // found on the device. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 253 | if (!validateGroupMembers(piGroupFromStore, membersOnDevice)) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 254 | log.warn("Group on device {} refers to members that are different " + |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 255 | "than those found in translation store: {}", |
| 256 | deviceId, handle); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 257 | return null; |
| 258 | } |
| 259 | if (mirrorEntry == null) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 260 | log.warn("Group handle not found in device mirror: {}", handle); |
| 261 | return null; |
| 262 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 263 | // Check that members from device are the same as in the translated group. |
| 264 | return addedGroup(translatedEntity.get().original(), mirrorEntry.lifeSec()); |
| 265 | } |
| 266 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 267 | private boolean validateGroupMembers( |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 268 | PiActionProfileGroup piGroupFromStore, |
| 269 | Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice) { |
| 270 | final Collection<PiActionProfileMember> groupMembers = |
| 271 | extractAllMemberInstancesOrNull(piGroupFromStore); |
| 272 | if (groupMembers == null) { |
| 273 | return false; |
| 274 | } |
| 275 | return groupMembers.stream().allMatch( |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 276 | memberFromStore -> memberFromStore.equals(membersOnDevice.get( |
| 277 | memberFromStore.handle(deviceId)))); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private Group addedGroup(Group original, long life) { |
| 281 | final DefaultGroup forgedGroup = new DefaultGroup(original.id(), original); |
| 282 | forgedGroup.setState(Group.GroupState.ADDED); |
| 283 | forgedGroup.setLife(life); |
| 284 | return forgedGroup; |
| 285 | } |
| 286 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 287 | private void processPdGroup(Group pdGroup, GroupOperation.Type opType) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 288 | // Translate. |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 289 | final PiActionProfileGroup piGroup; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 290 | try { |
| 291 | piGroup = groupTranslator.translate(pdGroup, pipeconf); |
| 292 | } catch (PiTranslationException e) { |
| 293 | log.warn("Unable to translate group, aborting {} operation: {} [{}]", |
| 294 | opType, e.getMessage(), pdGroup); |
| 295 | return; |
| 296 | } |
| 297 | final Operation operation = opType.equals(GroupOperation.Type.DELETE) |
| 298 | ? Operation.REMOVE : Operation.APPLY; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 299 | final PiActionProfileGroupHandle handle = piGroup.handle(deviceId); |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 300 | // Update translation store. |
| 301 | if (operation.equals(Operation.APPLY)) { |
| 302 | groupTranslator.learn(handle, new PiTranslatedEntity<>( |
| 303 | pdGroup, piGroup, handle)); |
| 304 | } else { |
| 305 | groupTranslator.forget(handle); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 306 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 307 | // Submit write and forget about it. |
| 308 | asyncWritePiGroup(piGroup, handle, operation); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 311 | private void asyncWritePiGroup( |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 312 | PiActionProfileGroup group, |
| 313 | PiActionProfileGroupHandle groupHandle, |
| 314 | Operation operation) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 315 | // Generate and submit write request to write both members and groups. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 316 | final Collection<PiActionProfileMember> members = extractAllMemberInstancesOrNull(group); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 317 | if (members == null) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 318 | return; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 319 | } |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 320 | final WriteRequest request = client.write(p4DeviceId, pipeconf); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 321 | WRITE_LOCKS.get(deviceId).lock(); |
| 322 | try { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 323 | if (operation == Operation.APPLY) { |
| 324 | // First insert/update members, then group. |
| 325 | members.forEach(m -> appendEntityToWriteRequestOrSkip( |
| 326 | request, m.handle(deviceId), m, memberMirror, operation)); |
| 327 | appendEntityToWriteRequestOrSkip( |
| 328 | request, groupHandle, group, groupMirror, operation); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 329 | } else { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 330 | // First remove group, then members. |
| 331 | appendEntityToWriteRequestOrSkip( |
| 332 | request, groupHandle, group, groupMirror, operation); |
| 333 | members.forEach(m -> appendEntityToWriteRequestOrSkip( |
| 334 | request, m.handle(deviceId), m, memberMirror, operation)); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 335 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 336 | if (request.pendingUpdates().isEmpty()) { |
| 337 | // Nothing to do. |
| 338 | return; |
| 339 | } |
| 340 | // Optimistically update mirror before response arrives to make |
| 341 | // sure any write after this sees the expected mirror state. If |
| 342 | // anything goes wrong, mirror will be re-synced during |
| 343 | // reconciliation. |
| 344 | groupMirror.applyWriteRequest(request); |
| 345 | memberMirror.applyWriteRequest(request); |
| 346 | request.submit().whenComplete((r, ex) -> { |
| 347 | if (ex != null) { |
| 348 | log.error("Exception writing PI group to " + deviceId, ex); |
| 349 | } else { |
| 350 | log.debug("Completed write of PI group to {} " + |
| 351 | "({} of {} updates succeeded)", |
| 352 | deviceId, r.success().size(), r.all().size()); |
| 353 | } |
| 354 | }); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 355 | } finally { |
| 356 | WRITE_LOCKS.get(deviceId).unlock(); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 360 | private <H extends PiHandle, E extends PiEntity> void appendEntityToWriteRequestOrSkip( |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 361 | WriteRequest writeRequest, H handle, E entityToApply, |
| 362 | P4RuntimeMirror<H, E> mirror, Operation operation) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 363 | final TimedEntry<E> entityOnDevice = mirror.get(handle); |
| 364 | switch (operation) { |
| 365 | case APPLY: |
| 366 | if (entityOnDevice == null) { |
| 367 | writeRequest.insert(entityToApply); |
| 368 | } else if (entityToApply.equals(entityOnDevice.entry())) { |
| 369 | // Skip writing if group is unchanged. |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 370 | return; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 371 | } else { |
| 372 | writeRequest.modify(entityToApply); |
| 373 | } |
| 374 | break; |
| 375 | case REMOVE: |
| 376 | if (entityOnDevice == null) { |
| 377 | // Skip deleting if group does not exist on device. |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 378 | return; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 379 | } else { |
| 380 | writeRequest.delete(handle); |
| 381 | } |
| 382 | break; |
| 383 | default: |
| 384 | log.error("Unrecognized operation {}", operation); |
| 385 | break; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 386 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | private Collection<PiActionProfileMember> extractAllMemberInstancesOrNull( |
| 390 | PiActionProfileGroup group) { |
| 391 | final Collection<PiActionProfileMember> instances = group.members().stream() |
| 392 | .map(PiActionProfileGroup.WeightedMember::instance) |
| 393 | .filter(Objects::nonNull) |
| 394 | .collect(Collectors.toList()); |
| 395 | if (instances.size() != group.members().size()) { |
| 396 | log.error("PiActionProfileGroup has {} member references, " + |
| 397 | "but only {} instances were found", |
| 398 | group.members().size(), instances.size()); |
| 399 | return null; |
| 400 | } |
| 401 | return instances; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 404 | enum Operation { |
| 405 | APPLY, REMOVE |
| 406 | } |
| 407 | } |