Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 31 | * Default infrastructure device model implementation. |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 32 | */ |
| 33 | public 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; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 40 | private final ChassisId chassisId; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 41 | |
tom | c649132 | 2014-09-19 15:27:40 -0700 | [diff] [blame] | 42 | // 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; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 49 | this.chassisId = null; |
tom | c649132 | 2014-09-19 15:27:40 -0700 | [diff] [blame] | 50 | } |
| 51 | |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 52 | /** |
| 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 Leenheer | bb38235 | 2015-04-23 18:20:34 -0700 | [diff] [blame] | 62 | * @param chassisId chassis id |
| 63 | * @param annotations optional key/value annotations |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 64 | */ |
| 65 | public DefaultDevice(ProviderId providerId, DeviceId id, Type type, |
| 66 | String manufacturer, String hwVersion, String swVersion, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 67 | String serialNumber, ChassisId chassisId, |
| 68 | Annotations... annotations) { |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 69 | super(providerId, id, annotations); |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 70 | this.type = type; |
| 71 | this.manufacturer = manufacturer; |
| 72 | this.hwVersion = hwVersion; |
| 73 | this.swVersion = swVersion; |
| 74 | this.serialNumber = serialNumber; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 75 | this.chassisId = chassisId; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public DeviceId id() { |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 80 | return (DeviceId) id; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 81 | } |
| 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 |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 109 | public ChassisId chassisId() { |
| 110 | return chassisId; |
| 111 | } |
| 112 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 113 | @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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 122 | /** |
| 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 Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 135 | driverService().getDriver(manufacturer, hwVersion, swVersion); |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 136 | } |
| 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 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 148 | @Override |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 149 | public int hashCode() { |
| 150 | return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 155 | if (this == obj) { |
| 156 | return true; |
| 157 | } |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 158 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 179 | .add("driver", driver() != null ? driver().name() : "") |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 180 | .toString(); |
| 181 | } |
| 182 | |
| 183 | } |