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; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 113 | this.chassisId = chassis; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 114 | this.defaultAvailable = defaultAvailable; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Creates a device description using the supplied information. |
| 119 | * @param base DeviceDescription to basic information |
| 120 | * @param annotations Annotations to use. |
| 121 | */ |
| 122 | public DefaultDeviceDescription(DeviceDescription base, |
| 123 | SparseAnnotations... annotations) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 124 | this(base.deviceUri(), base.type(), base.manufacturer(), |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 125 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 126 | base.chassisId(), base.isDefaultAvailable(), annotations); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Creates a device description using the supplied information. |
| 131 | * @param base DeviceDescription to basic information (except for type) |
| 132 | * @param type device type |
| 133 | * @param annotations Annotations to use. |
| 134 | */ |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 135 | public DefaultDeviceDescription(DeviceDescription base, Type type, |
| 136 | SparseAnnotations... annotations) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 137 | this(base.deviceUri(), type, base.manufacturer(), |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 138 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 139 | base.chassisId(), base.isDefaultAvailable(), annotations); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Creates a device description using the supplied information. |
| 144 | * |
| 145 | * @param base DeviceDescription to basic information (except for defaultAvailable) |
| 146 | * @param defaultAvailable whether device should be made available by default |
| 147 | * @param annotations Annotations to use. |
| 148 | */ |
| 149 | public DefaultDeviceDescription(DeviceDescription base, |
| 150 | boolean defaultAvailable, |
| 151 | SparseAnnotations... annotations) { |
| 152 | this(base.deviceUri(), base.type(), base.manufacturer(), |
| 153 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
| 154 | base.chassisId(), defaultAvailable, annotations); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 157 | /** |
| 158 | * Creates a device description using the supplied information. |
| 159 | * |
| 160 | * @param base base |
| 161 | * @param annotations annotations |
| 162 | * @return device description |
| 163 | */ |
| 164 | public static DefaultDeviceDescription copyReplacingAnnotation(DeviceDescription base, |
| 165 | SparseAnnotations annotations) { |
| 166 | return new DefaultDeviceDescription(base, annotations); |
| 167 | } |
| 168 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 169 | @Override |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 170 | public URI deviceUri() { |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 171 | return uri; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Type type() { |
| 176 | return type; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public String manufacturer() { |
| 181 | return manufacturer; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public String hwVersion() { |
| 186 | return hwVersion; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public String swVersion() { |
| 191 | return swVersion; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public String serialNumber() { |
| 196 | return serialNumber; |
| 197 | } |
| 198 | |
| 199 | @Override |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 200 | public ChassisId chassisId() { |
| 201 | return chassisId; |
| 202 | } |
| 203 | |
| 204 | @Override |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 205 | public boolean isDefaultAvailable() { |
| 206 | return defaultAvailable; |
| 207 | } |
| 208 | |
| 209 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 210 | public String toString() { |
| 211 | return toStringHelper(this) |
| 212 | .add("uri", uri).add("type", type).add("mfr", manufacturer) |
| 213 | .add("hw", hwVersion).add("sw", swVersion) |
| 214 | .add("serial", serialNumber) |
Pavel Likin | 9d49f54 | 2015-12-13 15:04:55 +0300 | [diff] [blame] | 215 | .add("annotations", annotations()) |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 216 | .toString(); |
| 217 | } |
| 218 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 219 | @Override |
| 220 | public int hashCode() { |
| 221 | return Objects.hashCode(super.hashCode(), uri, type, manufacturer, |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 222 | hwVersion, swVersion, serialNumber, chassisId, |
| 223 | defaultAvailable); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public boolean equals(Object object) { |
| 228 | if (object instanceof DefaultDeviceDescription) { |
| 229 | if (!super.equals(object)) { |
| 230 | return false; |
| 231 | } |
| 232 | DefaultDeviceDescription that = (DefaultDeviceDescription) object; |
| 233 | return Objects.equal(this.uri, that.uri) |
| 234 | && Objects.equal(this.type, that.type) |
| 235 | && Objects.equal(this.manufacturer, that.manufacturer) |
| 236 | && Objects.equal(this.hwVersion, that.hwVersion) |
| 237 | && Objects.equal(this.swVersion, that.swVersion) |
| 238 | && Objects.equal(this.serialNumber, that.serialNumber) |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 239 | && Objects.equal(this.chassisId, that.chassisId) |
| 240 | && Objects.equal(this.defaultAvailable, that.defaultAvailable); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 241 | } |
| 242 | return false; |
| 243 | } |
| 244 | |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 245 | // default constructor for serialization |
Ray Milkey | a6b2112 | 2016-10-24 17:18:35 -0700 | [diff] [blame] | 246 | DefaultDeviceDescription() { |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 247 | this.uri = null; |
| 248 | this.type = null; |
| 249 | this.manufacturer = null; |
| 250 | this.hwVersion = null; |
| 251 | this.swVersion = null; |
| 252 | this.serialNumber = null; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 253 | this.chassisId = null; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 254 | this.defaultAvailable = true; |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 255 | } |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 256 | } |