blob: 5472663003384f8325dc8dabd2bbb99e2dbe9734 [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.ImmutableMap;
Andrea Campanella80520b82016-01-05 17:55:29 -080019import com.google.common.collect.Lists;
alshabib975617b2015-04-09 13:26:53 -070020import com.google.common.collect.Maps;
Andrea Campanella80520b82016-01-05 17:55:29 -080021import org.slf4j.Logger;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080022
Andrea Campanella28dbe8f2016-01-28 17:51:58 -080023import java.util.ArrayList;
Andrea Campanella80520b82016-01-05 17:55:29 -080024import java.util.List;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080025import java.util.Map;
Thomas Vachuska635c2d72015-05-08 14:32:13 -070026import java.util.Objects;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080027import java.util.Set;
28
29import static com.google.common.base.MoreObjects.toStringHelper;
30import static com.google.common.base.Preconditions.checkArgument;
31import static com.google.common.base.Preconditions.checkNotNull;
32import static com.google.common.collect.ImmutableMap.copyOf;
Andrea Campanella80520b82016-01-05 17:55:29 -080033import static org.slf4j.LoggerFactory.getLogger;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080034
35/**
36 * Default implementation of extensible driver.
37 */
38public class DefaultDriver implements Driver {
39
Andrea Campanella80520b82016-01-05 17:55:29 -080040 private final Logger log = getLogger(getClass());
41
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080042 private final String name;
Andrea Campanella80520b82016-01-05 17:55:29 -080043 private final List<Driver> parents;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080044
45 private final String manufacturer;
46 private final String hwVersion;
47 private final String swVersion;
48
49 private final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours;
50 private final Map<String, String> properties;
51
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080052 /**
53 * Creates a driver with the specified name.
54 *
55 * @param name driver name
Thomas Vachuska635c2d72015-05-08 14:32:13 -070056 * @param parent optional parent driver
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080057 * @param manufacturer device manufacturer
58 * @param hwVersion device hardware version
59 * @param swVersion device software version
60 * @param behaviours device behaviour classes
61 * @param properties properties for configuration of device behaviour classes
62 */
Andrea Campanella80520b82016-01-05 17:55:29 -080063 @Deprecated
Thomas Vachuska635c2d72015-05-08 14:32:13 -070064 public DefaultDriver(String name, Driver parent, String manufacturer,
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080065 String hwVersion, String swVersion,
66 Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
67 Map<String, String> properties) {
68 this.name = checkNotNull(name, "Name cannot be null");
Andrea Campanella80520b82016-01-05 17:55:29 -080069 this.parents = parent == null ? null : Lists.newArrayList(parent);
70 this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null");
71 this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null");
72 this.swVersion = checkNotNull(swVersion, "SW version cannot be null");
73 this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null"));
74 this.properties = copyOf(checkNotNull(properties, "Properties cannot be null"));
75 }
76
77 /**
78 * Creates a driver with the specified name.
79 *
80 * @param name driver name
81 * @param parents optional parent drivers
82 * @param manufacturer device manufacturer
83 * @param hwVersion device hardware version
84 * @param swVersion device software version
85 * @param behaviours device behaviour classes
86 * @param properties properties for configuration of device behaviour classes
87 */
88 public DefaultDriver(String name, List<Driver> parents, String manufacturer,
89 String hwVersion, String swVersion,
90 Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
91 Map<String, String> properties) {
92 this.name = checkNotNull(name, "Name cannot be null");
93 this.parents = parents == null || parents.isEmpty() ? null : parents;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080094 this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null");
95 this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null");
96 this.swVersion = checkNotNull(swVersion, "SW version cannot be null");
97 this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null"));
98 this.properties = copyOf(checkNotNull(properties, "Properties cannot be null"));
99 }
100
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700101 @Override
102 public Driver merge(Driver other) {
Andrea Campanella80520b82016-01-05 17:55:29 -0800103 checkArgument(parents == null || Objects.equals(parent(), other.parent()),
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700104 "Parent drivers are not the same");
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800105 // Merge the behaviours.
alshabib975617b2015-04-09 13:26:53 -0700106 Map<Class<? extends Behaviour>, Class<? extends Behaviour>>
107 behaviours = Maps.newHashMap();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700108 behaviours.putAll(this.behaviours);
109 other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b)));
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800110
111 // Merge the properties.
112 ImmutableMap.Builder<String, String> properties = ImmutableMap.builder();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700113 properties.putAll(this.properties).putAll(other.properties());
Andrea Campanella28dbe8f2016-01-28 17:51:58 -0800114 List<Driver> completeParents = new ArrayList<>();
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800115
Andrea Campanella28dbe8f2016-01-28 17:51:58 -0800116 if (parents != null) {
117 parents.forEach(parent -> other.parents().forEach(otherParent -> {
118 if (otherParent.name().equals(parent.name())) {
119 completeParents.add(parent.merge(otherParent));
120 } else if (!completeParents.contains(otherParent)) {
121 completeParents.add(otherParent);
122 } else if (!completeParents.contains(parent)) {
123 completeParents.add(parent);
124 }
125 }));
126 }
127 return new DefaultDriver(name, completeParents.size() > 0 ? completeParents : other.parents(),
Andrea Campanella80520b82016-01-05 17:55:29 -0800128 manufacturer, hwVersion, swVersion,
alshabib975617b2015-04-09 13:26:53 -0700129 ImmutableMap.copyOf(behaviours), properties.build());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800130 }
131
132 @Override
133 public String name() {
134 return name;
135 }
136
137 @Override
138 public String manufacturer() {
139 return manufacturer;
140 }
141
142 @Override
143 public String hwVersion() {
144 return hwVersion;
145 }
146
147 @Override
148 public String swVersion() {
149 return swVersion;
150 }
151
152 @Override
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700153 public Driver parent() {
Andrea Campanella80520b82016-01-05 17:55:29 -0800154 return parents == null ? null : parents.get(0);
155 }
156
157 @Override
158 public List<Driver> parents() {
159 return parents;
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700160 }
161
162 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800163 public Set<Class<? extends Behaviour>> behaviours() {
164 return behaviours.keySet();
165 }
166
167 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700168 public Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour) {
169 return behaviours.get(behaviour);
170 }
171
172 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800173 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700174 return behaviours.containsKey(behaviourClass) ||
Andrea Campanella80520b82016-01-05 17:55:29 -0800175 (parents != null && parents.stream()
176 .filter(parent -> parent.hasBehaviour(behaviourClass)).count() > 0);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800177 }
178
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700179 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800180 public <T extends Behaviour> T createBehaviour(DriverData data,
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700181 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700182 T behaviour = createBehaviour(data, null, behaviourClass);
183 if (behaviour != null) {
184 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800185 } else if (parents != null) {
186 for (Driver parent : Lists.reverse(parents)) {
187 try {
188 return parent.createBehaviour(data, behaviourClass);
189 } catch (IllegalArgumentException e) {
190 log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
191 }
192 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700193 }
194 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700195 }
196
197 @Override
198 public <T extends Behaviour> T createBehaviour(DriverHandler handler,
199 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700200 T behaviour = createBehaviour(handler.data(), handler, behaviourClass);
201 if (behaviour != null) {
202 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800203 } else if (parents != null && !parents.isEmpty()) {
204 for (Driver parent : Lists.reverse(parents)) {
205 try {
206 return parent.createBehaviour(handler, behaviourClass);
207 } catch (IllegalArgumentException e) {
208 log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
209 }
210 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700211 }
212 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700213 }
214
215 // Creates an instance of behaviour primed with the specified driver data.
216 private <T extends Behaviour> T createBehaviour(DriverData data, DriverHandler handler,
217 Class<T> behaviourClass) {
CNluciusa66c3972015-09-06 20:31:29 +0800218 //checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
219 // "{} is applicable only to handler context", behaviourClass.getName());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800220
221 // Locate the implementation of the requested behaviour.
222 Class<? extends Behaviour> implementation = behaviours.get(behaviourClass);
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700223 if (implementation != null) {
224 // Create an instance of the behaviour and apply data as its context.
225 T behaviour = createBehaviour(behaviourClass, implementation);
226 behaviour.setData(data);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800227
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700228 // If this is a handler behaviour, also apply handler as its context.
229 if (handler != null) {
230 ((HandlerBehaviour) behaviour).setHandler(handler);
231 }
232 return behaviour;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700233 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700234 return null;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800235 }
236
237 @SuppressWarnings("unchecked")
238 private <T extends Behaviour> T createBehaviour(Class<T> behaviourClass,
239 Class<? extends Behaviour> implementation) {
240 try {
241 return (T) implementation.newInstance();
242 } catch (InstantiationException | IllegalAccessException e) {
243 // TODO: add a specific unchecked exception
244 throw new IllegalArgumentException("Unable to create behaviour", e);
245 }
246 }
247
248 @Override
249 public Set<String> keys() {
250 return properties.keySet();
251 }
252
253 @Override
254 public String value(String key) {
255 return properties.get(key);
256 }
257
258 @Override
259 public Map<String, String> properties() {
260 return properties;
261 }
262
263 @Override
264 public String toString() {
265 return toStringHelper(this)
266 .add("name", name)
Andrea Campanella80520b82016-01-05 17:55:29 -0800267 .add("parents", parents)
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800268 .add("manufacturer", manufacturer)
269 .add("hwVersion", hwVersion)
270 .add("swVersion", swVersion)
271 .add("behaviours", behaviours)
272 .add("properties", properties)
273 .toString();
274 }
275
Andrea Campanella238d96e2016-01-20 11:52:02 -0800276 @Override
277 public boolean equals(Object driverToBeCompared) {
278 if (this == driverToBeCompared) {
279 return true;
280 }
281 if (driverToBeCompared == null || getClass() != driverToBeCompared.getClass()) {
282 return false;
283 }
284
285 DefaultDriver driver = (DefaultDriver) driverToBeCompared;
286
287 return name.equals(driver.name());
288
289 }
290
291 @Override
292 public int hashCode() {
293 return Objects.hashCode(name);
294 }
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800295}