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 | |
| 17 | package org.onosproject.pipelines.fabric; |
| 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. |
| 33 | */ |
| 34 | public class AbstractFabricHandlerBehavior extends AbstractHandlerBehaviour { |
| 35 | |
| 36 | protected final Logger log = getLogger(getClass()); |
| 37 | |
| 38 | protected FabricCapabilities capabilities; |
| 39 | |
| 40 | @Override |
| 41 | public void setHandler(DriverHandler handler) { |
| 42 | super.setHandler(handler); |
| 43 | final PiPipeconfService pipeconfService = handler().get(PiPipeconfService.class); |
| 44 | setCapabilities(handler().data().deviceId(), pipeconfService); |
| 45 | } |
| 46 | |
| 47 | private void setCapabilities(DeviceId deviceId, PiPipeconfService pipeconfService) { |
| 48 | checkNotNull(deviceId); |
| 49 | checkNotNull(pipeconfService); |
| 50 | // Get pipeconf capabilities. |
| 51 | final PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId) |
| 52 | .orElse(null); |
| 53 | if (pipeconfId == null) { |
| 54 | throw new IllegalStateException(format( |
| 55 | "Unable to get pipeconf ID of device %s", deviceId.toString())); |
| 56 | } |
| 57 | if (!pipeconfService.getPipeconf(pipeconfId).isPresent()) { |
| 58 | throw new IllegalStateException(format( |
| 59 | "Pipeconf '%s' is not registered ", pipeconfId)); |
| 60 | } |
| 61 | this.capabilities = new FabricCapabilities( |
| 62 | pipeconfService.getPipeconf(pipeconfId).get()); |
| 63 | } |
| 64 | } |