blob: fe075767f23ce4fbf47d197d6c0f0708d8202e81 [file] [log] [blame]
Carmelo Casconeb5324e72018-11-25 02:26:32 -08001/*
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 Cascone36d5e7a2019-09-25 01:02:53 -070017package org.onosproject.pipelines.fabric.impl.behaviour;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080018
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.driver.AbstractHandlerBehaviour;
21import org.onosproject.net.driver.DriverHandler;
22import org.onosproject.net.pi.model.PiPipeconfId;
23import org.onosproject.net.pi.service.PiPipeconfService;
24import org.slf4j.Logger;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27import static java.lang.String.format;
28import static org.slf4j.LoggerFactory.getLogger;
29
30/**
31 * Abstract implementation of HandlerBehaviour for the fabric pipeconf
32 * behaviors.
Daniele Moro77ef35c2019-07-30 10:43:10 -070033 * <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 Casconeb5324e72018-11-25 02:26:32 -080036 */
Daniele Moro77ef35c2019-07-30 10:43:10 -070037public abstract class AbstractFabricHandlerBehavior extends AbstractHandlerBehaviour {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080038
39 protected final Logger log = getLogger(getClass());
40
41 protected FabricCapabilities capabilities;
42
Daniele Moro77ef35c2019-07-30 10:43:10 -070043 /**
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 Casconeb5324e72018-11-25 02:26:32 -080066 @Override
67 public void setHandler(DriverHandler handler) {
68 super.setHandler(handler);
69 final PiPipeconfService pipeconfService = handler().get(PiPipeconfService.class);
Daniele Moro77ef35c2019-07-30 10:43:10 -070070 setCapabilitiesFromHandler(handler().data().deviceId(), pipeconfService);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080071 }
72
Daniele Moro77ef35c2019-07-30 10:43:10 -070073 private void setCapabilitiesFromHandler(
74 DeviceId deviceId, PiPipeconfService pipeconfService) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080075 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}