Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [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.maclearner.app.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.Completion; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.opencord.maclearner.api.MacLearnerKey; |
| 24 | import org.opencord.maclearner.api.MacLearnerService; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | |
| 30 | import java.util.Map; |
| 31 | import 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") |
| 39 | public 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)); |
| 72 | if (!macAddress.isPresent()) { |
| 73 | 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 | } |