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 | |
| 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 | /** |
Thomas Vachuska | bb062ec | 2016-01-25 16:49:47 -0800 | [diff] [blame] | 48 | * Adds the specified driver to the provider. If a driver with the same |
| 49 | * does not exist yet, the specified one will be added. Otherwise, the |
| 50 | * existing driver will be merged with the new one and the result will be |
| 51 | * registered. |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 52 | * |
| 53 | * @param driver driver to be provided |
Thomas Vachuska | bb062ec | 2016-01-25 16:49:47 -0800 | [diff] [blame] | 54 | * @return registered driver |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 55 | */ |
Thomas Vachuska | bb062ec | 2016-01-25 16:49:47 -0800 | [diff] [blame] | 56 | public Driver addDriver(Driver driver) { |
| 57 | return drivers.compute(driver.name(), (name, oldDriver) -> |
| 58 | oldDriver == null ? driver : oldDriver.merge(driver)); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 62 | * Removes the specified drivers from the provider. |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 63 | * |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 64 | * @param drivers drivers to be removed |
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 | public void removeDrivers(Set<Driver> drivers) { |
| 67 | drivers.forEach(this::removeDriver); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Removes the specified driver from the provider. |
| 72 | * |
| 73 | * @param driver driver to be removed |
| 74 | */ |
| 75 | public void removeDriver(Driver driver) { |
| 76 | // TODO: make selective if possible |
| 77 | drivers.remove(driver.name()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public String toString() { |
| 82 | return toStringHelper(this).add("drivers", drivers).toString(); |
| 83 | } |
| 84 | } |