blob: e484ffa52e07f6f0175b3421eca9c3a62907490f [file] [log] [blame]
Qianqian Hu61a6a402016-02-16 15:18:05 +08001/*
Brian O'Connor4e33be22017-08-03 22:45:46 -07002 * Copyright 2016-present Open Networking Foundation
Qianqian Hu61a6a402016-02-16 15:18:05 +08003 *
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 Cascone58b53292019-09-30 12:35:31 -070016package org.opencord.aaa.cli;
Qianqian Hu61a6a402016-02-16 15:18:05 +080017
Matteo Scandolo120d40b2020-11-25 15:49:22 -080018import org.apache.karaf.shell.api.action.Argument;
Carmelo Cascone58b53292019-09-30 12:35:31 -070019import org.apache.karaf.shell.api.action.Command;
Matteo Scandolo120d40b2020-11-25 15:49:22 -080020import org.apache.karaf.shell.api.action.Completion;
Carmelo Cascone58b53292019-09-30 12:35:31 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jonathan Hart9d1ce802020-01-28 10:45:08 -080022import org.onlab.util.Tools;
Qianqian Hu61a6a402016-02-16 15:18:05 +080023import org.onosproject.cli.AbstractShellCommand;
Matteo Scandolo120d40b2020-11-25 15:49:22 -080024import org.onosproject.cli.net.DeviceIdCompleter;
Amit Ghosh6bf21a22017-08-23 12:45:09 +010025import org.onosproject.net.AnnotationKeys;
Matteo Scandolo120d40b2020-11-25 15:49:22 -080026import org.onosproject.net.DeviceId;
Matteo Scandolo13b28122020-05-01 10:08:07 -070027import org.onosproject.net.Port;
Amit Ghosh6bf21a22017-08-23 12:45:09 +010028import org.onosproject.net.device.DeviceService;
Jonathan Hartc41227c2020-01-28 16:56:49 -080029import org.onosproject.utils.Comparators;
Jonathan Hart612651f2019-11-25 09:21:43 -080030import org.opencord.aaa.AuthenticationRecord;
31import org.opencord.aaa.AuthenticationService;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000032import org.opencord.sadis.SadisService;
Amit Ghosh6bf21a22017-08-23 12:45:09 +010033import org.opencord.sadis.SubscriberAndDeviceInformation;
Qianqian Hu61a6a402016-02-16 15:18:05 +080034
Jonathan Hartc41227c2020-01-28 16:56:49 -080035import java.util.Comparator;
36import java.util.List;
Matteo Scandolo120d40b2020-11-25 15:49:22 -080037import java.util.stream.Collectors;
Jonathan Hartc41227c2020-01-28 16:56:49 -080038
39import static com.google.common.collect.Lists.newArrayList;
40
Qianqian Hu61a6a402016-02-16 15:18:05 +080041/**
42 * Shows the users in the aaa.
43 */
Carmelo Cascone58b53292019-09-30 12:35:31 -070044@Service
Qianqian Hu61a6a402016-02-16 15:18:05 +080045@Command(scope = "onos", name = "aaa-users",
46 description = "Shows the aaa users")
47public class AaaShowUsersCommand extends AbstractShellCommand {
Matteo Scandolo13b28122020-05-01 10:08:07 -070048
Matteo Scandolo120d40b2020-11-25 15:49:22 -080049 @Argument(index = 0, name = "deviceId", description = "Access device ID")
50 @Completion(DeviceIdCompleter.class)
51 private String strDeviceId = null;
52
Matteo Scandolo13b28122020-05-01 10:08:07 -070053 static final String UNKNOWN = "UNKNOWN";
54
Qianqian Hu61a6a402016-02-16 15:18:05 +080055 @Override
Carmelo Cascone58b53292019-09-30 12:35:31 -070056 protected void doExecute() {
Amit Ghosh6bf21a22017-08-23 12:45:09 +010057
Jonathan Hartc41227c2020-01-28 16:56:49 -080058 final Comparator<AuthenticationRecord> authenticationRecordComparator =
59 (a1, a2) -> Comparators.CONNECT_POINT_COMPARATOR.
60 compare(a1.supplicantConnectPoint(), a2.supplicantConnectPoint());
61
Jonathan Hart612651f2019-11-25 09:21:43 -080062 DeviceService devService = get(DeviceService.class);
63 SadisService sadisService = get(SadisService.class);
64 AuthenticationService authService = get(AuthenticationService.class);
Amit Ghosh6bf21a22017-08-23 12:45:09 +010065
Jonathan Hartc41227c2020-01-28 16:56:49 -080066 List<AuthenticationRecord> authentications = newArrayList(authService.getAuthenticationRecords());
67
68 authentications.sort(authenticationRecordComparator);
69
Matteo Scandolo120d40b2020-11-25 15:49:22 -080070 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 Hartc41227c2020-01-28 16:56:49 -080077 for (AuthenticationRecord auth : authentications) {
Matteo Scandolo13b28122020-05-01 10:08:07 -070078 String username = UNKNOWN;
Jonathan Hart612651f2019-11-25 09:21:43 -080079 if (auth.username() != null) {
80 username = new String(auth.username());
Amit Ghoshf739be52017-09-21 15:49:37 +010081 }
Matteo Scandolo13b28122020-05-01 10:08:07 -070082 String mac = UNKNOWN;
Jonathan Hart612651f2019-11-25 09:21:43 -080083 if (auth.supplicantAddress() != null) {
84 mac = auth.supplicantAddress().toString();
Amit Ghoshf739be52017-09-21 15:49:37 +010085 }
Amit Ghosh6bf21a22017-08-23 12:45:09 +010086
Matteo Scandolo13b28122020-05-01 10:08:07 -070087 Port port = devService.getPort(auth.supplicantConnectPoint());
Amit Ghosh6bf21a22017-08-23 12:45:09 +010088
Matteo Scandolo13b28122020-05-01 10:08:07 -070089 String nasPortId = UNKNOWN;
90
91 if (port != null) {
92 nasPortId = devService.getPort(auth.supplicantConnectPoint()).
93 annotations().value(AnnotationKeys.PORT_NAME);
94 }
95
Matteo Scandolo13b28122020-05-01 10:08:07 -070096 String subsId = UNKNOWN;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000097 SubscriberAndDeviceInformation subscriber = sadisService.getSubscriberInfoService().get(nasPortId);
Amit Ghosh6bf21a22017-08-23 12:45:09 +010098 if (subscriber != null) {
99 subsId = subscriber.nasPortId();
100 }
101
Jonathan Hart9d1ce802020-01-28 10:45:08 -0800102 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 Hu61a6a402016-02-16 15:18:05 +0800105 }
106 }
107}