blob: b2b5281cafc0e3a132a7748b707cea66e21dfdd5 [file] [log] [blame]
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -08001/*
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 */
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 /**
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 Vachuskaa8f4e7d2015-01-08 17:31:55 -080060 }
61 }
62
63 /**
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070064 * Removes the specified drivers from the provider.
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080065 *
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070066 * @param drivers drivers to be removed
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080067 */
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070068 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 Vachuskaa8f4e7d2015-01-08 17:31:55 -080080 }
81
82 @Override
83 public String toString() {
84 return toStringHelper(this).add("drivers", drivers).toString();
85 }
86}