Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 25 | |
| 26 | /** |
| 27 | * Default driver provider implementation. |
| 28 | */ |
| 29 | public class DefaultDriverProvider implements DriverProvider { |
| 30 | |
| 31 | private final Map<String, DefaultDriver> drivers = new HashMap<>(); |
| 32 | |
| 33 | @Override |
| 34 | public Set<Driver> getDrivers() { |
| 35 | return ImmutableSet.copyOf(drivers.values()); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Adds the specified driver to be provided. |
| 40 | * |
| 41 | * @param driverClasses driver to be provided |
| 42 | */ |
| 43 | public void addDrivers(Set<DefaultDriver> driverClasses) { |
| 44 | for (DefaultDriver driverClass : driverClasses) { |
| 45 | addDriver(driverClass); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Adds the specified driver to be provided. |
| 51 | * |
| 52 | * @param driverClass driver to be provided |
| 53 | */ |
| 54 | public void addDriver(DefaultDriver driverClass) { |
| 55 | DefaultDriver ddc = drivers.get(driverClass.name()); |
| 56 | if (ddc == null) { |
| 57 | // If we don't have the driver yet, just use the new one. |
| 58 | drivers.put(driverClass.name(), driverClass); |
| 59 | } else { |
| 60 | // Otherwise merge the existing driver with the new one and rebind. |
| 61 | drivers.put(driverClass.name(), ddc.merge(driverClass)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public String toString() { |
| 67 | return toStringHelper(this).add("drivers", drivers).toString(); |
| 68 | } |
| 69 | } |