Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | 53fcac2 | 2023-01-11 13:25:01 -0500 | [diff] [blame^] | 2 | * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 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.MacDeleteResult; |
| 24 | import org.opencord.maclearner.api.MacLearnerService; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | |
| 29 | /** |
| 30 | * Deletes MAC Address information of client connected to requested device and port. |
| 31 | */ |
| 32 | @Service |
| 33 | @Command(scope = "onos", name = "mac-learner-delete-mapping", |
| 34 | description = "Deletes MAC Address information of client connected to requested device and port") |
| 35 | public class MacLearnerDeleteMapping extends AbstractShellCommand { |
| 36 | |
| 37 | @Argument(index = 0, name = "deviceId", |
| 38 | description = "OpenFlow Device Id", |
| 39 | required = true) |
| 40 | @Completion(MappedDeviceIdCompleter.class) |
| 41 | String devId = null; |
| 42 | |
| 43 | @Argument(index = 1, name = "portNo", |
| 44 | description = "Integer value of Port Number", |
| 45 | required = true) |
| 46 | @Completion(MappedPortNumberCompleter.class) |
| 47 | Integer portNo; |
| 48 | |
| 49 | @Argument(index = 2, name = "vlanId", |
| 50 | description = "Short value of Vlan Id", |
| 51 | required = true) |
| 52 | Short vlanId; |
| 53 | |
| 54 | private static final String DELETE_MAPPING_SUCCESS = "Mac Mapping Successfully Deleted."; |
| 55 | private static final String DELETE_MAPPING_FAILURE = "Mac Mapping Deletion Failed."; |
| 56 | private static final String MAPPING_NOT_FOUND = "Mac Mapping requested to delete is not found."; |
| 57 | |
| 58 | @Override |
| 59 | protected void doExecute() { |
| 60 | MacLearnerService macLearnerService = AbstractShellCommand.get(MacLearnerService.class); |
| 61 | try { |
| 62 | if (portNo == null || devId == null || vlanId == null) { |
| 63 | throw new IllegalArgumentException(); |
| 64 | } |
| 65 | |
| 66 | MacDeleteResult result = macLearnerService.deleteMacMapping(DeviceId.deviceId(devId), |
| 67 | PortNumber.portNumber(portNo), |
| 68 | VlanId.vlanId(vlanId)); |
| 69 | switch (result) { |
| 70 | case SUCCESSFUL: |
| 71 | print(DELETE_MAPPING_SUCCESS); |
| 72 | break; |
| 73 | case NOT_EXIST: |
| 74 | print(MAPPING_NOT_FOUND); |
| 75 | break; |
| 76 | case UNSUCCESSFUL: |
| 77 | print(DELETE_MAPPING_FAILURE); |
| 78 | break; |
| 79 | default: |
| 80 | throw new IllegalArgumentException(); |
| 81 | } |
| 82 | |
| 83 | } catch (IllegalArgumentException e) { |
| 84 | String msg = String.format("Exception occurred while executing %s command", |
| 85 | this.getClass().getSimpleName()); |
| 86 | print(msg); |
| 87 | log.error(msg, e); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } |