Committed by
Gerrit Code Review
Modify AAA app to use the network configuation service
Change-Id: Ie7e12dfd9a3b80c55db2b55bdfdf431db8157d24
Showing
3 changed files
with
368 additions
and
118 deletions
| ... | @@ -16,17 +16,13 @@ | ... | @@ -16,17 +16,13 @@ |
| 16 | package org.onosproject.aaa; | 16 | package org.onosproject.aaa; |
| 17 | 17 | ||
| 18 | import java.net.InetAddress; | 18 | import java.net.InetAddress; |
| 19 | -import java.net.UnknownHostException; | ||
| 20 | import java.nio.ByteBuffer; | 19 | import java.nio.ByteBuffer; |
| 21 | -import java.util.Dictionary; | ||
| 22 | import java.util.Optional; | 20 | import java.util.Optional; |
| 23 | import java.util.Set; | 21 | import java.util.Set; |
| 24 | 22 | ||
| 25 | import org.apache.felix.scr.annotations.Activate; | 23 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; | 24 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; | 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | -import org.apache.felix.scr.annotations.Modified; | ||
| 29 | -import org.apache.felix.scr.annotations.Property; | ||
| 30 | import org.apache.felix.scr.annotations.Reference; | 26 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.onlab.packet.DeserializationException; | 28 | import org.onlab.packet.DeserializationException; |
| ... | @@ -43,14 +39,16 @@ import org.onlab.packet.RADIUSAttribute; | ... | @@ -43,14 +39,16 @@ import org.onlab.packet.RADIUSAttribute; |
| 43 | import org.onlab.packet.TpPort; | 39 | import org.onlab.packet.TpPort; |
| 44 | import org.onlab.packet.UDP; | 40 | import org.onlab.packet.UDP; |
| 45 | import org.onlab.packet.VlanId; | 41 | import org.onlab.packet.VlanId; |
| 46 | -import org.onlab.util.Tools; | ||
| 47 | -import org.onosproject.cfg.ComponentConfigService; | ||
| 48 | import org.onosproject.core.ApplicationId; | 42 | import org.onosproject.core.ApplicationId; |
| 49 | import org.onosproject.core.CoreService; | 43 | import org.onosproject.core.CoreService; |
| 50 | import org.onosproject.net.ConnectPoint; | 44 | import org.onosproject.net.ConnectPoint; |
| 51 | import org.onosproject.net.DeviceId; | 45 | import org.onosproject.net.DeviceId; |
| 52 | import org.onosproject.net.Host; | 46 | import org.onosproject.net.Host; |
| 53 | import org.onosproject.net.PortNumber; | 47 | import org.onosproject.net.PortNumber; |
| 48 | +import org.onosproject.net.config.ConfigFactory; | ||
| 49 | +import org.onosproject.net.config.NetworkConfigEvent; | ||
| 50 | +import org.onosproject.net.config.NetworkConfigListener; | ||
| 51 | +import org.onosproject.net.config.NetworkConfigRegistry; | ||
| 54 | import org.onosproject.net.flow.DefaultTrafficSelector; | 52 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 55 | import org.onosproject.net.flow.DefaultTrafficTreatment; | 53 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 56 | import org.onosproject.net.flow.TrafficSelector; | 54 | import org.onosproject.net.flow.TrafficSelector; |
| ... | @@ -63,43 +61,17 @@ import org.onosproject.net.packet.PacketContext; | ... | @@ -63,43 +61,17 @@ import org.onosproject.net.packet.PacketContext; |
| 63 | import org.onosproject.net.packet.PacketProcessor; | 61 | import org.onosproject.net.packet.PacketProcessor; |
| 64 | import org.onosproject.net.packet.PacketService; | 62 | import org.onosproject.net.packet.PacketService; |
| 65 | import org.onosproject.xosintegration.VoltTenantService; | 63 | import org.onosproject.xosintegration.VoltTenantService; |
| 66 | -import org.osgi.service.component.ComponentContext; | ||
| 67 | import org.slf4j.Logger; | 64 | import org.slf4j.Logger; |
| 68 | 65 | ||
| 69 | -import com.google.common.base.Strings; | 66 | +import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 70 | - | ||
| 71 | import static org.onosproject.net.packet.PacketPriority.CONTROL; | 67 | import static org.onosproject.net.packet.PacketPriority.CONTROL; |
| 72 | import static org.slf4j.LoggerFactory.getLogger; | 68 | import static org.slf4j.LoggerFactory.getLogger; |
| 73 | 69 | ||
| 74 | - | ||
| 75 | /** | 70 | /** |
| 76 | * AAA application for ONOS. | 71 | * AAA application for ONOS. |
| 77 | */ | 72 | */ |
| 78 | @Component(immediate = true) | 73 | @Component(immediate = true) |
| 79 | public class AAA { | 74 | public class AAA { |
| 80 | - // RADIUS server IP address | ||
| 81 | - private static final String DEFAULT_RADIUS_IP = "192.168.1.10"; | ||
| 82 | - | ||
| 83 | - // NAS IP address | ||
| 84 | - private static final String DEFAULT_NAS_IP = "192.168.1.11"; | ||
| 85 | - | ||
| 86 | - // RADIUS uplink port | ||
| 87 | - private static final int DEFAULT_RADIUS_UPLINK = 2; | ||
| 88 | - | ||
| 89 | - // RADIUS server shared secret | ||
| 90 | - private static final String DEFAULT_RADIUS_SECRET = "ONOSecret"; | ||
| 91 | - | ||
| 92 | - // RADIUS MAC address | ||
| 93 | - private static final String RADIUS_MAC_ADDRESS = "00:00:00:00:01:10"; | ||
| 94 | - | ||
| 95 | - // NAS MAC address | ||
| 96 | - private static final String NAS_MAC_ADDRESS = "00:00:00:00:10:01"; | ||
| 97 | - | ||
| 98 | - // Radius Switch Id | ||
| 99 | - private static final String DEFAULT_RADIUS_SWITCH = "of:90e2ba82f97791e9"; | ||
| 100 | - | ||
| 101 | - // Radius Port Number | ||
| 102 | - private static final String DEFAULT_RADIUS_PORT = "129"; | ||
| 103 | 75 | ||
| 104 | // for verbose output | 76 | // for verbose output |
| 105 | private final Logger log = getLogger(getClass()); | 77 | private final Logger log = getLogger(getClass()); |
| ... | @@ -121,46 +93,44 @@ public class AAA { | ... | @@ -121,46 +93,44 @@ public class AAA { |
| 121 | protected VoltTenantService voltTenantService; | 93 | protected VoltTenantService voltTenantService; |
| 122 | 94 | ||
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | - protected ComponentConfigService cfgService; | 96 | + protected NetworkConfigRegistry netCfgService; |
| 125 | 97 | ||
| 126 | - // Parsed RADIUS server IP address | 98 | + // Parsed RADIUS server addresses |
| 127 | - protected InetAddress parsedRadiusIpAddress; | 99 | + protected InetAddress radiusIpAddress; |
| 128 | - // Parsed NAS IP address | 100 | + protected String radiusMacAddress; |
| 129 | - protected InetAddress parsedNasIpAddress; | ||
| 130 | 101 | ||
| 131 | - // our application-specific event handler | 102 | + // NAS IP address |
| 132 | - private ReactivePacketProcessor processor = new ReactivePacketProcessor(); | 103 | + protected InetAddress nasIpAddress; |
| 133 | - | 104 | + protected String nasMacAddress; |
| 134 | - // our unique identifier | ||
| 135 | - private ApplicationId appId; | ||
| 136 | 105 | ||
| 137 | - @Property(name = "radiusIpAddress", value = DEFAULT_RADIUS_IP, | 106 | + // RADIUS server secret |
| 138 | - label = "RADIUS IP Address") | 107 | + protected String radiusSecret; |
| 139 | - protected String radiusIpAddress = DEFAULT_RADIUS_IP; | ||
| 140 | 108 | ||
| 141 | - @Property(name = "nasIpAddress", value = DEFAULT_NAS_IP, | 109 | + // ID of RADIUS switch |
| 142 | - label = "NAS IP Address") | 110 | + protected String radiusSwitch; |
| 143 | - protected String nasIpAddress = DEFAULT_NAS_IP; | ||
| 144 | 111 | ||
| 145 | - @Property(name = "radiusMacAddress", value = RADIUS_MAC_ADDRESS, | 112 | + // RADIUS port number |
| 146 | - label = "RADIUS MAC Address") | 113 | + protected long radiusPort; |
| 147 | - protected String radiusMacAddress = RADIUS_MAC_ADDRESS; | ||
| 148 | 114 | ||
| 149 | - @Property(name = "nasMacAddress", value = NAS_MAC_ADDRESS, | 115 | + // our application-specific event handler |
| 150 | - label = "NAS MAC Address") | 116 | + private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
| 151 | - protected String nasMacAddress = NAS_MAC_ADDRESS; | ||
| 152 | 117 | ||
| 153 | - @Property(name = "radiusSecret", value = DEFAULT_RADIUS_SECRET, | 118 | + // our unique identifier |
| 154 | - label = "RADIUS shared secret") | 119 | + private ApplicationId appId; |
| 155 | - protected String radiusSecret = DEFAULT_RADIUS_SECRET; | ||
| 156 | 120 | ||
| 157 | - @Property(name = "radiusSwitchId", value = DEFAULT_RADIUS_SWITCH, | 121 | + // Configuration properties factory |
| 158 | - label = "Radius switch") | 122 | + private final ConfigFactory factory = |
| 159 | - private String radiusSwitch = DEFAULT_RADIUS_SWITCH; | 123 | + new ConfigFactory<ApplicationId, AAAConfig>(APP_SUBJECT_FACTORY, |
| 124 | + AAAConfig.class, | ||
| 125 | + "AAA") { | ||
| 126 | + @Override | ||
| 127 | + public AAAConfig createConfig() { | ||
| 128 | + return new AAAConfig(); | ||
| 129 | + } | ||
| 130 | + }; | ||
| 160 | 131 | ||
| 161 | - @Property(name = "radiusPortNumber", value = DEFAULT_RADIUS_PORT, | 132 | + // Listener for config changes |
| 162 | - label = "Radius port") | 133 | + private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 163 | - private String radiusPort = DEFAULT_RADIUS_PORT; | ||
| 164 | 134 | ||
| 165 | /** | 135 | /** |
| 166 | * Builds an EAPOL packet based on the given parameters. | 136 | * Builds an EAPOL packet based on the given parameters. |
| ... | @@ -195,47 +165,16 @@ public class AAA { | ... | @@ -195,47 +165,16 @@ public class AAA { |
| 195 | return eth; | 165 | return eth; |
| 196 | } | 166 | } |
| 197 | 167 | ||
| 198 | - @Modified | ||
| 199 | - public void modified(ComponentContext context) { | ||
| 200 | - Dictionary<?, ?> properties = context.getProperties(); | ||
| 201 | - | ||
| 202 | - String s = Tools.get(properties, "radiusIpAddress"); | ||
| 203 | - try { | ||
| 204 | - parsedRadiusIpAddress = InetAddress.getByName(s); | ||
| 205 | - radiusIpAddress = Strings.isNullOrEmpty(s) ? DEFAULT_RADIUS_IP : s; | ||
| 206 | - } catch (UnknownHostException e) { | ||
| 207 | - log.error("Invalid RADIUS IP address specification: {}", s, e); | ||
| 208 | - } | ||
| 209 | - try { | ||
| 210 | - s = Tools.get(properties, "nasIpAddress"); | ||
| 211 | - parsedNasIpAddress = InetAddress.getByName(s); | ||
| 212 | - nasIpAddress = Strings.isNullOrEmpty(s) ? DEFAULT_NAS_IP : s; | ||
| 213 | - } catch (UnknownHostException e) { | ||
| 214 | - log.error("Invalid NAS IP address specification: {}", s, e); | ||
| 215 | - } | ||
| 216 | - | ||
| 217 | - s = Tools.get(properties, "radiusMacAddress"); | ||
| 218 | - radiusMacAddress = Strings.isNullOrEmpty(s) ? RADIUS_MAC_ADDRESS : s; | ||
| 219 | - | ||
| 220 | - s = Tools.get(properties, "nasMacAddress"); | ||
| 221 | - nasMacAddress = Strings.isNullOrEmpty(s) ? NAS_MAC_ADDRESS : s; | ||
| 222 | - | ||
| 223 | - s = Tools.get(properties, "radiusSecret"); | ||
| 224 | - radiusSecret = Strings.isNullOrEmpty(s) ? DEFAULT_RADIUS_SECRET : s; | ||
| 225 | - | ||
| 226 | - s = Tools.get(properties, "radiusSwitchId"); | ||
| 227 | - radiusSwitch = Strings.isNullOrEmpty(s) ? DEFAULT_RADIUS_SWITCH : s; | ||
| 228 | - | ||
| 229 | - s = Tools.get(properties, "radiusPortNumber"); | ||
| 230 | - radiusPort = Strings.isNullOrEmpty(s) ? DEFAULT_RADIUS_PORT : s; | ||
| 231 | - } | ||
| 232 | - | ||
| 233 | @Activate | 168 | @Activate |
| 234 | - public void activate(ComponentContext context) { | 169 | + public void activate() { |
| 235 | - cfgService.registerProperties(getClass()); | 170 | + netCfgService.addListener(cfgListener); |
| 236 | - modified(context); | 171 | + netCfgService.registerConfigFactory(factory); |
| 172 | + | ||
| 237 | // "org.onosproject.aaa" is the FQDN of our app | 173 | // "org.onosproject.aaa" is the FQDN of our app |
| 238 | appId = coreService.registerApplication("org.onosproject.aaa"); | 174 | appId = coreService.registerApplication("org.onosproject.aaa"); |
| 175 | + | ||
| 176 | + cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AAAConfig.class)); | ||
| 177 | + | ||
| 239 | // register our event handler | 178 | // register our event handler |
| 240 | packetService.addProcessor(processor, PacketProcessor.director(2)); | 179 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 241 | requestIntercepts(); | 180 | requestIntercepts(); |
| ... | @@ -247,8 +186,6 @@ public class AAA { | ... | @@ -247,8 +186,6 @@ public class AAA { |
| 247 | 186 | ||
| 248 | @Deactivate | 187 | @Deactivate |
| 249 | public void deactivate() { | 188 | public void deactivate() { |
| 250 | - cfgService.unregisterProperties(getClass(), false); | ||
| 251 | - | ||
| 252 | appId = coreService.registerApplication("org.onosproject.aaa"); | 189 | appId = coreService.registerApplication("org.onosproject.aaa"); |
| 253 | withdrawIntercepts(); | 190 | withdrawIntercepts(); |
| 254 | // de-register and null our handler | 191 | // de-register and null our handler |
| ... | @@ -316,7 +253,7 @@ public class AAA { | ... | @@ -316,7 +253,7 @@ public class AAA { |
| 316 | case IPV4: | 253 | case IPV4: |
| 317 | IPv4 ipv4Packet = (IPv4) ethPkt.getPayload(); | 254 | IPv4 ipv4Packet = (IPv4) ethPkt.getPayload(); |
| 318 | Ip4Address srcIp = Ip4Address.valueOf(ipv4Packet.getSourceAddress()); | 255 | Ip4Address srcIp = Ip4Address.valueOf(ipv4Packet.getSourceAddress()); |
| 319 | - Ip4Address radiusIp4Address = Ip4Address.valueOf(parsedRadiusIpAddress); | 256 | + Ip4Address radiusIp4Address = Ip4Address.valueOf(radiusIpAddress); |
| 320 | if (srcIp.equals(radiusIp4Address) && ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { | 257 | if (srcIp.equals(radiusIp4Address) && ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { |
| 321 | // TODO: check for port as well when it's configurable | 258 | // TODO: check for port as well when it's configurable |
| 322 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); | 259 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| ... | @@ -341,7 +278,7 @@ public class AAA { | ... | @@ -341,7 +278,7 @@ public class AAA { |
| 341 | * Creates and initializes common fields of a RADIUS packet. | 278 | * Creates and initializes common fields of a RADIUS packet. |
| 342 | * | 279 | * |
| 343 | * @param identifier RADIUS identifier | 280 | * @param identifier RADIUS identifier |
| 344 | - * @param eapPacket EAP packet | 281 | + * @param eapPacket EAP packet |
| 345 | * @return RADIUS packet | 282 | * @return RADIUS packet |
| 346 | */ | 283 | */ |
| 347 | private RADIUS getRadiusPayload(byte identifier, EAP eapPacket) { | 284 | private RADIUS getRadiusPayload(byte identifier, EAP eapPacket) { |
| ... | @@ -353,7 +290,7 @@ public class AAA { | ... | @@ -353,7 +290,7 @@ public class AAA { |
| 353 | eapPacket.getData()); | 290 | eapPacket.getData()); |
| 354 | 291 | ||
| 355 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, | 292 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
| 356 | - AAA.this.parsedNasIpAddress.getAddress()); | 293 | + AAA.this.nasIpAddress.getAddress()); |
| 357 | 294 | ||
| 358 | radiusPayload.encapsulateMessage(eapPacket); | 295 | radiusPayload.encapsulateMessage(eapPacket); |
| 359 | radiusPayload.addMessageAuthenticator(AAA.this.radiusSecret); | 296 | radiusPayload.addMessageAuthenticator(AAA.this.radiusSecret); |
| ... | @@ -519,8 +456,8 @@ public class AAA { | ... | @@ -519,8 +456,8 @@ public class AAA { |
| 519 | udp.setSourcePort((short) 1812); // TODO: make this configurable | 456 | udp.setSourcePort((short) 1812); // TODO: make this configurable |
| 520 | udp.setPayload(radiusMessage); | 457 | udp.setPayload(radiusMessage); |
| 521 | udp.setParent(ip4Packet); | 458 | udp.setParent(ip4Packet); |
| 522 | - ip4Packet.setSourceAddress(AAA.this.nasIpAddress); | 459 | + ip4Packet.setSourceAddress(AAA.this.nasIpAddress.getHostAddress()); |
| 523 | - ip4Packet.setDestinationAddress(AAA.this.radiusIpAddress); | 460 | + ip4Packet.setDestinationAddress(AAA.this.radiusIpAddress.getHostAddress()); |
| 524 | ip4Packet.setProtocol(IPv4.PROTOCOL_UDP); | 461 | ip4Packet.setProtocol(IPv4.PROTOCOL_UDP); |
| 525 | ip4Packet.setPayload(udp); | 462 | ip4Packet.setPayload(udp); |
| 526 | ip4Packet.setParent(ethPkt); | 463 | ip4Packet.setParent(ethPkt); |
| ... | @@ -530,7 +467,7 @@ public class AAA { | ... | @@ -530,7 +467,7 @@ public class AAA { |
| 530 | ethPkt.setPayload(ip4Packet); | 467 | ethPkt.setPayload(ip4Packet); |
| 531 | 468 | ||
| 532 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() | 469 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 533 | - .setOutput(PortNumber.portNumber(Integer.parseInt(radiusPort))).build(); | 470 | + .setOutput(PortNumber.portNumber(radiusPort)).build(); |
| 534 | OutboundPacket packet = new DefaultOutboundPacket(DeviceId.deviceId(radiusSwitch), | 471 | OutboundPacket packet = new DefaultOutboundPacket(DeviceId.deviceId(radiusSwitch), |
| 535 | treatment, ByteBuffer.wrap(ethPkt.serialize())); | 472 | treatment, ByteBuffer.wrap(ethPkt.serialize())); |
| 536 | packetService.emit(packet); | 473 | packetService.emit(packet); |
| ... | @@ -552,4 +489,65 @@ public class AAA { | ... | @@ -552,4 +489,65 @@ public class AAA { |
| 552 | 489 | ||
| 553 | } | 490 | } |
| 554 | 491 | ||
| 492 | + private class InternalConfigListener implements NetworkConfigListener { | ||
| 493 | + | ||
| 494 | + /** | ||
| 495 | + * Reconfigures the DHCP Server according to the configuration parameters passed. | ||
| 496 | + * | ||
| 497 | + * @param cfg configuration object | ||
| 498 | + */ | ||
| 499 | + private void reconfigureNetwork(AAAConfig cfg) { | ||
| 500 | + AAAConfig newCfg; | ||
| 501 | + if (cfg == null) { | ||
| 502 | + newCfg = new AAAConfig(); | ||
| 503 | + } else { | ||
| 504 | + newCfg = cfg; | ||
| 505 | + } | ||
| 506 | + if (newCfg.nasIp() != null) { | ||
| 507 | + nasIpAddress = newCfg.nasIp(); | ||
| 508 | + } | ||
| 509 | + if (newCfg.radiusIp() != null) { | ||
| 510 | + radiusIpAddress = newCfg.radiusIp(); | ||
| 511 | + } | ||
| 512 | + if (newCfg.radiusMac() != null) { | ||
| 513 | + radiusMacAddress = newCfg.radiusMac(); | ||
| 514 | + } | ||
| 515 | + if (newCfg.nasMac() != null) { | ||
| 516 | + nasMacAddress = newCfg.nasMac(); | ||
| 517 | + } | ||
| 518 | + if (newCfg.radiusSecret() != null) { | ||
| 519 | + radiusSecret = newCfg.radiusSecret(); | ||
| 520 | + } | ||
| 521 | + if (newCfg.radiusSwitch() != null) { | ||
| 522 | + radiusSwitch = newCfg.radiusSwitch(); | ||
| 523 | + } | ||
| 524 | + if (newCfg.radiusPort() != -1) { | ||
| 525 | + radiusPort = newCfg.radiusPort(); | ||
| 526 | + } | ||
| 527 | + | ||
| 528 | + log.info("AAA app configuration:"); | ||
| 529 | + log.info("NAS IP is {}", nasIpAddress); | ||
| 530 | + log.info("RADIUS IP is {}", radiusIpAddress); | ||
| 531 | + log.info("NAS MAC is {}", nasMacAddress); | ||
| 532 | + log.info("RADIUS MAC is {}", radiusMacAddress); | ||
| 533 | + log.info("RADIUS secret is {}", radiusSecret); | ||
| 534 | + log.info("RADIUS switch is {}", radiusSwitch); | ||
| 535 | + log.info("RADIUS port is {}", radiusPort); | ||
| 536 | + } | ||
| 537 | + | ||
| 538 | + @Override | ||
| 539 | + public void event(NetworkConfigEvent event) { | ||
| 540 | + | ||
| 541 | + if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || | ||
| 542 | + event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && | ||
| 543 | + event.configClass().equals(AAAConfig.class)) { | ||
| 544 | + | ||
| 545 | + AAAConfig cfg = netCfgService.getConfig(appId, AAAConfig.class); | ||
| 546 | + reconfigureNetwork(cfg); | ||
| 547 | + log.info("Reconfigured"); | ||
| 548 | + } | ||
| 549 | + } | ||
| 550 | + } | ||
| 551 | + | ||
| 552 | + | ||
| 555 | } | 553 | } | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.aaa; | ||
| 17 | + | ||
| 18 | +import java.net.InetAddress; | ||
| 19 | +import java.net.UnknownHostException; | ||
| 20 | + | ||
| 21 | +import org.onosproject.core.ApplicationId; | ||
| 22 | +import org.onosproject.net.config.Config; | ||
| 23 | +import org.onosproject.net.config.basics.BasicElementConfig; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Network config for the AAA app. | ||
| 27 | + */ | ||
| 28 | +public class AAAConfig extends Config<ApplicationId> { | ||
| 29 | + | ||
| 30 | + private static final String RADIUS_IP = "radiusIp"; | ||
| 31 | + private static final String RADIUS_MAC = "radiusMac"; | ||
| 32 | + private static final String NAS_IP = "nasIp"; | ||
| 33 | + private static final String NAS_MAC = "nasMac"; | ||
| 34 | + private static final String RADIUS_SECRET = "radiusSecret"; | ||
| 35 | + private static final String RADIUS_SWITCH = "radiusSwitch"; | ||
| 36 | + private static final String RADIUS_PORT = "radiusPort"; | ||
| 37 | + | ||
| 38 | + // RADIUS server IP address | ||
| 39 | + protected static final String DEFAULT_RADIUS_IP = "192.168.1.10"; | ||
| 40 | + | ||
| 41 | + // RADIUS MAC address | ||
| 42 | + protected static final String DEFAULT_RADIUS_MAC = "00:00:00:00:01:10"; | ||
| 43 | + | ||
| 44 | + // NAS IP address | ||
| 45 | + protected static final String DEFAULT_NAS_IP = "192.168.1.11"; | ||
| 46 | + | ||
| 47 | + // NAS MAC address | ||
| 48 | + protected static final String DEFAULT_NAS_MAC = "00:00:00:00:10:01"; | ||
| 49 | + | ||
| 50 | + // RADIUS uplink port | ||
| 51 | + protected static final int DEFAULT_RADIUS_UPLINK = 2; | ||
| 52 | + | ||
| 53 | + // RADIUS server shared secret | ||
| 54 | + protected static final String DEFAULT_RADIUS_SECRET = "ONOSecret"; | ||
| 55 | + | ||
| 56 | + // Radius Switch Id | ||
| 57 | + protected static final String DEFAULT_RADIUS_SWITCH = "of:90e2ba82f97791e9"; | ||
| 58 | + | ||
| 59 | + // Radius Port Number | ||
| 60 | + protected static final String DEFAULT_RADIUS_PORT = "129"; | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * Returns the NAS ip. | ||
| 64 | + * | ||
| 65 | + * @return ip address or null if not set | ||
| 66 | + */ | ||
| 67 | + public InetAddress nasIp() { | ||
| 68 | + try { | ||
| 69 | + if (object == null) { | ||
| 70 | + return InetAddress.getByName(DEFAULT_NAS_IP); | ||
| 71 | + } | ||
| 72 | + return InetAddress.getByName(get(NAS_IP, DEFAULT_NAS_IP)); | ||
| 73 | + } catch (UnknownHostException e) { | ||
| 74 | + return null; | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * Sets the NAS ip. | ||
| 80 | + * | ||
| 81 | + * @param ip new ip address; null to clear | ||
| 82 | + * @return self | ||
| 83 | + */ | ||
| 84 | + public BasicElementConfig nasIp(String ip) { | ||
| 85 | + return (BasicElementConfig) setOrClear(NAS_IP, ip); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + /** | ||
| 89 | + * Returns the RADIUS server ip. | ||
| 90 | + * | ||
| 91 | + * @return ip address or null if not set | ||
| 92 | + */ | ||
| 93 | + public InetAddress radiusIp() { | ||
| 94 | + try { | ||
| 95 | + if (object == null) { | ||
| 96 | + return InetAddress.getByName(DEFAULT_RADIUS_IP); | ||
| 97 | + } | ||
| 98 | + return InetAddress.getByName(get(RADIUS_IP, DEFAULT_RADIUS_IP)); | ||
| 99 | + } catch (UnknownHostException e) { | ||
| 100 | + return null; | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + /** | ||
| 105 | + * Sets the RADIUS server ip. | ||
| 106 | + * | ||
| 107 | + * @param ip new ip address; null to clear | ||
| 108 | + * @return self | ||
| 109 | + */ | ||
| 110 | + public BasicElementConfig radiusIp(String ip) { | ||
| 111 | + return (BasicElementConfig) setOrClear(RADIUS_IP, ip); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + /** | ||
| 115 | + * Returns the RADIUS MAC address. | ||
| 116 | + * | ||
| 117 | + * @return mac address or null if not set | ||
| 118 | + */ | ||
| 119 | + public String radiusMac() { | ||
| 120 | + if (object == null) { | ||
| 121 | + return DEFAULT_RADIUS_MAC; | ||
| 122 | + } | ||
| 123 | + return get(RADIUS_MAC, DEFAULT_RADIUS_MAC); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + /** | ||
| 127 | + * Sets the RADIUS MAC address. | ||
| 128 | + * | ||
| 129 | + * @param mac new MAC address; null to clear | ||
| 130 | + * @return self | ||
| 131 | + */ | ||
| 132 | + public BasicElementConfig radiusMac(String mac) { | ||
| 133 | + return (BasicElementConfig) setOrClear(RADIUS_MAC, mac); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + /** | ||
| 137 | + * Returns the RADIUS MAC address. | ||
| 138 | + * | ||
| 139 | + * @return mac address or null if not set | ||
| 140 | + */ | ||
| 141 | + public String nasMac() { | ||
| 142 | + if (object == null) { | ||
| 143 | + return DEFAULT_NAS_MAC; | ||
| 144 | + } | ||
| 145 | + return get(NAS_MAC, DEFAULT_NAS_MAC); | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + /** | ||
| 149 | + * Sets the RADIUS MAC address. | ||
| 150 | + * | ||
| 151 | + * @param mac new MAC address; null to clear | ||
| 152 | + * @return self | ||
| 153 | + */ | ||
| 154 | + public BasicElementConfig nasMac(String mac) { | ||
| 155 | + return (BasicElementConfig) setOrClear(NAS_MAC, mac); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + /** | ||
| 159 | + * Returns the RADIUS secret. | ||
| 160 | + * | ||
| 161 | + * @return radius secret or null if not set | ||
| 162 | + */ | ||
| 163 | + public String radiusSecret() { | ||
| 164 | + if (object == null) { | ||
| 165 | + return DEFAULT_RADIUS_SECRET; | ||
| 166 | + } | ||
| 167 | + return get(RADIUS_SECRET, DEFAULT_RADIUS_SECRET); | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + /** | ||
| 171 | + * Sets the RADIUS secret. | ||
| 172 | + * | ||
| 173 | + * @param secret new MAC address; null to clear | ||
| 174 | + * @return self | ||
| 175 | + */ | ||
| 176 | + public BasicElementConfig radiusSecret(String secret) { | ||
| 177 | + return (BasicElementConfig) setOrClear(RADIUS_SECRET, secret); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + /** | ||
| 181 | + * Returns the ID of the RADIUS switch. | ||
| 182 | + * | ||
| 183 | + * @return radius switch ID or null if not set | ||
| 184 | + */ | ||
| 185 | + public String radiusSwitch() { | ||
| 186 | + if (object == null) { | ||
| 187 | + return DEFAULT_RADIUS_SWITCH; | ||
| 188 | + } | ||
| 189 | + return get(RADIUS_SWITCH, DEFAULT_RADIUS_SWITCH); | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + /** | ||
| 193 | + * Sets the ID of the RADIUS switch. | ||
| 194 | + * | ||
| 195 | + * @param switchId new RADIUS switch ID; null to clear | ||
| 196 | + * @return self | ||
| 197 | + */ | ||
| 198 | + public BasicElementConfig radiusSwitch(String switchId) { | ||
| 199 | + return (BasicElementConfig) setOrClear(RADIUS_SWITCH, switchId); | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + /** | ||
| 203 | + * Returns the RADIUS port. | ||
| 204 | + * | ||
| 205 | + * @return radius port or null if not set | ||
| 206 | + */ | ||
| 207 | + public long radiusPort() { | ||
| 208 | + if (object == null) { | ||
| 209 | + return Integer.parseInt(DEFAULT_RADIUS_PORT); | ||
| 210 | + } | ||
| 211 | + return Integer.parseInt(get(RADIUS_PORT, "-1")); | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + /** | ||
| 215 | + * Sets the RADIUS port. | ||
| 216 | + * | ||
| 217 | + * @param port new RADIUS port; null to clear | ||
| 218 | + * @return self | ||
| 219 | + */ | ||
| 220 | + public BasicElementConfig radiusPort(long port) { | ||
| 221 | + return (BasicElementConfig) setOrClear(RADIUS_PORT, port); | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | +} |
| ... | @@ -23,7 +23,6 @@ import java.util.Set; | ... | @@ -23,7 +23,6 @@ import java.util.Set; |
| 23 | import org.junit.After; | 23 | import org.junit.After; |
| 24 | import org.junit.Before; | 24 | import org.junit.Before; |
| 25 | import org.junit.Test; | 25 | import org.junit.Test; |
| 26 | -import org.onlab.osgi.ComponentContextAdapter; | ||
| 27 | import org.onlab.packet.Data; | 26 | import org.onlab.packet.Data; |
| 28 | import org.onlab.packet.DeserializationException; | 27 | import org.onlab.packet.DeserializationException; |
| 29 | import org.onlab.packet.EAP; | 28 | import org.onlab.packet.EAP; |
| ... | @@ -37,12 +36,13 @@ import org.onlab.packet.RADIUS; | ... | @@ -37,12 +36,13 @@ import org.onlab.packet.RADIUS; |
| 37 | import org.onlab.packet.RADIUSAttribute; | 36 | import org.onlab.packet.RADIUSAttribute; |
| 38 | import org.onlab.packet.UDP; | 37 | import org.onlab.packet.UDP; |
| 39 | import org.onlab.packet.VlanId; | 38 | import org.onlab.packet.VlanId; |
| 40 | -import org.onosproject.cfg.ComponentConfigAdapter; | ||
| 41 | import org.onosproject.core.CoreServiceAdapter; | 39 | import org.onosproject.core.CoreServiceAdapter; |
| 42 | import org.onosproject.net.Annotations; | 40 | import org.onosproject.net.Annotations; |
| 43 | import org.onosproject.net.Host; | 41 | import org.onosproject.net.Host; |
| 44 | import org.onosproject.net.HostId; | 42 | import org.onosproject.net.HostId; |
| 45 | import org.onosproject.net.HostLocation; | 43 | import org.onosproject.net.HostLocation; |
| 44 | +import org.onosproject.net.config.Config; | ||
| 45 | +import org.onosproject.net.config.NetworkConfigRegistryAdapter; | ||
| 46 | import org.onosproject.net.host.HostServiceAdapter; | 46 | import org.onosproject.net.host.HostServiceAdapter; |
| 47 | import org.onosproject.net.packet.DefaultInboundPacket; | 47 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 48 | import org.onosproject.net.packet.DefaultPacketContext; | 48 | import org.onosproject.net.packet.DefaultPacketContext; |
| ... | @@ -173,6 +173,18 @@ public class AAATest { | ... | @@ -173,6 +173,18 @@ public class AAATest { |
| 173 | } | 173 | } |
| 174 | 174 | ||
| 175 | /** | 175 | /** |
| 176 | + * Mocks the network config registry. | ||
| 177 | + */ | ||
| 178 | + @SuppressWarnings("unchecked") | ||
| 179 | + private static final class TestNetworkConfigRegistry | ||
| 180 | + extends NetworkConfigRegistryAdapter { | ||
| 181 | + @Override | ||
| 182 | + public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { | ||
| 183 | + return (C) new AAAConfig(); | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + /** | ||
| 176 | * Sends an Ethernet packet to the process method of the Packet Processor. | 188 | * Sends an Ethernet packet to the process method of the Packet Processor. |
| 177 | * | 189 | * |
| 178 | * @param reply Ethernet packet | 190 | * @param reply Ethernet packet |
| ... | @@ -261,7 +273,7 @@ public class AAATest { | ... | @@ -261,7 +273,7 @@ public class AAATest { |
| 261 | 273 | ||
| 262 | IPv4 ipv4 = new IPv4(); | 274 | IPv4 ipv4 = new IPv4(); |
| 263 | ipv4.setProtocol(IPv4.PROTOCOL_UDP); | 275 | ipv4.setProtocol(IPv4.PROTOCOL_UDP); |
| 264 | - ipv4.setSourceAddress("127.0.0.1"); | 276 | + ipv4.setSourceAddress(aaa.radiusIpAddress.getHostAddress()); |
| 265 | 277 | ||
| 266 | String challenge = "1234"; | 278 | String challenge = "1234"; |
| 267 | 279 | ||
| ... | @@ -294,11 +306,11 @@ public class AAATest { | ... | @@ -294,11 +306,11 @@ public class AAATest { |
| 294 | @Before | 306 | @Before |
| 295 | public void setUp() { | 307 | public void setUp() { |
| 296 | aaa = new AAA(); | 308 | aaa = new AAA(); |
| 297 | - aaa.cfgService = new ComponentConfigAdapter(); | 309 | + aaa.netCfgService = new TestNetworkConfigRegistry(); |
| 298 | aaa.coreService = new CoreServiceAdapter(); | 310 | aaa.coreService = new CoreServiceAdapter(); |
| 299 | aaa.packetService = new MockPacketService(); | 311 | aaa.packetService = new MockPacketService(); |
| 300 | aaa.hostService = new MockHostService(); | 312 | aaa.hostService = new MockHostService(); |
| 301 | - aaa.activate(new ComponentContextAdapter()); | 313 | + aaa.activate(); |
| 302 | } | 314 | } |
| 303 | 315 | ||
| 304 | /** | 316 | /** |
| ... | @@ -328,9 +340,9 @@ public class AAATest { | ... | @@ -328,9 +340,9 @@ public class AAATest { |
| 328 | IPv4 ipv4 = (IPv4) supplicantPacket.getPayload(); | 340 | IPv4 ipv4 = (IPv4) supplicantPacket.getPayload(); |
| 329 | assertThat(ipv4, notNullValue()); | 341 | assertThat(ipv4, notNullValue()); |
| 330 | assertThat(IpAddress.valueOf(ipv4.getSourceAddress()).toString(), | 342 | assertThat(IpAddress.valueOf(ipv4.getSourceAddress()).toString(), |
| 331 | - is(aaa.nasIpAddress)); | 343 | + is(aaa.nasIpAddress.getHostAddress())); |
| 332 | assertThat(IpAddress.valueOf(ipv4.getDestinationAddress()).toString(), | 344 | assertThat(IpAddress.valueOf(ipv4.getDestinationAddress()).toString(), |
| 333 | - is(aaa.radiusIpAddress)); | 345 | + is(aaa.radiusIpAddress.getHostAddress())); |
| 334 | 346 | ||
| 335 | assertThat(ipv4.getPayload(), instanceOf(UDP.class)); | 347 | assertThat(ipv4.getPayload(), instanceOf(UDP.class)); |
| 336 | UDP udp = (UDP) ipv4.getPayload(); | 348 | UDP udp = (UDP) ipv4.getPayload(); |
| ... | @@ -418,7 +430,7 @@ public class AAATest { | ... | @@ -418,7 +430,7 @@ public class AAATest { |
| 418 | IpAddress.valueOf(IpAddress.Version.INET, | 430 | IpAddress.valueOf(IpAddress.Version.INET, |
| 419 | radiusAccessRequest.getAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP) | 431 | radiusAccessRequest.getAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP) |
| 420 | .getValue()); | 432 | .getValue()); |
| 421 | - assertThat(nasIp.toString(), is("127.0.0.1")); | 433 | + assertThat(nasIp.toString(), is(aaa.nasIpAddress.getHostAddress())); |
| 422 | 434 | ||
| 423 | // State machine should have been created by now | 435 | // State machine should have been created by now |
| 424 | 436 | ||
| ... | @@ -482,4 +494,20 @@ public class AAATest { | ... | @@ -482,4 +494,20 @@ public class AAATest { |
| 482 | assertThat(stateMachine, notNullValue()); | 494 | assertThat(stateMachine, notNullValue()); |
| 483 | assertThat(stateMachine.state(), is(StateMachine.STATE_AUTHORIZED)); | 495 | assertThat(stateMachine.state(), is(StateMachine.STATE_AUTHORIZED)); |
| 484 | } | 496 | } |
| 497 | + | ||
| 498 | + | ||
| 499 | + private static final String RADIUS_SECRET = "radiusSecret"; | ||
| 500 | + private static final String RADIUS_SWITCH = "radiusSwitch"; | ||
| 501 | + private static final String RADIUS_PORT = "radiusPort"; | ||
| 502 | + | ||
| 503 | + /** | ||
| 504 | + * Tests the default configuration. | ||
| 505 | + */ | ||
| 506 | + @Test | ||
| 507 | + public void testConfig() { | ||
| 508 | + assertThat(aaa.nasIpAddress.getHostAddress(), is(AAAConfig.DEFAULT_NAS_IP)); | ||
| 509 | + assertThat(aaa.nasMacAddress, is(AAAConfig.DEFAULT_NAS_MAC)); | ||
| 510 | + assertThat(aaa.radiusIpAddress.getHostAddress(), is(AAAConfig.DEFAULT_RADIUS_IP)); | ||
| 511 | + assertThat(aaa.radiusMacAddress, is(AAAConfig.DEFAULT_RADIUS_MAC)); | ||
| 512 | + } | ||
| 485 | } | 513 | } | ... | ... |
-
Please register or login to post a comment