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