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 | |
Matteo Scandolo | 120d40b | 2020-11-25 15:49:22 -0800 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Command; |
Matteo Scandolo | 120d40b | 2020-11-25 15:49:22 -0800 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 22 | import org.onlab.util.Tools; |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Matteo Scandolo | 120d40b | 2020-11-25 15:49:22 -0800 | [diff] [blame] | 24 | import org.onosproject.cli.net.DeviceIdCompleter; |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 25 | import org.onosproject.net.AnnotationKeys; |
Matteo Scandolo | 120d40b | 2020-11-25 15:49:22 -0800 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Matteo Scandolo | 13b2812 | 2020-05-01 10:08:07 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Port; |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 29 | import org.onosproject.utils.Comparators; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 30 | import org.opencord.aaa.AuthenticationRecord; |
| 31 | import org.opencord.aaa.AuthenticationService; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 32 | import org.opencord.sadis.SadisService; |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 33 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 34 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 35 | import java.util.Comparator; |
| 36 | import java.util.List; |
Matteo Scandolo | 120d40b | 2020-11-25 15:49:22 -0800 | [diff] [blame] | 37 | import java.util.stream.Collectors; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 38 | |
| 39 | import static com.google.common.collect.Lists.newArrayList; |
| 40 | |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 41 | /** |
| 42 | * Shows the users in the aaa. |
| 43 | */ |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 44 | @Service |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 45 | @Command(scope = "onos", name = "aaa-users", |
| 46 | description = "Shows the aaa users") |
| 47 | public class AaaShowUsersCommand extends AbstractShellCommand { |
Matteo Scandolo | 13b2812 | 2020-05-01 10:08:07 -0700 | [diff] [blame] | 48 | |
Matteo Scandolo | 120d40b | 2020-11-25 15:49:22 -0800 | [diff] [blame] | 49 | @Argument(index = 0, name = "deviceId", description = "Access device ID") |
| 50 | @Completion(DeviceIdCompleter.class) |
| 51 | private String strDeviceId = null; |
| 52 | |
Matteo Scandolo | 13b2812 | 2020-05-01 10:08:07 -0700 | [diff] [blame] | 53 | static final String UNKNOWN = "UNKNOWN"; |
| 54 | |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 55 | @Override |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 56 | protected void doExecute() { |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 57 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 58 | final Comparator<AuthenticationRecord> authenticationRecordComparator = |
| 59 | (a1, a2) -> Comparators.CONNECT_POINT_COMPARATOR. |
| 60 | compare(a1.supplicantConnectPoint(), a2.supplicantConnectPoint()); |
| 61 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 62 | DeviceService devService = get(DeviceService.class); |
| 63 | SadisService sadisService = get(SadisService.class); |
| 64 | AuthenticationService authService = get(AuthenticationService.class); |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 65 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 66 | List<AuthenticationRecord> authentications = newArrayList(authService.getAuthenticationRecords()); |
| 67 | |
| 68 | authentications.sort(authenticationRecordComparator); |
| 69 | |
Matteo Scandolo | 120d40b | 2020-11-25 15:49:22 -0800 | [diff] [blame] | 70 | if (strDeviceId != null && !strDeviceId.isEmpty()) { |
| 71 | DeviceId deviceId = DeviceId.deviceId(strDeviceId); |
| 72 | authentications = authentications.stream() |
| 73 | .filter(a -> a.supplicantConnectPoint().deviceId().equals(deviceId)) |
| 74 | .collect(Collectors.toList()); |
| 75 | } |
| 76 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 77 | for (AuthenticationRecord auth : authentications) { |
Matteo Scandolo | 13b2812 | 2020-05-01 10:08:07 -0700 | [diff] [blame] | 78 | String username = UNKNOWN; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 79 | if (auth.username() != null) { |
| 80 | username = new String(auth.username()); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 81 | } |
Matteo Scandolo | 13b2812 | 2020-05-01 10:08:07 -0700 | [diff] [blame] | 82 | String mac = UNKNOWN; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 83 | if (auth.supplicantAddress() != null) { |
| 84 | mac = auth.supplicantAddress().toString(); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 85 | } |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 86 | |
Matteo Scandolo | 13b2812 | 2020-05-01 10:08:07 -0700 | [diff] [blame] | 87 | Port port = devService.getPort(auth.supplicantConnectPoint()); |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 88 | |
Matteo Scandolo | 13b2812 | 2020-05-01 10:08:07 -0700 | [diff] [blame] | 89 | String nasPortId = UNKNOWN; |
| 90 | |
| 91 | if (port != null) { |
| 92 | nasPortId = devService.getPort(auth.supplicantConnectPoint()). |
| 93 | annotations().value(AnnotationKeys.PORT_NAME); |
| 94 | } |
| 95 | |
Matteo Scandolo | 13b2812 | 2020-05-01 10:08:07 -0700 | [diff] [blame] | 96 | String subsId = UNKNOWN; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 97 | SubscriberAndDeviceInformation subscriber = sadisService.getSubscriberInfoService().get(nasPortId); |
Amit Ghosh | 6bf21a2 | 2017-08-23 12:45:09 +0100 | [diff] [blame] | 98 | if (subscriber != null) { |
| 99 | subsId = subscriber.nasPortId(); |
| 100 | } |
| 101 | |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 102 | print("%s: %s, last-changed=%s, mac=%s, subid=%s, username=%s", |
| 103 | auth.supplicantConnectPoint(), auth.state(), Tools.timeAgo(auth.lastChanged()), |
| 104 | mac, subsId, username); |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |