alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [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.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.driver.Behaviour; |
| 27 | import org.onosproject.net.driver.DefaultDriverData; |
| 28 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 29 | import org.onosproject.net.driver.Driver; |
| 30 | import org.onosproject.net.driver.DriverAdminService; |
| 31 | import org.onosproject.net.driver.DriverData; |
| 32 | import org.onosproject.net.driver.DriverHandler; |
| 33 | import org.onosproject.net.driver.DriverProvider; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | import java.util.Map; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | |
| 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public class DriverManager implements DriverAdminService { |
| 44 | |
| 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | |
| 47 | private Set<DriverProvider> providers = Sets.newConcurrentHashSet(); |
| 48 | private Map<String, Driver> driverByName = Maps.newConcurrentMap(); |
| 49 | private Map<String, Driver> driverByKey = Maps.newConcurrentMap(); |
| 50 | |
| 51 | @Activate |
| 52 | protected void activate() { |
| 53 | log.info("Started"); |
| 54 | } |
| 55 | |
| 56 | @Deactivate |
| 57 | protected void deactivate() { |
| 58 | log.info("Stopped"); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | @Override |
| 63 | public Set<DriverProvider> getProviders() { |
| 64 | return ImmutableSet.copyOf(providers); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void registerProvider(DriverProvider provider) { |
| 69 | provider.getDrivers().forEach(driver -> { |
| 70 | driverByName.put(driver.name(), driver); |
| 71 | driverByKey.put(key(driver.manufacturer(), |
| 72 | driver.hwVersion(), |
| 73 | driver.swVersion()), driver); |
| 74 | }); |
| 75 | providers.add(provider); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void unregisterProvider(DriverProvider provider) { |
| 80 | provider.getDrivers().forEach(driver -> { |
| 81 | driverByName.remove(driver.name()); |
| 82 | driverByKey.remove(key(driver.manufacturer(), |
| 83 | driver.hwVersion(), |
| 84 | driver.swVersion())); |
| 85 | }); |
| 86 | providers.remove(provider); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Set<Driver> getDrivers(Class<? extends Behaviour>... withBehaviours) { |
| 91 | //TODO |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Driver getDriver(String driverName) { |
| 97 | //TODO: replace with fallback driver. |
| 98 | return driverByName.getOrDefault(driverName, driverByName.get("default")); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Driver getDriver(String mfr, String hw, String sw) { |
| 103 | return driverByKey.getOrDefault(key(mfr, hw, sw), driverByName.get("default")); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public DriverHandler createHandler(String driverName, DeviceId deviceId, String... credentials) { |
| 108 | Driver driver = driverByName.get(driverName); |
| 109 | return new DefaultDriverHandler(new DefaultDriverData(driver)); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public DriverHandler createHandler(DriverData data, DeviceId deviceId, String... credentials) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | private String key(String mfr, String hw, String sw) { |
| 118 | return String.format("%s-%s-%s", mfr, hw, sw); |
| 119 | } |
| 120 | |
| 121 | } |