blob: 0f066d3455da0ad4d5b9debd18ab8d8b72c99532 [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;
Matteo Scandolo13b28122020-05-01 10:08:07 -070023import org.onosproject.net.Port;
Amit Ghosh6bf21a22017-08-23 12:45:09 +010024import org.onosproject.net.device.DeviceService;
Jonathan Hartc41227c2020-01-28 16:56:49 -080025import org.onosproject.utils.Comparators;
Jonathan Hart612651f2019-11-25 09:21:43 -080026import org.opencord.aaa.AuthenticationRecord;
27import org.opencord.aaa.AuthenticationService;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000028import org.opencord.sadis.SadisService;
Amit Ghosh6bf21a22017-08-23 12:45:09 +010029import org.opencord.sadis.SubscriberAndDeviceInformation;
Qianqian Hu61a6a402016-02-16 15:18:05 +080030
Jonathan Hartc41227c2020-01-28 16:56:49 -080031import java.util.Comparator;
32import java.util.List;
33
34import static com.google.common.collect.Lists.newArrayList;
35
Qianqian Hu61a6a402016-02-16 15:18:05 +080036/**
37 * Shows the users in the aaa.
38 */
Carmelo Cascone58b53292019-09-30 12:35:31 -070039@Service
Qianqian Hu61a6a402016-02-16 15:18:05 +080040@Command(scope = "onos", name = "aaa-users",
41 description = "Shows the aaa users")
42public class AaaShowUsersCommand extends AbstractShellCommand {
Matteo Scandolo13b28122020-05-01 10:08:07 -070043
44 static final String UNKNOWN = "UNKNOWN";
45
Qianqian Hu61a6a402016-02-16 15:18:05 +080046 @Override
Carmelo Cascone58b53292019-09-30 12:35:31 -070047 protected void doExecute() {
Amit Ghosh6bf21a22017-08-23 12:45:09 +010048
Jonathan Hartc41227c2020-01-28 16:56:49 -080049 final Comparator<AuthenticationRecord> authenticationRecordComparator =
50 (a1, a2) -> Comparators.CONNECT_POINT_COMPARATOR.
51 compare(a1.supplicantConnectPoint(), a2.supplicantConnectPoint());
52
Jonathan Hart612651f2019-11-25 09:21:43 -080053 DeviceService devService = get(DeviceService.class);
54 SadisService sadisService = get(SadisService.class);
55 AuthenticationService authService = get(AuthenticationService.class);
Amit Ghosh6bf21a22017-08-23 12:45:09 +010056
Jonathan Hartc41227c2020-01-28 16:56:49 -080057 List<AuthenticationRecord> authentications = newArrayList(authService.getAuthenticationRecords());
58
59 authentications.sort(authenticationRecordComparator);
60
61 for (AuthenticationRecord auth : authentications) {
Matteo Scandolo13b28122020-05-01 10:08:07 -070062 String username = UNKNOWN;
Jonathan Hart612651f2019-11-25 09:21:43 -080063 if (auth.username() != null) {
64 username = new String(auth.username());
Amit Ghoshf739be52017-09-21 15:49:37 +010065 }
Matteo Scandolo13b28122020-05-01 10:08:07 -070066 String mac = UNKNOWN;
Jonathan Hart612651f2019-11-25 09:21:43 -080067 if (auth.supplicantAddress() != null) {
68 mac = auth.supplicantAddress().toString();
Amit Ghoshf739be52017-09-21 15:49:37 +010069 }
Amit Ghosh6bf21a22017-08-23 12:45:09 +010070
Matteo Scandolo13b28122020-05-01 10:08:07 -070071 Port port = devService.getPort(auth.supplicantConnectPoint());
Amit Ghosh6bf21a22017-08-23 12:45:09 +010072
Matteo Scandolo13b28122020-05-01 10:08:07 -070073 String nasPortId = UNKNOWN;
74
75 if (port != null) {
76 nasPortId = devService.getPort(auth.supplicantConnectPoint()).
77 annotations().value(AnnotationKeys.PORT_NAME);
78 }
79
80
81 String subsId = UNKNOWN;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000082 SubscriberAndDeviceInformation subscriber = sadisService.getSubscriberInfoService().get(nasPortId);
Amit Ghosh6bf21a22017-08-23 12:45:09 +010083 if (subscriber != null) {
84 subsId = subscriber.nasPortId();
85 }
86
Jonathan Hart9d1ce802020-01-28 10:45:08 -080087 print("%s: %s, last-changed=%s, mac=%s, subid=%s, username=%s",
88 auth.supplicantConnectPoint(), auth.state(), Tools.timeAgo(auth.lastChanged()),
89 mac, subsId, username);
Qianqian Hu61a6a402016-02-16 15:18:05 +080090 }
91 }
92}