blob: ea25ed5b20d2a350d1961b5b23d3a1a3859afc15 [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;
Carmelo Cascone0761cd32018-08-29 19:22:50 -070019import org.onlab.util.ItemNotFoundException;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080020import org.onosproject.net.driver.Behaviour;
21import org.onosproject.net.driver.DefaultDriverHandler;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080022import org.onosproject.net.driver.Driver;
23import org.onosproject.net.driver.DriverData;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080024import org.onosproject.net.driver.HandlerBehaviour;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080025import org.onosproject.net.provider.ProviderId;
tom3065d122014-09-03 21:56:43 -070026
27import java.util.Objects;
28
tomeadbb462014-09-07 16:10:19 -070029import static com.google.common.base.MoreObjects.toStringHelper;
Jordan Halterman0d89ea32017-06-13 10:42:36 -070030import static com.google.common.base.Preconditions.checkArgument;
tom3065d122014-09-03 21:56:43 -070031
32/**
tom4c6606f2014-09-07 11:11:21 -070033 * Default infrastructure device model implementation.
tom3065d122014-09-03 21:56:43 -070034 */
35public class DefaultDevice extends AbstractElement implements Device {
36
Jordan Halterman0d89ea32017-06-13 10:42:36 -070037 private static final int MANUFACTURER_MAX_LENGTH = 256;
38 private static final int HW_VERSION_MAX_LENGTH = 256;
39 private static final int SW_VERSION_MAX_LENGTH = 256;
40 private static final int SERIAL_NUMBER_MAX_LENGTH = 256;
41
tom3065d122014-09-03 21:56:43 -070042 private final Type type;
43 private final String manufacturer;
44 private final String serialNumber;
45 private final String hwVersion;
46 private final String swVersion;
alshabib7911a052014-10-16 17:49:37 -070047 private final ChassisId chassisId;
tom3065d122014-09-03 21:56:43 -070048
tomc6491322014-09-19 15:27:40 -070049 // For serialization
50 private DefaultDevice() {
51 this.type = null;
52 this.manufacturer = null;
53 this.hwVersion = null;
54 this.swVersion = null;
55 this.serialNumber = null;
alshabib7911a052014-10-16 17:49:37 -070056 this.chassisId = null;
tomc6491322014-09-19 15:27:40 -070057 }
58
tom3065d122014-09-03 21:56:43 -070059 /**
60 * Creates a network element attributed to the specified provider.
61 *
62 * @param providerId identity of the provider
63 * @param id device identifier
64 * @param type device type
65 * @param manufacturer device manufacturer
66 * @param hwVersion device HW version
67 * @param swVersion device SW version
68 * @param serialNumber device serial number
Marc De Leenheerbb382352015-04-23 18:20:34 -070069 * @param chassisId chassis id
70 * @param annotations optional key/value annotations
tom3065d122014-09-03 21:56:43 -070071 */
72 public DefaultDevice(ProviderId providerId, DeviceId id, Type type,
73 String manufacturer, String hwVersion, String swVersion,
alshabib7911a052014-10-16 17:49:37 -070074 String serialNumber, ChassisId chassisId,
75 Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -070076 super(providerId, id, annotations);
Jordan Halterman0d89ea32017-06-13 10:42:36 -070077 if (hwVersion != null) {
78 checkArgument(hwVersion.length() <= HW_VERSION_MAX_LENGTH,
79 "hwVersion exceeds maximum length " + HW_VERSION_MAX_LENGTH);
80 }
81 if (swVersion != null) {
82 checkArgument(swVersion.length() <= SW_VERSION_MAX_LENGTH,
83 "swVersion exceeds maximum length " + SW_VERSION_MAX_LENGTH);
84 }
85 if (manufacturer != null) {
86 checkArgument(manufacturer.length() <= MANUFACTURER_MAX_LENGTH,
87 "manufacturer exceeds maximum length " + MANUFACTURER_MAX_LENGTH);
88 }
89 if (serialNumber != null) {
90 checkArgument(serialNumber.length() <= SERIAL_NUMBER_MAX_LENGTH,
91 "serialNumber exceeds maximum length " + SERIAL_NUMBER_MAX_LENGTH);
92 }
tom3065d122014-09-03 21:56:43 -070093 this.type = type;
94 this.manufacturer = manufacturer;
95 this.hwVersion = hwVersion;
96 this.swVersion = swVersion;
97 this.serialNumber = serialNumber;
alshabib7911a052014-10-16 17:49:37 -070098 this.chassisId = chassisId;
tom3065d122014-09-03 21:56:43 -070099 }
100
101 @Override
102 public DeviceId id() {
tom5a9383a2014-10-02 07:33:52 -0700103 return (DeviceId) id;
tom3065d122014-09-03 21:56:43 -0700104 }
105
106 @Override
107 public Type type() {
108 return type;
109 }
110
111 @Override
112 public String manufacturer() {
113 return manufacturer;
114 }
115
116 @Override
117 public String hwVersion() {
118 return hwVersion;
119 }
120
121 @Override
122 public String swVersion() {
123 return swVersion;
124 }
125
126 @Override
127 public String serialNumber() {
128 return serialNumber;
129 }
130
131 @Override
alshabib7911a052014-10-16 17:49:37 -0700132 public ChassisId chassisId() {
133 return chassisId;
134 }
135
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800136 @Override
137 public <B extends Behaviour> B as(Class<B> projectionClass) {
138 if (HandlerBehaviour.class.isAssignableFrom(projectionClass)) {
139 bindAndCheckDriver();
140 return driver().createBehaviour(new DefaultDriverHandler(asData()), projectionClass);
141 }
142 return super.as(projectionClass);
143 }
144
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800145 /**
146 * Returns self as an immutable driver data instance.
147 *
148 * @return self as driver data
149 */
150 protected DriverData asData() {
151 return new DeviceDriverData();
152 }
153
154 @Override
155 protected Driver locateDriver() {
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700156 try {
157 return driverService().getDriver(id());
158 } catch (ItemNotFoundException e) {
159 return null;
160 }
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800161 }
162
163 /**
164 * Projection of the parent entity as a driver data entity.
165 */
166 protected class DeviceDriverData extends AnnotationDriverData {
167 @Override
168 public DeviceId deviceId() {
169 return id();
170 }
171 }
172
alshabib7911a052014-10-16 17:49:37 -0700173 @Override
tom3065d122014-09-03 21:56:43 -0700174 public int hashCode() {
175 return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber);
176 }
177
178 @Override
179 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700180 if (this == obj) {
181 return true;
182 }
tom3065d122014-09-03 21:56:43 -0700183 if (obj instanceof DefaultDevice) {
184 final DefaultDevice other = (DefaultDevice) obj;
185 return Objects.equals(this.id, other.id) &&
186 Objects.equals(this.type, other.type) &&
187 Objects.equals(this.manufacturer, other.manufacturer) &&
188 Objects.equals(this.hwVersion, other.hwVersion) &&
189 Objects.equals(this.swVersion, other.swVersion) &&
190 Objects.equals(this.serialNumber, other.serialNumber);
191 }
192 return false;
193 }
194
195 @Override
196 public String toString() {
197 return toStringHelper(this)
198 .add("id", id)
199 .add("type", type)
200 .add("manufacturer", manufacturer)
201 .add("hwVersion", hwVersion)
202 .add("swVersion", swVersion)
203 .add("serialNumber", serialNumber)
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800204 .add("driver", driver() != null ? driver().name() : "")
tom3065d122014-09-03 21:56:43 -0700205 .toString();
206 }
207
208}