blob: cc60be63853ce751f4f4a2ea3af832191974462d [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";
Andrea Campanellab75b4882016-01-15 15:15:09 -080032 private static final String MANUFACTURER = "manufacturer";
33 private static final String HW_VERSION = "hwVersion";
34 private static final String SW_VERSION = "swVersion";
35 private static final String SERIAL = "serial";
Claudine Chiu5951bda2016-02-19 04:04:52 +000036 private static final String DEVICE_KEY_ID = "deviceKeyId";
Thomas Vachuska36008462016-01-07 15:38:20 -080037
Jordan Halterman83949a12017-06-21 10:35:38 -070038 private static final int DRIVER_MAX_LENGTH = 256;
39 private static final int MANUFACTURER_MAX_LENGTH = 256;
40 private static final int HW_VERSION_MAX_LENGTH = 256;
41 private static final int SW_VERSION_MAX_LENGTH = 256;
42 private static final int SERIAL_MAX_LENGTH = 256;
43 private static final int MANAGEMENT_ADDRESS_MAX_LENGTH = 1024;
44
Thomas Vachuska36008462016-01-07 15:38:20 -080045 @Override
46 public boolean isValid() {
Jordan Halterman83949a12017-06-21 10:35:38 -070047 // Validate type/DeviceKeyId
48 type();
49 deviceKeyId();
50
51 return super.isValid()
52 && hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE,
Simon Huntbc30e682017-02-15 18:39:23 -080053 GRID_Y, GRID_X, UI_TYPE, RACK_ADDRESS, OWNER, TYPE, DRIVER,
54 MANUFACTURER, HW_VERSION, SW_VERSION, SERIAL,
Jordan Halterman83949a12017-06-21 10:35:38 -070055 MANAGEMENT_ADDRESS, DEVICE_KEY_ID)
56 && isValidLength(DRIVER, DRIVER_MAX_LENGTH)
57 && isValidLength(MANUFACTURER, MANUFACTURER_MAX_LENGTH)
58 && isValidLength(HW_VERSION, MANUFACTURER_MAX_LENGTH)
59 && isValidLength(SW_VERSION, MANUFACTURER_MAX_LENGTH)
60 && isValidLength(SERIAL, MANUFACTURER_MAX_LENGTH)
61 && isValidLength(MANAGEMENT_ADDRESS, MANAGEMENT_ADDRESS_MAX_LENGTH);
Thomas Vachuska36008462016-01-07 15:38:20 -080062 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070063
64 /**
65 * Returns the device type.
66 *
67 * @return device type override
68 */
69 public Device.Type type() {
Thomas Vachuska2d6c5992016-07-01 12:42:26 +020070 return get(TYPE, null, Device.Type.class);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070071 }
72
73 /**
74 * Sets the device type.
75 *
76 * @param type device type override
77 * @return self
78 */
79 public BasicDeviceConfig type(Device.Type type) {
80 return (BasicDeviceConfig) setOrClear(TYPE, type);
81 }
82
83 /**
84 * Returns the device driver name.
85 *
Andrea Campanellab75b4882016-01-15 15:15:09 -080086 * @return driver name or null if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -070087 */
88 public String driver() {
Thomas Vachuska138de8b2016-01-11 21:31:38 -080089 return get(DRIVER, null);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070090 }
91
92 /**
93 * Sets the driver name.
94 *
95 * @param driverName new driver name; null to clear
96 * @return self
97 */
Andrea Campanellab75b4882016-01-15 15:15:09 -080098 public BasicDeviceConfig driver(String driverName) {
Jordan Halterman83949a12017-06-21 10:35:38 -070099 checkArgument(driverName.length() <= DRIVER_MAX_LENGTH,
100 "driver exceeds maximum length " + DRIVER_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800101 return (BasicDeviceConfig) setOrClear(DRIVER, driverName);
102 }
103
104 /**
105 * Returns the device manufacturer.
106 *
107 * @return manufacturer or null if not set
108 */
109 public String manufacturer() {
110 return get(MANUFACTURER, null);
111 }
112
113 /**
114 * Sets the device manufacturer.
115 *
116 * @param manufacturerName new manufacturer; null to clear
117 * @return self
118 */
119 public BasicDeviceConfig manufacturer(String manufacturerName) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700120 checkArgument(manufacturerName.length() <= MANUFACTURER_MAX_LENGTH,
121 "manufacturer exceeds maximum length " + MANUFACTURER_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800122 return (BasicDeviceConfig) setOrClear(MANUFACTURER, manufacturerName);
123 }
124
125 /**
126 * Returns the device hardware version.
127 *
128 * @return hardware version or null if not set
129 */
130 public String hwVersion() {
131 return get(HW_VERSION, null);
132 }
133
134 /**
135 * Sets the device hardware version.
136 *
137 * @param hwVersion new hardware version; null to clear
138 * @return self
139 */
140 public BasicDeviceConfig hwVersion(String hwVersion) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700141 checkArgument(hwVersion.length() <= HW_VERSION_MAX_LENGTH,
142 "hwVersion exceeds maximum length " + HW_VERSION_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800143 return (BasicDeviceConfig) setOrClear(HW_VERSION, hwVersion);
144 }
145
146 /**
147 * Returns the device software version.
148 *
149 * @return software version or null if not set
150 */
151 public String swVersion() {
152 return get(SW_VERSION, null);
153 }
154
155 /**
156 * Sets the device software version.
157 *
158 * @param swVersion new software version; null to clear
159 * @return self
160 */
161 public BasicDeviceConfig swVersion(String swVersion) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700162 checkArgument(swVersion.length() <= SW_VERSION_MAX_LENGTH,
163 "swVersion exceeds maximum length " + SW_VERSION_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800164 return (BasicDeviceConfig) setOrClear(SW_VERSION, swVersion);
165 }
166
167 /**
168 * Returns the device serial number.
169 *
170 * @return serial number or null if not set
171 */
172 public String serial() {
173 return get(SERIAL, null);
174 }
175
176 /**
177 * Sets the device serial number.
178 *
179 * @param serial new serial number; null to clear
180 * @return self
181 */
182 public BasicDeviceConfig serial(String serial) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700183 checkArgument(serial.length() <= SERIAL_MAX_LENGTH,
184 "serial exceeds maximum length " + SERIAL_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800185 return (BasicDeviceConfig) setOrClear(SERIAL, serial);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700186 }
187
andreafe3308f2015-10-06 15:51:25 -0700188 /**
189 * Returns the device management ip (ip:port).
190 *
191 * @return device management address (ip:port) or null if not set
192 */
193 public String managementAddress() {
194 return get(MANAGEMENT_ADDRESS, null);
195 }
196
197 /**
Claudine Chiu5951bda2016-02-19 04:04:52 +0000198 * Sets the device management ip (ip:port).
andreafe3308f2015-10-06 15:51:25 -0700199 *
200 * @param managementAddress new device management address (ip:port); null to clear
201 * @return self
202 */
Andrea Campanellab75b4882016-01-15 15:15:09 -0800203 public BasicDeviceConfig managementAddress(String managementAddress) {
Jordan Halterman83949a12017-06-21 10:35:38 -0700204 checkArgument(managementAddress.length() <= MANAGEMENT_ADDRESS_MAX_LENGTH,
205 "serialNumber exceeds maximum length " + MANAGEMENT_ADDRESS_MAX_LENGTH);
Andrea Campanellab75b4882016-01-15 15:15:09 -0800206 return (BasicDeviceConfig) setOrClear(MANAGEMENT_ADDRESS, managementAddress);
andreafe3308f2015-10-06 15:51:25 -0700207 }
208
Claudine Chiu5951bda2016-02-19 04:04:52 +0000209 /**
210 * Returns the device key id.
211 *
212 * @return device key id or null if not set
213 */
214 public DeviceKeyId deviceKeyId() {
215 String s = get(DEVICE_KEY_ID, null);
216 return s == null ? null : DeviceKeyId.deviceKeyId(s);
217 }
218
219 /**
220 * Sets the device key id.
221 *
Simon Hunt1e20dae2016-10-28 11:26:26 -0700222 * @param deviceKeyId the new device key id; null to clear
Claudine Chiu5951bda2016-02-19 04:04:52 +0000223 * @return self
224 */
225 public BasicDeviceConfig deviceKeyId(DeviceKeyId deviceKeyId) {
226 return (BasicDeviceConfig) setOrClear(DEVICE_KEY_ID,
Simon Hunt1e20dae2016-10-28 11:26:26 -0700227 deviceKeyId != null ? deviceKeyId.id() : null);
Claudine Chiu5951bda2016-02-19 04:04:52 +0000228 }
229
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700230 // TODO: device port meta-data to be configured via BasicPortsConfig
Thomas Vachuska36008462016-01-07 15:38:20 -0800231 // TODO: device credentials/keys; in a separate config
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700232
233}