blob: c56226139608de8cf93533ceaee3bee13c22b90d [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
Carmelo Cascone58b53292019-09-30 12:35:31 -070018import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.lifecycle.Service;
Jonathan Hart9d1ce802020-01-28 10:45:08 -080020import org.onlab.util.Tools;
Qianqian Hu61a6a402016-02-16 15:18:05 +080021import org.onosproject.cli.AbstractShellCommand;
Amit Ghosh6bf21a22017-08-23 12:45:09 +010022import org.onosproject.net.AnnotationKeys;
23import org.onosproject.net.device.DeviceService;
Jonathan Hartc41227c2020-01-28 16:56:49 -080024import org.onosproject.utils.Comparators;
Jonathan Hart612651f2019-11-25 09:21:43 -080025import org.opencord.aaa.AuthenticationRecord;
26import org.opencord.aaa.AuthenticationService;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000027import org.opencord.sadis.SadisService;
Amit Ghosh6bf21a22017-08-23 12:45:09 +010028import org.opencord.sadis.SubscriberAndDeviceInformation;
Qianqian Hu61a6a402016-02-16 15:18:05 +080029
Jonathan Hartc41227c2020-01-28 16:56:49 -080030import java.util.Comparator;
31import java.util.List;
32
33import static com.google.common.collect.Lists.newArrayList;
34
Qianqian Hu61a6a402016-02-16 15:18:05 +080035/**
36 * Shows the users in the aaa.
37 */
Carmelo Cascone58b53292019-09-30 12:35:31 -070038@Service
Qianqian Hu61a6a402016-02-16 15:18:05 +080039@Command(scope = "onos", name = "aaa-users",
40 description = "Shows the aaa users")
41public class AaaShowUsersCommand extends AbstractShellCommand {
42 @Override
Carmelo Cascone58b53292019-09-30 12:35:31 -070043 protected void doExecute() {
Amit Ghosh6bf21a22017-08-23 12:45:09 +010044
Jonathan Hartc41227c2020-01-28 16:56:49 -080045 final Comparator<AuthenticationRecord> authenticationRecordComparator =
46 (a1, a2) -> Comparators.CONNECT_POINT_COMPARATOR.
47 compare(a1.supplicantConnectPoint(), a2.supplicantConnectPoint());
48
Jonathan Hart612651f2019-11-25 09:21:43 -080049 DeviceService devService = get(DeviceService.class);
50 SadisService sadisService = get(SadisService.class);
51 AuthenticationService authService = get(AuthenticationService.class);
Amit Ghosh6bf21a22017-08-23 12:45:09 +010052
Jonathan Hartc41227c2020-01-28 16:56:49 -080053 List<AuthenticationRecord> authentications = newArrayList(authService.getAuthenticationRecords());
54
55 authentications.sort(authenticationRecordComparator);
56
57 for (AuthenticationRecord auth : authentications) {
Amit Ghoshf739be52017-09-21 15:49:37 +010058 String username = "UNKNOWN";
Jonathan Hart612651f2019-11-25 09:21:43 -080059 if (auth.username() != null) {
60 username = new String(auth.username());
Amit Ghoshf739be52017-09-21 15:49:37 +010061 }
62 String mac = "UNKNOWN";
Jonathan Hart612651f2019-11-25 09:21:43 -080063 if (auth.supplicantAddress() != null) {
64 mac = auth.supplicantAddress().toString();
Amit Ghoshf739be52017-09-21 15:49:37 +010065 }
Amit Ghosh6bf21a22017-08-23 12:45:09 +010066
Jonathan Hart612651f2019-11-25 09:21:43 -080067 String nasPortId = devService.getPort(auth.supplicantConnectPoint()).
Amit Ghosh6bf21a22017-08-23 12:45:09 +010068 annotations().value(AnnotationKeys.PORT_NAME);
69
Amit Ghoshf739be52017-09-21 15:49:37 +010070 String subsId = "UNKNOWN";
Gamze Abaka1cfdb192018-10-25 11:39:19 +000071 SubscriberAndDeviceInformation subscriber = sadisService.getSubscriberInfoService().get(nasPortId);
Amit Ghosh6bf21a22017-08-23 12:45:09 +010072 if (subscriber != null) {
73 subsId = subscriber.nasPortId();
74 }
75
Jonathan Hart9d1ce802020-01-28 10:45:08 -080076 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 Hu61a6a402016-02-16 15:18:05 +080079 }
80 }
81}