blob: 47c5375ccf0f7a035f2a815f90371af840ef7342 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska96d55b12015-05-11 08:52:03 -07003 *
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 Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.net.config.basics;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070017
18import org.onosproject.net.Device;
19import org.onosproject.net.DeviceId;
Claudine Chiu5951bda2016-02-19 04:04:52 +000020import org.onosproject.net.key.DeviceKeyId;
Simon Hunt1e20dae2016-10-28 11:26:26 -070021
Jordan Halterman83949a12017-06-21 10:35:38 -070022import static com.google.common.base.Preconditions.checkArgument;
23
Thomas Vachuska96d55b12015-05-11 08:52:03 -070024/**
25 * Basic configuration for network infrastructure devices.
26 */
Thomas Vachuska36008462016-01-07 15:38:20 -080027public final class BasicDeviceConfig extends BasicElementConfig<DeviceId> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -070028
Thomas Vachuska36008462016-01-07 15:38:20 -080029 private static final String TYPE = "type";
30 private static final String DRIVER = "driver";
31 private static final String MANAGEMENT_ADDRESS = "managementAddress";
Carmelo Cascone3977ea42019-02-28 13:43:42 -080032 private static final String PIPECONF = "pipeconf";
Andrea Campanellab75b4882016-01-15 15:15:09 -080033 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";
Andrea Campanellaad95aaf2020-03-27 12:53:46 +010037 private static final String PURGE_ON_DISCONNECT = "purgeOnDisconnection";
Claudine Chiu5951bda2016-02-19 04:04:52 +000038 private static final String DEVICE_KEY_ID = "deviceKeyId";
Thomas Vachuska36008462016-01-07 15:38:20 -080039
Jordan Halterman83949a12017-06-21 10:35:38 -070040 private static final int DRIVER_MAX_LENGTH = 256;
41 private static final int MANUFACTURER_MAX_LENGTH = 256;
42 private static final int HW_VERSION_MAX_LENGTH = 256;
43 private static final int SW_VERSION_MAX_LENGTH = 256;
44 private static final int SERIAL_MAX_LENGTH = 256;
45 private static final int MANAGEMENT_ADDRESS_MAX_LENGTH = 1024;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080046 private static final int PIPECONF_MAX_LENGTH = 256;
Jordan Halterman83949a12017-06-21 10:35:38 -070047
Thomas Vachuska36008462016-01-07 15:38:20 -080048 @Override
49 public boolean isValid() {
Jordan Halterman83949a12017-06-21 10:35:38 -070050 // Validate type/DeviceKeyId
51 type();
52 deviceKeyId();
53
54 return super.isValid()
55 && hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE,
Thomas Vachuska0d933862018-04-06 00:29:30 -070056 GRID_Y, GRID_X, UI_TYPE, RACK_ADDRESS, OWNER, TYPE, DRIVER, ROLES,
Simon Huntbc30e682017-02-15 18:39:23 -080057 MANUFACTURER, HW_VERSION, SW_VERSION, SERIAL,
Andrea Campanellaad95aaf2020-03-27 12:53:46 +010058 MANAGEMENT_ADDRESS, PIPECONF, DEVICE_KEY_ID, PURGE_ON_DISCONNECT)
Jordan Halterman83949a12017-06-21 10:35:38 -070059 && isValidLength(DRIVER, DRIVER_MAX_LENGTH)
60 && isValidLength(MANUFACTURER, MANUFACTURER_MAX_LENGTH)
61 && isValidLength(HW_VERSION, MANUFACTURER_MAX_LENGTH)
62 && isValidLength(SW_VERSION, MANUFACTURER_MAX_LENGTH)
63 && isValidLength(SERIAL, MANUFACTURER_MAX_LENGTH)
Carmelo Cascone3977ea42019-02-28 13:43:42 -080064 && isValidLength(MANAGEMENT_ADDRESS, MANAGEMENT_ADDRESS_MAX_LENGTH)
65 && isValidLength(PIPECONF, PIPECONF_MAX_LENGTH);
Thomas Vachuska36008462016-01-07 15:38:20 -080066 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070067
68 /**
69 * Returns the device type.
70 *
71 * @return device type override
72 */
73 public Device.Type type() {
Thomas Vachuska2d6c5992016-07-01 12:42:26 +020074 return get(TYPE, null, Device.Type.class);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070075 }
76
77 /**
78 * Sets the device type.
79 *
80 * @param type device type override
81 * @return self
82 */
83 public BasicDeviceConfig type(Device.Type type) {
84 return (BasicDeviceConfig) setOrClear(TYPE, type);
85 }
86
87 /**
88 * Returns the device driver name.
89 *
Andrea Campanellab75b4882016-01-15 15:15:09 -080090 * @return driver name or null if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -070091 */
92 public String driver() {
Thomas Vachuska138de8b2016-01-11 21:31:38 -080093 return get(DRIVER, null);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070094 }
95
96 /**
97 * Sets the driver name.
98 *
99 * @param driverName new driver name; null to clear
100 * @return self
101 */
Andrea Campanellab75b4882016-01-15 15:15:09 -0800102 public BasicDeviceConfig driver(String driverName) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700103 checkArgument(driverName.length() <= DRIVER_MAX_LENGTH,
104 "driver exceeds maximum length " + DRIVER_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800105 return (BasicDeviceConfig) setOrClear(DRIVER, driverName);
106 }
107
108 /**
109 * Returns the device manufacturer.
110 *
111 * @return manufacturer or null if not set
112 */
113 public String manufacturer() {
114 return get(MANUFACTURER, null);
115 }
116
117 /**
118 * Sets the device manufacturer.
119 *
120 * @param manufacturerName new manufacturer; null to clear
121 * @return self
122 */
123 public BasicDeviceConfig manufacturer(String manufacturerName) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700124 checkArgument(manufacturerName.length() <= MANUFACTURER_MAX_LENGTH,
125 "manufacturer exceeds maximum length " + MANUFACTURER_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800126 return (BasicDeviceConfig) setOrClear(MANUFACTURER, manufacturerName);
127 }
128
129 /**
130 * Returns the device hardware version.
131 *
132 * @return hardware version or null if not set
133 */
134 public String hwVersion() {
135 return get(HW_VERSION, null);
136 }
137
138 /**
139 * Sets the device hardware version.
140 *
141 * @param hwVersion new hardware version; null to clear
142 * @return self
143 */
144 public BasicDeviceConfig hwVersion(String hwVersion) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700145 checkArgument(hwVersion.length() <= HW_VERSION_MAX_LENGTH,
146 "hwVersion exceeds maximum length " + HW_VERSION_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800147 return (BasicDeviceConfig) setOrClear(HW_VERSION, hwVersion);
148 }
149
150 /**
151 * Returns the device software version.
152 *
153 * @return software version or null if not set
154 */
155 public String swVersion() {
156 return get(SW_VERSION, null);
157 }
158
159 /**
160 * Sets the device software version.
161 *
162 * @param swVersion new software version; null to clear
163 * @return self
164 */
165 public BasicDeviceConfig swVersion(String swVersion) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700166 checkArgument(swVersion.length() <= SW_VERSION_MAX_LENGTH,
167 "swVersion exceeds maximum length " + SW_VERSION_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800168 return (BasicDeviceConfig) setOrClear(SW_VERSION, swVersion);
169 }
170
171 /**
172 * Returns the device serial number.
173 *
174 * @return serial number or null if not set
175 */
176 public String serial() {
177 return get(SERIAL, null);
178 }
179
180 /**
181 * Sets the device serial number.
182 *
183 * @param serial new serial number; null to clear
184 * @return self
185 */
186 public BasicDeviceConfig serial(String serial) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700187 checkArgument(serial.length() <= SERIAL_MAX_LENGTH,
188 "serial exceeds maximum length " + SERIAL_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800189 return (BasicDeviceConfig) setOrClear(SERIAL, serial);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700190 }
191
andreafe3308f2015-10-06 15:51:25 -0700192 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800193 * Returns the device management address (e.g, "ip:port" or full URI
194 * string).
andreafe3308f2015-10-06 15:51:25 -0700195 *
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800196 * @return device management address or null if not set
andreafe3308f2015-10-06 15:51:25 -0700197 */
198 public String managementAddress() {
199 return get(MANAGEMENT_ADDRESS, null);
200 }
201
202 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800203 * Returns the device pipeconf.
204 *
205 * @return device pipeconf or null if not set
206 */
207 public String pipeconf() {
208 return get(PIPECONF, null);
209 }
210
211 /**
Claudine Chiu5951bda2016-02-19 04:04:52 +0000212 * Sets the device management ip (ip:port).
andreafe3308f2015-10-06 15:51:25 -0700213 *
214 * @param managementAddress new device management address (ip:port); null to clear
215 * @return self
216 */
Andrea Campanellab75b4882016-01-15 15:15:09 -0800217 public BasicDeviceConfig managementAddress(String managementAddress) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700218 checkArgument(managementAddress.length() <= MANAGEMENT_ADDRESS_MAX_LENGTH,
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800219 "managementAddress exceeds maximum length " + MANAGEMENT_ADDRESS_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800220 return (BasicDeviceConfig) setOrClear(MANAGEMENT_ADDRESS, managementAddress);
andreafe3308f2015-10-06 15:51:25 -0700221 }
222
Claudine Chiu5951bda2016-02-19 04:04:52 +0000223 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800224 * Sets the device pipeconf.
225 *
226 * @param pipeconf new device pipeconf
227 * @return self
228 */
229 public BasicDeviceConfig pipeconf(String pipeconf) {
230 checkArgument(pipeconf.length() <= PIPECONF_MAX_LENGTH,
231 "pipeconf exceeds maximum length " + MANAGEMENT_ADDRESS_MAX_LENGTH);
232 return (BasicDeviceConfig) setOrClear(PIPECONF, pipeconf);
233 }
234
235 /**
Claudine Chiu5951bda2016-02-19 04:04:52 +0000236 * Returns the device key id.
237 *
238 * @return device key id or null if not set
239 */
240 public DeviceKeyId deviceKeyId() {
241 String s = get(DEVICE_KEY_ID, null);
242 return s == null ? null : DeviceKeyId.deviceKeyId(s);
243 }
244
245 /**
246 * Sets the device key id.
247 *
Simon Hunt1e20dae2016-10-28 11:26:26 -0700248 * @param deviceKeyId the new device key id; null to clear
Claudine Chiu5951bda2016-02-19 04:04:52 +0000249 * @return self
250 */
251 public BasicDeviceConfig deviceKeyId(DeviceKeyId deviceKeyId) {
252 return (BasicDeviceConfig) setOrClear(DEVICE_KEY_ID,
Simon Hunt1e20dae2016-10-28 11:26:26 -0700253 deviceKeyId != null ? deviceKeyId.id() : null);
Claudine Chiu5951bda2016-02-19 04:04:52 +0000254 }
255
Andrea Campanellaad95aaf2020-03-27 12:53:46 +0100256 /**
257 * Returns the device purgeOnDisconnection flag for this device.
258 *
259 * @return device purgeOnDisconnection, false if not set.
260 */
261 public boolean purgeOnDisconnection() {
262 return get(PURGE_ON_DISCONNECT, false);
263 }
264
265 /**
266 * Sets the purgeOnDisconnection flag for the device.
267 *
268 * @param purgeOnDisconnection purges flows, groups, meters on disconnection.
269 * @return self
270 */
271 public BasicDeviceConfig purgeOnDisconnection(boolean purgeOnDisconnection) {
272 return (BasicDeviceConfig) setOrClear(PURGE_ON_DISCONNECT, purgeOnDisconnection);
273 }
274
275 /**
276 * Returns if the device purgeOnDisconnection flag for this device has been explicitly configured.
277 *
278 * @return device purgeOnDisconnection explicitly configured, false if not.
279 */
280 public boolean isPurgeOnDisconnectionConfigured() {
281 return hasField(PURGE_ON_DISCONNECT);
282 }
283
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700284 // TODO: device port meta-data to be configured via BasicPortsConfig
Thomas Vachuska36008462016-01-07 15:38:20 -0800285 // TODO: device credentials/keys; in a separate config
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700286
287}