blob: 1455e2f027ed8c85aeaa0621be791469d3fc3ed0 [file] [log] [blame]
Amit Ghosha17354e2017-08-23 12:56:04 +01001/*
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 Casconede1e6e32019-07-15 19:39:08 -070016package org.opencord.dhcpl2relay.cli;
Amit Ghosha17354e2017-08-23 12:56:04 +010017
Matteo Scandoloabcc73e2020-11-25 16:02:17 -080018import org.apache.karaf.shell.api.action.Argument;
Carmelo Casconede1e6e32019-07-15 19:39:08 -070019import org.apache.karaf.shell.api.action.Command;
Matteo Scandoloabcc73e2020-11-25 16:02:17 -080020import org.apache.karaf.shell.api.action.Completion;
Carmelo Casconede1e6e32019-07-15 19:39:08 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Amit Ghosha17354e2017-08-23 12:56:04 +010022import org.onosproject.cli.AbstractShellCommand;
Matteo Scandoloabcc73e2020-11-25 16:02:17 -080023import org.onosproject.cli.net.DeviceIdCompleter;
24import org.onosproject.net.DeviceId;
25import org.opencord.dhcpl2relay.DhcpAllocationInfo;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080026import org.opencord.dhcpl2relay.DhcpL2RelayService;
Amit Ghosha17354e2017-08-23 12:56:04 +010027
Matteo Scandoloabcc73e2020-11-25 16:02:17 -080028import java.util.Map;
29import java.util.stream.Collectors;
30
Amit Ghosha17354e2017-08-23 12:56:04 +010031/**
32 * Shows the Successful DHCP allocations relayed by the dhcpl2relay.
33 */
Carmelo Casconede1e6e32019-07-15 19:39:08 -070034@Service
Amit Ghosha17354e2017-08-23 12:56:04 +010035@Command(scope = "onos", name = "dhcpl2relay-allocations",
36 description = "Shows the Successful DHCP allocations relayed by the dhcpl2relay")
37public class DhcpL2RelayAllocationsCommand extends AbstractShellCommand {
Matteo Scandoloabcc73e2020-11-25 16:02:17 -080038
39 @Argument(index = 0, name = "deviceId", description = "Access device ID")
40 @Completion(DeviceIdCompleter.class)
41 private String strDeviceId = null;
42
Amit Ghosha17354e2017-08-23 12:56:04 +010043 @Override
Carmelo Casconede1e6e32019-07-15 19:39:08 -070044 protected void doExecute() {
Jonathan Hart617bc3e2020-02-14 10:42:23 -080045 DhcpL2RelayService service = get(DhcpL2RelayService.class);
46
Matteo Scandoloabcc73e2020-11-25 16:02:17 -080047 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 Das87a73c62020-10-04 18:44:40 -070057 print("SubscriberId=%s,ConnectPoint=%s,State=%s,MAC=%s,VLAN=%s,"
Saurav Dasa94531f2020-09-12 14:49:27 -070058 + "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 Ghosha17354e2017-08-23 12:56:04 +010063 });
64 }
65}