Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.drivers.server.impl.devices; |
| 18 | |
| 19 | import org.onosproject.drivers.server.devices.CpuDevice; |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 20 | import org.onosproject.drivers.server.devices.nic.NicDevice; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 21 | import org.onosproject.drivers.server.devices.ServerDeviceDescription; |
| 22 | |
| 23 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 24 | import org.onosproject.net.SparseAnnotations; |
| 25 | import org.onlab.packet.ChassisId; |
| 26 | |
| 27 | import com.google.common.base.Objects; |
| 28 | |
| 29 | import java.net.URI; |
| 30 | import java.util.Collection; |
| 31 | |
| 32 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | import static org.onosproject.net.Device.Type; |
| 35 | |
| 36 | /** |
| 37 | * Default implementation of immutable server device description entity. |
| 38 | */ |
| 39 | public class DefaultServerDeviceDescription extends DefaultDeviceDescription |
| 40 | implements ServerDeviceDescription { |
| 41 | |
| 42 | private final Collection<CpuDevice> cpus; |
| 43 | private final Collection<NicDevice> nics; |
| 44 | |
| 45 | /** |
| 46 | * Creates a server device description using the supplied information. |
| 47 | * |
| 48 | * @param uri device URI |
| 49 | * @param type device type |
| 50 | * @param manufacturer device manufacturer |
| 51 | * @param hwVersion device HW version |
| 52 | * @param swVersion device SW version |
| 53 | * @param serialNumber device serial number |
| 54 | * @param chassis chassis id |
| 55 | * @param cpus set of CPUs |
| 56 | * @param nics set of network interface cards (NICs) |
| 57 | * @param annotations optional key/value annotations map |
| 58 | */ |
| 59 | public DefaultServerDeviceDescription( |
| 60 | URI uri, Type type, String manufacturer, |
| 61 | String hwVersion, String swVersion, |
| 62 | String serialNumber, ChassisId chassis, |
| 63 | Collection<CpuDevice> cpus, Collection<NicDevice> nics, |
| 64 | SparseAnnotations... annotations) { |
| 65 | this(uri, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 66 | chassis, true, cpus, nics, annotations); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates a server device description using the supplied information. |
| 71 | * |
| 72 | * @param uri device URI |
| 73 | * @param type device type |
| 74 | * @param manufacturer device manufacturer |
| 75 | * @param hwVersion device HW version |
| 76 | * @param swVersion device SW version |
| 77 | * @param serialNumber device serial number |
| 78 | * @param chassis chassis id |
| 79 | * @param cpus set of CPUs |
| 80 | * @param nics set of network interface cards (NICs) |
| 81 | * @param defaultAvailable optional whether device is by default available |
| 82 | * @param annotations optional key/value annotations map |
| 83 | */ |
| 84 | public DefaultServerDeviceDescription( |
| 85 | URI uri, Type type, String manufacturer, |
| 86 | String hwVersion, String swVersion, |
| 87 | String serialNumber, ChassisId chassis, |
| 88 | boolean defaultAvailable, |
| 89 | Collection<CpuDevice> cpus, Collection<NicDevice> nics, |
| 90 | SparseAnnotations... annotations) { |
| 91 | super( |
| 92 | uri, type, manufacturer, hwVersion, swVersion, |
| 93 | serialNumber, chassis, defaultAvailable, annotations |
| 94 | ); |
| 95 | |
| 96 | checkNotNull(cpus, "Device's set of CPUs cannot be null"); |
| 97 | checkNotNull(nics, "Device's set of NICs cannot be null"); |
| 98 | |
| 99 | this.cpus = cpus; |
| 100 | this.nics = nics; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Creates a server device description using the supplied information. |
| 105 | * @param base ServerDeviceDescription to basic information |
| 106 | * @param annotations Annotations to use. |
| 107 | */ |
| 108 | public DefaultServerDeviceDescription(ServerDeviceDescription base, |
| 109 | SparseAnnotations... annotations) { |
| 110 | this(base.deviceUri(), base.type(), base.manufacturer(), |
| 111 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
| 112 | base.chassisId(), base.isDefaultAvailable(), |
| 113 | base.cpus(), base.nics(), |
| 114 | annotations); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Creates a device description using the supplied information. |
| 119 | * @param base ServerDeviceDescription to basic information (except for type) |
| 120 | * @param type device type |
| 121 | * @param annotations Annotations to use. |
| 122 | */ |
| 123 | public DefaultServerDeviceDescription( |
| 124 | ServerDeviceDescription base, Type type, |
| 125 | SparseAnnotations... annotations) { |
| 126 | this(base.deviceUri(), type, base.manufacturer(), |
| 127 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
| 128 | base.chassisId(), base.isDefaultAvailable(), |
| 129 | base.cpus(), base.nics(), |
| 130 | annotations); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Creates a device description using the supplied information. |
| 135 | * |
| 136 | * @param base ServerDeviceDescription to basic information (except for defaultAvailable) |
| 137 | * @param defaultAvailable whether device should be made available by default |
| 138 | * @param annotations Annotations to use. |
| 139 | */ |
| 140 | public DefaultServerDeviceDescription( |
| 141 | ServerDeviceDescription base, |
| 142 | boolean defaultAvailable, |
| 143 | SparseAnnotations... annotations) { |
| 144 | this(base.deviceUri(), base.type(), base.manufacturer(), |
| 145 | base.hwVersion(), base.swVersion(), base.serialNumber(), |
| 146 | base.chassisId(), defaultAvailable, |
| 147 | base.cpus(), base.nics(), |
| 148 | annotations); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Collection<CpuDevice> cpus() { |
| 153 | return this.cpus; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Collection<NicDevice> nics() { |
| 158 | return this.nics; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public String toString() { |
| 163 | return toStringHelper(this) |
| 164 | .add("uri", deviceUri()) |
| 165 | .add("type", type()) |
| 166 | .add("manufacturer", manufacturer()) |
| 167 | .add("hwVersion", hwVersion()) |
| 168 | .add("swVersion", swVersion()) |
| 169 | .add("serial", serialNumber()) |
| 170 | .add("cpus", cpus) |
| 171 | .add("nics", nics) |
| 172 | .add("annotations", annotations()) |
| 173 | .toString(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int hashCode() { |
| 178 | return Objects.hashCode(super.hashCode(), cpus, nics); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean equals(Object object) { |
| 183 | if (object instanceof DefaultServerDeviceDescription) { |
| 184 | if (!super.equals(object)) { |
| 185 | return false; |
| 186 | } |
| 187 | DefaultServerDeviceDescription that = (DefaultServerDeviceDescription) object; |
| 188 | return Objects.equal(this.deviceUri(), that.deviceUri()) |
| 189 | && Objects.equal(this.type(), that.type()) |
| 190 | && Objects.equal(this.manufacturer(), that.manufacturer()) |
| 191 | && Objects.equal(this.hwVersion(), that.hwVersion()) |
| 192 | && Objects.equal(this.swVersion(), that.swVersion()) |
| 193 | && Objects.equal(this.serialNumber(), that.serialNumber()) |
| 194 | && Objects.equal(this.chassisId(), that.chassisId()) |
| 195 | && Objects.equal(this.cpus(), that.cpus()) |
| 196 | && Objects.equal(this.nics(), that.nics()) |
| 197 | && Objects.equal(this.isDefaultAvailable(), that.isDefaultAvailable()); |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | // Default constructor for serialization |
| 203 | DefaultServerDeviceDescription() { |
| 204 | super(null, null, null, null, null, null, null, (SparseAnnotations[]) null); |
| 205 | this.cpus = null; |
| 206 | this.nics = null; |
| 207 | } |
| 208 | } |