Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 4e33be2 | 2017-08-03 22:45:46 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -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 | */ |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 16 | package org.opencord.aaa; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 17 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 20 | import org.apache.commons.lang3.builder.ToStringBuilder; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 21 | |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 26 | import org.onosproject.net.config.Config; |
| 27 | import org.onosproject.net.config.basics.BasicElementConfig; |
| 28 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 29 | import java.net.InetAddress; |
| 30 | import java.net.UnknownHostException; |
| 31 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 32 | import java.util.HashSet; |
| 33 | import java.util.Set; |
| 34 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Network config for the AAA app. |
| 37 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 38 | public class AaaConfig extends Config<ApplicationId> { |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 39 | |
Jonathan Hart | f935b12 | 2018-07-11 16:16:02 -0700 | [diff] [blame] | 40 | private static final String RADIUS_HOST = "radiusHost"; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 41 | private static final String RADIUS_IP = "radiusIp"; |
ke han | 1fe3b0e | 2017-02-28 09:50:20 +0800 | [diff] [blame] | 42 | private static final String RADIUS_SERVER_PORT = "radiusServerPort"; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 43 | private static final String RADIUS_MAC = "radiusMac"; |
| 44 | private static final String NAS_IP = "nasIp"; |
| 45 | private static final String NAS_MAC = "nasMac"; |
| 46 | private static final String RADIUS_SECRET = "radiusSecret"; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 47 | private static final String RADIUS_VLAN_ID = "vlanId"; |
| 48 | private static final String RADIUS_VLAN_PRIORITY_BIT = "radiusPBit"; |
| 49 | private static final String RADIUS_CONNECTION_TYPE = "radiusConnectionType"; |
| 50 | private static final String RADIUS_SERVER_CONNECTPOINTS = "radiusServerConnectPoints"; |
| 51 | // Which packet customizer to use |
| 52 | // "packetCustomizer" : "sample" -- Means use SamplePAcketCustomizer |
| 53 | // "packetCustomizer" : "default" -- No customization of packets |
| 54 | // if param is missing it is treated as default |
| 55 | // This class should be a subclass of PacketCustomizer |
| 56 | private static final String PACKET_CUSTOMIZER = "packetCustomizer"; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 57 | |
| 58 | // RADIUS server IP address |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 59 | protected static final String DEFAULT_RADIUS_IP = "10.128.10.4"; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 60 | |
| 61 | // RADIUS MAC address |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 62 | public static final String DEFAULT_RADIUS_MAC = "00:00:00:00:01:10"; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 63 | |
| 64 | // NAS IP address |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 65 | public static final String DEFAULT_NAS_IP = "10.128.9.244"; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 66 | |
| 67 | // NAS MAC address |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 68 | public static final String DEFAULT_NAS_MAC = "00:00:00:00:10:01"; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 69 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 70 | // RADIUS server shared secret |
| 71 | protected static final String DEFAULT_RADIUS_SECRET = "ONOSecret"; |
| 72 | |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 73 | // Radius Server UDP Port Number |
| 74 | protected static final String DEFAULT_RADIUS_SERVER_PORT = "1812"; |
| 75 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 76 | // Radius Server Vlan ID |
| 77 | protected static final String DEFAULT_RADIUS_VLAN_ID = "4093"; |
| 78 | |
| 79 | // Radius Sever P-Bit |
| 80 | protected static final String DEFAULT_RADIUS_VLAN_PRIORITY_BIT = "3"; |
| 81 | |
| 82 | // Whether to use socket or not to communicate with RADIUS Server |
| 83 | protected static final String DEFAULT_RADIUS_CONNECTION_TYPE = "socket"; |
| 84 | |
| 85 | // Packet Customizer Default value |
| 86 | protected static final String DEFAULT_PACKET_CUSTOMIZER = "default"; |
| 87 | |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 88 | /** |
| 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 Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Returns the NAS ip. |
| 105 | * |
| 106 | * @return ip address or null if not set |
| 107 | */ |
| 108 | public InetAddress nasIp() { |
| 109 | try { |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 110 | return InetAddress.getByName(getStringProperty(NAS_IP, DEFAULT_NAS_IP)); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 111 | } 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 | |
Jonathan Hart | f935b12 | 2018-07-11 16:16:02 -0700 | [diff] [blame] | 126 | public String radiusHostName() { |
| 127 | return getStringProperty(RADIUS_HOST, null); |
| 128 | } |
| 129 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Returns the RADIUS server ip. |
| 132 | * |
| 133 | * @return ip address or null if not set |
| 134 | */ |
| 135 | public InetAddress radiusIp() { |
| 136 | try { |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 137 | return InetAddress.getByName(getStringProperty(RADIUS_IP, DEFAULT_RADIUS_IP)); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 138 | } catch (UnknownHostException e) { |
| 139 | return null; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Sets the RADIUS server ip. |
| 145 | * |
| 146 | * @param ip new ip address; null to clear |
| 147 | * @return self |
| 148 | */ |
| 149 | public BasicElementConfig radiusIp(String ip) { |
| 150 | return (BasicElementConfig) setOrClear(RADIUS_IP, ip); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the RADIUS MAC address. |
| 155 | * |
| 156 | * @return mac address or null if not set |
| 157 | */ |
| 158 | public String radiusMac() { |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 159 | return getStringProperty(RADIUS_MAC, DEFAULT_RADIUS_MAC); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Sets the RADIUS MAC address. |
| 164 | * |
| 165 | * @param mac new MAC address; null to clear |
| 166 | * @return self |
| 167 | */ |
| 168 | public BasicElementConfig radiusMac(String mac) { |
| 169 | return (BasicElementConfig) setOrClear(RADIUS_MAC, mac); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns the RADIUS MAC address. |
| 174 | * |
| 175 | * @return mac address or null if not set |
| 176 | */ |
| 177 | public String nasMac() { |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 178 | return getStringProperty(NAS_MAC, DEFAULT_NAS_MAC); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Sets the RADIUS MAC address. |
| 183 | * |
| 184 | * @param mac new MAC address; null to clear |
| 185 | * @return self |
| 186 | */ |
| 187 | public BasicElementConfig nasMac(String mac) { |
| 188 | return (BasicElementConfig) setOrClear(NAS_MAC, mac); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns the RADIUS secret. |
| 193 | * |
| 194 | * @return radius secret or null if not set |
| 195 | */ |
| 196 | public String radiusSecret() { |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 197 | return getStringProperty(RADIUS_SECRET, DEFAULT_RADIUS_SECRET); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets the RADIUS secret. |
| 202 | * |
| 203 | * @param secret new MAC address; null to clear |
| 204 | * @return self |
| 205 | */ |
| 206 | public BasicElementConfig radiusSecret(String secret) { |
| 207 | return (BasicElementConfig) setOrClear(RADIUS_SECRET, secret); |
| 208 | } |
| 209 | |
| 210 | /** |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 211 | * Returns the RADIUS server UDP port. |
| 212 | * |
| 213 | * @return radius server UDP port. |
| 214 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 215 | public short radiusServerUdpPort() { |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 216 | return Short.parseShort(getStringProperty(RADIUS_SERVER_PORT, |
| 217 | DEFAULT_RADIUS_SERVER_PORT)); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Sets the RADIUS port. |
| 222 | * |
| 223 | * @param port new RADIUS UDP port; -1 to clear |
| 224 | * @return self |
| 225 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 226 | public BasicElementConfig radiusServerUdpPort(short port) { |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 227 | return (BasicElementConfig) setOrClear(RADIUS_SERVER_PORT, (long) port); |
| 228 | } |
| 229 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 230 | /** |
| 231 | * Returns the RADIUS server vlan ID. |
| 232 | * |
| 233 | * @return Radius Server VLan id or default if not set |
| 234 | */ |
| 235 | public short radiusServerVlanId() { |
| 236 | return Short.parseShort(getStringProperty(RADIUS_VLAN_ID, DEFAULT_RADIUS_VLAN_ID)); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns the type of connection to use to communicate with the RADIUS Server. |
| 241 | * |
| 242 | * @return "socket" or "packet_out" |
| 243 | */ |
| 244 | public String radiusConnectionType() { |
| 245 | return getStringProperty(RADIUS_CONNECTION_TYPE, DEFAULT_RADIUS_CONNECTION_TYPE); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns the RADIUS server p-bit. |
| 250 | * |
| 251 | * @return Radius Server P-bit to use, default if not set |
| 252 | */ |
| 253 | public byte radiusServerPBit() { |
| 254 | return Byte.parseByte(getStringProperty(RADIUS_VLAN_PRIORITY_BIT, DEFAULT_RADIUS_VLAN_PRIORITY_BIT)); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Returns the PACKET CUSTOMIZER CLASS NAME. |
| 259 | * |
| 260 | * @return PACKET CUSTOMIZER, default if not set |
| 261 | */ |
| 262 | public String radiusPktCustomizer() { |
| 263 | return getStringProperty(PACKET_CUSTOMIZER, DEFAULT_PACKET_CUSTOMIZER); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns the List of ConnectPoints to reach the Radius Server. |
| 268 | * |
| 269 | * @return List of ConnectPoints |
| 270 | */ |
| 271 | public Set<ConnectPoint> radiusServerConnectPoints() { |
| 272 | if (object == null) { |
| 273 | return new HashSet<ConnectPoint>(); |
| 274 | } |
| 275 | |
| 276 | if (!object.has(RADIUS_SERVER_CONNECTPOINTS)) { |
| 277 | return ImmutableSet.of(); |
| 278 | } |
| 279 | |
| 280 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 281 | ArrayNode arrayNode = (ArrayNode) object.path(RADIUS_SERVER_CONNECTPOINTS); |
| 282 | for (JsonNode jsonNode : arrayNode) { |
| 283 | String portName = jsonNode.asText(null); |
| 284 | if (portName == null) { |
| 285 | return null; |
| 286 | } |
| 287 | try { |
| 288 | builder.add(ConnectPoint.deviceConnectPoint(portName)); |
| 289 | } catch (IllegalArgumentException e) { |
| 290 | return null; |
| 291 | } |
| 292 | } |
| 293 | return builder.build(); |
| 294 | } |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 295 | |
| 296 | @Override |
| 297 | public String toString() { |
| 298 | return ToStringBuilder.reflectionToString(this); |
| 299 | } |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 300 | } |