blob: 916a945913a4f32d54df0e82679e86deff960024 [file] [log] [blame]
Carmelo Casconed61fdb32017-10-30 10:09:57 -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 Casconed61fdb32017-10-30 10:09:57 -070019import org.onosproject.net.DeviceId;
Carmelo Cascone87892e22017-11-13 16:01:29 -080020import org.onosproject.net.behaviour.PiPipelineProgrammable;
Carmelo Casconee5b28722018-06-22 17:28:28 +020021import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Casconed61fdb32017-10-30 10:09:57 -070022import org.onosproject.p4runtime.api.P4RuntimeClient;
23import org.onosproject.p4runtime.api.P4RuntimeController;
24import org.slf4j.Logger;
25
26import java.nio.ByteBuffer;
27import java.util.Optional;
28import java.util.concurrent.CompletableFuture;
Carmelo Casconed61fdb32017-10-30 10:09:57 -070029
Carmelo Cascone3977ea42019-02-28 13:43:42 -080030import static java.util.concurrent.CompletableFuture.completedFuture;
Carmelo Casconed61fdb32017-10-30 10:09:57 -070031import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Abstract implementation of the PiPipelineProgrammable behaviours for a P4Runtime device.
35 */
Carmelo Casconee5b28722018-06-22 17:28:28 +020036public abstract class AbstractP4RuntimePipelineProgrammable
37 extends AbstractP4RuntimeHandlerBehaviour
Carmelo Casconed61fdb32017-10-30 10:09:57 -070038 implements PiPipelineProgrammable {
39
40 protected final Logger log = getLogger(getClass());
41
42 /**
43 * Returns a byte buffer representing the target-specific device data to be used in the SetPipelineConfig message.
44 *
45 * @param pipeconf pipeconf
46 * @return byte buffer
47 */
48 public abstract ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf);
49
50 @Override
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070051 public CompletableFuture<Boolean> setPipeconf(PiPipeconf pipeconf) {
Carmelo Casconed61fdb32017-10-30 10:09:57 -070052 DeviceId deviceId = handler().data().deviceId();
53 P4RuntimeController controller = handler().get(P4RuntimeController.class);
54
Carmelo Cascone158b8c42018-07-04 19:42:37 +020055 P4RuntimeClient client = controller.getClient(deviceId);
56 if (client == null) {
Carmelo Casconed61fdb32017-10-30 10:09:57 -070057 log.warn("Unable to find client for {}, aborting pipeconf deploy", deviceId);
Carmelo Cascone3977ea42019-02-28 13:43:42 -080058 return completedFuture(false);
Carmelo Casconed61fdb32017-10-30 10:09:57 -070059 }
Carmelo Casconed61fdb32017-10-30 10:09:57 -070060
61 ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf);
62 if (deviceDataBuffer == null) {
63 // Hopefully the child class logged the problem.
Carmelo Cascone3977ea42019-02-28 13:43:42 -080064 return completedFuture(false);
Carmelo Casconed61fdb32017-10-30 10:09:57 -070065 }
66
Carmelo Cascone3977ea42019-02-28 13:43:42 -080067 return client.setPipelineConfig(pipeconf, deviceDataBuffer);
Carmelo Casconed61fdb32017-10-30 10:09:57 -070068 }
69
70 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -080071 public CompletableFuture<Boolean> isPipeconfSet(PiPipeconf pipeconf) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070072 DeviceId deviceId = handler().data().deviceId();
73 P4RuntimeController controller = handler().get(P4RuntimeController.class);
74
75 P4RuntimeClient client = controller.getClient(deviceId);
76 if (client == null) {
77 log.warn("Unable to find client for {}, cannot check if pipeconf is set", deviceId);
Carmelo Cascone3977ea42019-02-28 13:43:42 -080078 return completedFuture(false);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070079 }
80
81 ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf);
82 if (deviceDataBuffer == null) {
83 // Hopefully the child class logged the problem.
Carmelo Cascone3977ea42019-02-28 13:43:42 -080084 return completedFuture(false);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070085 }
86
Carmelo Cascone3977ea42019-02-28 13:43:42 -080087 return client.isPipelineConfigSet(pipeconf, deviceDataBuffer);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070088 }
89
90 @Override
Carmelo Casconed61fdb32017-10-30 10:09:57 -070091 public abstract Optional<PiPipeconf> getDefaultPipeconf();
92}