blob: 8c7aa477e44809b237a7450b08b6781f292309ac [file] [log] [blame]
Ray Milkeyfcb623d2015-10-01 16:48:18 -07001/*
Brian O'Connor4e33be22017-08-03 22:45:46 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyfcb623d2015-10-01 16:48:18 -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 */
alshabib6d527452016-06-01 18:00:47 -070016package org.opencord.aaa;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070017
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
Matt Jeanneret2ff1a782018-06-13 15:24:25 -040020import org.apache.commons.lang3.builder.ToStringBuilder;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010021
22import com.google.common.collect.ImmutableSet;
23
Ray Milkeyfcb623d2015-10-01 16:48:18 -070024import org.onosproject.core.ApplicationId;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010025import org.onosproject.net.ConnectPoint;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070026import org.onosproject.net.config.Config;
27import org.onosproject.net.config.basics.BasicElementConfig;
28
Jonathan Hart092dfb22015-11-16 23:05:21 -080029import java.net.InetAddress;
30import java.net.UnknownHostException;
31
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010032import java.util.HashSet;
33import java.util.Set;
34
Ray Milkeyfcb623d2015-10-01 16:48:18 -070035/**
36 * Network config for the AAA app.
37 */
Jonathan Hart092dfb22015-11-16 23:05:21 -080038public class AaaConfig extends Config<ApplicationId> {
Ray Milkeyfcb623d2015-10-01 16:48:18 -070039
40 private static final String RADIUS_IP = "radiusIp";
ke han1fe3b0e2017-02-28 09:50:20 +080041 private static final String RADIUS_SERVER_PORT = "radiusServerPort";
Ray Milkeyfcb623d2015-10-01 16:48:18 -070042 private static final String RADIUS_MAC = "radiusMac";
43 private static final String NAS_IP = "nasIp";
44 private static final String NAS_MAC = "nasMac";
45 private static final String RADIUS_SECRET = "radiusSecret";
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010046 private static final String RADIUS_VLAN_ID = "vlanId";
47 private static final String RADIUS_VLAN_PRIORITY_BIT = "radiusPBit";
48 private static final String RADIUS_CONNECTION_TYPE = "radiusConnectionType";
49 private static final String RADIUS_SERVER_CONNECTPOINTS = "radiusServerConnectPoints";
50 // Which packet customizer to use
51 // "packetCustomizer" : "sample" -- Means use SamplePAcketCustomizer
52 // "packetCustomizer" : "default" -- No customization of packets
53 // if param is missing it is treated as default
54 // This class should be a subclass of PacketCustomizer
55 private static final String PACKET_CUSTOMIZER = "packetCustomizer";
Ray Milkeyfcb623d2015-10-01 16:48:18 -070056
57 // RADIUS server IP address
Ray Milkey967776a2015-10-07 14:37:17 -070058 protected static final String DEFAULT_RADIUS_IP = "10.128.10.4";
Ray Milkeyfcb623d2015-10-01 16:48:18 -070059
60 // RADIUS MAC address
61 protected static final String DEFAULT_RADIUS_MAC = "00:00:00:00:01:10";
62
63 // NAS IP address
Ray Milkey967776a2015-10-07 14:37:17 -070064 protected static final String DEFAULT_NAS_IP = "10.128.9.244";
Ray Milkeyfcb623d2015-10-01 16:48:18 -070065
66 // NAS MAC address
67 protected static final String DEFAULT_NAS_MAC = "00:00:00:00:10:01";
68
Ray Milkeyfcb623d2015-10-01 16:48:18 -070069 // RADIUS server shared secret
70 protected static final String DEFAULT_RADIUS_SECRET = "ONOSecret";
71
Ray Milkey5d99bd12015-10-06 15:41:30 -070072 // Radius Server UDP Port Number
73 protected static final String DEFAULT_RADIUS_SERVER_PORT = "1812";
74
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010075 // 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 // Whether to use socket or not to communicate with RADIUS Server
82 protected static final String DEFAULT_RADIUS_CONNECTION_TYPE = "socket";
83
84 // Packet Customizer Default value
85 protected static final String DEFAULT_PACKET_CUSTOMIZER = "default";
86
87
Ray Milkey5d99bd12015-10-06 15:41:30 -070088 /**
89 * Gets the value of a string property, protecting for an empty
90 * JSON object.
91 *
92 * @param name name of the property
93 * @param defaultValue default value if none has been specified
94 * @return String value if one os found, default value otherwise
95 */
96 private String getStringProperty(String name, String defaultValue) {
97 if (object == null) {
98 return defaultValue;
99 }
100 return get(name, defaultValue);
101 }
102
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700103 /**
104 * Returns the NAS ip.
105 *
106 * @return ip address or null if not set
107 */
108 public InetAddress nasIp() {
109 try {
Ray Milkey5d99bd12015-10-06 15:41:30 -0700110 return InetAddress.getByName(getStringProperty(NAS_IP, DEFAULT_NAS_IP));
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700111 } catch (UnknownHostException e) {
112 return null;
113 }
114 }
115
116 /**
117 * Sets the NAS ip.
118 *
119 * @param ip new ip address; null to clear
120 * @return self
121 */
122 public BasicElementConfig nasIp(String ip) {
123 return (BasicElementConfig) setOrClear(NAS_IP, ip);
124 }
125
126 /**
127 * Returns the RADIUS server ip.
128 *
129 * @return ip address or null if not set
130 */
131 public InetAddress radiusIp() {
132 try {
Ray Milkey5d99bd12015-10-06 15:41:30 -0700133 return InetAddress.getByName(getStringProperty(RADIUS_IP, DEFAULT_RADIUS_IP));
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700134 } catch (UnknownHostException e) {
135 return null;
136 }
137 }
138
139 /**
140 * Sets the RADIUS server ip.
141 *
142 * @param ip new ip address; null to clear
143 * @return self
144 */
145 public BasicElementConfig radiusIp(String ip) {
146 return (BasicElementConfig) setOrClear(RADIUS_IP, ip);
147 }
148
149 /**
150 * Returns the RADIUS MAC address.
151 *
152 * @return mac address or null if not set
153 */
154 public String radiusMac() {
Ray Milkey5d99bd12015-10-06 15:41:30 -0700155 return getStringProperty(RADIUS_MAC, DEFAULT_RADIUS_MAC);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700156 }
157
158 /**
159 * Sets the RADIUS MAC address.
160 *
161 * @param mac new MAC address; 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 RADIUS MAC address.
170 *
171 * @return mac address or null if not set
172 */
173 public String nasMac() {
Ray Milkey5d99bd12015-10-06 15:41:30 -0700174 return getStringProperty(NAS_MAC, DEFAULT_NAS_MAC);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700175 }
176
177 /**
178 * Sets the RADIUS MAC address.
179 *
180 * @param mac new MAC address; null to clear
181 * @return self
182 */
183 public BasicElementConfig nasMac(String mac) {
184 return (BasicElementConfig) setOrClear(NAS_MAC, mac);
185 }
186
187 /**
188 * Returns the RADIUS secret.
189 *
190 * @return radius secret or null if not set
191 */
192 public String radiusSecret() {
Ray Milkey5d99bd12015-10-06 15:41:30 -0700193 return getStringProperty(RADIUS_SECRET, DEFAULT_RADIUS_SECRET);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700194 }
195
196 /**
197 * Sets the RADIUS secret.
198 *
199 * @param secret new MAC address; null to clear
200 * @return self
201 */
202 public BasicElementConfig radiusSecret(String secret) {
203 return (BasicElementConfig) setOrClear(RADIUS_SECRET, secret);
204 }
205
206 /**
Ray Milkey5d99bd12015-10-06 15:41:30 -0700207 * Returns the RADIUS server UDP port.
208 *
209 * @return radius server UDP port.
210 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800211 public short radiusServerUdpPort() {
Ray Milkey5d99bd12015-10-06 15:41:30 -0700212 return Short.parseShort(getStringProperty(RADIUS_SERVER_PORT,
213 DEFAULT_RADIUS_SERVER_PORT));
214 }
215
216 /**
217 * Sets the RADIUS port.
218 *
219 * @param port new RADIUS UDP port; -1 to clear
220 * @return self
221 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800222 public BasicElementConfig radiusServerUdpPort(short port) {
Ray Milkey5d99bd12015-10-06 15:41:30 -0700223 return (BasicElementConfig) setOrClear(RADIUS_SERVER_PORT, (long) port);
224 }
225
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100226 /**
227 * Returns the RADIUS server vlan ID.
228 *
229 * @return Radius Server VLan id or default if not set
230 */
231 public short radiusServerVlanId() {
232 return Short.parseShort(getStringProperty(RADIUS_VLAN_ID, DEFAULT_RADIUS_VLAN_ID));
233 }
234
235 /**
236 * Returns the type of connection to use to communicate with the RADIUS Server.
237 *
238 * @return "socket" or "packet_out"
239 */
240 public String radiusConnectionType() {
241 return getStringProperty(RADIUS_CONNECTION_TYPE, DEFAULT_RADIUS_CONNECTION_TYPE);
242 }
243
244 /**
245 * Returns the RADIUS server p-bit.
246 *
247 * @return Radius Server P-bit to use, default if not set
248 */
249 public byte radiusServerPBit() {
250 return Byte.parseByte(getStringProperty(RADIUS_VLAN_PRIORITY_BIT, DEFAULT_RADIUS_VLAN_PRIORITY_BIT));
251 }
252
253 /**
254 * Returns the PACKET CUSTOMIZER CLASS NAME.
255 *
256 * @return PACKET CUSTOMIZER, default if not set
257 */
258 public String radiusPktCustomizer() {
259 return getStringProperty(PACKET_CUSTOMIZER, DEFAULT_PACKET_CUSTOMIZER);
260 }
261
262 /**
263 * Returns the List of ConnectPoints to reach the Radius Server.
264 *
265 * @return List of ConnectPoints
266 */
267 public Set<ConnectPoint> radiusServerConnectPoints() {
268 if (object == null) {
269 return new HashSet<ConnectPoint>();
270 }
271
272 if (!object.has(RADIUS_SERVER_CONNECTPOINTS)) {
273 return ImmutableSet.of();
274 }
275
276 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
277 ArrayNode arrayNode = (ArrayNode) object.path(RADIUS_SERVER_CONNECTPOINTS);
278 for (JsonNode jsonNode : arrayNode) {
279 String portName = jsonNode.asText(null);
280 if (portName == null) {
281 return null;
282 }
283 try {
284 builder.add(ConnectPoint.deviceConnectPoint(portName));
285 } catch (IllegalArgumentException e) {
286 return null;
287 }
288 }
289 return builder.build();
290 }
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400291
292 @Override
293 public String toString() {
294 return ToStringBuilder.reflectionToString(this);
295 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700296}