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