blob: 3f2ad29d513091a868307a486fe2c0a0265338c2 [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
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 */
16package org.onosproject.dhcp.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.MacAddress;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.dhcp.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")
30public 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}