Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 17 | package org.onosproject.pipelines.fabric.impl.behaviour; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 21 | import org.onosproject.net.driver.DriverHandler; |
| 22 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 23 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static java.lang.String.format; |
| 28 | import static org.slf4j.LoggerFactory.getLogger; |
| 29 | |
| 30 | /** |
| 31 | * Abstract implementation of HandlerBehaviour for the fabric pipeconf |
| 32 | * behaviors. |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 33 | * <p> |
| 34 | * All sub-classes must implement a default constructor, used by the abstract |
| 35 | * projectable model (i.e., {@link org.onosproject.net.Device#as(Class)}. |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 36 | */ |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 37 | public abstract class AbstractFabricHandlerBehavior extends AbstractHandlerBehaviour { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 38 | |
| 39 | protected final Logger log = getLogger(getClass()); |
| 40 | |
| 41 | protected FabricCapabilities capabilities; |
| 42 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Creates a new instance of this behavior with the given capabilities. |
| 45 | * Note: this constructor should be invoked only by other classes of this |
| 46 | * package that can retrieve capabilities on their own. |
| 47 | * <p> |
| 48 | * When using the abstract projectable model (i.e., {@link |
| 49 | * org.onosproject.net.Device#as(Class)}, capabilities will be set by the |
| 50 | * driver manager when calling {@link #setHandler(DriverHandler)}) |
| 51 | * |
| 52 | * @param capabilities capabilities |
| 53 | */ |
| 54 | protected AbstractFabricHandlerBehavior(FabricCapabilities capabilities) { |
| 55 | this.capabilities = capabilities; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Create a new instance of this behaviour. Used by the abstract projectable |
| 60 | * model (i.e., {@link org.onosproject.net.Device#as(Class)}. |
| 61 | */ |
| 62 | public AbstractFabricHandlerBehavior() { |
| 63 | // Do nothing |
| 64 | } |
| 65 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 66 | @Override |
| 67 | public void setHandler(DriverHandler handler) { |
| 68 | super.setHandler(handler); |
| 69 | final PiPipeconfService pipeconfService = handler().get(PiPipeconfService.class); |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 70 | setCapabilitiesFromHandler(handler().data().deviceId(), pipeconfService); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 73 | private void setCapabilitiesFromHandler( |
| 74 | DeviceId deviceId, PiPipeconfService pipeconfService) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 75 | checkNotNull(deviceId); |
| 76 | checkNotNull(pipeconfService); |
| 77 | // Get pipeconf capabilities. |
| 78 | final PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId) |
| 79 | .orElse(null); |
| 80 | if (pipeconfId == null) { |
| 81 | throw new IllegalStateException(format( |
| 82 | "Unable to get pipeconf ID of device %s", deviceId.toString())); |
| 83 | } |
| 84 | if (!pipeconfService.getPipeconf(pipeconfId).isPresent()) { |
| 85 | throw new IllegalStateException(format( |
| 86 | "Pipeconf '%s' is not registered ", pipeconfId)); |
| 87 | } |
| 88 | this.capabilities = new FabricCapabilities( |
| 89 | pipeconfService.getPipeconf(pipeconfId).get()); |
| 90 | } |
| 91 | } |