Committed by
Gerrit Code Review
AAA App refactoring
- optimized lookup of state machines - modified getter/setter method names to match ONOS project standards - made StateMachine local members private and add accesor methods - added unit tests for StateMachine lookups Change-Id: I5704ddc4d8b1b3c887be1262f2edd78965e4a8bf
Showing
3 changed files
with
278 additions
and
217 deletions
... | @@ -15,8 +15,13 @@ | ... | @@ -15,8 +15,13 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.aaa; | 16 | package org.onosproject.aaa; |
17 | 17 | ||
18 | -import com.google.common.base.Strings; | 18 | +import java.net.InetAddress; |
19 | -import com.google.common.collect.Maps; | 19 | +import java.net.UnknownHostException; |
20 | +import java.nio.ByteBuffer; | ||
21 | +import java.util.Dictionary; | ||
22 | +import java.util.Optional; | ||
23 | +import java.util.Set; | ||
24 | + | ||
20 | import org.apache.felix.scr.annotations.Activate; | 25 | import org.apache.felix.scr.annotations.Activate; |
21 | import org.apache.felix.scr.annotations.Component; | 26 | import org.apache.felix.scr.annotations.Component; |
22 | import org.apache.felix.scr.annotations.Deactivate; | 27 | import org.apache.felix.scr.annotations.Deactivate; |
... | @@ -61,15 +66,7 @@ import org.onosproject.xosintegration.VoltTenantService; | ... | @@ -61,15 +66,7 @@ import org.onosproject.xosintegration.VoltTenantService; |
61 | import org.osgi.service.component.ComponentContext; | 66 | import org.osgi.service.component.ComponentContext; |
62 | import org.slf4j.Logger; | 67 | import org.slf4j.Logger; |
63 | 68 | ||
64 | -import java.net.InetAddress; | 69 | +import com.google.common.base.Strings; |
65 | -import java.net.UnknownHostException; | ||
66 | -import java.nio.ByteBuffer; | ||
67 | -import java.util.Collections; | ||
68 | -import java.util.Dictionary; | ||
69 | -import java.util.Iterator; | ||
70 | -import java.util.Map; | ||
71 | -import java.util.Optional; | ||
72 | -import java.util.Set; | ||
73 | 70 | ||
74 | import static org.onosproject.net.packet.PacketPriority.CONTROL; | 71 | import static org.onosproject.net.packet.PacketPriority.CONTROL; |
75 | import static org.slf4j.LoggerFactory.getLogger; | 72 | import static org.slf4j.LoggerFactory.getLogger; |
... | @@ -80,35 +77,6 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -80,35 +77,6 @@ import static org.slf4j.LoggerFactory.getLogger; |
80 | */ | 77 | */ |
81 | @Component(immediate = true) | 78 | @Component(immediate = true) |
82 | public class AAA { | 79 | public class AAA { |
83 | - // a list of our dependencies : | ||
84 | - // to register with ONOS as an application - described next | ||
85 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
86 | - protected CoreService coreService; | ||
87 | - | ||
88 | - // to receive Packet-in events that we'll respond to | ||
89 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
90 | - protected PacketService packetService; | ||
91 | - | ||
92 | - // end host information | ||
93 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
94 | - protected HostService hostService; | ||
95 | - | ||
96 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
97 | - protected VoltTenantService voltTenantService; | ||
98 | - | ||
99 | - // for verbose output | ||
100 | - private final Logger log = getLogger(getClass()); | ||
101 | - | ||
102 | - // our application-specific event handler | ||
103 | - private ReactivePacketProcessor processor = new ReactivePacketProcessor(); | ||
104 | - | ||
105 | - // our unique identifier | ||
106 | - private ApplicationId appId; | ||
107 | - | ||
108 | - // Map of state machines. Each state machine is represented by an | ||
109 | - // unique identifier on the switch: dpid + port number | ||
110 | - Map stateMachineMap = null; | ||
111 | - | ||
112 | // RADIUS server IP address | 80 | // RADIUS server IP address |
113 | private static final String DEFAULT_RADIUS_IP = "192.168.1.10"; | 81 | private static final String DEFAULT_RADIUS_IP = "192.168.1.10"; |
114 | // NAS IP address | 82 | // NAS IP address |
... | @@ -125,43 +93,84 @@ public class AAA { | ... | @@ -125,43 +93,84 @@ public class AAA { |
125 | private static final String DEFAULT_RADIUS_SWITCH = "of:90e2ba82f97791e9"; | 93 | private static final String DEFAULT_RADIUS_SWITCH = "of:90e2ba82f97791e9"; |
126 | // Radius Port Number | 94 | // Radius Port Number |
127 | private static final String DEFAULT_RADIUS_PORT = "129"; | 95 | private static final String DEFAULT_RADIUS_PORT = "129"; |
128 | - | 96 | + // for verbose output |
97 | + private final Logger log = getLogger(getClass()); | ||
98 | + // a list of our dependencies : | ||
99 | + // to register with ONOS as an application - described next | ||
100 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
101 | + protected CoreService coreService; | ||
102 | + // to receive Packet-in events that we'll respond to | ||
103 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
104 | + protected PacketService packetService; | ||
105 | + // end host information | ||
106 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
107 | + protected HostService hostService; | ||
108 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
109 | + protected VoltTenantService voltTenantService; | ||
110 | + // Parsed RADIUS server IP address | ||
111 | + protected InetAddress parsedRadiusIpAddress; | ||
112 | + // Parsed NAS IP address | ||
113 | + protected InetAddress parsedNasIpAddress; | ||
114 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
115 | + protected ComponentConfigService cfgService; | ||
116 | + // our application-specific event handler | ||
117 | + private ReactivePacketProcessor processor = new ReactivePacketProcessor(); | ||
118 | + // our unique identifier | ||
119 | + private ApplicationId appId; | ||
129 | @Property(name = "radiusIpAddress", value = DEFAULT_RADIUS_IP, | 120 | @Property(name = "radiusIpAddress", value = DEFAULT_RADIUS_IP, |
130 | label = "RADIUS IP Address") | 121 | label = "RADIUS IP Address") |
131 | private String radiusIpAddress = DEFAULT_RADIUS_IP; | 122 | private String radiusIpAddress = DEFAULT_RADIUS_IP; |
132 | - | ||
133 | @Property(name = "nasIpAddress", value = DEFAULT_NAS_IP, | 123 | @Property(name = "nasIpAddress", value = DEFAULT_NAS_IP, |
134 | label = "NAS IP Address") | 124 | label = "NAS IP Address") |
135 | private String nasIpAddress = DEFAULT_NAS_IP; | 125 | private String nasIpAddress = DEFAULT_NAS_IP; |
136 | - | ||
137 | @Property(name = "radiusMacAddress", value = RADIUS_MAC_ADDRESS, | 126 | @Property(name = "radiusMacAddress", value = RADIUS_MAC_ADDRESS, |
138 | label = "RADIUS MAC Address") | 127 | label = "RADIUS MAC Address") |
139 | private String radiusMacAddress = RADIUS_MAC_ADDRESS; | 128 | private String radiusMacAddress = RADIUS_MAC_ADDRESS; |
140 | - | ||
141 | @Property(name = "nasMacAddress", value = NAS_MAC_ADDRESS, | 129 | @Property(name = "nasMacAddress", value = NAS_MAC_ADDRESS, |
142 | label = "NAS MAC Address") | 130 | label = "NAS MAC Address") |
143 | private String nasMacAddress = NAS_MAC_ADDRESS; | 131 | private String nasMacAddress = NAS_MAC_ADDRESS; |
144 | - | ||
145 | @Property(name = "radiusSecret", value = DEFAULT_RADIUS_SECRET, | 132 | @Property(name = "radiusSecret", value = DEFAULT_RADIUS_SECRET, |
146 | label = "RADIUS shared secret") | 133 | label = "RADIUS shared secret") |
147 | private String radiusSecret = DEFAULT_RADIUS_SECRET; | 134 | private String radiusSecret = DEFAULT_RADIUS_SECRET; |
148 | - | ||
149 | @Property(name = "radiusSwitchId", value = DEFAULT_RADIUS_SWITCH, | 135 | @Property(name = "radiusSwitchId", value = DEFAULT_RADIUS_SWITCH, |
150 | label = "Radius switch") | 136 | label = "Radius switch") |
151 | private String radiusSwitch = DEFAULT_RADIUS_SWITCH; | 137 | private String radiusSwitch = DEFAULT_RADIUS_SWITCH; |
152 | - | ||
153 | @Property(name = "radiusPortNumber", value = DEFAULT_RADIUS_PORT, | 138 | @Property(name = "radiusPortNumber", value = DEFAULT_RADIUS_PORT, |
154 | label = "Radius port") | 139 | label = "Radius port") |
155 | private String radiusPort = DEFAULT_RADIUS_PORT; | 140 | private String radiusPort = DEFAULT_RADIUS_PORT; |
156 | 141 | ||
157 | - // Parsed RADIUS server IP address | 142 | + /** |
158 | - protected InetAddress parsedRadiusIpAddress; | 143 | + * Builds an EAPOL packet based on the given parameters. |
144 | + * | ||
145 | + * @param dstMac destination MAC address | ||
146 | + * @param srcMac source MAC address | ||
147 | + * @param vlan vlan identifier | ||
148 | + * @param eapolType EAPOL type | ||
149 | + * @param eap EAP payload | ||
150 | + * @return Ethernet frame | ||
151 | + */ | ||
152 | + private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac, | ||
153 | + short vlan, byte eapolType, EAP eap) { | ||
159 | 154 | ||
160 | - // Parsed NAS IP address | 155 | + Ethernet eth = new Ethernet(); |
161 | - protected InetAddress parsedNasIpAddress; | 156 | + eth.setDestinationMACAddress(dstMac.toBytes()); |
157 | + eth.setSourceMACAddress(srcMac.toBytes()); | ||
158 | + eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); | ||
159 | + if (vlan != Ethernet.VLAN_UNTAGGED) { | ||
160 | + eth.setVlanID(vlan); | ||
161 | + } | ||
162 | + //eapol header | ||
163 | + EAPOL eapol = new EAPOL(); | ||
164 | + eapol.setEapolType(eapolType); | ||
165 | + eapol.setPacketLength(eap.getLength()); | ||
162 | 166 | ||
163 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 167 | + //eap part |
164 | - protected ComponentConfigService cfgService; | 168 | + eapol.setPayload(eap); |
169 | + | ||
170 | + eth.setPayload(eapol); | ||
171 | + eth.setPad(true); | ||
172 | + return eth; | ||
173 | + } | ||
165 | 174 | ||
166 | @Modified | 175 | @Modified |
167 | public void modified(ComponentContext context) { | 176 | public void modified(ComponentContext context) { |
... | @@ -207,11 +216,10 @@ public class AAA { | ... | @@ -207,11 +216,10 @@ public class AAA { |
207 | // register our event handler | 216 | // register our event handler |
208 | packetService.addProcessor(processor, PacketProcessor.director(2)); | 217 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
209 | requestIntercepts(); | 218 | requestIntercepts(); |
210 | - // Instantiate the map of the state machines | ||
211 | - stateMachineMap = Collections.synchronizedMap(Maps.newHashMap()); | ||
212 | 219 | ||
213 | - hostService.startMonitoringIp(IpAddress.valueOf(radiusIpAddress)); | 220 | + StateMachine.initializeMaps(); |
214 | 221 | ||
222 | + hostService.startMonitoringIp(IpAddress.valueOf(radiusIpAddress)); | ||
215 | } | 223 | } |
216 | 224 | ||
217 | @Deactivate | 225 | @Deactivate |
... | @@ -223,6 +231,7 @@ public class AAA { | ... | @@ -223,6 +231,7 @@ public class AAA { |
223 | // de-register and null our handler | 231 | // de-register and null our handler |
224 | packetService.removeProcessor(processor); | 232 | packetService.removeProcessor(processor); |
225 | processor = null; | 233 | processor = null; |
234 | + StateMachine.destroyMaps(); | ||
226 | } | 235 | } |
227 | 236 | ||
228 | /** | 237 | /** |
... | @@ -260,39 +269,6 @@ public class AAA { | ... | @@ -260,39 +269,6 @@ public class AAA { |
260 | packetService.cancelPackets(radSelector, CONTROL, appId); | 269 | packetService.cancelPackets(radSelector, CONTROL, appId); |
261 | } | 270 | } |
262 | 271 | ||
263 | - /** | ||
264 | - * Builds an EAPOL packet based on the given parameters. | ||
265 | - * | ||
266 | - * @param dstMac destination MAC address | ||
267 | - * @param srcMac source MAC address | ||
268 | - * @param vlan vlan identifier | ||
269 | - * @param eapolType EAPOL type | ||
270 | - * @param eap EAP payload | ||
271 | - * @return Ethernet frame | ||
272 | - */ | ||
273 | - private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac, | ||
274 | - short vlan, byte eapolType, EAP eap) { | ||
275 | - | ||
276 | - Ethernet eth = new Ethernet(); | ||
277 | - eth.setDestinationMACAddress(dstMac.toBytes()); | ||
278 | - eth.setSourceMACAddress(srcMac.toBytes()); | ||
279 | - eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); | ||
280 | - if (vlan != Ethernet.VLAN_UNTAGGED) { | ||
281 | - eth.setVlanID(vlan); | ||
282 | - } | ||
283 | - //eapol header | ||
284 | - EAPOL eapol = new EAPOL(); | ||
285 | - eapol.setEapolType(eapolType); | ||
286 | - eapol.setPacketLength(eap.getLength()); | ||
287 | - | ||
288 | - //eap part | ||
289 | - eapol.setPayload(eap); | ||
290 | - | ||
291 | - eth.setPayload(eapol); | ||
292 | - eth.setPad(true); | ||
293 | - return eth; | ||
294 | - } | ||
295 | - | ||
296 | // our handler defined as a private inner class | 272 | // our handler defined as a private inner class |
297 | 273 | ||
298 | /** | 274 | /** |
... | @@ -351,7 +327,11 @@ public class AAA { | ... | @@ -351,7 +327,11 @@ public class AAA { |
351 | DeviceId deviceId = inPacket.receivedFrom().deviceId(); | 327 | DeviceId deviceId = inPacket.receivedFrom().deviceId(); |
352 | PortNumber portNumber = inPacket.receivedFrom().port(); | 328 | PortNumber portNumber = inPacket.receivedFrom().port(); |
353 | String sessionId = deviceId.toString() + portNumber.toString(); | 329 | String sessionId = deviceId.toString() + portNumber.toString(); |
354 | - StateMachine stateMachine = getStateMachine(sessionId); | 330 | + StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId); |
331 | + if (stateMachine == null) { | ||
332 | + stateMachine = new StateMachine(sessionId, voltTenantService); | ||
333 | + } | ||
334 | + | ||
355 | 335 | ||
356 | EAPOL eapol = (EAPOL) ethPkt.getPayload(); | 336 | EAPOL eapol = (EAPOL) ethPkt.getPayload(); |
357 | 337 | ||
... | @@ -359,17 +339,17 @@ public class AAA { | ... | @@ -359,17 +339,17 @@ public class AAA { |
359 | case EAPOL.EAPOL_START: | 339 | case EAPOL.EAPOL_START: |
360 | try { | 340 | try { |
361 | stateMachine.start(); | 341 | stateMachine.start(); |
362 | - stateMachine.supplicantConnectpoint = inPacket.receivedFrom(); | 342 | + stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom()); |
363 | 343 | ||
364 | //send an EAP Request/Identify to the supplicant | 344 | //send an EAP Request/Identify to the supplicant |
365 | - EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.getIdentifier(), EAP.ATTR_IDENTITY, null); | 345 | + EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null); |
366 | Ethernet eth = buildEapolResponse(srcMAC, MacAddress.valueOf(1L), | 346 | Ethernet eth = buildEapolResponse(srcMAC, MacAddress.valueOf(1L), |
367 | ethPkt.getVlanID(), EAPOL.EAPOL_PACKET, | 347 | ethPkt.getVlanID(), EAPOL.EAPOL_PACKET, |
368 | eapPayload); | 348 | eapPayload); |
369 | - stateMachine.supplicantAddress = srcMAC; | 349 | + stateMachine.setSupplicantAddress(srcMAC); |
370 | - stateMachine.vlanId = ethPkt.getVlanID(); | 350 | + stateMachine.setVlanId(ethPkt.getVlanID()); |
371 | 351 | ||
372 | - this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint); | 352 | + this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
373 | } catch (StateMachineException e) { | 353 | } catch (StateMachineException e) { |
374 | e.printStackTrace(); | 354 | e.printStackTrace(); |
375 | } | 355 | } |
... | @@ -386,7 +366,7 @@ public class AAA { | ... | @@ -386,7 +366,7 @@ public class AAA { |
386 | //request id access to RADIUS | 366 | //request id access to RADIUS |
387 | RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, | 367 | RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
388 | eapPacket.getIdentifier()); | 368 | eapPacket.getIdentifier()); |
389 | - radiusPayload.setIdentifier(stateMachine.getIdentifier()); | 369 | + radiusPayload.setIdentifier(stateMachine.identifier()); |
390 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, | 370 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
391 | eapPacket.getData()); | 371 | eapPacket.getData()); |
392 | stateMachine.setUsername(eapPacket.getData()); | 372 | stateMachine.setUsername(eapPacket.getData()); |
... | @@ -409,20 +389,20 @@ public class AAA { | ... | @@ -409,20 +389,20 @@ public class AAA { |
409 | case EAP.ATTR_MD5: | 389 | case EAP.ATTR_MD5: |
410 | //verify if the EAP identifier corresponds to the challenge identifier from the client state | 390 | //verify if the EAP identifier corresponds to the challenge identifier from the client state |
411 | //machine. | 391 | //machine. |
412 | - if (eapPacket.getIdentifier() == stateMachine.getChallengeIdentifier()) { | 392 | + if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) { |
413 | //send the RADIUS challenge response | 393 | //send the RADIUS challenge response |
414 | RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, | 394 | RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
415 | eapPacket.getIdentifier()); | 395 | eapPacket.getIdentifier()); |
416 | - radiusPayload.setIdentifier(stateMachine.getChallengeIdentifier()); | 396 | + radiusPayload.setIdentifier(stateMachine.challengeIdentifier()); |
417 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, | 397 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
418 | - stateMachine.getUsername()); | 398 | + stateMachine.username()); |
419 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, | 399 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
420 | AAA.this.parsedNasIpAddress.getAddress()); | 400 | AAA.this.parsedNasIpAddress.getAddress()); |
421 | 401 | ||
422 | radiusPayload.encapsulateMessage(eapPacket); | 402 | radiusPayload.encapsulateMessage(eapPacket); |
423 | 403 | ||
424 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, | 404 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
425 | - stateMachine.getChallengeState()); | 405 | + stateMachine.challengeState()); |
426 | radiusPayload.addMessageAuthenticator(AAA.this.radiusSecret); | 406 | radiusPayload.addMessageAuthenticator(AAA.this.radiusSecret); |
427 | sendRadiusMessage(radiusPayload); | 407 | sendRadiusMessage(radiusPayload); |
428 | } | 408 | } |
... | @@ -432,16 +412,16 @@ public class AAA { | ... | @@ -432,16 +412,16 @@ public class AAA { |
432 | //request id access to RADIUS | 412 | //request id access to RADIUS |
433 | RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, | 413 | RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
434 | eapPacket.getIdentifier()); | 414 | eapPacket.getIdentifier()); |
435 | - radiusPayload.setIdentifier(stateMachine.getIdentifier()); | 415 | + radiusPayload.setIdentifier(stateMachine.identifier()); |
436 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, | 416 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
437 | - stateMachine.getUsername()); | 417 | + stateMachine.username()); |
438 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, | 418 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
439 | AAA.this.parsedNasIpAddress.getAddress()); | 419 | AAA.this.parsedNasIpAddress.getAddress()); |
440 | 420 | ||
441 | radiusPayload.encapsulateMessage(eapPacket); | 421 | radiusPayload.encapsulateMessage(eapPacket); |
442 | 422 | ||
443 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, | 423 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
444 | - stateMachine.getChallengeState()); | 424 | + stateMachine.challengeState()); |
445 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); | 425 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
446 | 426 | ||
447 | radiusPayload.addMessageAuthenticator(AAA.this.radiusSecret); | 427 | radiusPayload.addMessageAuthenticator(AAA.this.radiusSecret); |
... | @@ -468,7 +448,7 @@ public class AAA { | ... | @@ -468,7 +448,7 @@ public class AAA { |
468 | * @param radiusPacket RADIUS packet coming from the RADIUS server. | 448 | * @param radiusPacket RADIUS packet coming from the RADIUS server. |
469 | */ | 449 | */ |
470 | private void handleRadiusPacket(RADIUS radiusPacket) { | 450 | private void handleRadiusPacket(RADIUS radiusPacket) { |
471 | - StateMachine stateMachine = getStateMachineById(radiusPacket.getIdentifier()); | 451 | + StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier()); |
472 | if (stateMachine == null) { | 452 | if (stateMachine == null) { |
473 | log.error("Invalid session identifier, exiting..."); | 453 | log.error("Invalid session identifier, exiting..."); |
474 | return; | 454 | return; |
... | @@ -481,10 +461,10 @@ public class AAA { | ... | @@ -481,10 +461,10 @@ public class AAA { |
481 | byte[] challengeState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE).getValue(); | 461 | byte[] challengeState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE).getValue(); |
482 | eapPayload = radiusPacket.decapsulateMessage(); | 462 | eapPayload = radiusPacket.decapsulateMessage(); |
483 | stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState); | 463 | stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState); |
484 | - eth = buildEapolResponse(stateMachine.supplicantAddress, | 464 | + eth = buildEapolResponse(stateMachine.supplicantAddress(), |
485 | - MacAddress.valueOf(1L), stateMachine.vlanId, EAPOL.EAPOL_PACKET, | 465 | + MacAddress.valueOf(1L), stateMachine.vlanId(), EAPOL.EAPOL_PACKET, |
486 | eapPayload); | 466 | eapPayload); |
487 | - this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint); | 467 | + this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
488 | break; | 468 | break; |
489 | case RADIUS.RADIUS_CODE_ACCESS_ACCEPT: | 469 | case RADIUS.RADIUS_CODE_ACCESS_ACCEPT: |
490 | try { | 470 | try { |
... | @@ -493,10 +473,10 @@ public class AAA { | ... | @@ -493,10 +473,10 @@ public class AAA { |
493 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue(); | 473 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue(); |
494 | eapPayload = new EAP(); | 474 | eapPayload = new EAP(); |
495 | eapPayload = (EAP) eapPayload.deserialize(eapMessage, 0, eapMessage.length); | 475 | eapPayload = (EAP) eapPayload.deserialize(eapMessage, 0, eapMessage.length); |
496 | - eth = buildEapolResponse(stateMachine.supplicantAddress, | 476 | + eth = buildEapolResponse(stateMachine.supplicantAddress(), |
497 | - MacAddress.valueOf(1L), stateMachine.vlanId, EAPOL.EAPOL_PACKET, | 477 | + MacAddress.valueOf(1L), stateMachine.vlanId(), EAPOL.EAPOL_PACKET, |
498 | eapPayload); | 478 | eapPayload); |
499 | - this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint); | 479 | + this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
500 | 480 | ||
501 | stateMachine.authorizeAccess(); | 481 | stateMachine.authorizeAccess(); |
502 | } catch (StateMachineException e) { | 482 | } catch (StateMachineException e) { |
... | @@ -515,51 +495,6 @@ public class AAA { | ... | @@ -515,51 +495,6 @@ public class AAA { |
515 | } | 495 | } |
516 | } | 496 | } |
517 | 497 | ||
518 | - private StateMachine getStateMachineById(byte identifier) { | ||
519 | - StateMachine stateMachine = null; | ||
520 | - Set stateMachineSet = stateMachineMap.entrySet(); | ||
521 | - | ||
522 | - synchronized (stateMachineMap) { | ||
523 | - Iterator itr = stateMachineSet.iterator(); | ||
524 | - while (itr.hasNext()) { | ||
525 | - Map.Entry entry = (Map.Entry) itr.next(); | ||
526 | - stateMachine = (StateMachine) entry.getValue(); | ||
527 | - if (identifier == stateMachine.getIdentifier()) { | ||
528 | - //the state machine has already been created for this session session | ||
529 | - stateMachine = (StateMachine) entry.getValue(); | ||
530 | - break; | ||
531 | - } | ||
532 | - } | ||
533 | - } | ||
534 | - | ||
535 | - return stateMachine; | ||
536 | - } | ||
537 | - | ||
538 | - private StateMachine getStateMachine(String sessionId) { | ||
539 | - StateMachine stateMachine = null; | ||
540 | - Set stateMachineSet = stateMachineMap.entrySet(); | ||
541 | - | ||
542 | - synchronized (stateMachineMap) { | ||
543 | - Iterator itr = stateMachineSet.iterator(); | ||
544 | - while (itr.hasNext()) { | ||
545 | - | ||
546 | - Map.Entry entry = (Map.Entry) itr.next(); | ||
547 | - if (sessionId.equals(entry.getKey())) { | ||
548 | - //the state machine has already been created for this session session | ||
549 | - stateMachine = (StateMachine) entry.getValue(); | ||
550 | - break; | ||
551 | - } | ||
552 | - } | ||
553 | - } | ||
554 | - | ||
555 | - if (stateMachine == null) { | ||
556 | - stateMachine = new StateMachine(sessionId, voltTenantService); | ||
557 | - stateMachineMap.put(sessionId, stateMachine); | ||
558 | - } | ||
559 | - | ||
560 | - return stateMachine; | ||
561 | - } | ||
562 | - | ||
563 | private void sendRadiusMessage(RADIUS radiusMessage) { | 498 | private void sendRadiusMessage(RADIUS radiusMessage) { |
564 | Set<Host> hosts = hostService.getHostsByIp(IpAddress.valueOf(radiusIpAddress)); | 499 | Set<Host> hosts = hostService.getHostsByIp(IpAddress.valueOf(radiusIpAddress)); |
565 | Optional<Host> odst = hosts.stream().filter(h -> h.vlan().toShort() == VlanId.UNTAGGED).findFirst(); | 500 | Optional<Host> odst = hosts.stream().filter(h -> h.vlan().toShort() == VlanId.UNTAGGED).findFirst(); | ... | ... |
... | @@ -18,13 +18,16 @@ | ... | @@ -18,13 +18,16 @@ |
18 | 18 | ||
19 | package org.onosproject.aaa; | 19 | package org.onosproject.aaa; |
20 | 20 | ||
21 | +import java.util.BitSet; | ||
22 | +import java.util.Map; | ||
23 | + | ||
21 | import org.onlab.packet.MacAddress; | 24 | import org.onlab.packet.MacAddress; |
22 | import org.onosproject.net.ConnectPoint; | 25 | import org.onosproject.net.ConnectPoint; |
23 | import org.onosproject.xosintegration.VoltTenant; | 26 | import org.onosproject.xosintegration.VoltTenant; |
24 | import org.onosproject.xosintegration.VoltTenantService; | 27 | import org.onosproject.xosintegration.VoltTenantService; |
25 | import org.slf4j.Logger; | 28 | import org.slf4j.Logger; |
26 | 29 | ||
27 | -import java.util.BitSet; | 30 | +import com.google.common.collect.Maps; |
28 | 31 | ||
29 | import static org.slf4j.LoggerFactory.getLogger; | 32 | import static org.slf4j.LoggerFactory.getLogger; |
30 | 33 | ||
... | @@ -58,9 +61,9 @@ class StateMachine { | ... | @@ -58,9 +61,9 @@ class StateMachine { |
58 | private byte[] requestAuthenticator; | 61 | private byte[] requestAuthenticator; |
59 | 62 | ||
60 | // Supplicant connectivity info | 63 | // Supplicant connectivity info |
61 | - protected ConnectPoint supplicantConnectpoint; | 64 | + private ConnectPoint supplicantConnectpoint; |
62 | - protected MacAddress supplicantAddress; | 65 | + private MacAddress supplicantAddress; |
63 | - protected short vlanId; | 66 | + private short vlanId; |
64 | 67 | ||
65 | private String sessionId = null; | 68 | private String sessionId = null; |
66 | 69 | ||
... | @@ -109,8 +112,28 @@ class StateMachine { | ... | @@ -109,8 +112,28 @@ class StateMachine { |
109 | 112 | ||
110 | private int currentState = STATE_IDLE; | 113 | private int currentState = STATE_IDLE; |
111 | 114 | ||
115 | + // Maps of state machines. Each state machine is represented by an | ||
116 | + // unique identifier on the switch: dpid + port number | ||
117 | + private static Map<String, StateMachine> sessionIdMap; | ||
118 | + private static Map<Integer, StateMachine> identifierMap; | ||
112 | 119 | ||
113 | - /** | 120 | + public static void initializeMaps() { |
121 | + sessionIdMap = Maps.newConcurrentMap(); | ||
122 | + identifierMap = Maps.newConcurrentMap(); | ||
123 | + } | ||
124 | + | ||
125 | + public static void destroyMaps() { | ||
126 | + sessionIdMap = null; | ||
127 | + identifierMap = null; | ||
128 | + } | ||
129 | + | ||
130 | + public static StateMachine lookupStateMachineById(byte identifier) { | ||
131 | + return identifierMap.get((int) identifier); | ||
132 | + } | ||
133 | + | ||
134 | + public static StateMachine lookupStateMachineBySessionId(String sessionId) { | ||
135 | + return sessionIdMap.get(sessionId); | ||
136 | + } /** | ||
114 | * State Machine Constructor. | 137 | * State Machine Constructor. |
115 | * | 138 | * |
116 | * @param sessionId session Id represented by the switch dpid + port number | 139 | * @param sessionId session Id represented by the switch dpid + port number |
... | @@ -120,15 +143,69 @@ class StateMachine { | ... | @@ -120,15 +143,69 @@ class StateMachine { |
120 | log.info("Creating a new state machine for {}", sessionId); | 143 | log.info("Creating a new state machine for {}", sessionId); |
121 | this.sessionId = sessionId; | 144 | this.sessionId = sessionId; |
122 | this.voltService = voltService; | 145 | this.voltService = voltService; |
146 | + sessionIdMap.put(sessionId, this); | ||
147 | + } | ||
123 | 148 | ||
149 | + /** | ||
150 | + * Gets the connect point for the supplicant side. | ||
151 | + * | ||
152 | + * @return supplicant connect point | ||
153 | + */ | ||
154 | + public ConnectPoint supplicantConnectpoint() { | ||
155 | + return supplicantConnectpoint; | ||
156 | + } | ||
157 | + | ||
158 | + /** | ||
159 | + * Sets the supplicant side connect point. | ||
160 | + * | ||
161 | + * @param supplicantConnectpoint supplicant select point. | ||
162 | + */ | ||
163 | + public void setSupplicantConnectpoint(ConnectPoint supplicantConnectpoint) { | ||
164 | + this.supplicantConnectpoint = supplicantConnectpoint; | ||
124 | } | 165 | } |
125 | 166 | ||
126 | /** | 167 | /** |
127 | - * Get the client id that is requesting for access. | 168 | + * Gets the MAC address of the supplicant. |
169 | + * | ||
170 | + * @return supplicant MAC address | ||
171 | + */ | ||
172 | + public MacAddress supplicantAddress() { | ||
173 | + return supplicantAddress; | ||
174 | + } | ||
175 | + | ||
176 | + /** | ||
177 | + * Sets the supplicant MAC address. | ||
178 | + * | ||
179 | + * @param supplicantAddress new supplicant MAC address | ||
180 | + */ | ||
181 | + public void setSupplicantAddress(MacAddress supplicantAddress) { | ||
182 | + this.supplicantAddress = supplicantAddress; | ||
183 | + } | ||
184 | + | ||
185 | + /** | ||
186 | + * Gets the client's Vlan ID. | ||
187 | + * | ||
188 | + * @return client vlan ID | ||
189 | + */ | ||
190 | + public short vlanId() { | ||
191 | + return vlanId; | ||
192 | + } | ||
193 | + | ||
194 | + /** | ||
195 | + * Sets the client's vlan ID. | ||
196 | + * | ||
197 | + * @param vlanId new client vlan ID | ||
198 | + */ | ||
199 | + public void setVlanId(short vlanId) { | ||
200 | + this.vlanId = vlanId; | ||
201 | + } | ||
202 | + | ||
203 | + /** | ||
204 | + * Gets the client id that is requesting for access. | ||
128 | * | 205 | * |
129 | * @return The client id. | 206 | * @return The client id. |
130 | */ | 207 | */ |
131 | - public String getSessionId() { | 208 | + public String sessionId() { |
132 | return this.sessionId; | 209 | return this.sessionId; |
133 | } | 210 | } |
134 | 211 | ||
... | @@ -178,11 +255,11 @@ class StateMachine { | ... | @@ -178,11 +255,11 @@ class StateMachine { |
178 | } | 255 | } |
179 | 256 | ||
180 | /** | 257 | /** |
181 | - * Get the challenge EAP identifier set by the RADIUS. | 258 | + * Gets the challenge EAP identifier set by the RADIUS. |
182 | * | 259 | * |
183 | * @return The challenge EAP identifier. | 260 | * @return The challenge EAP identifier. |
184 | */ | 261 | */ |
185 | - protected byte getChallengeIdentifier() { | 262 | + protected byte challengeIdentifier() { |
186 | return this.challengeIdentifier; | 263 | return this.challengeIdentifier; |
187 | } | 264 | } |
188 | 265 | ||
... | @@ -198,11 +275,11 @@ class StateMachine { | ... | @@ -198,11 +275,11 @@ class StateMachine { |
198 | } | 275 | } |
199 | 276 | ||
200 | /** | 277 | /** |
201 | - * Get the challenge state set by the RADIUS. | 278 | + * Gets the challenge state set by the RADIUS. |
202 | * | 279 | * |
203 | * @return The challenge state. | 280 | * @return The challenge state. |
204 | */ | 281 | */ |
205 | - protected byte[] getChallengeState() { | 282 | + protected byte[] challengeState() { |
206 | return this.challengeState; | 283 | return this.challengeState; |
207 | } | 284 | } |
208 | 285 | ||
... | @@ -217,16 +294,16 @@ class StateMachine { | ... | @@ -217,16 +294,16 @@ class StateMachine { |
217 | 294 | ||
218 | 295 | ||
219 | /** | 296 | /** |
220 | - * Get the username. | 297 | + * Gets the username. |
221 | * | 298 | * |
222 | * @return The requestAuthenticator. | 299 | * @return The requestAuthenticator. |
223 | */ | 300 | */ |
224 | - protected byte[] getReqeustAuthenticator() { | 301 | + protected byte[] requestAuthenticator() { |
225 | return this.requestAuthenticator; | 302 | return this.requestAuthenticator; |
226 | } | 303 | } |
227 | 304 | ||
228 | /** | 305 | /** |
229 | - * Set the username. | 306 | + * Sets the authenticator. |
230 | * | 307 | * |
231 | * @param authenticator The username sent to the RADIUS upon access request. | 308 | * @param authenticator The username sent to the RADIUS upon access request. |
232 | */ | 309 | */ |
... | @@ -236,11 +313,11 @@ class StateMachine { | ... | @@ -236,11 +313,11 @@ class StateMachine { |
236 | 313 | ||
237 | 314 | ||
238 | /** | 315 | /** |
239 | - * Get the username. | 316 | + * Gets the username. |
240 | * | 317 | * |
241 | * @return The username. | 318 | * @return The username. |
242 | */ | 319 | */ |
243 | - protected byte[] getUsername() { | 320 | + protected byte[] username() { |
244 | return this.username; | 321 | return this.username; |
245 | } | 322 | } |
246 | 323 | ||
... | @@ -249,7 +326,7 @@ class StateMachine { | ... | @@ -249,7 +326,7 @@ class StateMachine { |
249 | * | 326 | * |
250 | * @return The state machine identifier. | 327 | * @return The state machine identifier. |
251 | */ | 328 | */ |
252 | - public byte getIdentifier() { | 329 | + public byte identifier() { |
253 | return (byte) this.identifier; | 330 | return (byte) this.identifier; |
254 | } | 331 | } |
255 | 332 | ||
... | @@ -284,6 +361,7 @@ class StateMachine { | ... | @@ -284,6 +361,7 @@ class StateMachine { |
284 | //move to the next state | 361 | //move to the next state |
285 | next(TRANSITION_START); | 362 | next(TRANSITION_START); |
286 | createIdentifier(); | 363 | createIdentifier(); |
364 | + identifierMap.put(identifier, this); | ||
287 | } | 365 | } |
288 | 366 | ||
289 | /** | 367 | /** |
... | @@ -349,16 +427,16 @@ class StateMachine { | ... | @@ -349,16 +427,16 @@ class StateMachine { |
349 | } | 427 | } |
350 | 428 | ||
351 | /** | 429 | /** |
352 | - * Get the current state. | 430 | + * Gets the current state. |
353 | * | 431 | * |
354 | * @return The current state. Could be STATE_IDLE, STATE_STARTED, STATE_PENDING, STATE_AUTHORIZED, | 432 | * @return The current state. Could be STATE_IDLE, STATE_STARTED, STATE_PENDING, STATE_AUTHORIZED, |
355 | * STATE_UNAUTHORIZED. | 433 | * STATE_UNAUTHORIZED. |
356 | */ | 434 | */ |
357 | - public int getState() { | 435 | + public int state() { |
358 | return currentState; | 436 | return currentState; |
359 | } | 437 | } |
360 | 438 | ||
361 | - | 439 | + @Override |
362 | public String toString() { | 440 | public String toString() { |
363 | return ("sessionId: " + this.sessionId) + "\t" + ("identifier: " + this.identifier) + "\t" + | 441 | return ("sessionId: " + this.sessionId) + "\t" + ("identifier: " + this.identifier) + "\t" + |
364 | ("state: " + this.currentState); | 442 | ("state: " + this.currentState); | ... | ... |
... | @@ -21,6 +21,7 @@ import org.junit.After; | ... | @@ -21,6 +21,7 @@ import org.junit.After; |
21 | import org.junit.Assert; | 21 | import org.junit.Assert; |
22 | import org.junit.Before; | 22 | import org.junit.Before; |
23 | import org.junit.Test; | 23 | import org.junit.Test; |
24 | +import static org.junit.Assert.*; | ||
24 | 25 | ||
25 | 26 | ||
26 | public class StateMachineTest { | 27 | public class StateMachineTest { |
... | @@ -30,6 +31,7 @@ public class StateMachineTest { | ... | @@ -30,6 +31,7 @@ public class StateMachineTest { |
30 | public void setUp() { | 31 | public void setUp() { |
31 | System.out.println("Set Up."); | 32 | System.out.println("Set Up."); |
32 | StateMachine.bitSet.clear(); | 33 | StateMachine.bitSet.clear(); |
34 | + StateMachine.initializeMaps(); | ||
33 | stateMachine = new StateMachine("session0", null); | 35 | stateMachine = new StateMachine("session0", null); |
34 | } | 36 | } |
35 | 37 | ||
... | @@ -37,6 +39,7 @@ public class StateMachineTest { | ... | @@ -37,6 +39,7 @@ public class StateMachineTest { |
37 | public void tearDown() { | 39 | public void tearDown() { |
38 | System.out.println("Tear Down."); | 40 | System.out.println("Tear Down."); |
39 | StateMachine.bitSet.clear(); | 41 | StateMachine.bitSet.clear(); |
42 | + StateMachine.destroyMaps(); | ||
40 | stateMachine = null; | 43 | stateMachine = null; |
41 | } | 44 | } |
42 | 45 | ||
... | @@ -46,19 +49,19 @@ public class StateMachineTest { | ... | @@ -46,19 +49,19 @@ public class StateMachineTest { |
46 | */ | 49 | */ |
47 | public void basic() throws StateMachineException { | 50 | public void basic() throws StateMachineException { |
48 | System.out.println("======= BASIC =======."); | 51 | System.out.println("======= BASIC =======."); |
49 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); | 52 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
50 | 53 | ||
51 | stateMachine.start(); | 54 | stateMachine.start(); |
52 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); | 55 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
53 | 56 | ||
54 | stateMachine.requestAccess(); | 57 | stateMachine.requestAccess(); |
55 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); | 58 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
56 | 59 | ||
57 | stateMachine.authorizeAccess(); | 60 | stateMachine.authorizeAccess(); |
58 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); | 61 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
59 | 62 | ||
60 | stateMachine.logoff(); | 63 | stateMachine.logoff(); |
61 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); | 64 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
62 | } | 65 | } |
63 | 66 | ||
64 | @Test | 67 | @Test |
... | @@ -68,19 +71,19 @@ public class StateMachineTest { | ... | @@ -68,19 +71,19 @@ public class StateMachineTest { |
68 | public void testIdleState() throws StateMachineException { | 71 | public void testIdleState() throws StateMachineException { |
69 | System.out.println("======= IDLE STATE TEST =======."); | 72 | System.out.println("======= IDLE STATE TEST =======."); |
70 | stateMachine.requestAccess(); | 73 | stateMachine.requestAccess(); |
71 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); | 74 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
72 | 75 | ||
73 | stateMachine.authorizeAccess(); | 76 | stateMachine.authorizeAccess(); |
74 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); | 77 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
75 | 78 | ||
76 | stateMachine.denyAccess(); | 79 | stateMachine.denyAccess(); |
77 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); | 80 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
78 | 81 | ||
79 | stateMachine.logoff(); | 82 | stateMachine.logoff(); |
80 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); | 83 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
81 | 84 | ||
82 | stateMachine.start(); | 85 | stateMachine.start(); |
83 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); | 86 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
84 | } | 87 | } |
85 | 88 | ||
86 | @Test | 89 | @Test |
... | @@ -92,19 +95,19 @@ public class StateMachineTest { | ... | @@ -92,19 +95,19 @@ public class StateMachineTest { |
92 | stateMachine.start(); | 95 | stateMachine.start(); |
93 | 96 | ||
94 | stateMachine.authorizeAccess(); | 97 | stateMachine.authorizeAccess(); |
95 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); | 98 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
96 | 99 | ||
97 | stateMachine.denyAccess(); | 100 | stateMachine.denyAccess(); |
98 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); | 101 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
99 | 102 | ||
100 | stateMachine.logoff(); | 103 | stateMachine.logoff(); |
101 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); | 104 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
102 | 105 | ||
103 | stateMachine.start(); | 106 | stateMachine.start(); |
104 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); | 107 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
105 | 108 | ||
106 | stateMachine.requestAccess(); | 109 | stateMachine.requestAccess(); |
107 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); | 110 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
108 | } | 111 | } |
109 | 112 | ||
110 | @Test | 113 | @Test |
... | @@ -118,19 +121,19 @@ public class StateMachineTest { | ... | @@ -118,19 +121,19 @@ public class StateMachineTest { |
118 | stateMachine.requestAccess(); | 121 | stateMachine.requestAccess(); |
119 | 122 | ||
120 | stateMachine.logoff(); | 123 | stateMachine.logoff(); |
121 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); | 124 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
122 | 125 | ||
123 | stateMachine.start(); | 126 | stateMachine.start(); |
124 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); | 127 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
125 | 128 | ||
126 | stateMachine.requestAccess(); | 129 | stateMachine.requestAccess(); |
127 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); | 130 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
128 | 131 | ||
129 | stateMachine.authorizeAccess(); | 132 | stateMachine.authorizeAccess(); |
130 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); | 133 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
131 | 134 | ||
132 | stateMachine.denyAccess(); | 135 | stateMachine.denyAccess(); |
133 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); | 136 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
134 | } | 137 | } |
135 | 138 | ||
136 | @Test | 139 | @Test |
... | @@ -144,19 +147,19 @@ public class StateMachineTest { | ... | @@ -144,19 +147,19 @@ public class StateMachineTest { |
144 | stateMachine.requestAccess(); | 147 | stateMachine.requestAccess(); |
145 | 148 | ||
146 | stateMachine.logoff(); | 149 | stateMachine.logoff(); |
147 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); | 150 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
148 | 151 | ||
149 | stateMachine.start(); | 152 | stateMachine.start(); |
150 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); | 153 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
151 | 154 | ||
152 | stateMachine.requestAccess(); | 155 | stateMachine.requestAccess(); |
153 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); | 156 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
154 | 157 | ||
155 | stateMachine.denyAccess(); | 158 | stateMachine.denyAccess(); |
156 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); | 159 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
157 | 160 | ||
158 | stateMachine.authorizeAccess(); | 161 | stateMachine.authorizeAccess(); |
159 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); | 162 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
160 | } | 163 | } |
161 | 164 | ||
162 | @Test | 165 | @Test |
... | @@ -170,19 +173,19 @@ public class StateMachineTest { | ... | @@ -170,19 +173,19 @@ public class StateMachineTest { |
170 | stateMachine.authorizeAccess(); | 173 | stateMachine.authorizeAccess(); |
171 | 174 | ||
172 | stateMachine.start(); | 175 | stateMachine.start(); |
173 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); | 176 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
174 | 177 | ||
175 | stateMachine.requestAccess(); | 178 | stateMachine.requestAccess(); |
176 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); | 179 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
177 | 180 | ||
178 | stateMachine.authorizeAccess(); | 181 | stateMachine.authorizeAccess(); |
179 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); | 182 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
180 | 183 | ||
181 | stateMachine.denyAccess(); | 184 | stateMachine.denyAccess(); |
182 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); | 185 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
183 | 186 | ||
184 | stateMachine.logoff(); | 187 | stateMachine.logoff(); |
185 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); | 188 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
186 | } | 189 | } |
187 | 190 | ||
188 | @Test | 191 | @Test |
... | @@ -196,27 +199,27 @@ public class StateMachineTest { | ... | @@ -196,27 +199,27 @@ public class StateMachineTest { |
196 | stateMachine.denyAccess(); | 199 | stateMachine.denyAccess(); |
197 | 200 | ||
198 | stateMachine.start(); | 201 | stateMachine.start(); |
199 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); | 202 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
200 | 203 | ||
201 | stateMachine.requestAccess(); | 204 | stateMachine.requestAccess(); |
202 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); | 205 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
203 | 206 | ||
204 | stateMachine.authorizeAccess(); | 207 | stateMachine.authorizeAccess(); |
205 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); | 208 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
206 | 209 | ||
207 | stateMachine.denyAccess(); | 210 | stateMachine.denyAccess(); |
208 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); | 211 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
209 | 212 | ||
210 | stateMachine.logoff(); | 213 | stateMachine.logoff(); |
211 | - Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); | 214 | + Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
212 | } | 215 | } |
213 | 216 | ||
214 | 217 | ||
215 | @Test | 218 | @Test |
216 | public void testIdentifierAvailability() throws StateMachineException { | 219 | public void testIdentifierAvailability() throws StateMachineException { |
217 | System.out.println("======= IDENTIFIER TEST =======."); | 220 | System.out.println("======= IDENTIFIER TEST =======."); |
218 | - byte identifier = stateMachine.getIdentifier(); | 221 | + byte identifier = stateMachine.identifier(); |
219 | - System.out.println("State: " + stateMachine.getState()); | 222 | + System.out.println("State: " + stateMachine.state()); |
220 | System.out.println("Identifier: " + Byte.toUnsignedInt(identifier)); | 223 | System.out.println("Identifier: " + Byte.toUnsignedInt(identifier)); |
221 | Assert.assertEquals(-1, identifier); | 224 | Assert.assertEquals(-1, identifier); |
222 | stateMachine.start(); | 225 | stateMachine.start(); |
... | @@ -230,7 +233,7 @@ public class StateMachineTest { | ... | @@ -230,7 +233,7 @@ public class StateMachineTest { |
230 | for (int i = 1; i <= 255; i++) { | 233 | for (int i = 1; i <= 255; i++) { |
231 | StateMachine sm = new StateMachine("session" + i, null); | 234 | StateMachine sm = new StateMachine("session" + i, null); |
232 | sm.start(); | 235 | sm.start(); |
233 | - byte id = sm.getIdentifier(); | 236 | + byte id = sm.identifier(); |
234 | Assert.assertEquals(i, Byte.toUnsignedInt(id)); | 237 | Assert.assertEquals(i, Byte.toUnsignedInt(id)); |
235 | if (i == 3) { | 238 | if (i == 3) { |
236 | sm3 = sm; | 239 | sm3 = sm; |
... | @@ -244,27 +247,72 @@ public class StateMachineTest { | ... | @@ -244,27 +247,72 @@ public class StateMachineTest { |
244 | 247 | ||
245 | //simulate the state machine for a specific session and logoff so we can free up a spot for an identifier | 248 | //simulate the state machine for a specific session and logoff so we can free up a spot for an identifier |
246 | //let's choose identifier 247 then we free up 3 | 249 | //let's choose identifier 247 then we free up 3 |
250 | + Assert.assertNotNull(sm247); | ||
247 | sm247.requestAccess(); | 251 | sm247.requestAccess(); |
248 | sm247.authorizeAccess(); | 252 | sm247.authorizeAccess(); |
249 | sm247.logoff(); | 253 | sm247.logoff(); |
250 | - sm247 = null; | ||
251 | 254 | ||
255 | + Assert.assertNotNull(sm3); | ||
252 | sm3.requestAccess(); | 256 | sm3.requestAccess(); |
253 | sm3.authorizeAccess(); | 257 | sm3.authorizeAccess(); |
254 | sm3.logoff(); | 258 | sm3.logoff(); |
255 | - sm3 = null; | ||
256 | 259 | ||
257 | StateMachine otherSM3 = new StateMachine("session3b", null); | 260 | StateMachine otherSM3 = new StateMachine("session3b", null); |
258 | otherSM3.start(); | 261 | otherSM3.start(); |
259 | otherSM3.requestAccess(); | 262 | otherSM3.requestAccess(); |
260 | - byte id3 = otherSM3.getIdentifier(); | 263 | + byte id3 = otherSM3.identifier(); |
261 | Assert.assertEquals(3, Byte.toUnsignedInt(id3)); | 264 | Assert.assertEquals(3, Byte.toUnsignedInt(id3)); |
262 | 265 | ||
263 | StateMachine otherSM247 = new StateMachine("session247b", null); | 266 | StateMachine otherSM247 = new StateMachine("session247b", null); |
264 | otherSM247.start(); | 267 | otherSM247.start(); |
265 | otherSM247.requestAccess(); | 268 | otherSM247.requestAccess(); |
266 | - byte id247 = otherSM247.getIdentifier(); | 269 | + byte id247 = otherSM247.identifier(); |
267 | Assert.assertEquals(247, Byte.toUnsignedInt(id247)); | 270 | Assert.assertEquals(247, Byte.toUnsignedInt(id247)); |
271 | + } | ||
268 | 272 | ||
273 | + @Test | ||
274 | + public void testSessionIdLookups() { | ||
275 | + String sessionId1 = "session1"; | ||
276 | + String sessionId2 = "session2"; | ||
277 | + String sessionId3 = "session3"; | ||
278 | + | ||
279 | + StateMachine machine1ShouldBeNull = | ||
280 | + StateMachine.lookupStateMachineBySessionId(sessionId1); | ||
281 | + assertNull(machine1ShouldBeNull); | ||
282 | + StateMachine machine2ShouldBeNull = | ||
283 | + StateMachine.lookupStateMachineBySessionId(sessionId2); | ||
284 | + assertNull(machine2ShouldBeNull); | ||
285 | + | ||
286 | + StateMachine stateMachine1 = new StateMachine(sessionId1, null); | ||
287 | + StateMachine stateMachine2 = new StateMachine(sessionId2, null); | ||
288 | + | ||
289 | + assertEquals(stateMachine1, | ||
290 | + StateMachine.lookupStateMachineBySessionId(sessionId1)); | ||
291 | + assertEquals(stateMachine2, | ||
292 | + StateMachine.lookupStateMachineBySessionId(sessionId2)); | ||
293 | + assertNull(StateMachine.lookupStateMachineBySessionId(sessionId3)); | ||
294 | + } | ||
295 | + | ||
296 | + @Test | ||
297 | + public void testIdentifierLookups() throws StateMachineException { | ||
298 | + String sessionId1 = "session1"; | ||
299 | + String sessionId2 = "session2"; | ||
300 | + | ||
301 | + StateMachine machine1ShouldBeNull = | ||
302 | + StateMachine.lookupStateMachineById((byte) 1); | ||
303 | + assertNull(machine1ShouldBeNull); | ||
304 | + StateMachine machine2ShouldBeNull = | ||
305 | + StateMachine.lookupStateMachineById((byte) 2); | ||
306 | + assertNull(machine2ShouldBeNull); | ||
307 | + | ||
308 | + StateMachine stateMachine1 = new StateMachine(sessionId1, null); | ||
309 | + stateMachine1.start(); | ||
310 | + StateMachine stateMachine2 = new StateMachine(sessionId2, null); | ||
311 | + stateMachine2.start(); | ||
312 | + | ||
313 | + assertEquals(stateMachine1, | ||
314 | + StateMachine.lookupStateMachineById(stateMachine1.identifier())); | ||
315 | + assertEquals(stateMachine2, | ||
316 | + StateMachine.lookupStateMachineById(stateMachine2.identifier())); | ||
269 | } | 317 | } |
270 | } | 318 | } | ... | ... |
-
Please register or login to post a comment