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; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 19 | import org.onosproject.net.driver.Behaviour; |
| 20 | import org.onosproject.net.driver.DefaultDriverHandler; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 21 | import org.onosproject.net.driver.Driver; |
| 22 | import org.onosproject.net.driver.DriverData; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 23 | import org.onosproject.net.driver.HandlerBehaviour; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 24 | import org.onosproject.net.provider.ProviderId; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.Objects; |
| 27 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 32 | * Default infrastructure device model implementation. |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 33 | */ |
| 34 | public class DefaultDevice extends AbstractElement implements Device { |
| 35 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 36 | 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 | |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 41 | private final Type type; |
| 42 | private final String manufacturer; |
| 43 | private final String serialNumber; |
| 44 | private final String hwVersion; |
| 45 | private final String swVersion; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 46 | private final ChassisId chassisId; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 47 | |
tom | c649132 | 2014-09-19 15:27:40 -0700 | [diff] [blame] | 48 | // 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; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 55 | this.chassisId = null; |
tom | c649132 | 2014-09-19 15:27:40 -0700 | [diff] [blame] | 56 | } |
| 57 | |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 58 | /** |
| 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 Leenheer | bb38235 | 2015-04-23 18:20:34 -0700 | [diff] [blame] | 68 | * @param chassisId chassis id |
| 69 | * @param annotations optional key/value annotations |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 70 | */ |
| 71 | public DefaultDevice(ProviderId providerId, DeviceId id, Type type, |
| 72 | String manufacturer, String hwVersion, String swVersion, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 73 | String serialNumber, ChassisId chassisId, |
| 74 | Annotations... annotations) { |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 75 | super(providerId, id, annotations); |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 76 | 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 | } |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 92 | this.type = type; |
| 93 | this.manufacturer = manufacturer; |
| 94 | this.hwVersion = hwVersion; |
| 95 | this.swVersion = swVersion; |
| 96 | this.serialNumber = serialNumber; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 97 | this.chassisId = chassisId; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public DeviceId id() { |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 102 | return (DeviceId) id; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 103 | } |
| 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 |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 131 | public ChassisId chassisId() { |
| 132 | return chassisId; |
| 133 | } |
| 134 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 135 | @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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 144 | /** |
| 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 Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 157 | driverService().getDriver(manufacturer, hwVersion, swVersion); |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 158 | } |
| 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 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 170 | @Override |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 171 | public int hashCode() { |
| 172 | return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 177 | if (this == obj) { |
| 178 | return true; |
| 179 | } |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 180 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 201 | .add("driver", driver() != null ? driver().name() : "") |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 202 | .toString(); |
| 203 | } |
| 204 | |
| 205 | } |