Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 17 | package org.onosproject.cli.net; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.incubator.net.intf.Interface; |
| 26 | import org.onosproject.incubator.net.intf.InterfaceAdminService; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.host.InterfaceIpAddress; |
| 29 | |
| 30 | import java.util.Set; |
| 31 | |
| 32 | /** |
| 33 | * Adds a new interface configuration. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "add-interface", |
| 36 | description = "Adds a new configured interface") |
| 37 | public class InterfaceAddCommand extends AbstractShellCommand { |
| 38 | |
| 39 | @Option(name = "-c", aliases = "--connectPoint", |
| 40 | description = "Device port that the interface is associated with", |
| 41 | required = true, multiValued = false) |
| 42 | private String connectPoint = null; |
| 43 | |
| 44 | @Option(name = "-m", aliases = "--mac", |
| 45 | description = "MAC address of the interface", |
Luca Prete | e3879f7 | 2015-10-16 11:19:53 +0200 | [diff] [blame] | 46 | required = false, multiValued = false) |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 47 | private String mac = null; |
| 48 | |
| 49 | @Option(name = "-i", aliases = "--ip", |
| 50 | description = "IP address configured on the interface\n" + |
| 51 | "(e.g. 10.0.1.1/24). Can be specified multiple times.", |
| 52 | required = false, multiValued = true) |
| 53 | private String[] ips = null; |
| 54 | |
| 55 | @Option(name = "-v", aliases = "--vlan", |
| 56 | description = "VLAN configured on the interface", |
| 57 | required = false, multiValued = false) |
| 58 | private String vlan = null; |
| 59 | |
| 60 | @Override |
| 61 | protected void execute() { |
| 62 | InterfaceAdminService interfaceService = get(InterfaceAdminService.class); |
| 63 | |
| 64 | Set<InterfaceIpAddress> ipAddresses = Sets.newHashSet(); |
| 65 | if (ips != null) { |
| 66 | for (String strIp : ips) { |
| 67 | ipAddresses.add(InterfaceIpAddress.valueOf(strIp)); |
| 68 | } |
| 69 | } |
| 70 | |
Luca Prete | e3879f7 | 2015-10-16 11:19:53 +0200 | [diff] [blame] | 71 | MacAddress macAddr = mac == null ? null : MacAddress.valueOf(mac); |
| 72 | |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 73 | VlanId vlanId = vlan == null ? VlanId.NONE : VlanId.vlanId(Short.parseShort(vlan)); |
| 74 | |
| 75 | Interface intf = new Interface(ConnectPoint.deviceConnectPoint(connectPoint), |
Luca Prete | e3879f7 | 2015-10-16 11:19:53 +0200 | [diff] [blame] | 76 | ipAddresses, macAddr, vlanId); |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 77 | |
| 78 | interfaceService.add(intf); |
| 79 | } |
| 80 | |
| 81 | } |