Simon Hunt | 439d6d8 | 2017-10-31 14:55:09 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.opencord.aaa.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Command; |
| 19 | import org.onosproject.cli.AbstractShellCommand; |
| 20 | import org.onosproject.net.AnnotationKeys; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.Port; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | import org.opencord.aaa.api.AaaService; |
| 25 | import org.opencord.aaa.api.AaaSession; |
| 26 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 27 | import org.opencord.sadis.SubscriberAndDeviceInformationService; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * Shows the users in the aaa. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "aaa-users", |
| 36 | description = "Displays the users with current AAA sessions") |
| 37 | public class AaaShowUsersCommand extends AbstractShellCommand { |
| 38 | |
| 39 | private static final String UNKNOWN = "UNKNOWN"; |
| 40 | |
| 41 | @Override |
| 42 | protected void execute() { |
| 43 | DeviceService devService = get(DeviceService.class); |
| 44 | SubscriberAndDeviceInformationService subsService = |
| 45 | get(SubscriberAndDeviceInformationService.class); |
| 46 | AaaService aaaService = get(AaaService.class); |
| 47 | |
| 48 | // TODO: add currentSessions() to AaaService API |
| 49 | // List<AaaSession> sessionList = aaaService.currentSessions(); |
| 50 | List<AaaSession> sessionList = new ArrayList<>(); |
| 51 | |
| 52 | for (AaaSession s : sessionList) { |
| 53 | String subsId = getSubscriberId(devService, subsService, s.getConnectPoint()); |
| 54 | print("UserName=%s,CurrentState=%s,DeviceId=%s,MAC=%s,PortNumber=%s,SubscriberId=%s", |
| 55 | s.username(), s.state(), s.deviceId(), s.macAddress(), s.portNumber(), subsId); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private String getSubscriberId(DeviceService devService, |
| 60 | SubscriberAndDeviceInformationService sadis, |
| 61 | ConnectPoint cp) { |
| 62 | Port p = devService.getPort(cp); |
| 63 | String nasPortId = p.annotations().value(AnnotationKeys.PORT_NAME); |
| 64 | SubscriberAndDeviceInformation subscriber = sadis.get(nasPortId); |
| 65 | return (subscriber == null) ? UNKNOWN : subscriber.nasPortId(); |
| 66 | } |
| 67 | } |