blob: fb5e892b410fb1f2b06783942bb05a041e960d8a [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;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040021import org.onosproject.net.DeviceId;
Carmelo Casconee75b7942017-11-21 17:14:49 -080022import org.onosproject.net.group.DefaultGroup;
Yi Tseng82512da2017-08-16 19:46:36 -070023import org.onosproject.net.group.Group;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040024import org.onosproject.net.group.GroupOperation;
25import org.onosproject.net.group.GroupOperations;
26import org.onosproject.net.group.GroupProgrammable;
Yi Tseng82512da2017-08-16 19:46:36 -070027import org.onosproject.net.group.GroupStore;
Carmelo Cascone87892e22017-11-13 16:01:29 -080028import org.onosproject.net.pi.model.PiActionProfileId;
Carmelo Casconee75b7942017-11-21 17:14:49 -080029import org.onosproject.net.pi.model.PiActionProfileModel;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040030import org.onosproject.net.pi.runtime.PiActionGroup;
Carmelo Casconee75b7942017-11-21 17:14:49 -080031import org.onosproject.net.pi.runtime.PiActionGroupHandle;
32import org.onosproject.net.pi.service.PiGroupTranslator;
33import org.onosproject.net.pi.service.PiTranslatedEntity;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080034import org.onosproject.net.pi.service.PiTranslationException;
Yi Tseng82512da2017-08-16 19:46:36 -070035import org.slf4j.Logger;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040036
Yi Tseng82512da2017-08-16 19:46:36 -070037import java.util.Collection;
38import java.util.Collections;
Yi Tseng82512da2017-08-16 19:46:36 -070039import java.util.Map;
Carmelo Casconee75b7942017-11-21 17:14:49 -080040import java.util.Objects;
Yi Tseng82512da2017-08-16 19:46:36 -070041import java.util.concurrent.CompletableFuture;
42import java.util.concurrent.ExecutionException;
Yi Tseng82512da2017-08-16 19:46:36 -070043import java.util.concurrent.locks.Lock;
44import java.util.concurrent.locks.ReentrantLock;
Carmelo Casconee75b7942017-11-21 17:14:49 -080045import java.util.stream.Collectors;
46import java.util.stream.Stream;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040047
Carmelo Casconee75b7942017-11-21 17:14:49 -080048import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE;
49import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT;
Yi Tseng82512da2017-08-16 19:46:36 -070050import static org.slf4j.LoggerFactory.getLogger;
51
52/**
53 * Implementation of the group programmable behaviour for P4Runtime.
54 */
Carmelo Casconee75b7942017-11-21 17:14:49 -080055public class P4RuntimeGroupProgrammable
56 extends AbstractP4RuntimeHandlerBehaviour
57 implements GroupProgrammable {
58
59 private enum Operation {
60 APPLY, REMOVE
61 }
62
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";
67
Yi Tseng82512da2017-08-16 19:46:36 -070068 private static final Logger log = getLogger(P4RuntimeGroupProgrammable.class);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040069
Carmelo Casconee75b7942017-11-21 17:14:49 -080070 // If true, we ignore re-installing groups that are already known in the
71 // device mirror.
72 private boolean checkMirrorBeforeUpdate = true;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040073
Carmelo Casconee75b7942017-11-21 17:14:49 -080074 private GroupStore groupStore;
75 private P4RuntimeGroupMirror groupMirror;
76 private PiGroupTranslator translator;
Yi Tseng82512da2017-08-16 19:46:36 -070077
78 // Needed to synchronize operations over the same group.
Carmelo Casconee75b7942017-11-21 17:14:49 -080079 private static final Map<PiActionGroupHandle, Lock> GROUP_LOCKS =
80 Maps.newConcurrentMap();
Yi Tseng82512da2017-08-16 19:46:36 -070081
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040082 @Override
Carmelo Casconee75b7942017-11-21 17:14:49 -080083 protected boolean setupBehaviour() {
84 if (!super.setupBehaviour()) {
85 return false;
86 }
87 groupMirror = this.handler().get(P4RuntimeGroupMirror.class);
88 groupStore = handler().get(GroupStore.class);
89 translator = piTranslationService.groupTranslator();
90 return true;
91 }
92
93 @Override
94 public void performGroupOperation(DeviceId deviceId,
95 GroupOperations groupOps) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +020096 if (!setupBehaviour()) {
Yi Tseng82512da2017-08-16 19:46:36 -070097 return;
98 }
Carmelo Casconee75b7942017-11-21 17:14:49 -080099 groupOps.operations().forEach(op -> processGroupOp(deviceId, op));
Yi Tseng82512da2017-08-16 19:46:36 -0700100 }
101
Yi Tseng82512da2017-08-16 19:46:36 -0700102 @Override
103 public Collection<Group> getGroups() {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200104 if (!setupBehaviour()) {
105 return Collections.emptyList();
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400106 }
Carmelo Casconee75b7942017-11-21 17:14:49 -0800107 return pipeconf.pipelineModel().actionProfiles().stream()
108 .map(PiActionProfileModel::id)
109 .flatMap(this::streamGroupsFromDevice)
110 .collect(Collectors.toList());
111 }
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400112
Carmelo Casconee75b7942017-11-21 17:14:49 -0800113 private void processGroupOp(DeviceId deviceId, GroupOperation groupOp) {
114 final Group pdGroup = groupStore.getGroup(deviceId, groupOp.groupId());
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400115
Carmelo Casconee75b7942017-11-21 17:14:49 -0800116 final PiActionGroup piGroup;
117 try {
118 piGroup = translator.translate(pdGroup, pipeconf);
119 } catch (PiTranslationException e) {
120 log.warn("Unable translate group, aborting {} operation: {}",
121 groupOp.opType(), e.getMessage());
122 return;
123 }
Yi Tseng82512da2017-08-16 19:46:36 -0700124
Carmelo Casconee75b7942017-11-21 17:14:49 -0800125 final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup);
126
127 final PiActionGroup groupOnDevice = groupMirror.get(handle) == null
128 ? null
129 : groupMirror.get(handle).entry();
130
131 final Lock lock = GROUP_LOCKS.computeIfAbsent(handle, k -> new ReentrantLock());
132 lock.lock();
133 try {
134 final Operation operation;
135 switch (groupOp.opType()) {
136 case ADD:
137 case MODIFY:
138 operation = Operation.APPLY;
139 break;
140 case DELETE:
141 operation = Operation.REMOVE;
142 break;
143 default:
144 log.warn("Group operation {} not supported", groupOp.opType());
145 return;
Yi Tseng82512da2017-08-16 19:46:36 -0700146 }
Carmelo Casconee75b7942017-11-21 17:14:49 -0800147 processPiGroup(handle, piGroup,
148 groupOnDevice, pdGroup, operation);
149 } finally {
150 lock.unlock();
Yi Tseng82512da2017-08-16 19:46:36 -0700151 }
152 }
153
Carmelo Casconee75b7942017-11-21 17:14:49 -0800154 private void processPiGroup(PiActionGroupHandle handle,
155 PiActionGroup groupToApply,
156 PiActionGroup groupOnDevice,
157 Group pdGroup, Operation operation) {
158 if (operation == Operation.APPLY) {
159 if (groupOnDevice != null) {
160 if (checkMirrorBeforeUpdate
161 && groupOnDevice.equals(groupToApply)) {
162 // Group on device has the same members, ignore operation.
163 return;
164 }
165 // Remove before adding it.
166 processPiGroup(handle, groupToApply, groupOnDevice,
167 pdGroup, Operation.REMOVE);
168 }
169 if (writeGroupToDevice(groupToApply)) {
170 groupMirror.put(handle, groupToApply);
171 translator.learn(handle, new PiTranslatedEntity<>(
172 pdGroup, groupToApply, handle));
173 }
174 } else {
175 if (deleteGroupFromDevice(groupToApply)) {
176 groupMirror.remove(handle);
177 translator.forget(handle);
178 }
179 }
180 }
181
182 private boolean writeGroupToDevice(PiActionGroup groupToApply) {
183 // First insert members, then group.
184 // The operation is deemed successful if both operations are successful.
185 // FIXME: add transactional semantics, i.e. remove members if group fails.
186 final boolean membersSuccess = completeFuture(
187 client.writeActionGroupMembers(groupToApply, INSERT, pipeconf),
188 ACT_GRP_MEMS_STR, INSERT_STR);
189 return membersSuccess && completeFuture(
190 client.writeActionGroup(groupToApply, INSERT, pipeconf),
191 ACT_GRP_STR, INSERT_STR);
192 }
193
194 private boolean deleteGroupFromDevice(PiActionGroup piActionGroup) {
195 // First delete group, then members.
196 // The operation is deemed successful if both operations are successful.
197 final boolean groupSuccess = completeFuture(
198 client.writeActionGroup(piActionGroup, DELETE, pipeconf),
199 ACT_GRP_STR, DELETE_STR);
200 return groupSuccess && completeFuture(
201 client.writeActionGroupMembers(piActionGroup, DELETE, pipeconf),
202 ACT_GRP_MEMS_STR, DELETE_STR);
203 }
204
205 private boolean completeFuture(CompletableFuture<Boolean> completableFuture,
206 String topic, String action) {
207 try {
208 if (completableFuture.get()) {
209 return true;
210 } else {
211 log.warn("Unable to {} {}", action, topic);
212 return false;
213 }
214 } catch (InterruptedException | ExecutionException e) {
215 log.warn("Exception while performing {} {}: {}", action, topic, e.getMessage());
216 log.debug("Exception", e);
217 return false;
218 }
219 }
220
221 private Stream<Group> streamGroupsFromDevice(PiActionProfileId actProfId) {
222 try {
223 // Read PI groups and return original PD one.
224 return client.dumpGroups(actProfId, pipeconf).get().stream()
225 .map(this::forgeGroupEntry)
226 .filter(Objects::nonNull);
227 } catch (ExecutionException | InterruptedException e) {
228 log.error("Exception while dumping groups from action profile '{}' on {}: {}",
229 actProfId.id(), deviceId, e);
230 return Stream.empty();
231 }
232 }
233
234 private Group forgeGroupEntry(PiActionGroup piGroup) {
235 final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup);
236 if (!translator.lookup(handle).isPresent()) {
237 log.warn("Missing PI group from translation store: {} - {}:{}",
238 pipeconf.id(), piGroup.actionProfileId(),
239 piGroup.id());
240 return null;
241 }
242 final long life = groupMirror.get(handle) != null
243 ? groupMirror.get(handle).lifeSec() : 0;
244 final Group original = translator.lookup(handle).get().original();
245 final DefaultGroup forgedGroup = new DefaultGroup(original.id(), original);
246 forgedGroup.setState(Group.GroupState.ADDED);
247 forgedGroup.setLife(life);
248 return forgedGroup;
249 }
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400250}