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; |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 20 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 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 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 31 | protected final Map<String, Driver> drivers = Maps.newConcurrentMap(); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 32 | |
| 33 | @Override |
| 34 | public Set<Driver> getDrivers() { |
| 35 | return ImmutableSet.copyOf(drivers.values()); |
| 36 | } |
| 37 | |
| 38 | /** |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 39 | * Adds the specified drivers to the provider. |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 40 | * |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 41 | * @param drivers drivers to be added |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 42 | */ |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 43 | public void addDrivers(Set<Driver> drivers) { |
| 44 | drivers.forEach(this::addDriver); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Adds the specified driver to the provider. |
| 49 | * |
| 50 | * @param driver driver to be provided |
| 51 | */ |
| 52 | public void addDriver(Driver driver) { |
| 53 | Driver ddc = drivers.get(driver.name()); |
| 54 | if (ddc == null) { |
| 55 | // If we don't have the driver yet, just use the new one. |
| 56 | drivers.put(driver.name(), driver); |
| 57 | } else { |
| 58 | // Otherwise merge the existing driver with the new one and rebind. |
| 59 | drivers.put(driver.name(), ddc.merge(driver)); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 64 | * Removes the specified drivers from the provider. |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 65 | * |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 66 | * @param drivers drivers to be removed |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 67 | */ |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 68 | public void removeDrivers(Set<Driver> drivers) { |
| 69 | drivers.forEach(this::removeDriver); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Removes the specified driver from the provider. |
| 74 | * |
| 75 | * @param driver driver to be removed |
| 76 | */ |
| 77 | public void removeDriver(Driver driver) { |
| 78 | // TODO: make selective if possible |
| 79 | drivers.remove(driver.name()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String toString() { |
| 84 | return toStringHelper(this).add("drivers", drivers).toString(); |
| 85 | } |
| 86 | } |