Ray Milkey
Committed by Gerrit Code Review

Modify AAA app to use the network configuation service

Change-Id: Ie7e12dfd9a3b80c55db2b55bdfdf431db8157d24
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 }
......