Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.p4runtime; |
| 18 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 19 | import com.google.common.base.Strings; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 22 | import org.onosproject.net.config.NetworkConfigService; |
| 23 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 26 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 27 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 28 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 29 | import org.onosproject.net.pi.service.PiTranslationService; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 30 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 31 | import org.onosproject.p4runtime.api.P4RuntimeClientKey; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 32 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 36 | import java.net.URI; |
| 37 | import java.net.URISyntaxException; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 38 | |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 41 | /** |
| 42 | * Abstract implementation of a behaviour handler for a P4Runtime device. |
| 43 | */ |
| 44 | public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour { |
| 45 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 46 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 47 | |
| 48 | // Initialized by setupBehaviour() |
| 49 | protected DeviceId deviceId; |
| 50 | protected DeviceService deviceService; |
| 51 | protected Device device; |
| 52 | protected P4RuntimeController controller; |
| 53 | protected PiPipeconf pipeconf; |
| 54 | protected P4RuntimeClient client; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 55 | protected PiTranslationService translationService; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 56 | |
| 57 | /** |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 58 | * Initializes this behaviour attributes. Returns true if the operation was |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 59 | * successful, false otherwise. This method assumes that the P4Runtime |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 60 | * controller already has a client for this device and that the device has |
| 61 | * been created in the core. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 62 | * |
| 63 | * @return true if successful, false otherwise |
| 64 | */ |
| 65 | protected boolean setupBehaviour() { |
| 66 | deviceId = handler().data().deviceId(); |
| 67 | |
| 68 | deviceService = handler().get(DeviceService.class); |
| 69 | device = deviceService.getDevice(deviceId); |
| 70 | if (device == null) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 71 | log.warn("Unable to find device with id {}", deviceId); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
| 75 | controller = handler().get(P4RuntimeController.class); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 76 | client = controller.getClient(deviceId); |
| 77 | if (client == null) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 78 | log.warn("Unable to find client for {}", deviceId); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 79 | return false; |
| 80 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 81 | |
| 82 | PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 83 | if (!piPipeconfService.getPipeconf(deviceId).isPresent()) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 84 | log.warn("Unable to get assigned pipeconf for {} (mapping " + |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 85 | "missing in PiPipeconfService)", |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 86 | deviceId); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 87 | return false; |
| 88 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 89 | pipeconf = piPipeconfService.getPipeconf(deviceId).get(); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 90 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 91 | translationService = handler().get(PiTranslationService.class); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 92 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /** |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 97 | * Returns an instance of the interpreter implementation for this device, |
| 98 | * null if an interpreter cannot be retrieved. |
| 99 | * |
| 100 | * @return interpreter or null |
| 101 | */ |
| 102 | PiPipelineInterpreter getInterpreter() { |
| 103 | if (!device.is(PiPipelineInterpreter.class)) { |
| 104 | log.warn("Unable to get interpreter for {}, missing behaviour", |
| 105 | deviceId); |
| 106 | return null; |
| 107 | } |
| 108 | return device.as(PiPipelineInterpreter.class); |
| 109 | } |
| 110 | |
| 111 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 112 | * Returns a P4Runtime client previsouly created for this device, null if |
| 113 | * such client does not exist. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 114 | * |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 115 | * @return client or null |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 116 | */ |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 117 | P4RuntimeClient getClientByKey() { |
| 118 | final P4RuntimeClientKey clientKey = clientKey(); |
| 119 | if (clientKey == null) { |
| 120 | return null; |
| 121 | } |
| 122 | return handler().get(P4RuntimeController.class).getClient(clientKey); |
| 123 | } |
| 124 | |
| 125 | protected P4RuntimeClientKey clientKey() { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 126 | deviceId = handler().data().deviceId(); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 127 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 128 | final BasicDeviceConfig cfg = handler().get(NetworkConfigService.class) |
| 129 | .getConfig(deviceId, BasicDeviceConfig.class); |
| 130 | if (cfg == null || Strings.isNullOrEmpty(cfg.managementAddress())) { |
| 131 | log.error("Missing or invalid config for {}, cannot derive " + |
| 132 | "P4Runtime server endpoints", deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 133 | return null; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 136 | try { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 137 | return new P4RuntimeClientKey( |
| 138 | deviceId, new URI(cfg.managementAddress())); |
| 139 | } catch (URISyntaxException e) { |
| 140 | log.error("Management address of {} is not a valid URI: {}", |
| 141 | deviceId, cfg.managementAddress()); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 142 | return null; |
| 143 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 144 | } |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 145 | |
| 146 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 147 | * Returns the value of the given driver property, if present, otherwise |
| 148 | * returns the given default value. |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 149 | * |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 150 | * @param propName property name |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 151 | * @param defaultVal default value |
| 152 | * @return boolean |
| 153 | */ |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 154 | boolean driverBoolProperty(String propName, boolean defaultVal) { |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 155 | checkNotNull(propName); |
| 156 | if (handler().driver().getProperty(propName) == null) { |
| 157 | return defaultVal; |
| 158 | } else { |
| 159 | return Boolean.parseBoolean(handler().driver().getProperty(propName)); |
| 160 | } |
| 161 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 162 | } |