blob: 6ffdbee795b6c708c1d36bdbd4d2a3abbf1ceeb5 [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
sangyun-han04c461d2017-03-02 20:29:39 +090018import com.google.common.collect.ImmutableList;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080019import com.google.common.collect.ImmutableMap;
Andrea Campanella80520b82016-01-05 17:55:29 -080020import com.google.common.collect.Lists;
alshabib975617b2015-04-09 13:26:53 -070021import com.google.common.collect.Maps;
Andrea Campanella80520b82016-01-05 17:55:29 -080022import org.slf4j.Logger;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080023
Andrea Campanella28dbe8f2016-01-28 17:51:58 -080024import java.util.ArrayList;
Andrea Campanella80520b82016-01-05 17:55:29 -080025import java.util.List;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080026import java.util.Map;
Thomas Vachuska635c2d72015-05-08 14:32:13 -070027import java.util.Objects;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080028import java.util.Set;
29
30import static com.google.common.base.MoreObjects.toStringHelper;
31import static com.google.common.base.Preconditions.checkArgument;
32import static com.google.common.base.Preconditions.checkNotNull;
33import static com.google.common.collect.ImmutableMap.copyOf;
Andrea Campanella80520b82016-01-05 17:55:29 -080034import static org.slf4j.LoggerFactory.getLogger;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080035
36/**
37 * Default implementation of extensible driver.
38 */
39public class DefaultDriver implements Driver {
40
Andrea Campanella80520b82016-01-05 17:55:29 -080041 private final Logger log = getLogger(getClass());
42
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080043 private final String name;
Andrea Campanella80520b82016-01-05 17:55:29 -080044 private final List<Driver> parents;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080045
46 private final String manufacturer;
47 private final String hwVersion;
48 private final String swVersion;
49
50 private final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours;
51 private final Map<String, String> properties;
52
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080053 /**
54 * Creates a driver with the specified name.
55 *
56 * @param name driver name
Andrea Campanella80520b82016-01-05 17:55:29 -080057 * @param parents optional parent drivers
58 * @param manufacturer device manufacturer
59 * @param hwVersion device hardware version
60 * @param swVersion device software version
61 * @param behaviours device behaviour classes
62 * @param properties properties for configuration of device behaviour classes
63 */
64 public DefaultDriver(String name, List<Driver> parents, String manufacturer,
65 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");
sangyun-han04c461d2017-03-02 20:29:39 +090069 this.parents = parents == null ? ImmutableList.of() : ImmutableList.copyOf(parents);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080070 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
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070077 @Override
78 public Driver merge(Driver other) {
Andrea Campanella80520b82016-01-05 17:55:29 -080079 checkArgument(parents == null || Objects.equals(parent(), other.parent()),
Thomas Vachuska635c2d72015-05-08 14:32:13 -070080 "Parent drivers are not the same");
sangyun-han04c461d2017-03-02 20:29:39 +090081
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080082 // Merge the behaviours.
alshabib975617b2015-04-09 13:26:53 -070083 Map<Class<? extends Behaviour>, Class<? extends Behaviour>>
84 behaviours = Maps.newHashMap();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070085 behaviours.putAll(this.behaviours);
86 other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b)));
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080087
88 // Merge the properties.
89 ImmutableMap.Builder<String, String> properties = ImmutableMap.builder();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070090 properties.putAll(this.properties).putAll(other.properties());
Andrea Campanella28dbe8f2016-01-28 17:51:58 -080091 List<Driver> completeParents = new ArrayList<>();
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080092
Andrea Campanella28dbe8f2016-01-28 17:51:58 -080093 if (parents != null) {
94 parents.forEach(parent -> other.parents().forEach(otherParent -> {
95 if (otherParent.name().equals(parent.name())) {
96 completeParents.add(parent.merge(otherParent));
97 } else if (!completeParents.contains(otherParent)) {
98 completeParents.add(otherParent);
99 } else if (!completeParents.contains(parent)) {
100 completeParents.add(parent);
101 }
102 }));
103 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800104 return new DefaultDriver(name, !completeParents.isEmpty() ? completeParents : other.parents(),
Andrea Campanella80520b82016-01-05 17:55:29 -0800105 manufacturer, hwVersion, swVersion,
alshabib975617b2015-04-09 13:26:53 -0700106 ImmutableMap.copyOf(behaviours), properties.build());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800107 }
108
109 @Override
110 public String name() {
111 return name;
112 }
113
114 @Override
115 public String manufacturer() {
116 return manufacturer;
117 }
118
119 @Override
120 public String hwVersion() {
121 return hwVersion;
122 }
123
124 @Override
125 public String swVersion() {
126 return swVersion;
127 }
128
129 @Override
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700130 public Driver parent() {
sangyun-han04c461d2017-03-02 20:29:39 +0900131 return parents.isEmpty() ? null : parents.get(0);
Andrea Campanella80520b82016-01-05 17:55:29 -0800132 }
133
134 @Override
135 public List<Driver> parents() {
136 return parents;
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700137 }
138
139 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800140 public Set<Class<? extends Behaviour>> behaviours() {
141 return behaviours.keySet();
142 }
143
144 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700145 public Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour) {
146 return behaviours.get(behaviour);
147 }
148
149 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800150 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700151 return behaviours.containsKey(behaviourClass) ||
Andrea Campanella80520b82016-01-05 17:55:29 -0800152 (parents != null && parents.stream()
153 .filter(parent -> parent.hasBehaviour(behaviourClass)).count() > 0);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800154 }
155
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700156 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800157 public <T extends Behaviour> T createBehaviour(DriverData data,
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700158 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700159 T behaviour = createBehaviour(data, null, behaviourClass);
160 if (behaviour != null) {
161 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800162 } else if (parents != null) {
163 for (Driver parent : Lists.reverse(parents)) {
164 try {
165 return parent.createBehaviour(data, behaviourClass);
166 } catch (IllegalArgumentException e) {
167 log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
168 }
169 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700170 }
171 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700172 }
173
174 @Override
175 public <T extends Behaviour> T createBehaviour(DriverHandler handler,
176 Class<T> behaviourClass) {
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700177 T behaviour = createBehaviour(handler.data(), handler, behaviourClass);
178 if (behaviour != null) {
179 return behaviour;
Andrea Campanella80520b82016-01-05 17:55:29 -0800180 } else if (parents != null && !parents.isEmpty()) {
181 for (Driver parent : Lists.reverse(parents)) {
182 try {
183 return parent.createBehaviour(handler, behaviourClass);
184 } catch (IllegalArgumentException e) {
185 log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
186 }
187 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700188 }
189 throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700190 }
191
192 // Creates an instance of behaviour primed with the specified driver data.
193 private <T extends Behaviour> T createBehaviour(DriverData data, DriverHandler handler,
194 Class<T> behaviourClass) {
CNluciusa66c3972015-09-06 20:31:29 +0800195 //checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
196 // "{} is applicable only to handler context", behaviourClass.getName());
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800197
198 // Locate the implementation of the requested behaviour.
199 Class<? extends Behaviour> implementation = behaviours.get(behaviourClass);
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700200 if (implementation != null) {
201 // Create an instance of the behaviour and apply data as its context.
202 T behaviour = createBehaviour(behaviourClass, implementation);
203 behaviour.setData(data);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800204
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700205 // If this is a handler behaviour, also apply handler as its context.
206 if (handler != null) {
207 ((HandlerBehaviour) behaviour).setHandler(handler);
208 }
209 return behaviour;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -0700210 }
Thomas Vachuska635c2d72015-05-08 14:32:13 -0700211 return null;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800212 }
213
214 @SuppressWarnings("unchecked")
215 private <T extends Behaviour> T createBehaviour(Class<T> behaviourClass,
216 Class<? extends Behaviour> implementation) {
217 try {
218 return (T) implementation.newInstance();
219 } catch (InstantiationException | IllegalAccessException e) {
220 // TODO: add a specific unchecked exception
221 throw new IllegalArgumentException("Unable to create behaviour", e);
222 }
223 }
224
225 @Override
226 public Set<String> keys() {
227 return properties.keySet();
228 }
229
230 @Override
231 public String value(String key) {
232 return properties.get(key);
233 }
234
235 @Override
236 public Map<String, String> properties() {
237 return properties;
238 }
239
240 @Override
241 public String toString() {
242 return toStringHelper(this)
243 .add("name", name)
Andrea Campanella80520b82016-01-05 17:55:29 -0800244 .add("parents", parents)
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800245 .add("manufacturer", manufacturer)
246 .add("hwVersion", hwVersion)
247 .add("swVersion", swVersion)
248 .add("behaviours", behaviours)
249 .add("properties", properties)
250 .toString();
251 }
252
Andrea Campanella238d96e2016-01-20 11:52:02 -0800253 @Override
254 public boolean equals(Object driverToBeCompared) {
255 if (this == driverToBeCompared) {
256 return true;
257 }
258 if (driverToBeCompared == null || getClass() != driverToBeCompared.getClass()) {
259 return false;
260 }
261
262 DefaultDriver driver = (DefaultDriver) driverToBeCompared;
263
264 return name.equals(driver.name());
265
266 }
267
268 @Override
269 public int hashCode() {
270 return Objects.hashCode(name);
271 }
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -0800272}