blob: acd49f440e7b00fc836c8ac6a47053514bad76ff [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.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
Ray Milkeyea125322016-02-16 13:35:09 -080062 * @deprecated 1.5.0 Falcon Release
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080063 */
Andrea Campanella80520b82016-01-05 17:55:29 -080064 @Deprecated
Thomas Vachuska635c2d72015-05-08 14:32:13 -070065 public DefaultDriver(String name, Driver parent, String manufacturer,
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080066 String hwVersion, String swVersion,
67 Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
68 Map<String, String> properties) {
69 this.name = checkNotNull(name, "Name cannot be null");
Andrea Campanella80520b82016-01-05 17:55:29 -080070 this.parents = parent == null ? null : Lists.newArrayList(parent);
71 this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null");
72 this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null");
73 this.swVersion = checkNotNull(swVersion, "SW version cannot be null");
74 this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null"));
75 this.properties = copyOf(checkNotNull(properties, "Properties cannot be null"));
76 }
77
78 /**
79 * Creates a driver with the specified name.
80 *
81 * @param name driver name
82 * @param parents optional parent drivers
83 * @param manufacturer device manufacturer
84 * @param hwVersion device hardware version
85 * @param swVersion device software version
86 * @param behaviours device behaviour classes
87 * @param properties properties for configuration of device behaviour classes
88 */
89 public DefaultDriver(String name, List<Driver> parents, String manufacturer,
90 String hwVersion, String swVersion,
91 Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
92 Map<String, String> properties) {
93 this.name = checkNotNull(name, "Name cannot be null");
94 this.parents = parents == null || parents.isEmpty() ? null : parents;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080095 this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null");
96 this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null");
97 this.swVersion = checkNotNull(swVersion, "SW version cannot be null");
98 this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null"));
99 this.properties = copyOf(checkNotNull(properties, "Properties cannot be null"));
100 }
101
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700102 @Override
103 public Driver merge(Driver other) {
Andrea Campanella80520b82016-01-05 17:55:29 -0800104 checkArgument(parents == null || Objects.equals(parent(), other.parent()),
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700105 "Parent drivers are not the same");
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800106 // Merge the behaviours.
alshabib975617b2015-04-09 13:26:53 -0700107 Map<Class<? extends Behaviour>, Class<? extends Behaviour>>
108 behaviours = Maps.newHashMap();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700109 behaviours.putAll(this.behaviours);
110 other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b)));
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800111
112 // Merge the properties.
113 ImmutableMap.Builder<String, String> properties = ImmutableMap.builder();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700114 properties.putAll(this.properties).putAll(other.properties());
Andrea Campanella28dbe8f2016-01-28 17:51:58 -0800115 List<Driver> completeParents = new ArrayList<>();
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800116
Andrea Campanella28dbe8f2016-01-28 17:51:58 -0800117 if (parents != null) {
118 parents.forEach(parent -> other.parents().forEach(otherParent -> {
119 if (otherParent.name().equals(parent.name())) {
120 completeParents.add(parent.merge(otherParent));
121 } else if (!completeParents.contains(otherParent)) {
122 completeParents.add(otherParent);
123 } else if (!completeParents.contains(parent)) {
124 completeParents.add(parent);
125 }
126 }));
127 }
128 return new DefaultDriver(name, completeParents.size() > 0 ? completeParents : other.parents(),
Andrea Campanella80520b82016-01-05 17:55:29 -0800129 manufacturer, hwVersion, swVersion,
alshabib975617b2015-04-09 13:26:53 -0700130 ImmutableMap.copyOf(behaviours), properties.build());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800131 }
132
133 @Override
134 public String name() {
135 return name;
136 }
137
138 @Override
139 public String manufacturer() {
140 return manufacturer;
141 }
142
143 @Override
144 public String hwVersion() {
145 return hwVersion;
146 }
147
148 @Override
149 public String swVersion() {
150 return swVersion;
151 }
152
153 @Override
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700154 public Driver parent() {
Andrea Campanella80520b82016-01-05 17:55:29 -0800155 return parents == null ? null : parents.get(0);
156 }
157
158 @Override
159 public List<Driver> parents() {
160 return parents;
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700161 }
162
163 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800164 public Set<Class<? extends Behaviour>> behaviours() {
165 return behaviours.keySet();
166 }
167
168 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700169 public Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour) {
170 return behaviours.get(behaviour);
171 }
172
173 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800174 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700175 return behaviours.containsKey(behaviourClass) ||
Andrea Campanella80520b82016-01-05 17:55:29 -0800176 (parents != null && parents.stream()
177 .filter(parent -> parent.hasBehaviour(behaviourClass)).count() > 0);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800178 }
179
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700180 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800181 public <T extends Behaviour> T createBehaviour(DriverData data,
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700182 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700183 T behaviour = createBehaviour(data, null, behaviourClass);
184 if (behaviour != null) {
185 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800186 } else if (parents != null) {
187 for (Driver parent : Lists.reverse(parents)) {
188 try {
189 return parent.createBehaviour(data, behaviourClass);
190 } catch (IllegalArgumentException e) {
191 log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
192 }
193 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700194 }
195 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700196 }
197
198 @Override
199 public <T extends Behaviour> T createBehaviour(DriverHandler handler,
200 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700201 T behaviour = createBehaviour(handler.data(), handler, behaviourClass);
202 if (behaviour != null) {
203 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800204 } else if (parents != null && !parents.isEmpty()) {
205 for (Driver parent : Lists.reverse(parents)) {
206 try {
207 return parent.createBehaviour(handler, behaviourClass);
208 } catch (IllegalArgumentException e) {
209 log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
210 }
211 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700212 }
213 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700214 }
215
216 // Creates an instance of behaviour primed with the specified driver data.
217 private <T extends Behaviour> T createBehaviour(DriverData data, DriverHandler handler,
218 Class<T> behaviourClass) {
CNluciusa66c3972015-09-06 20:31:29 +0800219 //checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
220 // "{} is applicable only to handler context", behaviourClass.getName());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800221
222 // Locate the implementation of the requested behaviour.
223 Class<? extends Behaviour> implementation = behaviours.get(behaviourClass);
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700224 if (implementation != null) {
225 // Create an instance of the behaviour and apply data as its context.
226 T behaviour = createBehaviour(behaviourClass, implementation);
227 behaviour.setData(data);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800228
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700229 // If this is a handler behaviour, also apply handler as its context.
230 if (handler != null) {
231 ((HandlerBehaviour) behaviour).setHandler(handler);
232 }
233 return behaviour;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700234 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700235 return null;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800236 }
237
238 @SuppressWarnings("unchecked")
239 private <T extends Behaviour> T createBehaviour(Class<T> behaviourClass,
240 Class<? extends Behaviour> implementation) {
241 try {
242 return (T) implementation.newInstance();
243 } catch (InstantiationException | IllegalAccessException e) {
244 // TODO: add a specific unchecked exception
245 throw new IllegalArgumentException("Unable to create behaviour", e);
246 }
247 }
248
249 @Override
250 public Set<String> keys() {
251 return properties.keySet();
252 }
253
254 @Override
255 public String value(String key) {
256 return properties.get(key);
257 }
258
259 @Override
260 public Map<String, String> properties() {
261 return properties;
262 }
263
264 @Override
265 public String toString() {
266 return toStringHelper(this)
267 .add("name", name)
Andrea Campanella80520b82016-01-05 17:55:29 -0800268 .add("parents", parents)
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800269 .add("manufacturer", manufacturer)
270 .add("hwVersion", hwVersion)
271 .add("swVersion", swVersion)
272 .add("behaviours", behaviours)
273 .add("properties", properties)
274 .toString();
275 }
276
Andrea Campanella238d96e2016-01-20 11:52:02 -0800277 @Override
278 public boolean equals(Object driverToBeCompared) {
279 if (this == driverToBeCompared) {
280 return true;
281 }
282 if (driverToBeCompared == null || getClass() != driverToBeCompared.getClass()) {
283 return false;
284 }
285
286 DefaultDriver driver = (DefaultDriver) driverToBeCompared;
287
288 return name.equals(driver.name());
289
290 }
291
292 @Override
293 public int hashCode() {
294 return Objects.hashCode(name);
295 }
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800296}