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 | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 19 | import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 20 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 21 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 22 | import org.onosproject.net.pi.service.PiTranslationService; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 23 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 24 | import org.onosproject.p4runtime.api.P4RuntimeController; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 25 | |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 27 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverUtils.extractP4DeviceId; |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 28 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 29 | /** |
| 30 | * Abstract implementation of a behaviour handler for a P4Runtime device. |
| 31 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 32 | public abstract class AbstractP4RuntimeHandlerBehaviour |
| 33 | extends AbstractGrpcHandlerBehaviour<P4RuntimeClient, P4RuntimeController> { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 34 | |
| 35 | // Initialized by setupBehaviour() |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 36 | protected Long p4DeviceId; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 37 | protected PiPipeconf pipeconf; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 38 | PiTranslationService translationService; |
| 39 | |
| 40 | |
Daniele Moro | 464e5ed | 2019-07-25 14:45:01 -0700 | [diff] [blame] | 41 | public AbstractP4RuntimeHandlerBehaviour() { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 42 | super(P4RuntimeController.class); |
| 43 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 44 | |
| 45 | /** |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 46 | * Initializes this behaviour attributes. Returns true if the operation was |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 47 | * successful, false otherwise. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 48 | * |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 49 | * @param opName name of the operation |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 50 | * @return true if successful, false otherwise |
| 51 | */ |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 52 | protected boolean setupBehaviour(String opName) { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 53 | if (!super.setupBehaviour(opName)) { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 54 | return false; |
| 55 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 56 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 57 | p4DeviceId = extractP4DeviceId(mgmtUriFromNetcfg()); |
| 58 | if (p4DeviceId == null) { |
| 59 | log.warn("Unable to obtain P4Runtime-internal device_id from " + |
| 60 | "config of {}, cannot perform {}", |
| 61 | deviceId, opName); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | final PiPipeconfService pipeconfService = handler().get( |
| 66 | PiPipeconfService.class); |
Carmelo Cascone | 5505a6d | 2019-04-17 20:03:24 -0700 | [diff] [blame] | 67 | pipeconf = pipeconfService.getPipeconf(deviceId).orElse(null); |
| 68 | if (pipeconf == null) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 69 | log.warn("Missing pipeconf for {}, cannot perform {}", deviceId, opName); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 70 | return false; |
| 71 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 72 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 73 | translationService = handler().get(PiTranslationService.class); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 74 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
| 78 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 79 | * Returns the value of the given driver property, if present, otherwise |
| 80 | * returns the given default value. |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 81 | * |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 82 | * @param propName property name |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 83 | * @param defaultVal default value |
| 84 | * @return boolean |
| 85 | */ |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 86 | boolean driverBoolProperty(String propName, boolean defaultVal) { |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 87 | checkNotNull(propName); |
| 88 | if (handler().driver().getProperty(propName) == null) { |
| 89 | return defaultVal; |
| 90 | } else { |
| 91 | return Boolean.parseBoolean(handler().driver().getProperty(propName)); |
| 92 | } |
| 93 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 94 | } |