Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 4e33be2 | 2017-08-03 22:45:46 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 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 | */ |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 16 | package org.opencord.aaa.cli; |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 17 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Command; |
| 19 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 20 | import org.onlab.util.Tools; |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame^] | 24 | import org.onosproject.utils.Comparators; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 25 | import org.opencord.aaa.AuthenticationRecord; |
| 26 | import org.opencord.aaa.AuthenticationService; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 27 | import org.opencord.sadis.SadisService; |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 28 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 29 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame^] | 30 | import java.util.Comparator; |
| 31 | import java.util.List; |
| 32 | |
| 33 | import static com.google.common.collect.Lists.newArrayList; |
| 34 | |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 35 | /** |
| 36 | * Shows the users in the aaa. |
| 37 | */ |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 38 | @Service |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 39 | @Command(scope = "onos", name = "aaa-users", |
| 40 | description = "Shows the aaa users") |
| 41 | public class AaaShowUsersCommand extends AbstractShellCommand { |
| 42 | @Override |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 43 | protected void doExecute() { |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 44 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame^] | 45 | final Comparator<AuthenticationRecord> authenticationRecordComparator = |
| 46 | (a1, a2) -> Comparators.CONNECT_POINT_COMPARATOR. |
| 47 | compare(a1.supplicantConnectPoint(), a2.supplicantConnectPoint()); |
| 48 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 49 | DeviceService devService = get(DeviceService.class); |
| 50 | SadisService sadisService = get(SadisService.class); |
| 51 | AuthenticationService authService = get(AuthenticationService.class); |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 52 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame^] | 53 | List<AuthenticationRecord> authentications = newArrayList(authService.getAuthenticationRecords()); |
| 54 | |
| 55 | authentications.sort(authenticationRecordComparator); |
| 56 | |
| 57 | for (AuthenticationRecord auth : authentications) { |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 58 | String username = "UNKNOWN"; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 59 | if (auth.username() != null) { |
| 60 | username = new String(auth.username()); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 61 | } |
| 62 | String mac = "UNKNOWN"; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 63 | if (auth.supplicantAddress() != null) { |
| 64 | mac = auth.supplicantAddress().toString(); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 65 | } |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 66 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 67 | String nasPortId = devService.getPort(auth.supplicantConnectPoint()). |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 68 | annotations().value(AnnotationKeys.PORT_NAME); |
| 69 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 70 | String subsId = "UNKNOWN"; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 71 | SubscriberAndDeviceInformation subscriber = sadisService.getSubscriberInfoService().get(nasPortId); |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 72 | if (subscriber != null) { |
| 73 | subsId = subscriber.nasPortId(); |
| 74 | } |
| 75 | |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 76 | print("%s: %s, last-changed=%s, mac=%s, subid=%s, username=%s", |
| 77 | auth.supplicantConnectPoint(), auth.state(), Tools.timeAgo(auth.lastChanged()), |
| 78 | mac, subsId, username); |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |