samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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.onosproject.dhcpserver.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.dhcpserver.DHCPService; |
| 24 | |
| 25 | /** |
| 26 | * Registers a static MAC Address to IP Mapping with the DHCP Server. |
| 27 | */ |
| 28 | @Command(scope = "onos", name = "dhcp-set-static-mapping", |
| 29 | description = "Registers a static MAC Address to IP Mapping with the DHCP Server") |
| 30 | public class DHCPSetStaticMapping extends AbstractShellCommand { |
| 31 | |
| 32 | @Argument(index = 0, name = "macAddr", |
| 33 | description = "MAC Address of the client", |
| 34 | required = true, multiValued = false) |
| 35 | String macAddr = null; |
| 36 | |
| 37 | @Argument(index = 1, name = "ipAddr", |
| 38 | description = "IP Address requested for static mapping", |
| 39 | required = true, multiValued = false) |
| 40 | String ipAddr = null; |
| 41 | |
| 42 | private static final String DHCP_SUCCESS = "Static Mapping Successfully Added."; |
| 43 | private static final String DHCP_FAILURE = "Static Mapping Failed. The IP maybe unavailable."; |
| 44 | @Override |
| 45 | protected void execute() { |
| 46 | DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class); |
| 47 | |
| 48 | try { |
| 49 | MacAddress macID = MacAddress.valueOf(macAddr); |
| 50 | Ip4Address ipAddress = Ip4Address.valueOf(ipAddr); |
| 51 | if (dhcpService.setStaticMapping(macID, ipAddress)) { |
| 52 | print(DHCP_SUCCESS); |
| 53 | } else { |
| 54 | print(DHCP_FAILURE); |
| 55 | } |
| 56 | |
| 57 | } catch (IllegalArgumentException e) { |
| 58 | print(e.getMessage()); |
| 59 | } |
| 60 | } |
| 61 | } |