blob: 66fd4bcdd0fdd8012aa5ed82842ae88317eaf704 [file] [log] [blame]
Tunahan Sezen03e55272020-04-18 09:18:53 +00001/*
Joey Armstrong53fcac22023-01-11 13:25:01 -05002 * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors
Tunahan Sezen03e55272020-04-18 09:18:53 +00003 *
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 */
16package org.opencord.maclearner.app.cli;
17
18import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Completion;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onlab.packet.VlanId;
23import org.opencord.maclearner.api.MacLearnerKey;
24import org.opencord.maclearner.api.MacLearnerService;
25import org.onlab.packet.MacAddress;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29
30import java.util.Map;
31import java.util.Optional;
32
33/**
34 * Gets MAC Address information of client connected to requested device and port.
35 */
36@Service
37@Command(scope = "onos", name = "mac-learner-get-mapping",
38 description = "Gets MAC Address information of client connected to requested device and port")
39public class MacLearnerGetMapping extends AbstractShellCommand {
40
41 @Argument(index = 0, name = "deviceId",
42 description = "OpenFlow Device Id")
43 @Completion(MappedDeviceIdCompleter.class)
44 String devId = null;
45
46 @Argument(index = 1, name = "portNo",
47 description = "Integer value of Port Number")
48 @Completion(MappedPortNumberCompleter.class)
49 Integer portNo;
50
51 @Argument(index = 2, name = "vlanId",
52 description = "Short value of Vlan Id")
53 Short vlanId;
54
55 @Override
56 protected void doExecute() {
57 try {
58 MacLearnerService macLearnerService = AbstractShellCommand.get(MacLearnerService.class);
59 if (portNo == null && devId == null && vlanId == null) {
60 Map<MacLearnerKey, MacAddress> mapMacAddressMap = macLearnerService.getAllMappings();
61 for (Map.Entry<MacLearnerKey, MacAddress> entry : mapMacAddressMap.entrySet()) {
62 print("Client with MAC: %s and VlanID: %s, uses port number: %s of device with id: %s",
63 entry.getValue(),
64 entry.getKey().getVlanId(),
65 entry.getKey().getPortNumber(),
66 entry.getKey().getDeviceId());
67 }
68 } else if (portNo != null && devId != null && vlanId != null) {
69 Optional<MacAddress> macAddress = macLearnerService.getMacMapping(DeviceId.deviceId(devId),
70 PortNumber.portNumber(portNo),
71 VlanId.vlanId(vlanId));
Andrea Campanellada3be772022-03-14 16:17:05 +010072 if (macAddress.isEmpty()) {
Tunahan Sezen03e55272020-04-18 09:18:53 +000073 print("MAC Address not found with given parameters.\nUse -1 for VlanId=None");
74 } else {
75 print(String.format("MAC: %s", macAddress.get()));
76 }
77 } else {
78 print("Either device id, port number and vlan id must be entered or not at all!");
79 }
80
81 } catch (IllegalArgumentException e) {
82 String msg = String.format("Exception occurred while executing %s command",
83 this.getClass().getSimpleName());
84 print(msg);
85 log.error(msg, e);
86 }
87 }
88
89}