blob: 6d91a3cdbb34a44e4de1190a4b79443d1a3e835f [file] [log] [blame]
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -08003 *
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 */
16package org.onosproject.net.driver;
17
18import com.google.common.collect.ImmutableSet;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070019import com.google.common.collect.Maps;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080020
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080021import java.util.Map;
22import java.util.Set;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25
26/**
27 * Default driver provider implementation.
28 */
29public class DefaultDriverProvider implements DriverProvider {
30
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070031 protected final Map<String, Driver> drivers = Maps.newConcurrentMap();
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080032
33 @Override
34 public Set<Driver> getDrivers() {
35 return ImmutableSet.copyOf(drivers.values());
36 }
37
38 /**
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070039 * Adds the specified drivers to the provider.
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080040 *
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070041 * @param drivers drivers to be added
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080042 */
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070043 public void addDrivers(Set<Driver> drivers) {
44 drivers.forEach(this::addDriver);
45 }
46
47 /**
Thomas Vachuskabb062ec2016-01-25 16:49:47 -080048 * 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 Vachuska5c2f8132015-04-08 23:09:08 -070052 *
53 * @param driver driver to be provided
Thomas Vachuskabb062ec2016-01-25 16:49:47 -080054 * @return registered driver
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070055 */
Thomas Vachuskabb062ec2016-01-25 16:49:47 -080056 public Driver addDriver(Driver driver) {
57 return drivers.compute(driver.name(), (name, oldDriver) ->
58 oldDriver == null ? driver : oldDriver.merge(driver));
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080059 }
60
61 /**
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070062 * Removes the specified drivers from the provider.
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080063 *
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070064 * @param drivers drivers to be removed
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080065 */
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070066 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 Vachuskaa8f4e7d2015-01-08 17:31:55 -080078 }
79
80 @Override
81 public String toString() {
82 return toStringHelper(this).add("drivers", drivers).toString();
83 }
84}