blob: a331f690cee42211192f94c4b3265b28fc1bdc8a [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;
alshabib7911a052014-10-16 17:49:37 -0700113 this.chassisId = chassis;
helenyrwufd296b62016-06-22 17:43:02 -0700114 this.defaultAvailable = defaultAvailable;
tomd1900f32014-09-03 14:08:16 -0700115 }
116
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700117 /**
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 Hartd9df7bd2015-11-10 17:10:25 -0800124 this(base.deviceUri(), base.type(), base.manufacturer(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700125 base.hwVersion(), base.swVersion(), base.serialNumber(),
helenyrwufd296b62016-06-22 17:43:02 -0700126 base.chassisId(), base.isDefaultAvailable(), annotations);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700127 }
128
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700129 /**
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 */
helenyrwufd296b62016-06-22 17:43:02 -0700135 public DefaultDeviceDescription(DeviceDescription base, Type type,
136 SparseAnnotations... annotations) {
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800137 this(base.deviceUri(), type, base.manufacturer(),
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700138 base.hwVersion(), base.swVersion(), base.serialNumber(),
helenyrwufd296b62016-06-22 17:43:02 -0700139 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 Lele3a0cdd52015-07-21 14:16:31 -0700155 }
156
Palash Kalaa06a6162017-11-15 20:42:40 +0900157 /**
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
tomd1900f32014-09-03 14:08:16 -0700169 @Override
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800170 public URI deviceUri() {
tomd1900f32014-09-03 14:08:16 -0700171 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
alshabib7911a052014-10-16 17:49:37 -0700200 public ChassisId chassisId() {
201 return chassisId;
202 }
203
204 @Override
helenyrwufd296b62016-06-22 17:43:02 -0700205 public boolean isDefaultAvailable() {
206 return defaultAvailable;
207 }
208
209 @Override
tomd1900f32014-09-03 14:08:16 -0700210 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 Likin9d49f542015-12-13 15:04:55 +0300215 .add("annotations", annotations())
tomd1900f32014-09-03 14:08:16 -0700216 .toString();
217 }
218
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800219 @Override
220 public int hashCode() {
221 return Objects.hashCode(super.hashCode(), uri, type, manufacturer,
helenyrwufd296b62016-06-22 17:43:02 -0700222 hwVersion, swVersion, serialNumber, chassisId,
223 defaultAvailable);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800224 }
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)
helenyrwufd296b62016-06-22 17:43:02 -0700239 && Objects.equal(this.chassisId, that.chassisId)
240 && Objects.equal(this.defaultAvailable, that.defaultAvailable);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800241 }
242 return false;
243 }
244
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -0700245 // default constructor for serialization
Ray Milkeya6b21122016-10-24 17:18:35 -0700246 DefaultDeviceDescription() {
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -0700247 this.uri = null;
248 this.type = null;
249 this.manufacturer = null;
250 this.hwVersion = null;
251 this.swVersion = null;
252 this.serialNumber = null;
alshabib7911a052014-10-16 17:49:37 -0700253 this.chassisId = null;
helenyrwufd296b62016-06-22 17:43:02 -0700254 this.defaultAvailable = true;
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -0700255 }
tomd1900f32014-09-03 14:08:16 -0700256}