Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 17 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 18 | import org.onlab.packet.ChassisId; |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 19 | import org.onlab.util.ItemNotFoundException; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 20 | import org.onosproject.net.driver.Behaviour; |
| 21 | import org.onosproject.net.driver.DefaultDriverHandler; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 22 | import org.onosproject.net.driver.Driver; |
| 23 | import org.onosproject.net.driver.DriverData; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 24 | import org.onosproject.net.driver.HandlerBehaviour; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 25 | import org.onosproject.net.provider.ProviderId; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Objects; |
| 28 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 33 | * Default infrastructure device model implementation. |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 34 | */ |
| 35 | public class DefaultDevice extends AbstractElement implements Device { |
| 36 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 37 | 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 | |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 42 | private final Type type; |
| 43 | private final String manufacturer; |
| 44 | private final String serialNumber; |
| 45 | private final String hwVersion; |
| 46 | private final String swVersion; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 47 | private final ChassisId chassisId; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 48 | |
tom | c649132 | 2014-09-19 15:27:40 -0700 | [diff] [blame] | 49 | // 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; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 56 | this.chassisId = null; |
tom | c649132 | 2014-09-19 15:27:40 -0700 | [diff] [blame] | 57 | } |
| 58 | |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 59 | /** |
| 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 Leenheer | bb38235 | 2015-04-23 18:20:34 -0700 | [diff] [blame] | 69 | * @param chassisId chassis id |
| 70 | * @param annotations optional key/value annotations |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 71 | */ |
| 72 | public DefaultDevice(ProviderId providerId, DeviceId id, Type type, |
| 73 | String manufacturer, String hwVersion, String swVersion, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 74 | String serialNumber, ChassisId chassisId, |
| 75 | Annotations... annotations) { |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 76 | super(providerId, id, annotations); |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 77 | 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 | } |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 93 | this.type = type; |
| 94 | this.manufacturer = manufacturer; |
| 95 | this.hwVersion = hwVersion; |
| 96 | this.swVersion = swVersion; |
| 97 | this.serialNumber = serialNumber; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 98 | this.chassisId = chassisId; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public DeviceId id() { |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 103 | return (DeviceId) id; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 104 | } |
| 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 |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 132 | public ChassisId chassisId() { |
| 133 | return chassisId; |
| 134 | } |
| 135 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 136 | @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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 145 | /** |
| 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 Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 156 | try { |
| 157 | return driverService().getDriver(id()); |
| 158 | } catch (ItemNotFoundException e) { |
| 159 | return null; |
| 160 | } |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 161 | } |
| 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 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 173 | @Override |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 174 | public int hashCode() { |
| 175 | return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 180 | if (this == obj) { |
| 181 | return true; |
| 182 | } |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 183 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 204 | .add("driver", driver() != null ? driver().name() : "") |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 205 | .toString(); |
| 206 | } |
| 207 | |
| 208 | } |