Refactoring of AAA app classes

- break up multi compilation unit class
- improve exception handling
- start on general clean up

Change-Id: Ic0b4e19a25c2fc1d64c73bfc4273c82cbcaf5d45
This commit is contained in:
Ray Milkey 2015-09-24 08:36:45 -07:00 committed by Gerrit Code Review
parent d06f93f2ab
commit c3fca7fa69
3 changed files with 177 additions and 163 deletions

View File

@ -137,7 +137,7 @@ class StateMachine {
*/ */
private void createIdentifier() throws StateMachineException { private void createIdentifier() throws StateMachineException {
log.debug("Creating Identifier."); log.debug("Creating Identifier.");
int index = -1; int index;
try { try {
//find the first available spot for identifier assignment //find the first available spot for identifier assignment
@ -267,7 +267,7 @@ class StateMachine {
/** /**
* Move to the next state. * Move to the next state.
* *
* @param msg * @param msg message
*/ */
private void next(int msg) { private void next(int msg) {
currentState = transition[currentState][msg]; currentState = transition[currentState][msg];
@ -280,14 +280,10 @@ class StateMachine {
* @throws StateMachineException if authentication protocol is violated * @throws StateMachineException if authentication protocol is violated
*/ */
public void start() throws StateMachineException { public void start() throws StateMachineException {
try {
states[currentState].start(); states[currentState].start();
//move to the next state //move to the next state
next(TRANSITION_START); next(TRANSITION_START);
createIdentifier(); createIdentifier();
} catch (StateMachineInvalidTransitionException e) {
e.printStackTrace();
}
} }
/** /**
@ -297,13 +293,9 @@ class StateMachine {
* @throws StateMachineException if authentication protocol is violated * @throws StateMachineException if authentication protocol is violated
*/ */
public void requestAccess() throws StateMachineException { public void requestAccess() throws StateMachineException {
try {
states[currentState].requestAccess(); states[currentState].requestAccess();
//move to the next state //move to the next state
next(TRANSITION_REQUEST_ACCESS); next(TRANSITION_REQUEST_ACCESS);
} catch (StateMachineInvalidTransitionException e) {
e.printStackTrace();
}
} }
/** /**
@ -313,7 +305,6 @@ class StateMachine {
* @throws StateMachineException if authentication protocol is violated * @throws StateMachineException if authentication protocol is violated
*/ */
public void authorizeAccess() throws StateMachineException { public void authorizeAccess() throws StateMachineException {
try {
states[currentState].radiusAccepted(); states[currentState].radiusAccepted();
//move to the next state //move to the next state
next(TRANSITION_AUTHORIZE_ACCESS); next(TRANSITION_AUTHORIZE_ACCESS);
@ -330,10 +321,6 @@ class StateMachine {
} }
deleteIdentifier(); deleteIdentifier();
} catch (StateMachineInvalidTransitionException e) {
e.printStackTrace();
}
} }
/** /**
@ -343,14 +330,10 @@ class StateMachine {
* @throws StateMachineException if authentication protocol is violated * @throws StateMachineException if authentication protocol is violated
*/ */
public void denyAccess() throws StateMachineException { public void denyAccess() throws StateMachineException {
try {
states[currentState].radiusDenied(); states[currentState].radiusDenied();
//move to the next state //move to the next state
next(TRANSITION_DENY_ACCESS); next(TRANSITION_DENY_ACCESS);
deleteIdentifier(); deleteIdentifier();
} catch (StateMachineInvalidTransitionException e) {
e.printStackTrace();
}
} }
/** /**
@ -360,13 +343,9 @@ class StateMachine {
* @throws StateMachineException if authentication protocol is violated * @throws StateMachineException if authentication protocol is violated
*/ */
public void logoff() throws StateMachineException { public void logoff() throws StateMachineException {
try {
states[currentState].logoff(); states[currentState].logoff();
//move to the next state //move to the next state
next(TRANSITION_LOGOFF); next(TRANSITION_LOGOFF);
} catch (StateMachineInvalidTransitionException e) {
e.printStackTrace();
}
} }
/** /**
@ -384,11 +363,8 @@ class StateMachine {
return ("sessionId: " + this.sessionId) + "\t" + ("identifier: " + this.identifier) + "\t" + return ("sessionId: " + this.sessionId) + "\t" + ("identifier: " + this.identifier) + "\t" +
("state: " + this.currentState); ("state: " + this.currentState);
} }
}
// FIXME: A source file should contain no more than one top-level entity! abstract class State {
abstract class State {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
private String name = "State"; private String name = "State";
@ -412,36 +388,36 @@ abstract class State {
public void logoff() throws StateMachineInvalidTransitionException { public void logoff() throws StateMachineInvalidTransitionException {
log.warn("LOGOFF transition from this state is not allowed."); log.warn("LOGOFF transition from this state is not allowed.");
} }
} }
/** /**
* Idle state: supplicant is logged of from the network. * Idle state: supplicant is logged of from the network.
*/ */
class Idle extends State { class Idle extends State {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
private String name = "IDLE_STATE"; private String name = "IDLE_STATE";
public void start() { public void start() {
log.info("Moving from IDLE state to STARTED state."); log.info("Moving from IDLE state to STARTED state.");
} }
} }
/** /**
* Started state: supplicant has entered the network and informed the authenticator. * Started state: supplicant has entered the network and informed the authenticator.
*/ */
class Started extends State { class Started extends State {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
private String name = "STARTED_STATE"; private String name = "STARTED_STATE";
public void requestAccess() { public void requestAccess() {
log.info("Moving from STARTED state to PENDING state."); log.info("Moving from STARTED state to PENDING state.");
} }
} }
/** /**
* Pending state: supplicant has been identified by the authenticator but has not access yet. * Pending state: supplicant has been identified by the authenticator but has not access yet.
*/ */
class Pending extends State { class Pending extends State {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
private String name = "PENDING_STATE"; private String name = "PENDING_STATE";
@ -452,12 +428,12 @@ class Pending extends State {
public void radiusDenied() { public void radiusDenied() {
log.info("Moving from PENDING state to UNAUTHORIZED state."); log.info("Moving from PENDING state to UNAUTHORIZED state.");
} }
} }
/** /**
* Authorized state: supplicant port has been accepted, access is granted. * Authorized state: supplicant port has been accepted, access is granted.
*/ */
class Authorized extends State { class Authorized extends State {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
private String name = "AUTHORIZED_STATE"; private String name = "AUTHORIZED_STATE";
@ -465,36 +441,19 @@ class Authorized extends State {
log.info("Moving from AUTHORIZED state to IDLE state."); log.info("Moving from AUTHORIZED state to IDLE state.");
} }
} }
/** /**
* Unauthorized state: supplicant port has been rejected, access is denied. * Unauthorized state: supplicant port has been rejected, access is denied.
*/ */
class Unauthorized extends State { class Unauthorized extends State {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
private String name = "UNAUTHORIZED_STATE"; private String name = "UNAUTHORIZED_STATE";
public void logoff() { public void logoff() {
log.info("Moving from UNAUTHORIZED state to IDLE state."); log.info("Moving from UNAUTHORIZED state to IDLE state.");
} }
}
/**
* Exception for the State Machine.
*/
class StateMachineException extends Exception {
public StateMachineException(String message) {
super(message);
} }
}
/**
* Exception raised when the transition from one state to another is invalid.
*/
class StateMachineInvalidTransitionException extends StateMachineException {
public StateMachineInvalidTransitionException(String message) {
super(message);
}
} }

View File

@ -0,0 +1,28 @@
/*
*
* Copyright 2015 AT&T Foundry
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.aaa;
/**
* Exception for the State Machine.
*/
class StateMachineException extends Exception {
public StateMachineException(String message) {
super(message);
}
}

View File

@ -0,0 +1,27 @@
/*
*
* Copyright 2015 AT&T Foundry
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.aaa;
/**
* Exception raised when the transition from one state to another is invalid.
*/
class StateMachineInvalidTransitionException extends StateMachineException {
public StateMachineInvalidTransitionException(String message) {
super(message);
}
}