CORD-1615 add CLI command to support the removal of state machines
Change-Id: I428bff2a8e502cc7b1c73f804792c36a8f847b57
diff --git a/src/main/java/org/opencord/aaa/AaaResetDeviceCommand.java b/src/main/java/org/opencord/aaa/AaaResetDeviceCommand.java
new file mode 100644
index 0000000..f58d3de
--- /dev/null
+++ b/src/main/java/org/opencord/aaa/AaaResetDeviceCommand.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.opencord.aaa;
+
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Argument;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onlab.packet.MacAddress;
+
+@Command(scope = "onos", name = "aaa-reset-device",
+ description = "Resets the authentication state machine for a given device")
+public class AaaResetDeviceCommand extends AbstractShellCommand {
+ @Argument(index = 0, name = "mac", description = "MAC of device to reset authention state",
+ required = true, multiValued = true)
+ private String[] macs = null;
+
+ @Override
+ protected void execute() {
+ for (String mac : macs) {
+ StateMachine.deleteByMac(MacAddress.valueOf(mac));
+ }
+ }
+}
diff --git a/src/main/java/org/opencord/aaa/AaaShowUsersCommand.java b/src/main/java/org/opencord/aaa/AaaShowUsersCommand.java
index ca1d840..e2c13c3 100644
--- a/src/main/java/org/opencord/aaa/AaaShowUsersCommand.java
+++ b/src/main/java/org/opencord/aaa/AaaShowUsersCommand.java
@@ -37,8 +37,9 @@
String deviceId = stateMachine.supplicantConnectpoint().deviceId().toString();
String portNum = stateMachine.supplicantConnectpoint().port().toString();
String username = new String(stateMachine.username());
- print("UserName=%s,CurrentState=%s,DeviceId=%s,PortNumber=%s",
- username, state[stateMachine.state()], deviceId, portNum);
+ String mac = stateMachine.supplicantAddress().toString();
+ print("UserName=%s,CurrentState=%s,DeviceId=%s,MAC=%s,PortNumber=%s",
+ username, state[stateMachine.state()], deviceId, mac, portNum);
}
}
}
diff --git a/src/main/java/org/opencord/aaa/StateMachine.java b/src/main/java/org/opencord/aaa/StateMachine.java
index 4a00966..6795c43 100644
--- a/src/main/java/org/opencord/aaa/StateMachine.java
+++ b/src/main/java/org/opencord/aaa/StateMachine.java
@@ -149,6 +149,29 @@
}
/**
+ * Deletes authentication state machine records for a given MAC address.
+ * @param mac mac address of the suppliant who's state machine should be removed
+ */
+ public static void deleteByMac(MacAddress mac) {
+
+ // Walk the map from session IDs to state machines looking for a MAC match
+ for (Map.Entry<String, StateMachine> e : sessionIdMap.entrySet()) {
+
+ // If a MAC match is found then delete the entry from the session ID
+ // and identifier map as well as call delete identifier to clean up
+ // the identifier bit set.
+ if (e.getValue() != null && e.getValue().supplicantAddress != null &&
+ e.getValue().supplicantAddress.equals(mac)) {
+ sessionIdMap.remove(e.getValue().sessionId);
+ if (e.getValue().identifier != -1) {
+ deleteStateMachineMapping(e.getValue());
+ }
+ break;
+ }
+ }
+ }
+
+ /**
* State Machine Constructor.
*
* @param sessionId session Id represented by the switch dpid + port number
@@ -302,7 +325,6 @@
this.username = username;
}
-
/**
* Gets the username.
*
diff --git a/src/test/java/org/opencord/aaa/StateMachineTest.java b/src/test/java/org/opencord/aaa/StateMachineTest.java
index 3f95134..3d4ea27 100644
--- a/src/test/java/org/opencord/aaa/StateMachineTest.java
+++ b/src/test/java/org/opencord/aaa/StateMachineTest.java
@@ -20,8 +20,11 @@
import org.junit.Before;
import org.junit.Test;
+import org.onlab.packet.MacAddress;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertNotNull;
import org.onlab.packet.VlanId;
@@ -258,4 +261,42 @@
assertEquals(stateMachine2,
StateMachine.lookupStateMachineById(stateMachine2.identifier()));
}
+
+ @Test
+ /**
+ * Test state machine deletes
+ */
+ public void testStateMachineReset() throws StateMachineException {
+
+ int count = 256;
+
+ //StateMachine.initializeMaps();
+ StateMachine.lookupStateMachineById((byte) 1);
+
+ // Instantiate a bunch of state machines
+ for (int i = 0; i < count; i += 1) {
+ String mac = String.format("00:00:00:00:00:%02x", i);
+ StateMachine sm = new StateMachine(mac, VlanId.vlanId((short) i));
+ sm.start();
+ sm.setSupplicantAddress(MacAddress.valueOf(mac));
+ }
+
+ // Verify all state machines with a "even" MAC exist
+ for (int i = 0; i < count; i += 2) {
+ String mac = String.format("00:00:00:00:00:%02x", i);
+ assertNotNull(StateMachine.lookupStateMachineBySessionId(mac));
+ }
+
+ // Delete all state machines with a "even" MAC
+ for (int i = 0; i < count; i += 2) {
+ String mac = String.format("00:00:00:00:00:%02x", i);
+ StateMachine.deleteByMac(MacAddress.valueOf(mac));
+ }
+
+ // Verify all the delete state machines no long exist
+ for (int i = 0; i < count; i += 2) {
+ String mac = String.format("00:00:00:00:00:%02x", i);
+ assertNull(StateMachine.lookupStateMachineBySessionId(mac));
+ }
+ }
}