Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -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 | */ |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.basics; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
| 18 | import org.onosproject.net.Device; |
| 19 | import org.onosproject.net.DeviceId; |
Claudine Chiu | 5951bda | 2016-02-19 04:04:52 +0000 | [diff] [blame] | 20 | import org.onosproject.net.key.DeviceKeyId; |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 21 | |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkArgument; |
| 23 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Basic configuration for network infrastructure devices. |
| 26 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 27 | public final class BasicDeviceConfig extends BasicElementConfig<DeviceId> { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 28 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 29 | private static final String TYPE = "type"; |
| 30 | private static final String DRIVER = "driver"; |
| 31 | private static final String MANAGEMENT_ADDRESS = "managementAddress"; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 32 | private static final String PIPECONF = "pipeconf"; |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 33 | private static final String MANUFACTURER = "manufacturer"; |
| 34 | private static final String HW_VERSION = "hwVersion"; |
| 35 | private static final String SW_VERSION = "swVersion"; |
| 36 | private static final String SERIAL = "serial"; |
Claudine Chiu | 5951bda | 2016-02-19 04:04:52 +0000 | [diff] [blame] | 37 | private static final String DEVICE_KEY_ID = "deviceKeyId"; |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 38 | |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 39 | private static final int DRIVER_MAX_LENGTH = 256; |
| 40 | private static final int MANUFACTURER_MAX_LENGTH = 256; |
| 41 | private static final int HW_VERSION_MAX_LENGTH = 256; |
| 42 | private static final int SW_VERSION_MAX_LENGTH = 256; |
| 43 | private static final int SERIAL_MAX_LENGTH = 256; |
| 44 | private static final int MANAGEMENT_ADDRESS_MAX_LENGTH = 1024; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 45 | private static final int PIPECONF_MAX_LENGTH = 256; |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 46 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 47 | @Override |
| 48 | public boolean isValid() { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 49 | // Validate type/DeviceKeyId |
| 50 | type(); |
| 51 | deviceKeyId(); |
| 52 | |
| 53 | return super.isValid() |
| 54 | && hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE, |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 55 | GRID_Y, GRID_X, UI_TYPE, RACK_ADDRESS, OWNER, TYPE, DRIVER, ROLES, |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 56 | MANUFACTURER, HW_VERSION, SW_VERSION, SERIAL, |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 57 | MANAGEMENT_ADDRESS, PIPECONF, DEVICE_KEY_ID) |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 58 | && isValidLength(DRIVER, DRIVER_MAX_LENGTH) |
| 59 | && isValidLength(MANUFACTURER, MANUFACTURER_MAX_LENGTH) |
| 60 | && isValidLength(HW_VERSION, MANUFACTURER_MAX_LENGTH) |
| 61 | && isValidLength(SW_VERSION, MANUFACTURER_MAX_LENGTH) |
| 62 | && isValidLength(SERIAL, MANUFACTURER_MAX_LENGTH) |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 63 | && isValidLength(MANAGEMENT_ADDRESS, MANAGEMENT_ADDRESS_MAX_LENGTH) |
| 64 | && isValidLength(PIPECONF, PIPECONF_MAX_LENGTH); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 65 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Returns the device type. |
| 69 | * |
| 70 | * @return device type override |
| 71 | */ |
| 72 | public Device.Type type() { |
Thomas Vachuska | 2d6c599 | 2016-07-01 12:42:26 +0200 | [diff] [blame] | 73 | return get(TYPE, null, Device.Type.class); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Sets the device type. |
| 78 | * |
| 79 | * @param type device type override |
| 80 | * @return self |
| 81 | */ |
| 82 | public BasicDeviceConfig type(Device.Type type) { |
| 83 | return (BasicDeviceConfig) setOrClear(TYPE, type); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the device driver name. |
| 88 | * |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 89 | * @return driver name or null if not set |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 90 | */ |
| 91 | public String driver() { |
Thomas Vachuska | 138de8b | 2016-01-11 21:31:38 -0800 | [diff] [blame] | 92 | return get(DRIVER, null); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sets the driver name. |
| 97 | * |
| 98 | * @param driverName new driver name; null to clear |
| 99 | * @return self |
| 100 | */ |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 101 | public BasicDeviceConfig driver(String driverName) { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 102 | checkArgument(driverName.length() <= DRIVER_MAX_LENGTH, |
| 103 | "driver exceeds maximum length " + DRIVER_MAX_LENGTH); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 104 | return (BasicDeviceConfig) setOrClear(DRIVER, driverName); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the device manufacturer. |
| 109 | * |
| 110 | * @return manufacturer or null if not set |
| 111 | */ |
| 112 | public String manufacturer() { |
| 113 | return get(MANUFACTURER, null); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Sets the device manufacturer. |
| 118 | * |
| 119 | * @param manufacturerName new manufacturer; null to clear |
| 120 | * @return self |
| 121 | */ |
| 122 | public BasicDeviceConfig manufacturer(String manufacturerName) { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 123 | checkArgument(manufacturerName.length() <= MANUFACTURER_MAX_LENGTH, |
| 124 | "manufacturer exceeds maximum length " + MANUFACTURER_MAX_LENGTH); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 125 | return (BasicDeviceConfig) setOrClear(MANUFACTURER, manufacturerName); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the device hardware version. |
| 130 | * |
| 131 | * @return hardware version or null if not set |
| 132 | */ |
| 133 | public String hwVersion() { |
| 134 | return get(HW_VERSION, null); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sets the device hardware version. |
| 139 | * |
| 140 | * @param hwVersion new hardware version; null to clear |
| 141 | * @return self |
| 142 | */ |
| 143 | public BasicDeviceConfig hwVersion(String hwVersion) { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 144 | checkArgument(hwVersion.length() <= HW_VERSION_MAX_LENGTH, |
| 145 | "hwVersion exceeds maximum length " + HW_VERSION_MAX_LENGTH); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 146 | return (BasicDeviceConfig) setOrClear(HW_VERSION, hwVersion); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the device software version. |
| 151 | * |
| 152 | * @return software version or null if not set |
| 153 | */ |
| 154 | public String swVersion() { |
| 155 | return get(SW_VERSION, null); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Sets the device software version. |
| 160 | * |
| 161 | * @param swVersion new software version; null to clear |
| 162 | * @return self |
| 163 | */ |
| 164 | public BasicDeviceConfig swVersion(String swVersion) { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 165 | checkArgument(swVersion.length() <= SW_VERSION_MAX_LENGTH, |
| 166 | "swVersion exceeds maximum length " + SW_VERSION_MAX_LENGTH); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 167 | return (BasicDeviceConfig) setOrClear(SW_VERSION, swVersion); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the device serial number. |
| 172 | * |
| 173 | * @return serial number or null if not set |
| 174 | */ |
| 175 | public String serial() { |
| 176 | return get(SERIAL, null); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Sets the device serial number. |
| 181 | * |
| 182 | * @param serial new serial number; null to clear |
| 183 | * @return self |
| 184 | */ |
| 185 | public BasicDeviceConfig serial(String serial) { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 186 | checkArgument(serial.length() <= SERIAL_MAX_LENGTH, |
| 187 | "serial exceeds maximum length " + SERIAL_MAX_LENGTH); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 188 | return (BasicDeviceConfig) setOrClear(SERIAL, serial); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 189 | } |
| 190 | |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 191 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 192 | * Returns the device management address (e.g, "ip:port" or full URI |
| 193 | * string). |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 194 | * |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 195 | * @return device management address or null if not set |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 196 | */ |
| 197 | public String managementAddress() { |
| 198 | return get(MANAGEMENT_ADDRESS, null); |
| 199 | } |
| 200 | |
| 201 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 202 | * Returns the device pipeconf. |
| 203 | * |
| 204 | * @return device pipeconf or null if not set |
| 205 | */ |
| 206 | public String pipeconf() { |
| 207 | return get(PIPECONF, null); |
| 208 | } |
| 209 | |
| 210 | /** |
Claudine Chiu | 5951bda | 2016-02-19 04:04:52 +0000 | [diff] [blame] | 211 | * Sets the device management ip (ip:port). |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 212 | * |
| 213 | * @param managementAddress new device management address (ip:port); null to clear |
| 214 | * @return self |
| 215 | */ |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 216 | public BasicDeviceConfig managementAddress(String managementAddress) { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 217 | checkArgument(managementAddress.length() <= MANAGEMENT_ADDRESS_MAX_LENGTH, |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 218 | "managementAddress exceeds maximum length " + MANAGEMENT_ADDRESS_MAX_LENGTH); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 219 | return (BasicDeviceConfig) setOrClear(MANAGEMENT_ADDRESS, managementAddress); |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Claudine Chiu | 5951bda | 2016-02-19 04:04:52 +0000 | [diff] [blame] | 222 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 223 | * Sets the device pipeconf. |
| 224 | * |
| 225 | * @param pipeconf new device pipeconf |
| 226 | * @return self |
| 227 | */ |
| 228 | public BasicDeviceConfig pipeconf(String pipeconf) { |
| 229 | checkArgument(pipeconf.length() <= PIPECONF_MAX_LENGTH, |
| 230 | "pipeconf exceeds maximum length " + MANAGEMENT_ADDRESS_MAX_LENGTH); |
| 231 | return (BasicDeviceConfig) setOrClear(PIPECONF, pipeconf); |
| 232 | } |
| 233 | |
| 234 | /** |
Claudine Chiu | 5951bda | 2016-02-19 04:04:52 +0000 | [diff] [blame] | 235 | * Returns the device key id. |
| 236 | * |
| 237 | * @return device key id or null if not set |
| 238 | */ |
| 239 | public DeviceKeyId deviceKeyId() { |
| 240 | String s = get(DEVICE_KEY_ID, null); |
| 241 | return s == null ? null : DeviceKeyId.deviceKeyId(s); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Sets the device key id. |
| 246 | * |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 247 | * @param deviceKeyId the new device key id; null to clear |
Claudine Chiu | 5951bda | 2016-02-19 04:04:52 +0000 | [diff] [blame] | 248 | * @return self |
| 249 | */ |
| 250 | public BasicDeviceConfig deviceKeyId(DeviceKeyId deviceKeyId) { |
| 251 | return (BasicDeviceConfig) setOrClear(DEVICE_KEY_ID, |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 252 | deviceKeyId != null ? deviceKeyId.id() : null); |
Claudine Chiu | 5951bda | 2016-02-19 04:04:52 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 255 | // TODO: device port meta-data to be configured via BasicPortsConfig |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 256 | // TODO: device credentials/keys; in a separate config |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 257 | |
| 258 | } |