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.ImmutableMap; |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
alshabib | 975617b | 2015-04-09 13:26:53 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 21 | import org.slf4j.Logger; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 22 | |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 23 | import java.util.List; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 24 | import java.util.Map; |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 25 | import java.util.Objects; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 26 | import java.util.Set; |
| 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | import static com.google.common.base.Preconditions.checkArgument; |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static com.google.common.collect.ImmutableMap.copyOf; |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 32 | import static org.slf4j.LoggerFactory.getLogger; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Default implementation of extensible driver. |
| 36 | */ |
| 37 | public class DefaultDriver implements Driver { |
| 38 | |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 39 | private final Logger log = getLogger(getClass()); |
| 40 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 41 | private final String name; |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 42 | private final List<Driver> parents; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 43 | |
| 44 | private final String manufacturer; |
| 45 | private final String hwVersion; |
| 46 | private final String swVersion; |
| 47 | |
| 48 | private final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours; |
| 49 | private final Map<String, String> properties; |
| 50 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Creates a driver with the specified name. |
| 53 | * |
| 54 | * @param name driver name |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 55 | * @param parent optional parent driver |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 56 | * @param manufacturer device manufacturer |
| 57 | * @param hwVersion device hardware version |
| 58 | * @param swVersion device software version |
| 59 | * @param behaviours device behaviour classes |
| 60 | * @param properties properties for configuration of device behaviour classes |
| 61 | */ |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 62 | @Deprecated |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 63 | public DefaultDriver(String name, Driver parent, String manufacturer, |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 64 | String hwVersion, String swVersion, |
| 65 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours, |
| 66 | Map<String, String> properties) { |
| 67 | this.name = checkNotNull(name, "Name cannot be null"); |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 68 | this.parents = parent == null ? null : Lists.newArrayList(parent); |
| 69 | this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null"); |
| 70 | this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null"); |
| 71 | this.swVersion = checkNotNull(swVersion, "SW version cannot be null"); |
| 72 | this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null")); |
| 73 | this.properties = copyOf(checkNotNull(properties, "Properties cannot be null")); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Creates a driver with the specified name. |
| 78 | * |
| 79 | * @param name driver name |
| 80 | * @param parents optional parent drivers |
| 81 | * @param manufacturer device manufacturer |
| 82 | * @param hwVersion device hardware version |
| 83 | * @param swVersion device software version |
| 84 | * @param behaviours device behaviour classes |
| 85 | * @param properties properties for configuration of device behaviour classes |
| 86 | */ |
| 87 | public DefaultDriver(String name, List<Driver> parents, String manufacturer, |
| 88 | String hwVersion, String swVersion, |
| 89 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours, |
| 90 | Map<String, String> properties) { |
| 91 | this.name = checkNotNull(name, "Name cannot be null"); |
| 92 | this.parents = parents == null || parents.isEmpty() ? null : parents; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 93 | this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null"); |
| 94 | this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null"); |
| 95 | this.swVersion = checkNotNull(swVersion, "SW version cannot be null"); |
| 96 | this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null")); |
| 97 | this.properties = copyOf(checkNotNull(properties, "Properties cannot be null")); |
| 98 | } |
| 99 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public Driver merge(Driver other) { |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 102 | checkArgument(parents == null || Objects.equals(parent(), other.parent()), |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 103 | "Parent drivers are not the same"); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 104 | // Merge the behaviours. |
alshabib | 975617b | 2015-04-09 13:26:53 -0700 | [diff] [blame] | 105 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> |
| 106 | behaviours = Maps.newHashMap(); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 107 | behaviours.putAll(this.behaviours); |
| 108 | other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b))); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 109 | |
| 110 | // Merge the properties. |
| 111 | ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 112 | properties.putAll(this.properties).putAll(other.properties()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 113 | |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 114 | return new DefaultDriver(name, other.parents(), |
| 115 | manufacturer, hwVersion, swVersion, |
alshabib | 975617b | 2015-04-09 13:26:53 -0700 | [diff] [blame] | 116 | ImmutableMap.copyOf(behaviours), properties.build()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public String name() { |
| 121 | return name; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public String manufacturer() { |
| 126 | return manufacturer; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String hwVersion() { |
| 131 | return hwVersion; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public String swVersion() { |
| 136 | return swVersion; |
| 137 | } |
| 138 | |
| 139 | @Override |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 140 | public Driver parent() { |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 141 | return parents == null ? null : parents.get(0); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public List<Driver> parents() { |
| 146 | return parents; |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 150 | public Set<Class<? extends Behaviour>> behaviours() { |
| 151 | return behaviours.keySet(); |
| 152 | } |
| 153 | |
| 154 | @Override |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 155 | public Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour) { |
| 156 | return behaviours.get(behaviour); |
| 157 | } |
| 158 | |
| 159 | @Override |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 160 | public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) { |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 161 | return behaviours.containsKey(behaviourClass) || |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 162 | (parents != null && parents.stream() |
| 163 | .filter(parent -> parent.hasBehaviour(behaviourClass)).count() > 0); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 166 | @Override |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 167 | public <T extends Behaviour> T createBehaviour(DriverData data, |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 168 | Class<T> behaviourClass) { |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 169 | T behaviour = createBehaviour(data, null, behaviourClass); |
| 170 | if (behaviour != null) { |
| 171 | return behaviour; |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 172 | } else if (parents != null) { |
| 173 | for (Driver parent : Lists.reverse(parents)) { |
| 174 | try { |
| 175 | return parent.createBehaviour(data, behaviourClass); |
| 176 | } catch (IllegalArgumentException e) { |
| 177 | log.debug("Parent {} does not support behaviour {}", parent, behaviourClass); |
| 178 | } |
| 179 | } |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 180 | } |
| 181 | throw new IllegalArgumentException(behaviourClass.getName() + " not supported"); |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public <T extends Behaviour> T createBehaviour(DriverHandler handler, |
| 186 | Class<T> behaviourClass) { |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 187 | T behaviour = createBehaviour(handler.data(), handler, behaviourClass); |
| 188 | if (behaviour != null) { |
| 189 | return behaviour; |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 190 | } else if (parents != null && !parents.isEmpty()) { |
| 191 | for (Driver parent : Lists.reverse(parents)) { |
| 192 | try { |
| 193 | return parent.createBehaviour(handler, behaviourClass); |
| 194 | } catch (IllegalArgumentException e) { |
| 195 | log.debug("Parent {} does not support behaviour {}", parent, behaviourClass); |
| 196 | } |
| 197 | } |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 198 | } |
| 199 | throw new IllegalArgumentException(behaviourClass.getName() + " not supported"); |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Creates an instance of behaviour primed with the specified driver data. |
| 203 | private <T extends Behaviour> T createBehaviour(DriverData data, DriverHandler handler, |
| 204 | Class<T> behaviourClass) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 205 | //checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass), |
| 206 | // "{} is applicable only to handler context", behaviourClass.getName()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 207 | |
| 208 | // Locate the implementation of the requested behaviour. |
| 209 | Class<? extends Behaviour> implementation = behaviours.get(behaviourClass); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 210 | if (implementation != null) { |
| 211 | // Create an instance of the behaviour and apply data as its context. |
| 212 | T behaviour = createBehaviour(behaviourClass, implementation); |
| 213 | behaviour.setData(data); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 214 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 215 | // If this is a handler behaviour, also apply handler as its context. |
| 216 | if (handler != null) { |
| 217 | ((HandlerBehaviour) behaviour).setHandler(handler); |
| 218 | } |
| 219 | return behaviour; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 220 | } |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 221 | return null; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @SuppressWarnings("unchecked") |
| 225 | private <T extends Behaviour> T createBehaviour(Class<T> behaviourClass, |
| 226 | Class<? extends Behaviour> implementation) { |
| 227 | try { |
| 228 | return (T) implementation.newInstance(); |
| 229 | } catch (InstantiationException | IllegalAccessException e) { |
| 230 | // TODO: add a specific unchecked exception |
| 231 | throw new IllegalArgumentException("Unable to create behaviour", e); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Set<String> keys() { |
| 237 | return properties.keySet(); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public String value(String key) { |
| 242 | return properties.get(key); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Map<String, String> properties() { |
| 247 | return properties; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public String toString() { |
| 252 | return toStringHelper(this) |
| 253 | .add("name", name) |
Andrea Campanella | 80520b8 | 2016-01-05 17:55:29 -0800 | [diff] [blame] | 254 | .add("parents", parents) |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 255 | .add("manufacturer", manufacturer) |
| 256 | .add("hwVersion", hwVersion) |
| 257 | .add("swVersion", swVersion) |
| 258 | .add("behaviours", behaviours) |
| 259 | .add("properties", properties) |
| 260 | .toString(); |
| 261 | } |
| 262 | |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 263 | @Override |
| 264 | public boolean equals(Object driverToBeCompared) { |
| 265 | if (this == driverToBeCompared) { |
| 266 | return true; |
| 267 | } |
| 268 | if (driverToBeCompared == null || getClass() != driverToBeCompared.getClass()) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | DefaultDriver driver = (DefaultDriver) driverToBeCompared; |
| 273 | |
| 274 | return name.equals(driver.name()); |
| 275 | |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public int hashCode() { |
| 280 | return Objects.hashCode(name); |
| 281 | } |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 282 | } |