Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.device; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.net.AbstractDescription; |
| 19 | import org.onosproject.net.SparseAnnotations; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 20 | import org.onlab.packet.ChassisId; |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 21 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 22 | import java.net.URI; |
| 23 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import static org.onosproject.net.Device.Type; |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 28 | import com.google.common.base.Objects; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Default implementation of immutable device description entity. |
| 32 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 33 | public class DefaultDeviceDescription extends AbstractDescription |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 34 | implements DeviceDescription { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 35 | |
| 36 | private static final int MANUFACTURER_MAX_LENGTH = 256; |
| 37 | private static final int HW_VERSION_MAX_LENGTH = 256; |
| 38 | private static final int SW_VERSION_MAX_LENGTH = 256; |
| 39 | private static final int SERIAL_NUMBER_MAX_LENGTH = 256; |
| 40 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 41 | private final URI uri; |
| 42 | private final Type type; |
| 43 | private final String manufacturer; |
| 44 | private final String hwVersion; |
| 45 | private final String swVersion; |
| 46 | private final String serialNumber; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 47 | private final ChassisId chassisId; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 48 | private final boolean defaultAvailable; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Creates a device description using the supplied information. |
| 52 | * |
| 53 | * @param uri device URI |
| 54 | * @param type device type |
| 55 | * @param manufacturer device manufacturer |
| 56 | * @param hwVersion device HW version |
| 57 | * @param swVersion device SW version |
| 58 | * @param serialNumber device serial number |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 59 | * @param chassis chassis id |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 60 | * @param annotations optional key/value annotations map |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 61 | */ |
| 62 | public DefaultDeviceDescription(URI uri, Type type, String manufacturer, |
| 63 | String hwVersion, String swVersion, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 64 | String serialNumber, ChassisId chassis, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 65 | SparseAnnotations... annotations) { |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 66 | this(uri, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 67 | chassis, true, annotations); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates a device description using the supplied information. |
| 72 | * |
| 73 | * @param uri device URI |
| 74 | * @param type device type |
| 75 | * @param manufacturer device manufacturer |
| 76 | * @param hwVersion device HW version |
| 77 | * @param swVersion device SW version |
| 78 | * @param serialNumber device serial number |
| 79 | * @param chassis chassis id |
| 80 | * @param defaultAvailable optional whether device is by default available |
| 81 | * @param annotations optional key/value annotations map |
| 82 | */ |
| 83 | public DefaultDeviceDescription(URI uri, Type type, String manufacturer, |
| 84 | String hwVersion, String swVersion, |
| 85 | String serialNumber, ChassisId chassis, |
| 86 | boolean defaultAvailable, |
| 87 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 88 | super(annotations); |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 89 | this.uri = checkNotNull(uri, "Device URI cannot be null"); |
| 90 | this.type = checkNotNull(type, "Device type cannot be null"); |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 91 | |
| 92 | if (hwVersion != null) { |
| 93 | checkArgument(hwVersion.length() <= HW_VERSION_MAX_LENGTH, |
| 94 | "hwVersion exceeds maximum length " + HW_VERSION_MAX_LENGTH); |
| 95 | } |
| 96 | if (swVersion != null) { |
| 97 | checkArgument(swVersion.length() <= SW_VERSION_MAX_LENGTH, |
| 98 | "swVersion exceeds maximum length " + SW_VERSION_MAX_LENGTH); |
| 99 | } |
| 100 | if (manufacturer != null) { |
| 101 | checkArgument(manufacturer.length() <= MANUFACTURER_MAX_LENGTH, |
| 102 | "manufacturer exceeds maximum length " + MANUFACTURER_MAX_LENGTH); |
| 103 | } |
| 104 | if (serialNumber != null) { |
| 105 | checkArgument(serialNumber.length() <= SERIAL_NUMBER_MAX_LENGTH, |
| 106 | "serialNumber exceeds maximum length " + SERIAL_NUMBER_MAX_LENGTH); |
| 107 | } |
| 108 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 109 | this.manufacturer = manufacturer; |
| 110 | this.hwVersion = hwVersion; |
| 111 | this.swVersion = swVersion; |
| 112 | this.serialNumber = serialNumber; |
Andrea Campanella | f468858 | 2019-11-28 14:31:02 +0100 | [diff] [blame] | 113 | //Avoid propagation of null chassisID and substitute it with UNKNOWN |
| 114 | if (chassis == null) { |
| 115 | chassis = new ChassisId(); |
| 116 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 117 | this.chassisId = chassis; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 118 | this.defaultAvailable = defaultAvailable; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Creates a device description using the supplied information. |
| 123 | * @param base DeviceDescription to basic information |
| 124 | * @param annotations Annotations to use. |
| 125 | */ |
| 126 | public DefaultDeviceDescription(DeviceDescription base, |
| 127 | SparseAnnotations... annotations) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 128 | this(base.deviceUri(), base.type(), base.manufacturer(), |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 129 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 130 | base.chassisId(), base.isDefaultAvailable(), annotations); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Creates a device description using the supplied information. |
| 135 | * @param base DeviceDescription to basic information (except for type) |
| 136 | * @param type device type |
| 137 | * @param annotations Annotations to use. |
| 138 | */ |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 139 | public DefaultDeviceDescription(DeviceDescription base, Type type, |
| 140 | SparseAnnotations... annotations) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 141 | this(base.deviceUri(), type, base.manufacturer(), |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 142 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 143 | base.chassisId(), base.isDefaultAvailable(), annotations); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates a device description using the supplied information. |
| 148 | * |
| 149 | * @param base DeviceDescription to basic information (except for defaultAvailable) |
| 150 | * @param defaultAvailable whether device should be made available by default |
| 151 | * @param annotations Annotations to use. |
| 152 | */ |
| 153 | public DefaultDeviceDescription(DeviceDescription base, |
| 154 | boolean defaultAvailable, |
| 155 | SparseAnnotations... annotations) { |
| 156 | this(base.deviceUri(), base.type(), base.manufacturer(), |
| 157 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
| 158 | base.chassisId(), defaultAvailable, annotations); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 161 | /** |
| 162 | * Creates a device description using the supplied information. |
| 163 | * |
| 164 | * @param base base |
| 165 | * @param annotations annotations |
| 166 | * @return device description |
| 167 | */ |
| 168 | public static DefaultDeviceDescription copyReplacingAnnotation(DeviceDescription base, |
| 169 | SparseAnnotations annotations) { |
| 170 | return new DefaultDeviceDescription(base, annotations); |
| 171 | } |
| 172 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 173 | @Override |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 174 | public URI deviceUri() { |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 175 | return uri; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public Type type() { |
| 180 | return type; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public String manufacturer() { |
| 185 | return manufacturer; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public String hwVersion() { |
| 190 | return hwVersion; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public String swVersion() { |
| 195 | return swVersion; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public String serialNumber() { |
| 200 | return serialNumber; |
| 201 | } |
| 202 | |
| 203 | @Override |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 204 | public ChassisId chassisId() { |
| 205 | return chassisId; |
| 206 | } |
| 207 | |
| 208 | @Override |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 209 | public boolean isDefaultAvailable() { |
| 210 | return defaultAvailable; |
| 211 | } |
| 212 | |
| 213 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 214 | public String toString() { |
| 215 | return toStringHelper(this) |
| 216 | .add("uri", uri).add("type", type).add("mfr", manufacturer) |
| 217 | .add("hw", hwVersion).add("sw", swVersion) |
| 218 | .add("serial", serialNumber) |
Kieran McPeake | e1b418f | 2019-05-23 13:42:13 +0100 | [diff] [blame] | 219 | .add("chassisId", chassisId) |
pierventre | e1f8010 | 2021-10-01 22:01:22 +0200 | [diff] [blame] | 220 | .add("defaultAvailable", defaultAvailable) |
Pavel Likin | 9d49f54 | 2015-12-13 15:04:55 +0300 | [diff] [blame] | 221 | .add("annotations", annotations()) |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 222 | .toString(); |
| 223 | } |
| 224 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 225 | @Override |
| 226 | public int hashCode() { |
| 227 | return Objects.hashCode(super.hashCode(), uri, type, manufacturer, |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 228 | hwVersion, swVersion, serialNumber, chassisId, |
| 229 | defaultAvailable); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public boolean equals(Object object) { |
| 234 | if (object instanceof DefaultDeviceDescription) { |
| 235 | if (!super.equals(object)) { |
| 236 | return false; |
| 237 | } |
| 238 | DefaultDeviceDescription that = (DefaultDeviceDescription) object; |
| 239 | return Objects.equal(this.uri, that.uri) |
| 240 | && Objects.equal(this.type, that.type) |
| 241 | && Objects.equal(this.manufacturer, that.manufacturer) |
| 242 | && Objects.equal(this.hwVersion, that.hwVersion) |
| 243 | && Objects.equal(this.swVersion, that.swVersion) |
| 244 | && Objects.equal(this.serialNumber, that.serialNumber) |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 245 | && Objects.equal(this.chassisId, that.chassisId) |
| 246 | && Objects.equal(this.defaultAvailable, that.defaultAvailable); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 251 | // default constructor for serialization |
Ray Milkey | a6b2112 | 2016-10-24 17:18:35 -0700 | [diff] [blame] | 252 | DefaultDeviceDescription() { |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 253 | this.uri = null; |
| 254 | this.type = null; |
| 255 | this.manufacturer = null; |
| 256 | this.hwVersion = null; |
| 257 | this.swVersion = null; |
| 258 | this.serialNumber = null; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 259 | this.chassisId = null; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 260 | this.defaultAvailable = true; |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 261 | } |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 262 | } |