blob: b5e6a169ab7e091b8e7b3389c27ec6ba6ee74017 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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;
tom3065d122014-09-03 21:56:43 -070029
30/**
tom4c6606f2014-09-07 11:11:21 -070031 * Default infrastructure device model implementation.
tom3065d122014-09-03 21:56:43 -070032 */
33public class DefaultDevice extends AbstractElement implements Device {
34
35 private final Type type;
36 private final String manufacturer;
37 private final String serialNumber;
38 private final String hwVersion;
39 private final String swVersion;
alshabib7911a052014-10-16 17:49:37 -070040 private final ChassisId chassisId;
tom3065d122014-09-03 21:56:43 -070041
tomc6491322014-09-19 15:27:40 -070042 // For serialization
43 private DefaultDevice() {
44 this.type = null;
45 this.manufacturer = null;
46 this.hwVersion = null;
47 this.swVersion = null;
48 this.serialNumber = null;
alshabib7911a052014-10-16 17:49:37 -070049 this.chassisId = null;
tomc6491322014-09-19 15:27:40 -070050 }
51
tom3065d122014-09-03 21:56:43 -070052 /**
53 * Creates a network element attributed to the specified provider.
54 *
55 * @param providerId identity of the provider
56 * @param id device identifier
57 * @param type device type
58 * @param manufacturer device manufacturer
59 * @param hwVersion device HW version
60 * @param swVersion device SW version
61 * @param serialNumber device serial number
Marc De Leenheerbb382352015-04-23 18:20:34 -070062 * @param chassisId chassis id
63 * @param annotations optional key/value annotations
tom3065d122014-09-03 21:56:43 -070064 */
65 public DefaultDevice(ProviderId providerId, DeviceId id, Type type,
66 String manufacturer, String hwVersion, String swVersion,
alshabib7911a052014-10-16 17:49:37 -070067 String serialNumber, ChassisId chassisId,
68 Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -070069 super(providerId, id, annotations);
tom3065d122014-09-03 21:56:43 -070070 this.type = type;
71 this.manufacturer = manufacturer;
72 this.hwVersion = hwVersion;
73 this.swVersion = swVersion;
74 this.serialNumber = serialNumber;
alshabib7911a052014-10-16 17:49:37 -070075 this.chassisId = chassisId;
tom3065d122014-09-03 21:56:43 -070076 }
77
78 @Override
79 public DeviceId id() {
tom5a9383a2014-10-02 07:33:52 -070080 return (DeviceId) id;
tom3065d122014-09-03 21:56:43 -070081 }
82
83 @Override
84 public Type type() {
85 return type;
86 }
87
88 @Override
89 public String manufacturer() {
90 return manufacturer;
91 }
92
93 @Override
94 public String hwVersion() {
95 return hwVersion;
96 }
97
98 @Override
99 public String swVersion() {
100 return swVersion;
101 }
102
103 @Override
104 public String serialNumber() {
105 return serialNumber;
106 }
107
108 @Override
alshabib7911a052014-10-16 17:49:37 -0700109 public ChassisId chassisId() {
110 return chassisId;
111 }
112
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800113 @Override
114 public <B extends Behaviour> B as(Class<B> projectionClass) {
115 if (HandlerBehaviour.class.isAssignableFrom(projectionClass)) {
116 bindAndCheckDriver();
117 return driver().createBehaviour(new DefaultDriverHandler(asData()), projectionClass);
118 }
119 return super.as(projectionClass);
120 }
121
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800122 /**
123 * Returns self as an immutable driver data instance.
124 *
125 * @return self as driver data
126 */
127 protected DriverData asData() {
128 return new DeviceDriverData();
129 }
130
131 @Override
132 protected Driver locateDriver() {
133 Driver driver = super.locateDriver();
134 return driver != null ? driver :
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800135 driverService().getDriver(manufacturer, hwVersion, swVersion);
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800136 }
137
138 /**
139 * Projection of the parent entity as a driver data entity.
140 */
141 protected class DeviceDriverData extends AnnotationDriverData {
142 @Override
143 public DeviceId deviceId() {
144 return id();
145 }
146 }
147
alshabib7911a052014-10-16 17:49:37 -0700148 @Override
tom3065d122014-09-03 21:56:43 -0700149 public int hashCode() {
150 return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber);
151 }
152
153 @Override
154 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700155 if (this == obj) {
156 return true;
157 }
tom3065d122014-09-03 21:56:43 -0700158 if (obj instanceof DefaultDevice) {
159 final DefaultDevice other = (DefaultDevice) obj;
160 return Objects.equals(this.id, other.id) &&
161 Objects.equals(this.type, other.type) &&
162 Objects.equals(this.manufacturer, other.manufacturer) &&
163 Objects.equals(this.hwVersion, other.hwVersion) &&
164 Objects.equals(this.swVersion, other.swVersion) &&
165 Objects.equals(this.serialNumber, other.serialNumber);
166 }
167 return false;
168 }
169
170 @Override
171 public String toString() {
172 return toStringHelper(this)
173 .add("id", id)
174 .add("type", type)
175 .add("manufacturer", manufacturer)
176 .add("hwVersion", hwVersion)
177 .add("swVersion", swVersion)
178 .add("serialNumber", serialNumber)
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800179 .add("driver", driver() != null ? driver().name() : "")
tom3065d122014-09-03 21:56:43 -0700180 .toString();
181 }
182
183}