blob: 8e2b54d4cf2a09273865d66287954ec390c524c2 [file] [log] [blame]
Carmelo Casconee3a7c742017-09-01 01:25:52 +02001/*
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 Casconee3a7c742017-09-01 01:25:52 +020019import org.onosproject.net.Device;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.device.DeviceService;
22import org.onosproject.net.driver.AbstractHandlerBehaviour;
23import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080024import org.onosproject.net.pi.service.PiPipeconfService;
25import org.onosproject.net.pi.service.PiTranslationService;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020026import org.onosproject.p4runtime.api.P4RuntimeClient;
27import org.onosproject.p4runtime.api.P4RuntimeController;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Carmelo Cascone3da671a2018-02-12 10:43:35 -080031import static com.google.common.base.Preconditions.checkNotNull;
32
Carmelo Casconee3a7c742017-09-01 01:25:52 +020033/**
34 * Abstract implementation of a behaviour handler for a P4Runtime device.
35 */
36public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour {
37
38 public static final String P4RUNTIME_SERVER_ADDR_KEY = "p4runtime_ip";
39 public static final String P4RUNTIME_SERVER_PORT_KEY = "p4runtime_port";
40 public static final String P4RUNTIME_DEVICE_ID_KEY = "p4runtime_deviceId";
41
42 protected final Logger log = LoggerFactory.getLogger(getClass());
43
44 // Initialized by setupBehaviour()
45 protected DeviceId deviceId;
46 protected DeviceService deviceService;
47 protected Device device;
48 protected P4RuntimeController controller;
49 protected PiPipeconf pipeconf;
50 protected P4RuntimeClient client;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020051 protected PiTranslationService piTranslationService;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020052
53 /**
Carmelo Cascone3da671a2018-02-12 10:43:35 -080054 * Initializes this behaviour attributes. Returns true if the operation was
55 * successful, false otherwise. This method assumes that the P4runtime
56 * controller already has a client for this device and that the device has
57 * been created in the core.
Carmelo Casconee3a7c742017-09-01 01:25:52 +020058 *
59 * @return true if successful, false otherwise
60 */
61 protected boolean setupBehaviour() {
62 deviceId = handler().data().deviceId();
63
64 deviceService = handler().get(DeviceService.class);
65 device = deviceService.getDevice(deviceId);
66 if (device == null) {
67 log.warn("Unable to find device with id {}, aborting operation", deviceId);
68 return false;
69 }
70
71 controller = handler().get(P4RuntimeController.class);
72 if (!controller.hasClient(deviceId)) {
73 log.warn("Unable to find client for {}, aborting operation", deviceId);
74 return false;
75 }
76 client = controller.getClient(deviceId);
77
78 PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
79 if (!piPipeconfService.ofDevice(deviceId).isPresent() ||
80 !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
81 log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId);
82 return false;
83 }
84 pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
85
Carmelo Cascone87b9b392017-10-02 18:33:20 +020086 piTranslationService = handler().get(PiTranslationService.class);
87
Carmelo Casconee3a7c742017-09-01 01:25:52 +020088 return true;
89 }
90
91 /**
Carmelo Cascone3da671a2018-02-12 10:43:35 -080092 * Create a P4Runtime client for this device. Returns true if the operation
93 * was successful, false otherwise.
Carmelo Casconee3a7c742017-09-01 01:25:52 +020094 *
95 * @return true if successful, false otherwise
96 */
97 protected boolean createClient() {
98 deviceId = handler().data().deviceId();
99 controller = handler().get(P4RuntimeController.class);
100
Carmelo Cascone44448a52018-06-25 23:36:57 +0200101 final String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY);
102 final String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY);
103 final String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY);
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200104
105 if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) {
106 log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})",
107 deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY);
108 return false;
109 }
110
Carmelo Cascone44448a52018-06-25 23:36:57 +0200111 if (!controller.createClient(deviceId, serverAddr,
112 Integer.parseUnsignedInt(serverPortString),
113 Long.parseUnsignedLong(p4DeviceIdString))) {
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200114 log.warn("Unable to create client for {}, aborting operation", deviceId);
115 return false;
116 }
117
118 return true;
119 }
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800120
121 /**
122 * Returns the value of the given driver property, if present,
123 * otherwise returns the given default value.
124 *
125 * @param propName property name
126 * @param defaultVal default value
127 * @return boolean
128 */
129 protected boolean driverBoolProperty(String propName, boolean defaultVal) {
130 checkNotNull(propName);
131 if (handler().driver().getProperty(propName) == null) {
132 return defaultVal;
133 } else {
134 return Boolean.parseBoolean(handler().driver().getProperty(propName));
135 }
136 }
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200137}