Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 17 | package org.onosproject.drivers.p4runtime; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 18 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 21 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeGroupMirror; |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 22 | import org.onosproject.drivers.p4runtime.mirror.TimedEntry; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 24 | import org.onosproject.net.group.DefaultGroup; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 25 | import org.onosproject.net.group.Group; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 26 | import org.onosproject.net.group.GroupOperation; |
| 27 | import org.onosproject.net.group.GroupOperations; |
| 28 | import org.onosproject.net.group.GroupProgrammable; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 29 | import org.onosproject.net.group.GroupStore; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiActionProfileId; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiActionProfileModel; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 32 | import org.onosproject.net.pi.runtime.PiActionGroup; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 33 | import org.onosproject.net.pi.runtime.PiActionGroupHandle; |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 34 | import org.onosproject.net.pi.runtime.PiActionGroupMember; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 35 | import org.onosproject.net.pi.service.PiGroupTranslator; |
| 36 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 37 | import org.onosproject.net.pi.service.PiTranslationException; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 39 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 40 | import java.util.Collection; |
| 41 | import java.util.Collections; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 42 | import java.util.Map; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 43 | import java.util.Objects; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 44 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 45 | import java.util.concurrent.locks.Lock; |
| 46 | import java.util.concurrent.locks.ReentrantLock; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 47 | import java.util.stream.Collectors; |
| 48 | import java.util.stream.Stream; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 49 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 50 | import static java.lang.String.format; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 51 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE; |
| 52 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT; |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 53 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.MODIFY; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
| 56 | /** |
| 57 | * Implementation of the group programmable behaviour for P4Runtime. |
| 58 | */ |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 59 | public class P4RuntimeGroupProgrammable |
| 60 | extends AbstractP4RuntimeHandlerBehaviour |
| 61 | implements GroupProgrammable { |
| 62 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 63 | private static final String ACT_GRP_MEMS_STR = "action group members"; |
| 64 | private static final String DELETE_STR = "delete"; |
| 65 | private static final String ACT_GRP_STR = "action group"; |
| 66 | private static final String INSERT_STR = "insert"; |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 67 | private static final String MODIFY_STR = "modify"; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 68 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 69 | private static final Logger log = getLogger(P4RuntimeGroupProgrammable.class); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 70 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 71 | // If true, we ignore re-installing groups that are already known in the |
| 72 | // device mirror. |
Yi Tseng | f325a60 | 2018-06-27 18:26:33 +0800 | [diff] [blame] | 73 | private static final String CHECK_MIRROR_BEFORE_UPDATE = "checkMirrorBeforeUpdate"; |
| 74 | private static final boolean DEFAULT_CHECK_MIRROR_BEFORE_UPDATE = true; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 75 | |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 76 | // If true, we avoid querying the device and return what's already known by |
| 77 | // the ONOS store. |
Yi Tseng | f325a60 | 2018-06-27 18:26:33 +0800 | [diff] [blame] | 78 | private static final String IGNORE_DEVICE_WHEN_GET = "ignoreDeviceWhenGet"; |
| 79 | private static final boolean DEFAULT_IGNORE_DEVICE_WHEN_GET = false; |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 80 | |
Esin Karaman | 971fb7f | 2017-12-28 13:44:52 +0000 | [diff] [blame] | 81 | protected GroupStore groupStore; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 82 | private P4RuntimeGroupMirror groupMirror; |
| 83 | private PiGroupTranslator translator; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 84 | |
| 85 | // Needed to synchronize operations over the same group. |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 86 | private static final Map<PiActionGroupHandle, Lock> GROUP_LOCKS = |
| 87 | Maps.newConcurrentMap(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 88 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 89 | @Override |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 90 | protected boolean setupBehaviour() { |
| 91 | if (!super.setupBehaviour()) { |
| 92 | return false; |
| 93 | } |
| 94 | groupMirror = this.handler().get(P4RuntimeGroupMirror.class); |
| 95 | groupStore = handler().get(GroupStore.class); |
| 96 | translator = piTranslationService.groupTranslator(); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void performGroupOperation(DeviceId deviceId, |
| 102 | GroupOperations groupOps) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 103 | if (!setupBehaviour()) { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 104 | return; |
| 105 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 106 | groupOps.operations().forEach(op -> processGroupOp(deviceId, op)); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public Collection<Group> getGroups() { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 111 | if (!setupBehaviour()) { |
| 112 | return Collections.emptyList(); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 113 | } |
Yi Tseng | f325a60 | 2018-06-27 18:26:33 +0800 | [diff] [blame] | 114 | if (!driverBoolProperty(IGNORE_DEVICE_WHEN_GET, DEFAULT_IGNORE_DEVICE_WHEN_GET)) { |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 115 | return pipeconf.pipelineModel().actionProfiles().stream() |
| 116 | .map(PiActionProfileModel::id) |
| 117 | .flatMap(this::streamGroupsFromDevice) |
| 118 | .collect(Collectors.toList()); |
| 119 | } else { |
| 120 | return groupMirror.getAll(deviceId).stream() |
| 121 | .map(TimedEntry::entry) |
| 122 | .map(this::forgeGroupEntry) |
| 123 | .collect(Collectors.toList()); |
| 124 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 125 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 126 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 127 | private void processGroupOp(DeviceId deviceId, GroupOperation groupOp) { |
| 128 | final Group pdGroup = groupStore.getGroup(deviceId, groupOp.groupId()); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 129 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 130 | final PiActionGroup piGroup; |
| 131 | try { |
| 132 | piGroup = translator.translate(pdGroup, pipeconf); |
| 133 | } catch (PiTranslationException e) { |
| 134 | log.warn("Unable translate group, aborting {} operation: {}", |
| 135 | groupOp.opType(), e.getMessage()); |
| 136 | return; |
| 137 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 138 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 139 | final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup); |
| 140 | |
| 141 | final PiActionGroup groupOnDevice = groupMirror.get(handle) == null |
| 142 | ? null |
| 143 | : groupMirror.get(handle).entry(); |
| 144 | |
| 145 | final Lock lock = GROUP_LOCKS.computeIfAbsent(handle, k -> new ReentrantLock()); |
| 146 | lock.lock(); |
| 147 | try { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 148 | processPiGroup(handle, piGroup, |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 149 | groupOnDevice, pdGroup, groupOp.opType()); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 150 | } finally { |
| 151 | lock.unlock(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 155 | private void processPiGroup(PiActionGroupHandle handle, |
| 156 | PiActionGroup groupToApply, |
| 157 | PiActionGroup groupOnDevice, |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 158 | Group pdGroup, GroupOperation.Type operationType) { |
| 159 | if (operationType == GroupOperation.Type.ADD) { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 160 | if (groupOnDevice != null) { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 161 | log.warn("Unable to add group {} since group already on device {}", |
| 162 | groupToApply.id(), deviceId); |
| 163 | log.debug("To apply: {}", groupToApply); |
| 164 | log.debug("On device: {}", groupOnDevice); |
| 165 | return; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 166 | } |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 167 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 168 | if (writeGroupToDevice(groupToApply)) { |
| 169 | groupMirror.put(handle, groupToApply); |
| 170 | translator.learn(handle, new PiTranslatedEntity<>( |
| 171 | pdGroup, groupToApply, handle)); |
| 172 | } |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 173 | } else if (operationType == GroupOperation.Type.MODIFY) { |
| 174 | if (groupOnDevice == null) { |
| 175 | log.warn("Group {} does not exists on device {}, can not modify it", |
| 176 | groupToApply.id(), deviceId); |
| 177 | return; |
| 178 | } |
Yi Tseng | f325a60 | 2018-06-27 18:26:33 +0800 | [diff] [blame] | 179 | if (driverBoolProperty(CHECK_MIRROR_BEFORE_UPDATE, DEFAULT_CHECK_MIRROR_BEFORE_UPDATE) |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 180 | && groupOnDevice.equals(groupToApply)) { |
| 181 | // Group on device has the same members, ignore operation. |
| 182 | return; |
| 183 | } |
| 184 | if (modifyGroupFromDevice(groupToApply, groupOnDevice)) { |
| 185 | groupMirror.put(handle, groupToApply); |
| 186 | translator.learn(handle, |
| 187 | new PiTranslatedEntity<>(pdGroup, groupToApply, handle)); |
| 188 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 189 | } else { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 190 | if (groupOnDevice == null) { |
| 191 | log.warn("Unable to remove group {} from device {} since it does" + |
| 192 | "not exists on device.", groupToApply.id(), deviceId); |
| 193 | return; |
| 194 | } |
| 195 | if (deleteGroupFromDevice(groupOnDevice)) { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 196 | groupMirror.remove(handle); |
| 197 | translator.forget(handle); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 202 | private boolean modifyGroupFromDevice(PiActionGroup groupToApply, PiActionGroup groupOnDevice) { |
| 203 | PiActionProfileId groupProfileId = groupToApply.actionProfileId(); |
| 204 | Collection<PiActionGroupMember> membersToRemove = Sets.newHashSet(groupOnDevice.members()); |
| 205 | membersToRemove.removeAll(groupToApply.members()); |
| 206 | Collection<PiActionGroupMember> membersToAdd = Sets.newHashSet(groupToApply.members()); |
| 207 | membersToAdd.removeAll(groupOnDevice.members()); |
| 208 | |
| 209 | if (!membersToAdd.isEmpty() && |
| 210 | !completeFuture(client.writeActionGroupMembers(groupProfileId, membersToAdd, INSERT, pipeconf), |
| 211 | ACT_GRP_MEMS_STR, INSERT_STR)) { |
| 212 | // remove what we added |
| 213 | completeFuture(client.writeActionGroupMembers(groupProfileId, membersToAdd, DELETE, pipeconf), |
| 214 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | if (!completeFuture(client.writeActionGroup(groupToApply, MODIFY, pipeconf), |
| 219 | ACT_GRP_STR, MODIFY_STR)) { |
| 220 | // recover group information |
| 221 | completeFuture(client.writeActionGroup(groupOnDevice, MODIFY, pipeconf), |
| 222 | ACT_GRP_STR, MODIFY_STR); |
| 223 | // remove what we added |
| 224 | completeFuture(client.writeActionGroupMembers(groupProfileId, membersToAdd, DELETE, pipeconf), |
| 225 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | if (!membersToRemove.isEmpty() && |
| 230 | !completeFuture(client.writeActionGroupMembers(groupProfileId, membersToRemove, DELETE, pipeconf), |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 231 | ACT_GRP_MEMS_STR, DELETE_STR)) { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 232 | // add what we removed |
| 233 | completeFuture(client.writeActionGroupMembers(groupProfileId, membersToRemove, INSERT, pipeconf), |
| 234 | ACT_GRP_MEMS_STR, DELETE_STR); |
| 235 | // recover group information |
| 236 | completeFuture(client.writeActionGroup(groupOnDevice, MODIFY, pipeconf), |
| 237 | ACT_GRP_STR, MODIFY_STR); |
| 238 | // remove what we added |
| 239 | completeFuture(client.writeActionGroupMembers(groupProfileId, membersToAdd, DELETE, pipeconf), |
| 240 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | return true; |
| 245 | } |
| 246 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 247 | private boolean writeGroupToDevice(PiActionGroup groupToApply) { |
| 248 | // First insert members, then group. |
| 249 | // The operation is deemed successful if both operations are successful. |
| 250 | // FIXME: add transactional semantics, i.e. remove members if group fails. |
| 251 | final boolean membersSuccess = completeFuture( |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 252 | client.writeActionGroupMembers(groupToApply.actionProfileId(), |
| 253 | groupToApply.members(), |
| 254 | INSERT, pipeconf), |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 255 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 256 | return membersSuccess && completeFuture( |
| 257 | client.writeActionGroup(groupToApply, INSERT, pipeconf), |
| 258 | ACT_GRP_STR, INSERT_STR); |
| 259 | } |
| 260 | |
| 261 | private boolean deleteGroupFromDevice(PiActionGroup piActionGroup) { |
| 262 | // First delete group, then members. |
| 263 | // The operation is deemed successful if both operations are successful. |
| 264 | final boolean groupSuccess = completeFuture( |
| 265 | client.writeActionGroup(piActionGroup, DELETE, pipeconf), |
| 266 | ACT_GRP_STR, DELETE_STR); |
| 267 | return groupSuccess && completeFuture( |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 268 | client.writeActionGroupMembers(piActionGroup.actionProfileId(), |
| 269 | piActionGroup.members(), |
| 270 | DELETE, pipeconf), |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 271 | ACT_GRP_MEMS_STR, DELETE_STR); |
| 272 | } |
| 273 | |
| 274 | private boolean completeFuture(CompletableFuture<Boolean> completableFuture, |
| 275 | String topic, String action) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 276 | return getFutureWithDeadline( |
| 277 | completableFuture, format("performing %s %s", action, topic), false); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private Stream<Group> streamGroupsFromDevice(PiActionProfileId actProfId) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 281 | // Read PI groups and return original PD one. |
| 282 | Collection<PiActionGroup> groups = getFutureWithDeadline( |
| 283 | client.dumpGroups(actProfId, pipeconf), |
| 284 | "dumping groups", Collections.emptyList()); |
| 285 | return groups.stream() |
| 286 | .map(this::forgeGroupEntry) |
| 287 | .filter(Objects::nonNull); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | private Group forgeGroupEntry(PiActionGroup piGroup) { |
| 291 | final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup); |
| 292 | if (!translator.lookup(handle).isPresent()) { |
| 293 | log.warn("Missing PI group from translation store: {} - {}:{}", |
| 294 | pipeconf.id(), piGroup.actionProfileId(), |
| 295 | piGroup.id()); |
| 296 | return null; |
| 297 | } |
| 298 | final long life = groupMirror.get(handle) != null |
| 299 | ? groupMirror.get(handle).lifeSec() : 0; |
| 300 | final Group original = translator.lookup(handle).get().original(); |
| 301 | final DefaultGroup forgedGroup = new DefaultGroup(original.id(), original); |
| 302 | forgedGroup.setState(Group.GroupState.ADDED); |
| 303 | forgedGroup.setLife(life); |
| 304 | return forgedGroup; |
| 305 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 306 | } |