Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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 | |
| 17 | package org.opencord.olt.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.Completion; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.cli.net.DeviceIdCompleter; |
| 25 | import org.onosproject.cli.net.PortNumberCompleter; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.opencord.olt.impl.OltFlowServiceInterface; |
| 29 | import org.opencord.olt.impl.OltPortStatus; |
| 30 | import org.opencord.olt.impl.ServiceKey; |
| 31 | import org.opencord.sadis.UniTagInformation; |
| 32 | |
| 33 | import java.util.HashMap; |
| 34 | import java.util.Map; |
| 35 | |
| 36 | /** |
| 37 | * Shows ports' status of an OLT. |
| 38 | */ |
| 39 | @Service |
| 40 | @Command(scope = "onos", name = "volt-port-status", |
| 41 | description = "Shows information about the OLT ports (default EAPOL, subscriber flows)") |
| 42 | public class ShowPortStatus extends AbstractShellCommand { |
| 43 | |
| 44 | @Argument(index = 0, name = "deviceId", description = "Access device ID", |
| 45 | required = false, multiValued = false) |
| 46 | @Completion(DeviceIdCompleter.class) |
| 47 | private String strDeviceId = null; |
| 48 | |
| 49 | @Argument(index = 1, name = "port", description = "Subscriber port number", |
| 50 | required = false, multiValued = false) |
| 51 | @Completion(PortNumberCompleter.class) |
| 52 | private String strPort = null; |
| 53 | |
| 54 | @Override |
| 55 | protected void doExecute() { |
| 56 | |
| 57 | OltFlowServiceInterface service = AbstractShellCommand.get(OltFlowServiceInterface.class); |
| 58 | Map<ServiceKey, OltPortStatus> connectPointStatus = service.getConnectPointStatus(); |
| 59 | if (connectPointStatus.isEmpty()) { |
| 60 | print("No ports handled by the olt app"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | Map<DeviceId, Map<PortNumber, Map<UniTagInformation, OltPortStatus>>> sortedStatus = new HashMap<>(); |
| 65 | |
| 66 | DeviceId deviceId = strDeviceId != null ? DeviceId.deviceId(strDeviceId) : null; |
| 67 | PortNumber portNumber = strPort != null ? PortNumber.portNumber(strPort) : null; |
| 68 | |
| 69 | connectPointStatus.forEach((sk, fs) -> { |
| 70 | if (deviceId != null && !deviceId.equals(sk.getPort().connectPoint() |
| 71 | .deviceId())) { |
| 72 | return; |
| 73 | } |
| 74 | if (portNumber != null && !portNumber.equals(sk.getPort().connectPoint().port())) { |
| 75 | return; |
| 76 | } |
| 77 | sortedStatus.compute(sk.getPort().connectPoint().deviceId(), (id, portMap) -> { |
| 78 | if (portMap == null) { |
| 79 | portMap = new HashMap<>(); |
| 80 | } |
| 81 | portMap.compute(sk.getPort().connectPoint().port(), (id2, ps) -> { |
| 82 | if (ps == null) { |
| 83 | ps = new HashMap<>(); |
| 84 | } |
| 85 | ps.put(sk.getService(), fs); |
| 86 | return ps; |
| 87 | }); |
| 88 | |
| 89 | return portMap; |
| 90 | }); |
| 91 | }); |
| 92 | |
| 93 | sortedStatus.forEach(this::display); |
| 94 | } |
| 95 | |
| 96 | private void display(DeviceId deviceId, Map<PortNumber, Map<UniTagInformation, OltPortStatus>> ports) { |
| 97 | print("deviceId=%s, managedPorts=%d", deviceId, ports.size()); |
| 98 | |
| 99 | ports.forEach((port, subscribers) -> { |
| 100 | print("\tport=%s", port); |
| 101 | subscribers.forEach((uti, status) -> { |
| 102 | print("\t\tservice=%s defaultEapolStatus=%s subscriberFlowsStatus=%s dhcpStatus=%s", |
| 103 | uti.getServiceName(), status.defaultEapolStatus, |
| 104 | status.subscriberFlowsStatus, status.dhcpStatus); |
| 105 | }); |
| 106 | }); |
| 107 | } |
| 108 | } |