blob: 401740a00ac3fa90dcc2bdf5dd88decd880222c9 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 Vachuska3afbc7f2016-02-01 15:55:38 -080019import org.onosproject.net.driver.Driver;
20import org.onosproject.net.driver.DriverData;
21import org.onosproject.net.provider.ProviderId;
tom3065d122014-09-03 21:56:43 -070022
23import java.util.Objects;
24
tomeadbb462014-09-07 16:10:19 -070025import static com.google.common.base.MoreObjects.toStringHelper;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080026import static org.onlab.util.Tools.nullIsNotFound;
tom3065d122014-09-03 21:56:43 -070027
28/**
tom4c6606f2014-09-07 11:11:21 -070029 * Default infrastructure device model implementation.
tom3065d122014-09-03 21:56:43 -070030 */
31public class DefaultDevice extends AbstractElement implements Device {
32
33 private final Type type;
34 private final String manufacturer;
35 private final String serialNumber;
36 private final String hwVersion;
37 private final String swVersion;
alshabib7911a052014-10-16 17:49:37 -070038 private final ChassisId chassisId;
tom3065d122014-09-03 21:56:43 -070039
tomc6491322014-09-19 15:27:40 -070040 // For serialization
41 private DefaultDevice() {
42 this.type = null;
43 this.manufacturer = null;
44 this.hwVersion = null;
45 this.swVersion = null;
46 this.serialNumber = null;
alshabib7911a052014-10-16 17:49:37 -070047 this.chassisId = null;
tomc6491322014-09-19 15:27:40 -070048 }
49
tom3065d122014-09-03 21:56:43 -070050 /**
51 * Creates a network element attributed to the specified provider.
52 *
53 * @param providerId identity of the provider
54 * @param id device identifier
55 * @param type device type
56 * @param manufacturer device manufacturer
57 * @param hwVersion device HW version
58 * @param swVersion device SW version
59 * @param serialNumber device serial number
Marc De Leenheerbb382352015-04-23 18:20:34 -070060 * @param chassisId chassis id
61 * @param annotations optional key/value annotations
tom3065d122014-09-03 21:56:43 -070062 */
63 public DefaultDevice(ProviderId providerId, DeviceId id, Type type,
64 String manufacturer, String hwVersion, String swVersion,
alshabib7911a052014-10-16 17:49:37 -070065 String serialNumber, ChassisId chassisId,
66 Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -070067 super(providerId, id, annotations);
tom3065d122014-09-03 21:56:43 -070068 this.type = type;
69 this.manufacturer = manufacturer;
70 this.hwVersion = hwVersion;
71 this.swVersion = swVersion;
72 this.serialNumber = serialNumber;
alshabib7911a052014-10-16 17:49:37 -070073 this.chassisId = chassisId;
tom3065d122014-09-03 21:56:43 -070074 }
75
76 @Override
77 public DeviceId id() {
tom5a9383a2014-10-02 07:33:52 -070078 return (DeviceId) id;
tom3065d122014-09-03 21:56:43 -070079 }
80
81 @Override
82 public Type type() {
83 return type;
84 }
85
86 @Override
87 public String manufacturer() {
88 return manufacturer;
89 }
90
91 @Override
92 public String hwVersion() {
93 return hwVersion;
94 }
95
96 @Override
97 public String swVersion() {
98 return swVersion;
99 }
100
101 @Override
102 public String serialNumber() {
103 return serialNumber;
104 }
105
106 @Override
alshabib7911a052014-10-16 17:49:37 -0700107 public ChassisId chassisId() {
108 return chassisId;
109 }
110
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800111 /**
112 * Returns self as an immutable driver data instance.
113 *
114 * @return self as driver data
115 */
116 protected DriverData asData() {
117 return new DeviceDriverData();
118 }
119
120 @Override
121 protected Driver locateDriver() {
122 Driver driver = super.locateDriver();
123 return driver != null ? driver :
124 nullIsNotFound(driverService().getDriver(manufacturer, hwVersion, swVersion),
125 "Driver not found");
126 }
127
128 /**
129 * Projection of the parent entity as a driver data entity.
130 */
131 protected class DeviceDriverData extends AnnotationDriverData {
132 @Override
133 public DeviceId deviceId() {
134 return id();
135 }
136 }
137
alshabib7911a052014-10-16 17:49:37 -0700138 @Override
tom3065d122014-09-03 21:56:43 -0700139 public int hashCode() {
140 return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber);
141 }
142
143 @Override
144 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700145 if (this == obj) {
146 return true;
147 }
tom3065d122014-09-03 21:56:43 -0700148 if (obj instanceof DefaultDevice) {
149 final DefaultDevice other = (DefaultDevice) obj;
150 return Objects.equals(this.id, other.id) &&
151 Objects.equals(this.type, other.type) &&
152 Objects.equals(this.manufacturer, other.manufacturer) &&
153 Objects.equals(this.hwVersion, other.hwVersion) &&
154 Objects.equals(this.swVersion, other.swVersion) &&
155 Objects.equals(this.serialNumber, other.serialNumber);
156 }
157 return false;
158 }
159
160 @Override
161 public String toString() {
162 return toStringHelper(this)
163 .add("id", id)
164 .add("type", type)
165 .add("manufacturer", manufacturer)
166 .add("hwVersion", hwVersion)
167 .add("swVersion", swVersion)
168 .add("serialNumber", serialNumber)
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800169 .add("driver", driver() != null ? driver().name() : "")
tom3065d122014-09-03 21:56:43 -0700170 .toString();
171 }
172
173}