blob: e1ce890472cf951f78e564c5a13b2462f0e27e19 [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
danielcd9deed2015-10-30 17:16:16 +090018import com.google.common.collect.Lists;
samanwita palf28207b2015-09-04 10:41:56 -070019import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.MacAddress;
23import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070024import org.onosproject.dhcp.DhcpService;
samanwita palf28207b2015-09-04 10:41:56 -070025
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 Vachuskaa1da42e2015-09-09 00:45:22 -070031public class DhcpSetStaticMapping extends AbstractShellCommand {
samanwita palf28207b2015-09-04 10:41:56 -070032
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 Vachuskaa1da42e2015-09-09 00:45:22 -070047 DhcpService dhcpService = AbstractShellCommand.get(DhcpService.class);
samanwita palf28207b2015-09-04 10:41:56 -070048
49 try {
50 MacAddress macID = MacAddress.valueOf(macAddr);
51 Ip4Address ipAddress = Ip4Address.valueOf(ipAddr);
danielcd9deed2015-10-30 17:16:16 +090052 if (dhcpService.setStaticMapping(macID, ipAddress, false, Lists.newArrayList())) {
samanwita palf28207b2015-09-04 10:41:56 -070053 print(DHCP_SUCCESS);
54 } else {
55 print(DHCP_FAILURE);
56 }
57
58 } catch (IllegalArgumentException e) {
59 print(e.getMessage());
60 }
61 }
62}