blob: 9d9cc1bdd1dde250c186c3a84cb26c8a7935f697 [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;
tom3065d122014-09-03 21:56:43 -070017
alshabib7911a052014-10-16 17:49:37 -070018import org.onlab.packet.ChassisId;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080019import org.onosproject.net.driver.Behaviour;
20import org.onosproject.net.driver.DefaultDriverHandler;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080021import org.onosproject.net.driver.Driver;
22import org.onosproject.net.driver.DriverData;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080023import org.onosproject.net.driver.HandlerBehaviour;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080024import org.onosproject.net.provider.ProviderId;
tom3065d122014-09-03 21:56:43 -070025
26import java.util.Objects;
27
tomeadbb462014-09-07 16:10:19 -070028import static com.google.common.base.MoreObjects.toStringHelper;
Jordan Halterman0d89ea32017-06-13 10:42:36 -070029import static com.google.common.base.Preconditions.checkArgument;
tom3065d122014-09-03 21:56:43 -070030
31/**
tom4c6606f2014-09-07 11:11:21 -070032 * Default infrastructure device model implementation.
tom3065d122014-09-03 21:56:43 -070033 */
34public class DefaultDevice extends AbstractElement implements Device {
35
Jordan Halterman0d89ea32017-06-13 10:42:36 -070036 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
tom3065d122014-09-03 21:56:43 -070041 private final Type type;
42 private final String manufacturer;
43 private final String serialNumber;
44 private final String hwVersion;
45 private final String swVersion;
alshabib7911a052014-10-16 17:49:37 -070046 private final ChassisId chassisId;
tom3065d122014-09-03 21:56:43 -070047
tomc6491322014-09-19 15:27:40 -070048 // For serialization
49 private DefaultDevice() {
50 this.type = null;
51 this.manufacturer = null;
52 this.hwVersion = null;
53 this.swVersion = null;
54 this.serialNumber = null;
alshabib7911a052014-10-16 17:49:37 -070055 this.chassisId = null;
tomc6491322014-09-19 15:27:40 -070056 }
57
tom3065d122014-09-03 21:56:43 -070058 /**
59 * Creates a network element attributed to the specified provider.
60 *
61 * @param providerId identity of the provider
62 * @param id device identifier
63 * @param type device type
64 * @param manufacturer device manufacturer
65 * @param hwVersion device HW version
66 * @param swVersion device SW version
67 * @param serialNumber device serial number
Marc De Leenheerbb382352015-04-23 18:20:34 -070068 * @param chassisId chassis id
69 * @param annotations optional key/value annotations
tom3065d122014-09-03 21:56:43 -070070 */
71 public DefaultDevice(ProviderId providerId, DeviceId id, Type type,
72 String manufacturer, String hwVersion, String swVersion,
alshabib7911a052014-10-16 17:49:37 -070073 String serialNumber, ChassisId chassisId,
74 Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -070075 super(providerId, id, annotations);
Jordan Halterman0d89ea32017-06-13 10:42:36 -070076 if (hwVersion != null) {
77 checkArgument(hwVersion.length() <= HW_VERSION_MAX_LENGTH,
78 "hwVersion exceeds maximum length " + HW_VERSION_MAX_LENGTH);
79 }
80 if (swVersion != null) {
81 checkArgument(swVersion.length() <= SW_VERSION_MAX_LENGTH,
82 "swVersion exceeds maximum length " + SW_VERSION_MAX_LENGTH);
83 }
84 if (manufacturer != null) {
85 checkArgument(manufacturer.length() <= MANUFACTURER_MAX_LENGTH,
86 "manufacturer exceeds maximum length " + MANUFACTURER_MAX_LENGTH);
87 }
88 if (serialNumber != null) {
89 checkArgument(serialNumber.length() <= SERIAL_NUMBER_MAX_LENGTH,
90 "serialNumber exceeds maximum length " + SERIAL_NUMBER_MAX_LENGTH);
91 }
tom3065d122014-09-03 21:56:43 -070092 this.type = type;
93 this.manufacturer = manufacturer;
94 this.hwVersion = hwVersion;
95 this.swVersion = swVersion;
96 this.serialNumber = serialNumber;
alshabib7911a052014-10-16 17:49:37 -070097 this.chassisId = chassisId;
tom3065d122014-09-03 21:56:43 -070098 }
99
100 @Override
101 public DeviceId id() {
tom5a9383a2014-10-02 07:33:52 -0700102 return (DeviceId) id;
tom3065d122014-09-03 21:56:43 -0700103 }
104
105 @Override
106 public Type type() {
107 return type;
108 }
109
110 @Override
111 public String manufacturer() {
112 return manufacturer;
113 }
114
115 @Override
116 public String hwVersion() {
117 return hwVersion;
118 }
119
120 @Override
121 public String swVersion() {
122 return swVersion;
123 }
124
125 @Override
126 public String serialNumber() {
127 return serialNumber;
128 }
129
130 @Override
alshabib7911a052014-10-16 17:49:37 -0700131 public ChassisId chassisId() {
132 return chassisId;
133 }
134
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800135 @Override
136 public <B extends Behaviour> B as(Class<B> projectionClass) {
137 if (HandlerBehaviour.class.isAssignableFrom(projectionClass)) {
138 bindAndCheckDriver();
139 return driver().createBehaviour(new DefaultDriverHandler(asData()), projectionClass);
140 }
141 return super.as(projectionClass);
142 }
143
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800144 /**
145 * Returns self as an immutable driver data instance.
146 *
147 * @return self as driver data
148 */
149 protected DriverData asData() {
150 return new DeviceDriverData();
151 }
152
153 @Override
154 protected Driver locateDriver() {
155 Driver driver = super.locateDriver();
156 return driver != null ? driver :
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800157 driverService().getDriver(manufacturer, hwVersion, swVersion);
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800158 }
159
160 /**
161 * Projection of the parent entity as a driver data entity.
162 */
163 protected class DeviceDriverData extends AnnotationDriverData {
164 @Override
165 public DeviceId deviceId() {
166 return id();
167 }
168 }
169
alshabib7911a052014-10-16 17:49:37 -0700170 @Override
tom3065d122014-09-03 21:56:43 -0700171 public int hashCode() {
172 return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber);
173 }
174
175 @Override
176 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700177 if (this == obj) {
178 return true;
179 }
tom3065d122014-09-03 21:56:43 -0700180 if (obj instanceof DefaultDevice) {
181 final DefaultDevice other = (DefaultDevice) obj;
182 return Objects.equals(this.id, other.id) &&
183 Objects.equals(this.type, other.type) &&
184 Objects.equals(this.manufacturer, other.manufacturer) &&
185 Objects.equals(this.hwVersion, other.hwVersion) &&
186 Objects.equals(this.swVersion, other.swVersion) &&
187 Objects.equals(this.serialNumber, other.serialNumber);
188 }
189 return false;
190 }
191
192 @Override
193 public String toString() {
194 return toStringHelper(this)
195 .add("id", id)
196 .add("type", type)
197 .add("manufacturer", manufacturer)
198 .add("hwVersion", hwVersion)
199 .add("swVersion", swVersion)
200 .add("serialNumber", serialNumber)
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800201 .add("driver", driver() != null ? driver().name() : "")
tom3065d122014-09-03 21:56:43 -0700202 .toString();
203 }
204
205}