Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | |
| 19 | import org.onosproject.net.Device; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.device.DeviceService; |
| 22 | import org.onosproject.net.driver.DriverHandler; |
| 23 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import java.io.UnsupportedEncodingException; |
| 28 | import java.net.URI; |
| 29 | import java.net.URLDecoder; |
| 30 | |
| 31 | /** |
| 32 | * Utility class for the P4Runtime driver. |
| 33 | */ |
| 34 | final class P4RuntimeDriverUtils { |
| 35 | |
| 36 | private static final String DEVICE_ID_PARAM = "device_id="; |
| 37 | |
| 38 | private static final Logger log = LoggerFactory.getLogger(P4RuntimeDriverUtils.class); |
| 39 | |
| 40 | private P4RuntimeDriverUtils() { |
| 41 | // Hide constructor. |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns an instance of the interpreter implementation for this device, |
| 46 | * null if an interpreter cannot be retrieved. |
| 47 | * |
| 48 | * @param handler driver handler |
| 49 | * @return interpreter or null |
| 50 | */ |
| 51 | static PiPipelineInterpreter getInterpreter(DriverHandler handler) { |
| 52 | final DeviceId deviceId = handler.data().deviceId(); |
| 53 | final Device device = handler.get(DeviceService.class).getDevice(deviceId); |
| 54 | if (device == null) { |
| 55 | log.warn("Unable to find device {}, cannot get interpreter", deviceId); |
| 56 | return null; |
| 57 | } |
| 58 | if (!device.is(PiPipelineInterpreter.class)) { |
| 59 | log.warn("Unable to get interpreter for {}, missing behaviour", |
| 60 | deviceId); |
| 61 | return null; |
| 62 | } |
| 63 | return device.as(PiPipelineInterpreter.class); |
| 64 | } |
| 65 | |
| 66 | static Long extractP4DeviceId(URI uri) { |
| 67 | if (uri == null) { |
| 68 | return null; |
| 69 | } |
| 70 | String[] segments = uri.getRawQuery().split("&"); |
| 71 | try { |
| 72 | for (String s : segments) { |
| 73 | if (s.startsWith(DEVICE_ID_PARAM)) { |
| 74 | return Long.parseUnsignedLong( |
| 75 | URLDecoder.decode( |
| 76 | s.substring(DEVICE_ID_PARAM.length()), "utf-8")); |
| 77 | } |
| 78 | } |
| 79 | } catch (UnsupportedEncodingException e) { |
| 80 | log.error("Unable to decode P4Runtime-internal device_id from URI {}: {}", |
| 81 | uri, e.toString()); |
| 82 | } catch (NumberFormatException e) { |
| 83 | log.error("Invalid P4Runtime-internal device_id in URI {}: {}", |
| 84 | uri, e.toString()); |
| 85 | } |
| 86 | log.error("Missing P4Runtime-internal device_id in URI {}", uri); |
| 87 | return null; |
| 88 | } |
| 89 | } |