blob: 31ee45be5f2b770c6d7e6f9ccf2e7683ce6fc9dc [file] [log] [blame]
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04003 *
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 Campanella0288c872017-08-07 18:32:51 +020017package org.onosproject.drivers.p4runtime;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040018
Yi Tseng82512da2017-08-16 19:46:36 -070019import com.google.common.collect.Maps;
Carmelo Casconee75b7942017-11-21 17:14:49 -080020import org.onosproject.drivers.p4runtime.mirror.P4RuntimeGroupMirror;
Yi Tseng76737cf2018-01-31 17:13:21 -080021import org.onosproject.drivers.p4runtime.mirror.TimedEntry;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040022import org.onosproject.net.DeviceId;
Carmelo Casconee75b7942017-11-21 17:14:49 -080023import org.onosproject.net.group.DefaultGroup;
Yi Tseng82512da2017-08-16 19:46:36 -070024import org.onosproject.net.group.Group;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040025import org.onosproject.net.group.GroupOperation;
26import org.onosproject.net.group.GroupOperations;
27import org.onosproject.net.group.GroupProgrammable;
Yi Tseng82512da2017-08-16 19:46:36 -070028import org.onosproject.net.group.GroupStore;
Carmelo Cascone87892e22017-11-13 16:01:29 -080029import org.onosproject.net.pi.model.PiActionProfileId;
Carmelo Casconee75b7942017-11-21 17:14:49 -080030import org.onosproject.net.pi.model.PiActionProfileModel;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040031import org.onosproject.net.pi.runtime.PiActionGroup;
Carmelo Casconee75b7942017-11-21 17:14:49 -080032import org.onosproject.net.pi.runtime.PiActionGroupHandle;
33import org.onosproject.net.pi.service.PiGroupTranslator;
34import org.onosproject.net.pi.service.PiTranslatedEntity;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080035import org.onosproject.net.pi.service.PiTranslationException;
Yi Tseng82512da2017-08-16 19:46:36 -070036import org.slf4j.Logger;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040037
Yi Tseng82512da2017-08-16 19:46:36 -070038import java.util.Collection;
39import java.util.Collections;
Yi Tseng82512da2017-08-16 19:46:36 -070040import java.util.Map;
Carmelo Casconee75b7942017-11-21 17:14:49 -080041import java.util.Objects;
Yi Tseng82512da2017-08-16 19:46:36 -070042import java.util.concurrent.CompletableFuture;
43import java.util.concurrent.ExecutionException;
Yi Tseng82512da2017-08-16 19:46:36 -070044import java.util.concurrent.locks.Lock;
45import java.util.concurrent.locks.ReentrantLock;
Carmelo Casconee75b7942017-11-21 17:14:49 -080046import java.util.stream.Collectors;
47import java.util.stream.Stream;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040048
Carmelo Casconee75b7942017-11-21 17:14:49 -080049import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE;
50import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT;
Yi Tseng82512da2017-08-16 19:46:36 -070051import static org.slf4j.LoggerFactory.getLogger;
52
53/**
54 * Implementation of the group programmable behaviour for P4Runtime.
55 */
Carmelo Casconee75b7942017-11-21 17:14:49 -080056public class P4RuntimeGroupProgrammable
57 extends AbstractP4RuntimeHandlerBehaviour
58 implements GroupProgrammable {
59
60 private enum Operation {
61 APPLY, REMOVE
62 }
63
64 private static final String ACT_GRP_MEMS_STR = "action group members";
65 private static final String DELETE_STR = "delete";
66 private static final String ACT_GRP_STR = "action group";
67 private static final String INSERT_STR = "insert";
68
Yi Tseng82512da2017-08-16 19:46:36 -070069 private static final Logger log = getLogger(P4RuntimeGroupProgrammable.class);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040070
Carmelo Casconee75b7942017-11-21 17:14:49 -080071 // If true, we ignore re-installing groups that are already known in the
72 // device mirror.
73 private boolean checkMirrorBeforeUpdate = true;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040074
Yi Tseng76737cf2018-01-31 17:13:21 -080075 // If true, we avoid querying the device and return what's already known by
76 // the ONOS store.
77 private boolean ignoreDeviceWhenGet = true;
78
Carmelo Casconee75b7942017-11-21 17:14:49 -080079 private GroupStore groupStore;
80 private P4RuntimeGroupMirror groupMirror;
81 private PiGroupTranslator translator;
Yi Tseng82512da2017-08-16 19:46:36 -070082
83 // Needed to synchronize operations over the same group.
Carmelo Casconee75b7942017-11-21 17:14:49 -080084 private static final Map<PiActionGroupHandle, Lock> GROUP_LOCKS =
85 Maps.newConcurrentMap();
Yi Tseng82512da2017-08-16 19:46:36 -070086
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040087 @Override
Carmelo Casconee75b7942017-11-21 17:14:49 -080088 protected boolean setupBehaviour() {
89 if (!super.setupBehaviour()) {
90 return false;
91 }
92 groupMirror = this.handler().get(P4RuntimeGroupMirror.class);
93 groupStore = handler().get(GroupStore.class);
94 translator = piTranslationService.groupTranslator();
95 return true;
96 }
97
98 @Override
99 public void performGroupOperation(DeviceId deviceId,
100 GroupOperations groupOps) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200101 if (!setupBehaviour()) {
Yi Tseng82512da2017-08-16 19:46:36 -0700102 return;
103 }
Carmelo Casconee75b7942017-11-21 17:14:49 -0800104 groupOps.operations().forEach(op -> processGroupOp(deviceId, op));
Yi Tseng82512da2017-08-16 19:46:36 -0700105 }
106
Yi Tseng82512da2017-08-16 19:46:36 -0700107 @Override
108 public Collection<Group> getGroups() {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200109 if (!setupBehaviour()) {
110 return Collections.emptyList();
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400111 }
Yi Tseng76737cf2018-01-31 17:13:21 -0800112 if (!ignoreDeviceWhenGet) {
113 return pipeconf.pipelineModel().actionProfiles().stream()
114 .map(PiActionProfileModel::id)
115 .flatMap(this::streamGroupsFromDevice)
116 .collect(Collectors.toList());
117 } else {
118 return groupMirror.getAll(deviceId).stream()
119 .map(TimedEntry::entry)
120 .map(this::forgeGroupEntry)
121 .collect(Collectors.toList());
122 }
Carmelo Casconee75b7942017-11-21 17:14:49 -0800123 }
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400124
Carmelo Casconee75b7942017-11-21 17:14:49 -0800125 private void processGroupOp(DeviceId deviceId, GroupOperation groupOp) {
126 final Group pdGroup = groupStore.getGroup(deviceId, groupOp.groupId());
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400127
Carmelo Casconee75b7942017-11-21 17:14:49 -0800128 final PiActionGroup piGroup;
129 try {
130 piGroup = translator.translate(pdGroup, pipeconf);
131 } catch (PiTranslationException e) {
132 log.warn("Unable translate group, aborting {} operation: {}",
133 groupOp.opType(), e.getMessage());
134 return;
135 }
Yi Tseng82512da2017-08-16 19:46:36 -0700136
Carmelo Casconee75b7942017-11-21 17:14:49 -0800137 final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup);
138
139 final PiActionGroup groupOnDevice = groupMirror.get(handle) == null
140 ? null
141 : groupMirror.get(handle).entry();
142
143 final Lock lock = GROUP_LOCKS.computeIfAbsent(handle, k -> new ReentrantLock());
144 lock.lock();
145 try {
146 final Operation operation;
147 switch (groupOp.opType()) {
148 case ADD:
149 case MODIFY:
150 operation = Operation.APPLY;
151 break;
152 case DELETE:
153 operation = Operation.REMOVE;
154 break;
155 default:
156 log.warn("Group operation {} not supported", groupOp.opType());
157 return;
Yi Tseng82512da2017-08-16 19:46:36 -0700158 }
Carmelo Casconee75b7942017-11-21 17:14:49 -0800159 processPiGroup(handle, piGroup,
160 groupOnDevice, pdGroup, operation);
161 } finally {
162 lock.unlock();
Yi Tseng82512da2017-08-16 19:46:36 -0700163 }
164 }
165
Carmelo Casconee75b7942017-11-21 17:14:49 -0800166 private void processPiGroup(PiActionGroupHandle handle,
167 PiActionGroup groupToApply,
168 PiActionGroup groupOnDevice,
169 Group pdGroup, Operation operation) {
170 if (operation == Operation.APPLY) {
171 if (groupOnDevice != null) {
172 if (checkMirrorBeforeUpdate
173 && groupOnDevice.equals(groupToApply)) {
174 // Group on device has the same members, ignore operation.
175 return;
176 }
177 // Remove before adding it.
178 processPiGroup(handle, groupToApply, groupOnDevice,
179 pdGroup, Operation.REMOVE);
180 }
181 if (writeGroupToDevice(groupToApply)) {
182 groupMirror.put(handle, groupToApply);
183 translator.learn(handle, new PiTranslatedEntity<>(
184 pdGroup, groupToApply, handle));
185 }
186 } else {
187 if (deleteGroupFromDevice(groupToApply)) {
188 groupMirror.remove(handle);
189 translator.forget(handle);
190 }
191 }
192 }
193
194 private boolean writeGroupToDevice(PiActionGroup groupToApply) {
195 // First insert members, then group.
196 // The operation is deemed successful if both operations are successful.
197 // FIXME: add transactional semantics, i.e. remove members if group fails.
198 final boolean membersSuccess = completeFuture(
199 client.writeActionGroupMembers(groupToApply, INSERT, pipeconf),
200 ACT_GRP_MEMS_STR, INSERT_STR);
201 return membersSuccess && completeFuture(
202 client.writeActionGroup(groupToApply, INSERT, pipeconf),
203 ACT_GRP_STR, INSERT_STR);
204 }
205
206 private boolean deleteGroupFromDevice(PiActionGroup piActionGroup) {
207 // First delete group, then members.
208 // The operation is deemed successful if both operations are successful.
209 final boolean groupSuccess = completeFuture(
210 client.writeActionGroup(piActionGroup, DELETE, pipeconf),
211 ACT_GRP_STR, DELETE_STR);
212 return groupSuccess && completeFuture(
213 client.writeActionGroupMembers(piActionGroup, DELETE, pipeconf),
214 ACT_GRP_MEMS_STR, DELETE_STR);
215 }
216
217 private boolean completeFuture(CompletableFuture<Boolean> completableFuture,
218 String topic, String action) {
219 try {
220 if (completableFuture.get()) {
221 return true;
222 } else {
223 log.warn("Unable to {} {}", action, topic);
224 return false;
225 }
226 } catch (InterruptedException | ExecutionException e) {
227 log.warn("Exception while performing {} {}: {}", action, topic, e.getMessage());
228 log.debug("Exception", e);
229 return false;
230 }
231 }
232
233 private Stream<Group> streamGroupsFromDevice(PiActionProfileId actProfId) {
234 try {
235 // Read PI groups and return original PD one.
236 return client.dumpGroups(actProfId, pipeconf).get().stream()
237 .map(this::forgeGroupEntry)
238 .filter(Objects::nonNull);
239 } catch (ExecutionException | InterruptedException e) {
240 log.error("Exception while dumping groups from action profile '{}' on {}: {}",
241 actProfId.id(), deviceId, e);
242 return Stream.empty();
243 }
244 }
245
246 private Group forgeGroupEntry(PiActionGroup piGroup) {
247 final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup);
248 if (!translator.lookup(handle).isPresent()) {
249 log.warn("Missing PI group from translation store: {} - {}:{}",
250 pipeconf.id(), piGroup.actionProfileId(),
251 piGroup.id());
252 return null;
253 }
254 final long life = groupMirror.get(handle) != null
255 ? groupMirror.get(handle).lifeSec() : 0;
256 final Group original = translator.lookup(handle).get().original();
257 final DefaultGroup forgedGroup = new DefaultGroup(original.id(), original);
258 forgedGroup.setState(Group.GroupState.ADDED);
259 forgedGroup.setLife(life);
260 return forgedGroup;
261 }
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400262}