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