blob: 9a85fbd6cceec1b14ec9a9c0ae0f79d145890e5f [file] [log] [blame]
Simon Hunt6ee94c82017-10-30 16:19:00 -07001/*
2 * Copyright 2017-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 */
16package org.opencord.aaa.api;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.google.common.collect.ImmutableSet;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.config.Config;
24import org.onosproject.net.config.basics.BasicElementConfig;
25
26import java.net.InetAddress;
27import java.net.UnknownHostException;
28import java.util.Set;
29
30import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
31
32/**
33 * Network configuration for the AAA application.
34 */
35public class AaaConfig extends Config<ApplicationId> {
36
37 private static final String RADIUS_IP = "radiusIp";
38 private static final String RADIUS_SERVER_PORT = "radiusServerPort";
39 private static final String RADIUS_MAC = "radiusMac";
40 private static final String NAS_IP = "nasIp";
41 private static final String NAS_MAC = "nasMac";
42 private static final String RADIUS_SECRET = "radiusSecret";
43
44 private static final String RADIUS_VLAN_ID = "vlanId";
45 private static final String RADIUS_VLAN_PRIORITY_BIT = "radiusPBit";
46 private static final String RADIUS_CONNECTION_TYPE = "radiusConnectionType";
47 private static final String RADIUS_SERVER_CONNECTPOINTS = "radiusServerConnectPoints";
48
49 // === Configuration default values
50
51 // RADIUS server IP address
52 protected static final String DEFAULT_RADIUS_IP = "10.128.10.4";
53
54 /**
55 * Default RADIUS MAC address.
56 */
57 public static final String DEFAULT_RADIUS_MAC = "00:00:00:00:01:10";
58
59 /**
60 * Default NAS IP address.
61 */
62 public static final String DEFAULT_NAS_IP = "10.128.9.244";
63
64 /**
65 * Default NAS MAC address.
66 */
67 public static final String DEFAULT_NAS_MAC = "00:00:00:00:10:01";
68
69 // RADIUS server shared secret
70 protected static final String DEFAULT_RADIUS_SECRET = "ONOSecret";
71
72 // RADIUS Server UDP Port Number
73 protected static final String DEFAULT_RADIUS_SERVER_PORT = "1812";
74
75 // RADIUS Server Vlan ID
76 protected static final String DEFAULT_RADIUS_VLAN_ID = "4093";
77
78 // RADIUS Sever P-Bit
79 protected static final String DEFAULT_RADIUS_VLAN_PRIORITY_BIT = "3";
80
81 // Method of communication with the RADIUS server
82 protected static final String DEFAULT_RADIUS_CONNECTION_TYPE = "socket";
83
84
85 /**
86 * Returns the value of the specified string property from this
87 * configuration object, if such a property is defined; otherwise
88 * returns the specified default value.
89 *
90 * @param name name of the property
91 * @param defaultValue default value if no such property defined
92 * @return property value if one is defined, default value otherwise
93 */
94 private String getStringProperty(String name, String defaultValue) {
95 return (object == null) ? defaultValue : get(name, defaultValue);
96 }
97
98 /**
99 * Returns the NAS IP address if defined, otherwise returns the default
100 * value {@value #DEFAULT_NAS_IP}.
101 *
102 * @return NAS IP address
103 */
104 public InetAddress nasIp() {
105 try {
106 return InetAddress.getByName(getStringProperty(NAS_IP,
107 DEFAULT_NAS_IP));
108 } catch (UnknownHostException e) {
109 return null;
110 }
111 }
112
113 /**
114 * Sets the NAS IP address. Use null to clear the property.
115 *
116 * @param ip new IP address to set; specify null to clear
117 * @return self
118 */
119 public BasicElementConfig nasIp(String ip) {
120 return (BasicElementConfig) setOrClear(NAS_IP, ip);
121 }
122
123 /**
124 * Returns the RADIUS server IP address if defined, otherwise returns
125 * the default value {@value #DEFAULT_RADIUS_IP}.
126 *
127 * @return RADIUS server IP address
128 */
129 public InetAddress radiusIp() {
130 try {
131 return InetAddress.getByName(getStringProperty(RADIUS_IP,
132 DEFAULT_RADIUS_IP));
133 } catch (UnknownHostException e) {
134 return null;
135 }
136 }
137
138 /**
139 * Sets the RADIUS server IP address. Use null to clear the property.
140 *
141 * @param ip new IP address to set; specify null to clear
142 * @return self
143 */
144 public BasicElementConfig radiusIp(String ip) {
145 return (BasicElementConfig) setOrClear(RADIUS_IP, ip);
146 }
147
148 /**
149 * Returns the RADIUS server MAC address if defined, otherwise returns the
150 * default value {@value #DEFAULT_RADIUS_MAC}.
151 *
152 * @return RADIUS server MAC address
153 */
154 public String radiusMac() {
155 return getStringProperty(RADIUS_MAC, DEFAULT_RADIUS_MAC);
156 }
157
158 /**
159 * Sets the RADIUS MAC address. Use null to clear the property.
160 *
161 * @param mac new MAC address to set; specify null to clear
162 * @return self
163 */
164 public BasicElementConfig radiusMac(String mac) {
165 return (BasicElementConfig) setOrClear(RADIUS_MAC, mac);
166 }
167
168 /**
169 * Returns the NAS MAC address if defined; otherwise returns the
170 * default value {@value #DEFAULT_NAS_MAC}.
171 *
172 * @return NAS MAC address
173 */
174 public String nasMac() {
175 return getStringProperty(NAS_MAC, DEFAULT_NAS_MAC);
176 }
177
178 /**
179 * Sets the NAS MAC address. Use null to clear the property.
180 *
181 * @param mac new MAC address to set; specify null to clear
182 * @return self
183 */
184 public BasicElementConfig nasMac(String mac) {
185 return (BasicElementConfig) setOrClear(NAS_MAC, mac);
186 }
187
188 /**
189 * Returns the RADIUS secret if defined; otherwise returns the
190 * default value {@value #DEFAULT_RADIUS_SECRET}.
191 *
192 * @return RADIUS secret
193 */
194 public String radiusSecret() {
195 return getStringProperty(RADIUS_SECRET, DEFAULT_RADIUS_SECRET);
196 }
197
198 /**
199 * Sets the RADIUS secret. Use null to clear the property.
200 *
201 * @param secret new RADIUS secret to set; specify null to clear
202 * @return self
203 */
204 public BasicElementConfig radiusSecret(String secret) {
205 return (BasicElementConfig) setOrClear(RADIUS_SECRET, secret);
206 }
207
208 /**
209 * Returns the RADIUS server UDP port if defined; otherwise returns the
210 * default value {@value #DEFAULT_RADIUS_SERVER_PORT}.
211 *
212 * @return RADIUS server UDP port
213 */
214 public short radiusServerUdpPort() {
215 return Short.parseShort(getStringProperty(RADIUS_SERVER_PORT,
216 DEFAULT_RADIUS_SERVER_PORT));
217 }
218
219 /**
220 * Sets the RADIUS server UDP port. Use -1 to clear the property.
221 *
222 * @param port new RADIUS server UDP port to set; specify -1 to clear
223 * @return self
224 */
225 public BasicElementConfig radiusServerUdpPort(short port) {
226 return (BasicElementConfig) setOrClear(RADIUS_SERVER_PORT, (long) port);
227 }
228
229 // The following properties have getters only...
230
231 /**
232 * Returns the RADIUS server VLAN ID if defined; otherwise returns the
233 * default value {@value #DEFAULT_RADIUS_VLAN_ID}.
234 *
235 * @return RADIUS Server VLAN ID
236 */
237 public short radiusServerVlanId() {
238 return Short.parseShort(getStringProperty(RADIUS_VLAN_ID,
239 DEFAULT_RADIUS_VLAN_ID));
240 }
241
242 /**
243 * Returns the type of connection to use to communicate with the
244 * RADIUS Server.
245 *
246 * @return "socket" or "packet_out"
247 */
248 public String radiusConnectionType() {
249 return getStringProperty(RADIUS_CONNECTION_TYPE,
250 DEFAULT_RADIUS_CONNECTION_TYPE);
251 }
252
253 /**
254 * Returns the RADIUS server VLAN priority bit (p-bit) if defined; otherwise
255 * returns the default value {@value #DEFAULT_RADIUS_VLAN_PRIORITY_BIT}.
256 *
257 * @return RADIUS server P-bit to use
258 */
259 public byte radiusServerPBit() {
260 return Byte.parseByte(getStringProperty(RADIUS_VLAN_PRIORITY_BIT,
261 DEFAULT_RADIUS_VLAN_PRIORITY_BIT));
262 }
263
264 /**
265 * Returns the set of connect points that may be used to reach the
266 * RADIUS server.
267 * <p>
268 * This method will return null if any string representation of the
269 * connect ports are malformed.
270 *
271 * @return the set of connect points to reach RADIUS
272 */
273 public Set<ConnectPoint> radiusServerConnectPoints() {
274 if (object == null || !object.has(RADIUS_SERVER_CONNECTPOINTS)) {
275 return ImmutableSet.of();
276 }
277
278 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
279 ArrayNode cps = (ArrayNode) object.path(RADIUS_SERVER_CONNECTPOINTS);
280 for (JsonNode jsonNode : cps) {
281 String portName = jsonNode.asText(null);
282 if (portName == null) {
283 return null;
284 }
285
286 try {
287 builder.add(deviceConnectPoint(portName));
288 } catch (IllegalArgumentException e) {
289 return null;
290 }
291 }
292 return builder.build();
293 }
294}