Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 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 | package org.onosproject.net.driver; |
| 17 | |
Thomas Vachuska | d5d113b | 2015-07-10 12:12:01 -0700 | [diff] [blame] | 18 | import org.onlab.osgi.DefaultServiceDirectory; |
| 19 | import org.onlab.osgi.ServiceDirectory; |
| 20 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 21 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 22 | |
| 23 | /** |
| 24 | * Default implementation of driver handler. |
| 25 | */ |
| 26 | public class DefaultDriverHandler implements DriverHandler { |
| 27 | |
Thomas Vachuska | 7b3fe3a | 2016-02-01 11:26:22 -0800 | [diff] [blame] | 28 | private final DriverData data; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 29 | |
Thomas Vachuska | d5d113b | 2015-07-10 12:12:01 -0700 | [diff] [blame] | 30 | // Reference to service directory to provide run-time context. |
| 31 | protected static ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 32 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Creates new driver handler with the attached driver data. |
| 35 | * |
| 36 | * @param data driver data to attach |
| 37 | */ |
Thomas Vachuska | 7b3fe3a | 2016-02-01 11:26:22 -0800 | [diff] [blame] | 38 | public DefaultDriverHandler(DriverData data) { |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 39 | this.data = data; |
| 40 | } |
| 41 | |
| 42 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 43 | public Driver driver() { |
| 44 | return data.driver(); |
| 45 | } |
| 46 | |
| 47 | @Override |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 48 | public DriverData data() { |
| 49 | return data; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public <T extends Behaviour> T behaviour(Class<T> behaviourClass) { |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 54 | return data.driver().createBehaviour(this, behaviourClass); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
Thomas Vachuska | d5d113b | 2015-07-10 12:12:01 -0700 | [diff] [blame] | 58 | public <T> T get(Class<T> serviceClass) { |
| 59 | return serviceDirectory.get(serviceClass); |
| 60 | } |
| 61 | |
| 62 | @Override |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 63 | public String toString() { |
Thomas Vachuska | d5d113b | 2015-07-10 12:12:01 -0700 | [diff] [blame] | 64 | return toStringHelper(this).add("data", data).toString(); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } |