blob: 781faf94985cde138c3ea15c8a928084030dd980 [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 Campanella80520b82016-01-05 17:55:29 -080023import java.util.List;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080024import java.util.Map;
Thomas Vachuska635c2d72015-05-08 14:32:13 -070025import java.util.Objects;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080026import java.util.Set;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31import static com.google.common.collect.ImmutableMap.copyOf;
Andrea Campanella80520b82016-01-05 17:55:29 -080032import static org.slf4j.LoggerFactory.getLogger;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080033
34/**
35 * Default implementation of extensible driver.
36 */
37public class DefaultDriver implements Driver {
38
Andrea Campanella80520b82016-01-05 17:55:29 -080039 private final Logger log = getLogger(getClass());
40
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080041 private final String name;
Andrea Campanella80520b82016-01-05 17:55:29 -080042 private final List<Driver> parents;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080043
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 Vachuskaa8f4e7d2015-01-08 17:31:55 -080051 /**
52 * Creates a driver with the specified name.
53 *
54 * @param name driver name
Thomas Vachuska635c2d72015-05-08 14:32:13 -070055 * @param parent optional parent driver
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080056 * @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 Campanella80520b82016-01-05 17:55:29 -080062 @Deprecated
Thomas Vachuska635c2d72015-05-08 14:32:13 -070063 public DefaultDriver(String name, Driver parent, String manufacturer,
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080064 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 Campanella80520b82016-01-05 17:55:29 -080068 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 Vachuskaa8f4e7d2015-01-08 17:31:55 -080093 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 Vachuska5c2f8132015-04-08 23:09:08 -0700100 @Override
101 public Driver merge(Driver other) {
Andrea Campanella80520b82016-01-05 17:55:29 -0800102 checkArgument(parents == null || Objects.equals(parent(), other.parent()),
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700103 "Parent drivers are not the same");
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800104 // Merge the behaviours.
alshabib975617b2015-04-09 13:26:53 -0700105 Map<Class<? extends Behaviour>, Class<? extends Behaviour>>
106 behaviours = Maps.newHashMap();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700107 behaviours.putAll(this.behaviours);
108 other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b)));
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800109
110 // Merge the properties.
111 ImmutableMap.Builder<String, String> properties = ImmutableMap.builder();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700112 properties.putAll(this.properties).putAll(other.properties());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800113
Andrea Campanella80520b82016-01-05 17:55:29 -0800114 return new DefaultDriver(name, other.parents(),
115 manufacturer, hwVersion, swVersion,
alshabib975617b2015-04-09 13:26:53 -0700116 ImmutableMap.copyOf(behaviours), properties.build());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800117 }
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 Vachuska635c2d72015-05-08 14:32:13 -0700140 public Driver parent() {
Andrea Campanella80520b82016-01-05 17:55:29 -0800141 return parents == null ? null : parents.get(0);
142 }
143
144 @Override
145 public List<Driver> parents() {
146 return parents;
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700147 }
148
149 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800150 public Set<Class<? extends Behaviour>> behaviours() {
151 return behaviours.keySet();
152 }
153
154 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700155 public Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour) {
156 return behaviours.get(behaviour);
157 }
158
159 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800160 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700161 return behaviours.containsKey(behaviourClass) ||
Andrea Campanella80520b82016-01-05 17:55:29 -0800162 (parents != null && parents.stream()
163 .filter(parent -> parent.hasBehaviour(behaviourClass)).count() > 0);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800164 }
165
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700166 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800167 public <T extends Behaviour> T createBehaviour(DriverData data,
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700168 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700169 T behaviour = createBehaviour(data, null, behaviourClass);
170 if (behaviour != null) {
171 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800172 } 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 Vachuska635c2d72015-05-08 14:32:13 -0700180 }
181 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700182 }
183
184 @Override
185 public <T extends Behaviour> T createBehaviour(DriverHandler handler,
186 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700187 T behaviour = createBehaviour(handler.data(), handler, behaviourClass);
188 if (behaviour != null) {
189 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800190 } 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 Vachuska635c2d72015-05-08 14:32:13 -0700198 }
199 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700200 }
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) {
CNluciusa66c3972015-09-06 20:31:29 +0800205 //checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
206 // "{} is applicable only to handler context", behaviourClass.getName());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800207
208 // Locate the implementation of the requested behaviour.
209 Class<? extends Behaviour> implementation = behaviours.get(behaviourClass);
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700210 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 Vachuskaa8f4e7d2015-01-08 17:31:55 -0800214
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700215 // 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 Vachuskafacc3f52015-04-10 08:58:36 -0700220 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700221 return null;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800222 }
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 Campanella80520b82016-01-05 17:55:29 -0800254 .add("parents", parents)
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800255 .add("manufacturer", manufacturer)
256 .add("hwVersion", hwVersion)
257 .add("swVersion", swVersion)
258 .add("behaviours", behaviours)
259 .add("properties", properties)
260 .toString();
261 }
262
Andrea Campanella238d96e2016-01-20 11:52:02 -0800263 @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 Vachuskaa8f4e7d2015-01-08 17:31:55 -0800282}