blob: d1d6902772b695803cea5446e2f9e9c76ce4157d [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
Carmelo Cascone5505a6d2019-04-17 20:03:24 -0700102 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 Casconee44592f2018-09-12 02:24:47 -0700118 }
119
120 @Override
121 public Collection<Group> getGroups() {
Carmelo Casconec32976e2019-04-08 14:50:52 -0700122 if (!setupBehaviour("getGroups()")) {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700123 return Collections.emptyList();
124 }
Carmelo Casconee44592f2018-09-12 02:24:47 -0700125
126 if (driverBoolProperty(READ_ACTION_GROUPS_FROM_MIRROR,
127 DEFAULT_READ_ACTION_GROUPS_FROM_MIRROR)) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800128 return getGroupsFromMirror();
Carmelo Casconee44592f2018-09-12 02:24:47 -0700129 }
130
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800131 // Dump groups and members from device for all action profiles.
Carmelo Casconec2be50a2019-04-10 00:15:39 -0700132 final P4RuntimeReadClient.ReadRequest request = client.read(
133 p4DeviceId, pipeconf);
Daniele Morod900fe42021-02-11 16:12:57 +0100134
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 Cascone4c289b72019-01-22 15:30:45 -0800145 .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 Cascone99c59db2019-01-17 15:39:35 -0800156 final Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice =
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800157 response.all(PiActionProfileMember.class).stream()
158 .collect(toMap(m -> m.handle(deviceId), m -> m));
Carmelo Casconee44592f2018-09-12 02:24:47 -0700159
160 // Sync mirrors.
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800161 groupMirror.sync(deviceId, groupsOnDevice);
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800162 memberMirror.sync(deviceId, membersOnDevice.values());
Carmelo Casconee44592f2018-09-12 02:24:47 -0700163
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800164 // Retrieve the original PD group before translation.
Carmelo Casconee44592f2018-09-12 02:24:47 -0700165 final List<Group> result = Lists.newArrayList();
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800166 final List<PiActionProfileGroup> groupsToRemove = Lists.newArrayList();
167 final Set<PiActionProfileMemberHandle> memberHandlesToKeep = Sets.newHashSet();
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800168 for (PiActionProfileGroup piGroup : groupsOnDevice) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800169 final Group pdGroup = checkAndForgeGroupEntry(piGroup, membersOnDevice);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700170 if (pdGroup == null) {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700171 // Entry is on device but is inconsistent with controller state.
172 // Mark for removal.
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800173 groupsToRemove.add(piGroup);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700174 } else {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700175 result.add(pdGroup);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800176 // 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 Casconee44592f2018-09-12 02:24:47 -0700181 }
182 }
183
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700184 // Trigger clean up of inconsistent groups and members (if any). Also
185 // take care of removing any orphan member, e.g. from a
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800186 // partial/unsuccessful group insertion.
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800187 final Set<PiActionProfileMemberHandle> memberHandlesToRemove = Sets.difference(
188 membersOnDevice.keySet(), memberHandlesToKeep);
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800189 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 Casconec2be50a2019-04-10 00:15:39 -0700195 client.write(p4DeviceId, pipeconf)
Carmelo Cascone61469462019-03-05 23:59:11 -0800196 .delete(groupHandlesToRemove)
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700197 .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 Cascone4c289b72019-01-22 15:30:45 -0800210 }
Carmelo Casconee44592f2018-09-12 02:24:47 -0700211
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800212 // Done.
Carmelo Casconee44592f2018-09-12 02:24:47 -0700213 return result;
214 }
215
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800216 private Collection<Group> getGroupsFromMirror() {
217 final Map<PiActionProfileMemberHandle, PiActionProfileMember> members =
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800218 memberMirror.getAll(deviceId).stream()
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700219 .map(TimedEntry::entry)
220 .collect(toMap(e -> e.handle(deviceId), e -> e));
Carmelo Casconee44592f2018-09-12 02:24:47 -0700221 return groupMirror.getAll(deviceId).stream()
222 .map(TimedEntry::entry)
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800223 .map(g -> checkAndForgeGroupEntry(
224 g, members))
Carmelo Casconee44592f2018-09-12 02:24:47 -0700225 .filter(Objects::nonNull)
226 .collect(Collectors.toList());
227 }
228
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800229 private Group checkAndForgeGroupEntry(
230 PiActionProfileGroup piGroupOnDevice,
231 Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice) {
232 final PiActionProfileGroupHandle handle = PiActionProfileGroupHandle.of(
233 deviceId, piGroupOnDevice);
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700234 final Optional<PiTranslatedEntity<Group, PiActionProfileGroup>>
Carmelo Casconee44592f2018-09-12 02:24:47 -0700235 translatedEntity = groupTranslator.lookup(handle);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800236 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 Casconee44592f2018-09-12 02:24:47 -0700239 if (!translatedEntity.isPresent()) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800240 log.warn("Group not found in translation store: {}", handle);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700241 return null;
242 }
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800243 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 Casconee44592f2018-09-12 02:24:47 -0700248 return null;
249 }
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800250 // 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 Cascone4c289b72019-01-22 15:30:45 -0800253 if (!validateGroupMembers(piGroupFromStore, membersOnDevice)) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800254 log.warn("Group on device {} refers to members that are different " +
Carmelo Casconec2be50a2019-04-10 00:15:39 -0700255 "than those found in translation store: {}",
256 deviceId, handle);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800257 return null;
258 }
259 if (mirrorEntry == null) {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700260 log.warn("Group handle not found in device mirror: {}", handle);
261 return null;
262 }
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800263 // Check that members from device are the same as in the translated group.
264 return addedGroup(translatedEntity.get().original(), mirrorEntry.lifeSec());
265 }
266
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800267 private boolean validateGroupMembers(
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800268 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 Cascone4c289b72019-01-22 15:30:45 -0800276 memberFromStore -> memberFromStore.equals(membersOnDevice.get(
277 memberFromStore.handle(deviceId))));
Carmelo Casconee44592f2018-09-12 02:24:47 -0700278 }
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 Cascone99c59db2019-01-17 15:39:35 -0800287 private void processPdGroup(Group pdGroup, GroupOperation.Type opType) {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700288 // Translate.
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700289 final PiActionProfileGroup piGroup;
Carmelo Casconee44592f2018-09-12 02:24:47 -0700290 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 Cascone4c289b72019-01-22 15:30:45 -0800299 final PiActionProfileGroupHandle handle = piGroup.handle(deviceId);
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700300 // 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 Casconee44592f2018-09-12 02:24:47 -0700306 }
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700307 // Submit write and forget about it.
308 asyncWritePiGroup(piGroup, handle, operation);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700309 }
310
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700311 private void asyncWritePiGroup(
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800312 PiActionProfileGroup group,
313 PiActionProfileGroupHandle groupHandle,
314 Operation operation) {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700315 // Generate and submit write request to write both members and groups.
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800316 final Collection<PiActionProfileMember> members = extractAllMemberInstancesOrNull(group);
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800317 if (members == null) {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700318 return;
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800319 }
Carmelo Casconec2be50a2019-04-10 00:15:39 -0700320 final WriteRequest request = client.write(p4DeviceId, pipeconf);
Carmelo Cascone61469462019-03-05 23:59:11 -0800321 WRITE_LOCKS.get(deviceId).lock();
322 try {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700323 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 Cascone61469462019-03-05 23:59:11 -0800329 } else {
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700330 // 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 Cascone61469462019-03-05 23:59:11 -0800335 }
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700336 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 Cascone61469462019-03-05 23:59:11 -0800355 } finally {
356 WRITE_LOCKS.get(deviceId).unlock();
Carmelo Casconee44592f2018-09-12 02:24:47 -0700357 }
358 }
359
Carmelo Casconeb4863b32019-03-13 18:54:34 -0700360 private <H extends PiHandle, E extends PiEntity> void appendEntityToWriteRequestOrSkip(
Carmelo Cascone61469462019-03-05 23:59:11 -0800361 WriteRequest writeRequest, H handle, E entityToApply,
362 P4RuntimeMirror<H, E> mirror, Operation operation) {
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800363 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 Casconeb4863b32019-03-13 18:54:34 -0700370 return;
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800371 } 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 Casconeb4863b32019-03-13 18:54:34 -0700378 return;
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800379 } else {
380 writeRequest.delete(handle);
381 }
382 break;
383 default:
384 log.error("Unrecognized operation {}", operation);
385 break;
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800386 }
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800387 }
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 Casconee44592f2018-09-12 02:24:47 -0700402 }
403
Carmelo Casconee44592f2018-09-12 02:24:47 -0700404 enum Operation {
405 APPLY, REMOVE
406 }
407}