Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.driver; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
alshabib | 975617b | 2015-04-09 13:26:53 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 20 | |
| 21 | import java.util.Map; |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 22 | import java.util.Objects; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 23 | import java.util.Set; |
| 24 | |
| 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | import static com.google.common.collect.ImmutableMap.copyOf; |
| 29 | |
| 30 | /** |
| 31 | * Default implementation of extensible driver. |
| 32 | */ |
| 33 | public class DefaultDriver implements Driver { |
| 34 | |
| 35 | private final String name; |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 36 | private final Driver parent; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 37 | |
| 38 | private final String manufacturer; |
| 39 | private final String hwVersion; |
| 40 | private final String swVersion; |
| 41 | |
| 42 | private final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours; |
| 43 | private final Map<String, String> properties; |
| 44 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 45 | /** |
| 46 | * Creates a driver with the specified name. |
| 47 | * |
| 48 | * @param name driver name |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 49 | * @param parent optional parent driver |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 50 | * @param manufacturer device manufacturer |
| 51 | * @param hwVersion device hardware version |
| 52 | * @param swVersion device software version |
| 53 | * @param behaviours device behaviour classes |
| 54 | * @param properties properties for configuration of device behaviour classes |
| 55 | */ |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 56 | public DefaultDriver(String name, Driver parent, String manufacturer, |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 57 | String hwVersion, String swVersion, |
| 58 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours, |
| 59 | Map<String, String> properties) { |
| 60 | this.name = checkNotNull(name, "Name cannot be null"); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 61 | this.parent = parent; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 62 | this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null"); |
| 63 | this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null"); |
| 64 | this.swVersion = checkNotNull(swVersion, "SW version cannot be null"); |
| 65 | this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null")); |
| 66 | this.properties = copyOf(checkNotNull(properties, "Properties cannot be null")); |
| 67 | } |
| 68 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public Driver merge(Driver other) { |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 71 | checkArgument(parent == null || Objects.equals(parent, other.parent()), |
| 72 | "Parent drivers are not the same"); |
| 73 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 74 | // Merge the behaviours. |
alshabib | 975617b | 2015-04-09 13:26:53 -0700 | [diff] [blame] | 75 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> |
| 76 | behaviours = Maps.newHashMap(); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 77 | behaviours.putAll(this.behaviours); |
| 78 | other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b))); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 79 | |
| 80 | // Merge the properties. |
| 81 | ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 82 | properties.putAll(this.properties).putAll(other.properties()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 83 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 84 | return new DefaultDriver(name, other.parent(), manufacturer, hwVersion, swVersion, |
alshabib | 975617b | 2015-04-09 13:26:53 -0700 | [diff] [blame] | 85 | ImmutableMap.copyOf(behaviours), properties.build()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public String name() { |
| 90 | return name; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String manufacturer() { |
| 95 | return manufacturer; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String hwVersion() { |
| 100 | return hwVersion; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String swVersion() { |
| 105 | return swVersion; |
| 106 | } |
| 107 | |
| 108 | @Override |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 109 | public Driver parent() { |
| 110 | return parent; |
| 111 | } |
| 112 | |
| 113 | @Override |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 114 | public Set<Class<? extends Behaviour>> behaviours() { |
| 115 | return behaviours.keySet(); |
| 116 | } |
| 117 | |
| 118 | @Override |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 119 | public Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour) { |
| 120 | return behaviours.get(behaviour); |
| 121 | } |
| 122 | |
| 123 | @Override |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 124 | public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) { |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 125 | return behaviours.containsKey(behaviourClass) || |
| 126 | (parent != null && parent.hasBehaviour(behaviourClass)); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 129 | @Override |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 130 | public <T extends Behaviour> T createBehaviour(DriverData data, |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 131 | Class<T> behaviourClass) { |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 132 | T behaviour = createBehaviour(data, null, behaviourClass); |
| 133 | if (behaviour != null) { |
| 134 | return behaviour; |
| 135 | } else if (parent != null) { |
| 136 | return parent.createBehaviour(data, behaviourClass); |
| 137 | } |
| 138 | throw new IllegalArgumentException(behaviourClass.getName() + " not supported"); |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public <T extends Behaviour> T createBehaviour(DriverHandler handler, |
| 143 | Class<T> behaviourClass) { |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 144 | T behaviour = createBehaviour(handler.data(), handler, behaviourClass); |
| 145 | if (behaviour != null) { |
| 146 | return behaviour; |
| 147 | } else if (parent != null) { |
| 148 | return parent.createBehaviour(handler, behaviourClass); |
| 149 | } |
| 150 | throw new IllegalArgumentException(behaviourClass.getName() + " not supported"); |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Creates an instance of behaviour primed with the specified driver data. |
| 154 | private <T extends Behaviour> T createBehaviour(DriverData data, DriverHandler handler, |
| 155 | Class<T> behaviourClass) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 156 | //checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass), |
| 157 | // "{} is applicable only to handler context", behaviourClass.getName()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 158 | |
| 159 | // Locate the implementation of the requested behaviour. |
| 160 | Class<? extends Behaviour> implementation = behaviours.get(behaviourClass); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 161 | if (implementation != null) { |
| 162 | // Create an instance of the behaviour and apply data as its context. |
| 163 | T behaviour = createBehaviour(behaviourClass, implementation); |
| 164 | behaviour.setData(data); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 165 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 166 | // If this is a handler behaviour, also apply handler as its context. |
| 167 | if (handler != null) { |
| 168 | ((HandlerBehaviour) behaviour).setHandler(handler); |
| 169 | } |
| 170 | return behaviour; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 171 | } |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 172 | return null; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @SuppressWarnings("unchecked") |
| 176 | private <T extends Behaviour> T createBehaviour(Class<T> behaviourClass, |
| 177 | Class<? extends Behaviour> implementation) { |
| 178 | try { |
| 179 | return (T) implementation.newInstance(); |
| 180 | } catch (InstantiationException | IllegalAccessException e) { |
| 181 | // TODO: add a specific unchecked exception |
| 182 | throw new IllegalArgumentException("Unable to create behaviour", e); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Set<String> keys() { |
| 188 | return properties.keySet(); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public String value(String key) { |
| 193 | return properties.get(key); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public Map<String, String> properties() { |
| 198 | return properties; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public String toString() { |
| 203 | return toStringHelper(this) |
| 204 | .add("name", name) |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 205 | .add("parent", parent) |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 206 | .add("manufacturer", manufacturer) |
| 207 | .add("hwVersion", hwVersion) |
| 208 | .add("swVersion", swVersion) |
| 209 | .add("behaviours", behaviours) |
| 210 | .add("properties", properties) |
| 211 | .toString(); |
| 212 | } |
| 213 | |
| 214 | } |