blob: 271e678667dc21a75eeda91de8f3583b9bc43e4b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.device;
tomd1900f32014-09-03 14:08:16 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.net.AbstractDescription;
19import org.onosproject.net.SparseAnnotations;
alshabib7911a052014-10-16 17:49:37 -070020import org.onlab.packet.ChassisId;
tom27ae0e62014-10-01 20:35:01 -070021
tomd1900f32014-09-03 14:08:16 -070022import java.net.URI;
23
tomeadbb462014-09-07 16:10:19 -070024import static com.google.common.base.MoreObjects.toStringHelper;
Jordan Halterman83949a12017-06-21 10:35:38 -070025import static com.google.common.base.Preconditions.checkArgument;
tomd1900f32014-09-03 14:08:16 -070026import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import static org.onosproject.net.Device.Type;
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -080028import com.google.common.base.Objects;
tomd1900f32014-09-03 14:08:16 -070029
30/**
31 * Default implementation of immutable device description entity.
32 */
tomf5d85d42014-10-02 05:27:56 -070033public class DefaultDeviceDescription extends AbstractDescription
tom27ae0e62014-10-01 20:35:01 -070034 implements DeviceDescription {
Jordan Halterman83949a12017-06-21 10:35:38 -070035
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
tomd1900f32014-09-03 14:08:16 -070041 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;
alshabib7911a052014-10-16 17:49:37 -070047 private final ChassisId chassisId;
helenyrwufd296b62016-06-22 17:43:02 -070048 private final boolean defaultAvailable;
tomd1900f32014-09-03 14:08:16 -070049
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 Leenheerb473b9d2015-02-06 15:21:03 -080059 * @param chassis chassis id
tom27ae0e62014-10-01 20:35:01 -070060 * @param annotations optional key/value annotations map
tomd1900f32014-09-03 14:08:16 -070061 */
62 public DefaultDeviceDescription(URI uri, Type type, String manufacturer,
63 String hwVersion, String swVersion,
alshabib7911a052014-10-16 17:49:37 -070064 String serialNumber, ChassisId chassis,
tomf5d85d42014-10-02 05:27:56 -070065 SparseAnnotations... annotations) {
helenyrwufd296b62016-06-22 17:43:02 -070066 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) {
tom27ae0e62014-10-01 20:35:01 -070088 super(annotations);
tomd1900f32014-09-03 14:08:16 -070089 this.uri = checkNotNull(uri, "Device URI cannot be null");
90 this.type = checkNotNull(type, "Device type cannot be null");
Jordan Halterman83949a12017-06-21 10:35:38 -070091
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
tomd1900f32014-09-03 14:08:16 -0700109 this.manufacturer = manufacturer;
110 this.hwVersion = hwVersion;
111 this.swVersion = swVersion;
112 this.serialNumber = serialNumber;
Andrea Campanellaf4688582019-11-28 14:31:02 +0100113 //Avoid propagation of null chassisID and substitute it with UNKNOWN
114 if (chassis == null) {
115 chassis = new ChassisId();
116 }
alshabib7911a052014-10-16 17:49:37 -0700117 this.chassisId = chassis;
helenyrwufd296b62016-06-22 17:43:02 -0700118 this.defaultAvailable = defaultAvailable;
tomd1900f32014-09-03 14:08:16 -0700119 }
120
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700121 /**
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 Hartd9df7bd2015-11-10 17:10:25 -0800128 this(base.deviceUri(), base.type(), base.manufacturer(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700129 base.hwVersion(), base.swVersion(), base.serialNumber(),
helenyrwufd296b62016-06-22 17:43:02 -0700130 base.chassisId(), base.isDefaultAvailable(), annotations);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700131 }
132
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700133 /**
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 */
helenyrwufd296b62016-06-22 17:43:02 -0700139 public DefaultDeviceDescription(DeviceDescription base, Type type,
140 SparseAnnotations... annotations) {
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800141 this(base.deviceUri(), type, base.manufacturer(),
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700142 base.hwVersion(), base.swVersion(), base.serialNumber(),
helenyrwufd296b62016-06-22 17:43:02 -0700143 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 Lele3a0cdd52015-07-21 14:16:31 -0700159 }
160
Palash Kalaa06a6162017-11-15 20:42:40 +0900161 /**
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
tomd1900f32014-09-03 14:08:16 -0700173 @Override
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800174 public URI deviceUri() {
tomd1900f32014-09-03 14:08:16 -0700175 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
alshabib7911a052014-10-16 17:49:37 -0700204 public ChassisId chassisId() {
205 return chassisId;
206 }
207
208 @Override
helenyrwufd296b62016-06-22 17:43:02 -0700209 public boolean isDefaultAvailable() {
210 return defaultAvailable;
211 }
212
213 @Override
tomd1900f32014-09-03 14:08:16 -0700214 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 McPeakee1b418f2019-05-23 13:42:13 +0100219 .add("chassisId", chassisId)
pierventree1f80102021-10-01 22:01:22 +0200220 .add("defaultAvailable", defaultAvailable)
Pavel Likin9d49f542015-12-13 15:04:55 +0300221 .add("annotations", annotations())
tomd1900f32014-09-03 14:08:16 -0700222 .toString();
223 }
224
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800225 @Override
226 public int hashCode() {
227 return Objects.hashCode(super.hashCode(), uri, type, manufacturer,
helenyrwufd296b62016-06-22 17:43:02 -0700228 hwVersion, swVersion, serialNumber, chassisId,
229 defaultAvailable);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800230 }
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)
helenyrwufd296b62016-06-22 17:43:02 -0700245 && Objects.equal(this.chassisId, that.chassisId)
246 && Objects.equal(this.defaultAvailable, that.defaultAvailable);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800247 }
248 return false;
249 }
250
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -0700251 // default constructor for serialization
Ray Milkeya6b21122016-10-24 17:18:35 -0700252 DefaultDeviceDescription() {
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -0700253 this.uri = null;
254 this.type = null;
255 this.manufacturer = null;
256 this.hwVersion = null;
257 this.swVersion = null;
258 this.serialNumber = null;
alshabib7911a052014-10-16 17:49:37 -0700259 this.chassisId = null;
helenyrwufd296b62016-06-22 17:43:02 -0700260 this.defaultAvailable = true;
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -0700261 }
tomd1900f32014-09-03 14:08:16 -0700262}