Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | */ |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 16 | package org.opencord.dhcpl2relay.cli; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 17 | |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Command; |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.net.DeviceIdCompleter; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 26 | import org.opencord.dhcpl2relay.DhcpL2RelayService; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 27 | |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 28 | import java.util.Map; |
| 29 | import java.util.stream.Collectors; |
| 30 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 31 | /** |
| 32 | * Shows the Successful DHCP allocations relayed by the dhcpl2relay. |
| 33 | */ |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 34 | @Service |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 35 | @Command(scope = "onos", name = "dhcpl2relay-allocations", |
| 36 | description = "Shows the Successful DHCP allocations relayed by the dhcpl2relay") |
| 37 | public class DhcpL2RelayAllocationsCommand extends AbstractShellCommand { |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 38 | |
| 39 | @Argument(index = 0, name = "deviceId", description = "Access device ID") |
| 40 | @Completion(DeviceIdCompleter.class) |
| 41 | private String strDeviceId = null; |
| 42 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 43 | @Override |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 44 | protected void doExecute() { |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 45 | DhcpL2RelayService service = get(DhcpL2RelayService.class); |
| 46 | |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 47 | Map<String, DhcpAllocationInfo> allocations = service.getAllocationInfo(); |
| 48 | |
| 49 | if (strDeviceId != null && !strDeviceId.isEmpty()) { |
| 50 | DeviceId deviceId = DeviceId.deviceId(strDeviceId); |
| 51 | allocations = allocations.entrySet().stream() |
| 52 | .filter(a -> a.getValue().location().deviceId().equals(deviceId)) |
| 53 | .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); |
| 54 | } |
| 55 | |
| 56 | allocations.forEach((key, value) -> { |
Saurav Das | 87a73c6 | 2020-10-04 18:44:40 -0700 | [diff] [blame] | 57 | print("SubscriberId=%s,ConnectPoint=%s,State=%s,MAC=%s,VLAN=%s," |
Saurav Das | a94531f | 2020-09-12 14:49:27 -0700 | [diff] [blame] | 58 | + "CircuitId=%s,IP Allocated=%s,Allocation Timestamp=%s", |
| 59 | value.subscriberId(), value.location(), value.type(), |
| 60 | value.macAddress().toString(), value.vlanId().toString(), |
| 61 | value.circuitId(), value.ipAddress().getIp4Address().toString(), |
| 62 | value.allocationTime().toString()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 63 | }); |
| 64 | } |
| 65 | } |