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); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 134 | pipeconf.pipelineModel().actionProfiles() |
| 135 | .stream().map(PiActionProfileModel::id) |
| 136 | .forEach(id -> request.actionProfileGroups(id) |
| 137 | .actionProfileMembers(id)); |
| 138 | final P4RuntimeReadClient.ReadResponse response = request.submitSync(); |
| 139 | |
| 140 | if (!response.isSuccess()) { |
| 141 | // Error at client level. |
| 142 | return Collections.emptyList(); |
| 143 | } |
| 144 | |
| 145 | final Collection<PiActionProfileGroup> groupsOnDevice = response.all( |
| 146 | PiActionProfileGroup.class); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 147 | final Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice = |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 148 | response.all(PiActionProfileMember.class).stream() |
| 149 | .collect(toMap(m -> m.handle(deviceId), m -> m)); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 150 | |
| 151 | // Sync mirrors. |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 152 | groupMirror.sync(deviceId, groupsOnDevice); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 153 | memberMirror.sync(deviceId, membersOnDevice.values()); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 154 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 155 | // Retrieve the original PD group before translation. |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 156 | final List<Group> result = Lists.newArrayList(); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 157 | final List<PiActionProfileGroup> groupsToRemove = Lists.newArrayList(); |
| 158 | final Set<PiActionProfileMemberHandle> memberHandlesToKeep = Sets.newHashSet(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 159 | for (PiActionProfileGroup piGroup : groupsOnDevice) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 160 | final Group pdGroup = checkAndForgeGroupEntry(piGroup, membersOnDevice); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 161 | if (pdGroup == null) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 162 | // Entry is on device but is inconsistent with controller state. |
| 163 | // Mark for removal. |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 164 | groupsToRemove.add(piGroup); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 165 | } else { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 166 | result.add(pdGroup); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 167 | // Keep track of member handles used in groups. |
| 168 | piGroup.members().stream() |
| 169 | .map(m -> PiActionProfileMemberHandle.of( |
| 170 | deviceId, piGroup.actionProfile(), m.id())) |
| 171 | .forEach(memberHandlesToKeep::add); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 175 | // Trigger clean up of inconsistent groups and members (if any). Also |
| 176 | // take care of removing any orphan member, e.g. from a |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 177 | // partial/unsuccessful group insertion. |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 178 | final Set<PiActionProfileMemberHandle> memberHandlesToRemove = Sets.difference( |
| 179 | membersOnDevice.keySet(), memberHandlesToKeep); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 180 | final Set<PiActionProfileGroupHandle> groupHandlesToRemove = groupsToRemove |
| 181 | .stream().map(g -> g.handle(deviceId)).collect(toSet()); |
| 182 | if (groupHandlesToRemove.size() + memberHandlesToRemove.size() > 0) { |
| 183 | log.warn("Cleaning up {} action profile groups and " + |
| 184 | "{} members on {}...", |
| 185 | groupHandlesToRemove.size(), memberHandlesToRemove.size(), deviceId); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 186 | client.write(p4DeviceId, pipeconf) |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 187 | .delete(groupHandlesToRemove) |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 188 | .delete(memberHandlesToRemove) |
| 189 | .submit().whenComplete((r, ex) -> { |
| 190 | if (ex != null) { |
| 191 | log.error("Exception removing inconsistent group/members", ex); |
| 192 | } else { |
| 193 | log.debug("Completed removal of inconsistent " + |
| 194 | "groups/members ({} of {} updates succeeded)", |
| 195 | r.success().size(), r.all().size()); |
| 196 | groupMirror.applyWriteResponse(r); |
| 197 | memberMirror.applyWriteResponse(r); |
| 198 | } |
| 199 | }); |
| 200 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 201 | } |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 202 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 203 | // Done. |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 204 | return result; |
| 205 | } |
| 206 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 207 | private Collection<Group> getGroupsFromMirror() { |
| 208 | final Map<PiActionProfileMemberHandle, PiActionProfileMember> members = |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 209 | memberMirror.getAll(deviceId).stream() |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 210 | .map(TimedEntry::entry) |
| 211 | .collect(toMap(e -> e.handle(deviceId), e -> e)); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 212 | return groupMirror.getAll(deviceId).stream() |
| 213 | .map(TimedEntry::entry) |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 214 | .map(g -> checkAndForgeGroupEntry( |
| 215 | g, members)) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 216 | .filter(Objects::nonNull) |
| 217 | .collect(Collectors.toList()); |
| 218 | } |
| 219 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 220 | private Group checkAndForgeGroupEntry( |
| 221 | PiActionProfileGroup piGroupOnDevice, |
| 222 | Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice) { |
| 223 | final PiActionProfileGroupHandle handle = PiActionProfileGroupHandle.of( |
| 224 | deviceId, piGroupOnDevice); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 225 | final Optional<PiTranslatedEntity<Group, PiActionProfileGroup>> |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 226 | translatedEntity = groupTranslator.lookup(handle); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 227 | final TimedEntry<PiActionProfileGroup> mirrorEntry = groupMirror.get(handle); |
| 228 | // Check that entry obtained from device is consistent with what is known |
| 229 | // by the translation store. |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 230 | if (!translatedEntity.isPresent()) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 231 | log.warn("Group not found in translation store: {}", handle); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 232 | return null; |
| 233 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 234 | final PiActionProfileGroup piGroupFromStore = translatedEntity.get().translated(); |
| 235 | if (!piGroupFromStore.equals(piGroupOnDevice)) { |
| 236 | log.warn("Group on device {} is different from the one in " + |
| 237 | "translation store: {} [device={}, store={}]", |
| 238 | deviceId, handle, piGroupOnDevice, piGroupFromStore); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 239 | return null; |
| 240 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 241 | // Groups in P4Runtime contains only a reference to members. Check that |
| 242 | // the actual member instances in the translation store are the same |
| 243 | // found on the device. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 244 | if (!validateGroupMembers(piGroupFromStore, membersOnDevice)) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 245 | log.warn("Group on device {} refers to members that are different " + |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 246 | "than those found in translation store: {}", |
| 247 | deviceId, handle); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 248 | return null; |
| 249 | } |
| 250 | if (mirrorEntry == null) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 251 | log.warn("Group handle not found in device mirror: {}", handle); |
| 252 | return null; |
| 253 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 254 | // Check that members from device are the same as in the translated group. |
| 255 | return addedGroup(translatedEntity.get().original(), mirrorEntry.lifeSec()); |
| 256 | } |
| 257 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 258 | private boolean validateGroupMembers( |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 259 | PiActionProfileGroup piGroupFromStore, |
| 260 | Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice) { |
| 261 | final Collection<PiActionProfileMember> groupMembers = |
| 262 | extractAllMemberInstancesOrNull(piGroupFromStore); |
| 263 | if (groupMembers == null) { |
| 264 | return false; |
| 265 | } |
| 266 | return groupMembers.stream().allMatch( |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 267 | memberFromStore -> memberFromStore.equals(membersOnDevice.get( |
| 268 | memberFromStore.handle(deviceId)))); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | private Group addedGroup(Group original, long life) { |
| 272 | final DefaultGroup forgedGroup = new DefaultGroup(original.id(), original); |
| 273 | forgedGroup.setState(Group.GroupState.ADDED); |
| 274 | forgedGroup.setLife(life); |
| 275 | return forgedGroup; |
| 276 | } |
| 277 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 278 | private void processPdGroup(Group pdGroup, GroupOperation.Type opType) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 279 | // Translate. |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 280 | final PiActionProfileGroup piGroup; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 281 | try { |
| 282 | piGroup = groupTranslator.translate(pdGroup, pipeconf); |
| 283 | } catch (PiTranslationException e) { |
| 284 | log.warn("Unable to translate group, aborting {} operation: {} [{}]", |
| 285 | opType, e.getMessage(), pdGroup); |
| 286 | return; |
| 287 | } |
| 288 | final Operation operation = opType.equals(GroupOperation.Type.DELETE) |
| 289 | ? Operation.REMOVE : Operation.APPLY; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 290 | final PiActionProfileGroupHandle handle = piGroup.handle(deviceId); |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 291 | // Update translation store. |
| 292 | if (operation.equals(Operation.APPLY)) { |
| 293 | groupTranslator.learn(handle, new PiTranslatedEntity<>( |
| 294 | pdGroup, piGroup, handle)); |
| 295 | } else { |
| 296 | groupTranslator.forget(handle); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 297 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 298 | // Submit write and forget about it. |
| 299 | asyncWritePiGroup(piGroup, handle, operation); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 302 | private void asyncWritePiGroup( |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 303 | PiActionProfileGroup group, |
| 304 | PiActionProfileGroupHandle groupHandle, |
| 305 | Operation operation) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 306 | // Generate and submit write request to write both members and groups. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 307 | final Collection<PiActionProfileMember> members = extractAllMemberInstancesOrNull(group); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 308 | if (members == null) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 309 | return; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 310 | } |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 311 | final WriteRequest request = client.write(p4DeviceId, pipeconf); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 312 | WRITE_LOCKS.get(deviceId).lock(); |
| 313 | try { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 314 | if (operation == Operation.APPLY) { |
| 315 | // First insert/update members, then group. |
| 316 | members.forEach(m -> appendEntityToWriteRequestOrSkip( |
| 317 | request, m.handle(deviceId), m, memberMirror, operation)); |
| 318 | appendEntityToWriteRequestOrSkip( |
| 319 | request, groupHandle, group, groupMirror, operation); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 320 | } else { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 321 | // First remove group, then members. |
| 322 | appendEntityToWriteRequestOrSkip( |
| 323 | request, groupHandle, group, groupMirror, operation); |
| 324 | members.forEach(m -> appendEntityToWriteRequestOrSkip( |
| 325 | request, m.handle(deviceId), m, memberMirror, operation)); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 326 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 327 | if (request.pendingUpdates().isEmpty()) { |
| 328 | // Nothing to do. |
| 329 | return; |
| 330 | } |
| 331 | // Optimistically update mirror before response arrives to make |
| 332 | // sure any write after this sees the expected mirror state. If |
| 333 | // anything goes wrong, mirror will be re-synced during |
| 334 | // reconciliation. |
| 335 | groupMirror.applyWriteRequest(request); |
| 336 | memberMirror.applyWriteRequest(request); |
| 337 | request.submit().whenComplete((r, ex) -> { |
| 338 | if (ex != null) { |
| 339 | log.error("Exception writing PI group to " + deviceId, ex); |
| 340 | } else { |
| 341 | log.debug("Completed write of PI group to {} " + |
| 342 | "({} of {} updates succeeded)", |
| 343 | deviceId, r.success().size(), r.all().size()); |
| 344 | } |
| 345 | }); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 346 | } finally { |
| 347 | WRITE_LOCKS.get(deviceId).unlock(); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 351 | private <H extends PiHandle, E extends PiEntity> void appendEntityToWriteRequestOrSkip( |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 352 | WriteRequest writeRequest, H handle, E entityToApply, |
| 353 | P4RuntimeMirror<H, E> mirror, Operation operation) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 354 | final TimedEntry<E> entityOnDevice = mirror.get(handle); |
| 355 | switch (operation) { |
| 356 | case APPLY: |
| 357 | if (entityOnDevice == null) { |
| 358 | writeRequest.insert(entityToApply); |
| 359 | } else if (entityToApply.equals(entityOnDevice.entry())) { |
| 360 | // Skip writing if group is unchanged. |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 361 | return; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 362 | } else { |
| 363 | writeRequest.modify(entityToApply); |
| 364 | } |
| 365 | break; |
| 366 | case REMOVE: |
| 367 | if (entityOnDevice == null) { |
| 368 | // Skip deleting if group does not exist on device. |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 369 | return; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 370 | } else { |
| 371 | writeRequest.delete(handle); |
| 372 | } |
| 373 | break; |
| 374 | default: |
| 375 | log.error("Unrecognized operation {}", operation); |
| 376 | break; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 377 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | private Collection<PiActionProfileMember> extractAllMemberInstancesOrNull( |
| 381 | PiActionProfileGroup group) { |
| 382 | final Collection<PiActionProfileMember> instances = group.members().stream() |
| 383 | .map(PiActionProfileGroup.WeightedMember::instance) |
| 384 | .filter(Objects::nonNull) |
| 385 | .collect(Collectors.toList()); |
| 386 | if (instances.size() != group.members().size()) { |
| 387 | log.error("PiActionProfileGroup has {} member references, " + |
| 388 | "but only {} instances were found", |
| 389 | group.members().size(), instances.size()); |
| 390 | return null; |
| 391 | } |
| 392 | return instances; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 395 | enum Operation { |
| 396 | APPLY, REMOVE |
| 397 | } |
| 398 | } |