blob: d14b917425a369029053b6fbd2365af2dd251ea2 [file] [log] [blame]
Carmelo Casconee44592f2018-09-12 02:24:47 -07001/*
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
17package org.onosproject.drivers.p4runtime;
18
Carmelo Casconee44592f2018-09-12 02:24:47 -070019import com.google.common.collect.Lists;
Carmelo Casconee44592f2018-09-12 02:24:47 -070020import com.google.common.collect.Sets;
Carmelo Cascone61469462019-03-05 23:59:11 -080021import com.google.common.util.concurrent.Striped;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070022import org.onosproject.drivers.p4runtime.mirror.P4RuntimeActionProfileGroupMirror;
Carmelo Casconee44592f2018-09-12 02:24:47 -070023import org.onosproject.drivers.p4runtime.mirror.P4RuntimeActionProfileMemberMirror;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080024import org.onosproject.drivers.p4runtime.mirror.P4RuntimeMirror;
Carmelo Casconee44592f2018-09-12 02:24:47 -070025import org.onosproject.drivers.p4runtime.mirror.TimedEntry;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.group.DefaultGroup;
28import org.onosproject.net.group.DefaultGroupDescription;
29import org.onosproject.net.group.Group;
30import org.onosproject.net.group.GroupDescription;
31import org.onosproject.net.group.GroupOperation;
32import org.onosproject.net.group.GroupOperations;
33import org.onosproject.net.group.GroupProgrammable;
34import org.onosproject.net.group.GroupStore;
Carmelo Casconee44592f2018-09-12 02:24:47 -070035import org.onosproject.net.pi.model.PiActionProfileModel;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070036import org.onosproject.net.pi.runtime.PiActionProfileGroup;
37import org.onosproject.net.pi.runtime.PiActionProfileGroupHandle;
38import org.onosproject.net.pi.runtime.PiActionProfileMember;
39import org.onosproject.net.pi.runtime.PiActionProfileMemberHandle;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080040import org.onosproject.net.pi.runtime.PiEntity;
41import org.onosproject.net.pi.runtime.PiHandle;
Carmelo Casconee44592f2018-09-12 02:24:47 -070042import org.onosproject.net.pi.service.PiGroupTranslator;
43import org.onosproject.net.pi.service.PiTranslatedEntity;
44import org.onosproject.net.pi.service.PiTranslationException;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080045import org.onosproject.p4runtime.api.P4RuntimeReadClient;
Carmelo Cascone61469462019-03-05 23:59:11 -080046import org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest;
Carmelo Casconee44592f2018-09-12 02:24:47 -070047
48import java.util.Collection;
49import java.util.Collections;
50import java.util.List;
51import java.util.Map;
52import java.util.Objects;
53import java.util.Optional;
54import java.util.Set;
Carmelo Cascone61469462019-03-05 23:59:11 -080055import java.util.concurrent.locks.Lock;
Carmelo Casconee44592f2018-09-12 02:24:47 -070056import java.util.stream.Collectors;
Carmelo Casconee44592f2018-09-12 02:24:47 -070057
Carmelo Cascone99c59db2019-01-17 15:39:35 -080058import static java.util.stream.Collectors.toMap;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080059import static java.util.stream.Collectors.toSet;
Carmelo Casconee44592f2018-09-12 02:24:47 -070060
61/**
62 * Implementation of GroupProgrammable to handle action profile groups in
63 * P4Runtime.
64 */
65public 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 Cascone61469462019-03-05 23:59:11 -080074 // 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 Casconee44592f2018-09-12 02:24:47 -070078 protected GroupStore groupStore;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070079 private P4RuntimeActionProfileGroupMirror groupMirror;
Carmelo Casconee44592f2018-09-12 02:24:47 -070080 private P4RuntimeActionProfileMemberMirror memberMirror;
81 private PiGroupTranslator groupTranslator;
82
Carmelo Casconee44592f2018-09-12 02:24:47 -070083 @Override
Carmelo Casconec32976e2019-04-08 14:50:52 -070084 protected boolean setupBehaviour(String opName) {
85 if (!super.setupBehaviour(opName)) {
Carmelo Casconee44592f2018-09-12 02:24:47 -070086 return false;
87 }
Carmelo Casconecb4327a2018-09-11 15:17:23 -070088 groupMirror = this.handler().get(P4RuntimeActionProfileGroupMirror.class);
Carmelo Casconee44592f2018-09-12 02:24:47 -070089 memberMirror = this.handler().get(P4RuntimeActionProfileMemberMirror.class);
90 groupStore = handler().get(GroupStore.class);
Yi Tsengd7716482018-10-31 15:34:30 -070091 groupTranslator = translationService.groupTranslator();
Carmelo Casconee44592f2018-09-12 02:24:47 -070092 return true;
93 }
94
95 @Override
96 public void performGroupOperation(DeviceId deviceId,
97 GroupOperations groupOps) {
Carmelo Casconec32976e2019-04-08 14:50:52 -070098 if (!setupBehaviour("performGroupOperation()")) {
Carmelo Casconee44592f2018-09-12 02:24:47 -070099 return;
100 }
101
102 groupOps.operations().stream()
103 .filter(op -> !op.groupType().equals(GroupDescription.Type.ALL))
104 .forEach(op -> {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800105 // ONOS-7785 We need the group app cookie (which includes
106 // the action profile ID) but this is not part of the
107 // GroupDescription.
Carmelo Casconee44592f2018-09-12 02:24:47 -0700108 Group groupOnStore = groupStore.getGroup(deviceId, op.groupId());
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800109 if (groupOnStore == null) {
Carmelo Casconec32976e2019-04-08 14:50:52 -0700110 log.warn("Unable to find group {} in store, aborting {} operation [{}]",
111 op.groupId(), op.opType(), op);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800112 return;
113 }
Carmelo Casconee44592f2018-09-12 02:24:47 -0700114 GroupDescription groupDesc = new DefaultGroupDescription(
115 deviceId, op.groupType(), op.buckets(), groupOnStore.appCookie(),
116 op.groupId().id(), groupOnStore.appId());
117 DefaultGroup groupToApply = new DefaultGroup(op.groupId(), groupDesc);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800118 processPdGroup(groupToApply, op.opType());
Carmelo Casconee44592f2018-09-12 02:24:47 -0700119 });
120 }
121
122 @Override
123 public Collection<Group> getGroups() {
Carmelo Casconec32976e2019-04-08 14:50:52 -0700124 if (!setupBehaviour("getGroups()")) {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700125 return Collections.emptyList();
126 }
Carmelo Casconee44592f2018-09-12 02:24:47 -0700127
128 if (driverBoolProperty(READ_ACTION_GROUPS_FROM_MIRROR,
129 DEFAULT_READ_ACTION_GROUPS_FROM_MIRROR)) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800130 return getGroupsFromMirror();
Carmelo Casconee44592f2018-09-12 02:24:47 -0700131 }
132
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800133 // Dump groups and members from device for all action profiles.
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800134 final P4RuntimeReadClient.ReadRequest request = client.read(pipeconf);
135 pipeconf.pipelineModel().actionProfiles()
136 .stream().map(PiActionProfileModel::id)
137 .forEach(id -> request.actionProfileGroups(id)
138 .actionProfileMembers(id));
139 final P4RuntimeReadClient.ReadResponse response = request.submitSync();
140
141 if (!response.isSuccess()) {
142 // Error at client level.
143 return Collections.emptyList();
144 }
145
146 final Collection<PiActionProfileGroup> groupsOnDevice = response.all(
147 PiActionProfileGroup.class);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800148 final Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice =
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800149 response.all(PiActionProfileMember.class).stream()
150 .collect(toMap(m -> m.handle(deviceId), m -> m));
Carmelo Casconee44592f2018-09-12 02:24:47 -0700151
152 // Sync mirrors.
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800153 groupMirror.sync(deviceId, groupsOnDevice);
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800154 memberMirror.sync(deviceId, membersOnDevice.values());
Carmelo Casconee44592f2018-09-12 02:24:47 -0700155
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800156 // Retrieve the original PD group before translation.
Carmelo Casconee44592f2018-09-12 02:24:47 -0700157 final List<Group> result = Lists.newArrayList();
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800158 final List<PiActionProfileGroup> groupsToRemove = Lists.newArrayList();
159 final Set<PiActionProfileMemberHandle> memberHandlesToKeep = Sets.newHashSet();
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800160 for (PiActionProfileGroup piGroup : groupsOnDevice) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800161 final Group pdGroup = checkAndForgeGroupEntry(piGroup, membersOnDevice);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700162 if (pdGroup == null) {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700163 // Entry is on device but is inconsistent with controller state.
164 // Mark for removal.
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800165 groupsToRemove.add(piGroup);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700166 } else {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700167 result.add(pdGroup);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800168 // Keep track of member handles used in groups.
169 piGroup.members().stream()
170 .map(m -> PiActionProfileMemberHandle.of(
171 deviceId, piGroup.actionProfile(), m.id()))
172 .forEach(memberHandlesToKeep::add);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700173 }
174 }
175
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700176 // Trigger clean up of inconsistent groups and members (if any). Also
177 // take care of removing any orphan member, e.g. from a
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800178 // partial/unsuccessful group insertion.
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800179 final Set<PiActionProfileMemberHandle> memberHandlesToRemove = Sets.difference(
180 membersOnDevice.keySet(), memberHandlesToKeep);
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800181 final Set<PiActionProfileGroupHandle> groupHandlesToRemove = groupsToRemove
182 .stream().map(g -> g.handle(deviceId)).collect(toSet());
183 if (groupHandlesToRemove.size() + memberHandlesToRemove.size() > 0) {
184 log.warn("Cleaning up {} action profile groups and " +
185 "{} members on {}...",
186 groupHandlesToRemove.size(), memberHandlesToRemove.size(), deviceId);
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700187 client.write(pipeconf)
Carmelo Cascone61469462019-03-05 23:59:11 -0800188 .delete(groupHandlesToRemove)
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700189 .delete(memberHandlesToRemove)
190 .submit().whenComplete((r, ex) -> {
191 if (ex != null) {
192 log.error("Exception removing inconsistent group/members", ex);
193 } else {
194 log.debug("Completed removal of inconsistent " +
195 "groups/members ({} of {} updates succeeded)",
196 r.success().size(), r.all().size());
197 groupMirror.applyWriteResponse(r);
198 memberMirror.applyWriteResponse(r);
199 }
200 });
201
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800202 }
Carmelo Casconee44592f2018-09-12 02:24:47 -0700203
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800204 // Done.
Carmelo Casconee44592f2018-09-12 02:24:47 -0700205 return result;
206 }
207
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800208 private Collection<Group> getGroupsFromMirror() {
209 final Map<PiActionProfileMemberHandle, PiActionProfileMember> members =
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800210 memberMirror.getAll(deviceId).stream()
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700211 .map(TimedEntry::entry)
212 .collect(toMap(e -> e.handle(deviceId), e -> e));
Carmelo Casconee44592f2018-09-12 02:24:47 -0700213 return groupMirror.getAll(deviceId).stream()
214 .map(TimedEntry::entry)
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800215 .map(g -> checkAndForgeGroupEntry(
216 g, members))
Carmelo Casconee44592f2018-09-12 02:24:47 -0700217 .filter(Objects::nonNull)
218 .collect(Collectors.toList());
219 }
220
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800221 private Group checkAndForgeGroupEntry(
222 PiActionProfileGroup piGroupOnDevice,
223 Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice) {
224 final PiActionProfileGroupHandle handle = PiActionProfileGroupHandle.of(
225 deviceId, piGroupOnDevice);
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700226 final Optional<PiTranslatedEntity<Group, PiActionProfileGroup>>
Carmelo Casconee44592f2018-09-12 02:24:47 -0700227 translatedEntity = groupTranslator.lookup(handle);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800228 final TimedEntry<PiActionProfileGroup> mirrorEntry = groupMirror.get(handle);
229 // Check that entry obtained from device is consistent with what is known
230 // by the translation store.
Carmelo Casconee44592f2018-09-12 02:24:47 -0700231 if (!translatedEntity.isPresent()) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800232 log.warn("Group not found in translation store: {}", handle);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700233 return null;
234 }
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800235 final PiActionProfileGroup piGroupFromStore = translatedEntity.get().translated();
236 if (!piGroupFromStore.equals(piGroupOnDevice)) {
237 log.warn("Group on device {} is different from the one in " +
238 "translation store: {} [device={}, store={}]",
239 deviceId, handle, piGroupOnDevice, piGroupFromStore);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700240 return null;
241 }
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800242 // Groups in P4Runtime contains only a reference to members. Check that
243 // the actual member instances in the translation store are the same
244 // found on the device.
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800245 if (!validateGroupMembers(piGroupFromStore, membersOnDevice)) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800246 log.warn("Group on device {} refers to members that are different " +
247 "than those found in translation store: {}", handle);
248 return null;
249 }
250 if (mirrorEntry == null) {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700251 log.warn("Group handle not found in device mirror: {}", handle);
252 return null;
253 }
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800254 // Check that members from device are the same as in the translated group.
255 return addedGroup(translatedEntity.get().original(), mirrorEntry.lifeSec());
256 }
257
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800258 private boolean validateGroupMembers(
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800259 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 Cascone4c289b72019-01-22 15:30:45 -0800267 memberFromStore -> memberFromStore.equals(membersOnDevice.get(
268 memberFromStore.handle(deviceId))));
Carmelo Casconee44592f2018-09-12 02:24:47 -0700269 }
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 Cascone99c59db2019-01-17 15:39:35 -0800278 private void processPdGroup(Group pdGroup, GroupOperation.Type opType) {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700279 // Translate.
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700280 final PiActionProfileGroup piGroup;
Carmelo Casconee44592f2018-09-12 02:24:47 -0700281 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 Cascone4c289b72019-01-22 15:30:45 -0800290 final PiActionProfileGroupHandle handle = piGroup.handle(deviceId);
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700291 // 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 Casconee44592f2018-09-12 02:24:47 -0700297 }
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700298 // Submit write and forget about it.
299 asyncWritePiGroup(piGroup, handle, operation);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700300 }
301
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700302 private void asyncWritePiGroup(
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800303 PiActionProfileGroup group,
304 PiActionProfileGroupHandle groupHandle,
305 Operation operation) {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700306 // Generate and submit write request to write both members and groups.
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800307 final Collection<PiActionProfileMember> members = extractAllMemberInstancesOrNull(group);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800308 if (members == null) {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700309 return;
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800310 }
Carmelo Cascone61469462019-03-05 23:59:11 -0800311 final WriteRequest request = client.write(pipeconf);
Carmelo Cascone61469462019-03-05 23:59:11 -0800312 WRITE_LOCKS.get(deviceId).lock();
313 try {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700314 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 Cascone61469462019-03-05 23:59:11 -0800320 } else {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700321 // 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 Cascone61469462019-03-05 23:59:11 -0800326 }
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700327 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 Cascone61469462019-03-05 23:59:11 -0800346 } finally {
347 WRITE_LOCKS.get(deviceId).unlock();
Carmelo Casconee44592f2018-09-12 02:24:47 -0700348 }
349 }
350
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700351 private <H extends PiHandle, E extends PiEntity> void appendEntityToWriteRequestOrSkip(
Carmelo Cascone61469462019-03-05 23:59:11 -0800352 WriteRequest writeRequest, H handle, E entityToApply,
353 P4RuntimeMirror<H, E> mirror, Operation operation) {
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800354 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 Casconeb4863b32019-03-13 18:54:34 -0700361 return;
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800362 } 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 Casconeb4863b32019-03-13 18:54:34 -0700369 return;
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800370 } else {
371 writeRequest.delete(handle);
372 }
373 break;
374 default:
375 log.error("Unrecognized operation {}", operation);
376 break;
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800377 }
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800378 }
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 Casconee44592f2018-09-12 02:24:47 -0700393 }
394
Carmelo Casconee44592f2018-09-12 02:24:47 -0700395 enum Operation {
396 APPLY, REMOVE
397 }
398}