blob: f1d1fb4b3f711b66f5293c7920955a961985db38 [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");
104
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());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800114
Andrea Campanella80520b82016-01-05 17:55:29 -0800115 return new DefaultDriver(name, other.parents(),
116 manufacturer, hwVersion, swVersion,
alshabib975617b2015-04-09 13:26:53 -0700117 ImmutableMap.copyOf(behaviours), properties.build());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800118 }
119
120 @Override
121 public String name() {
122 return name;
123 }
124
125 @Override
126 public String manufacturer() {
127 return manufacturer;
128 }
129
130 @Override
131 public String hwVersion() {
132 return hwVersion;
133 }
134
135 @Override
136 public String swVersion() {
137 return swVersion;
138 }
139
140 @Override
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700141 public Driver parent() {
Andrea Campanella80520b82016-01-05 17:55:29 -0800142 return parents == null ? null : parents.get(0);
143 }
144
145 @Override
146 public List<Driver> parents() {
147 return parents;
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700148 }
149
150 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800151 public Set<Class<? extends Behaviour>> behaviours() {
152 return behaviours.keySet();
153 }
154
155 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700156 public Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour) {
157 return behaviours.get(behaviour);
158 }
159
160 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800161 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700162 return behaviours.containsKey(behaviourClass) ||
Andrea Campanella80520b82016-01-05 17:55:29 -0800163 (parents != null && parents.stream()
164 .filter(parent -> parent.hasBehaviour(behaviourClass)).count() > 0);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800165 }
166
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700167 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800168 public <T extends Behaviour> T createBehaviour(DriverData data,
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700169 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700170 T behaviour = createBehaviour(data, null, behaviourClass);
171 if (behaviour != null) {
172 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800173 } else if (parents != null) {
174 for (Driver parent : Lists.reverse(parents)) {
175 try {
176 return parent.createBehaviour(data, behaviourClass);
177 } catch (IllegalArgumentException e) {
178 log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
179 }
180 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700181 }
182 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700183 }
184
185 @Override
186 public <T extends Behaviour> T createBehaviour(DriverHandler handler,
187 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700188 T behaviour = createBehaviour(handler.data(), handler, behaviourClass);
189 if (behaviour != null) {
190 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800191 } else if (parents != null && !parents.isEmpty()) {
192 for (Driver parent : Lists.reverse(parents)) {
193 try {
194 return parent.createBehaviour(handler, behaviourClass);
195 } catch (IllegalArgumentException e) {
196 log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
197 }
198 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700199 }
200 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700201 }
202
203 // Creates an instance of behaviour primed with the specified driver data.
204 private <T extends Behaviour> T createBehaviour(DriverData data, DriverHandler handler,
205 Class<T> behaviourClass) {
CNluciusa66c3972015-09-06 20:31:29 +0800206 //checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
207 // "{} is applicable only to handler context", behaviourClass.getName());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800208
209 // Locate the implementation of the requested behaviour.
210 Class<? extends Behaviour> implementation = behaviours.get(behaviourClass);
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700211 if (implementation != null) {
212 // Create an instance of the behaviour and apply data as its context.
213 T behaviour = createBehaviour(behaviourClass, implementation);
214 behaviour.setData(data);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800215
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700216 // If this is a handler behaviour, also apply handler as its context.
217 if (handler != null) {
218 ((HandlerBehaviour) behaviour).setHandler(handler);
219 }
220 return behaviour;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700221 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700222 return null;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800223 }
224
225 @SuppressWarnings("unchecked")
226 private <T extends Behaviour> T createBehaviour(Class<T> behaviourClass,
227 Class<? extends Behaviour> implementation) {
228 try {
229 return (T) implementation.newInstance();
230 } catch (InstantiationException | IllegalAccessException e) {
231 // TODO: add a specific unchecked exception
232 throw new IllegalArgumentException("Unable to create behaviour", e);
233 }
234 }
235
236 @Override
237 public Set<String> keys() {
238 return properties.keySet();
239 }
240
241 @Override
242 public String value(String key) {
243 return properties.get(key);
244 }
245
246 @Override
247 public Map<String, String> properties() {
248 return properties;
249 }
250
251 @Override
252 public String toString() {
253 return toStringHelper(this)
254 .add("name", name)
Andrea Campanella80520b82016-01-05 17:55:29 -0800255 .add("parents", parents)
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800256 .add("manufacturer", manufacturer)
257 .add("hwVersion", hwVersion)
258 .add("swVersion", swVersion)
259 .add("behaviours", behaviours)
260 .add("properties", properties)
261 .toString();
262 }
263
264}