blob: ee111577229346aa4dbe774bb2545aca00bcdbbe [file] [log] [blame]
Jonathan Hart0bdf8372015-10-08 14:30:36 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Hart0bdf8372015-10-08 14:30:36 -07003 *
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
17package org.onosproject.cli.net;
18
Jonathan Hart00cddda2016-02-16 10:30:37 -080019import com.google.common.collect.Lists;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070022import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.apache.karaf.shell.api.action.Option;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070025import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070028import org.onosproject.cli.PlaceholderCompleter;
Ray Milkeyfacf2862017-08-03 11:58:29 -070029import org.onosproject.net.intf.Interface;
30import org.onosproject.net.intf.InterfaceAdminService;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070031import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.host.InterfaceIpAddress;
33
Jonathan Hart00cddda2016-02-16 10:30:37 -080034import java.util.List;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070035
36/**
37 * Adds a new interface configuration.
38 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010040@Command(scope = "onos", name = "interface-add",
Jonathan Hart0bdf8372015-10-08 14:30:36 -070041 description = "Adds a new configured interface")
42public class InterfaceAddCommand extends AbstractShellCommand {
43
Jonathan Hart7dbe6292015-11-10 08:33:17 -080044 @Argument(index = 0, name = "port",
Jonathan Hart0bdf8372015-10-08 14:30:36 -070045 description = "Device port that the interface is associated with",
46 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070047 @Completion(ConnectPointCompleter.class)
Jonathan Hart0bdf8372015-10-08 14:30:36 -070048 private String connectPoint = null;
49
Jonathan Hart7dbe6292015-11-10 08:33:17 -080050 @Argument(index = 1, name = "name", description = "Interface name",
51 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070052 @Completion(PlaceholderCompleter.class)
Jonathan Hart7dbe6292015-11-10 08:33:17 -080053 private String name = null;
54
Jonathan Hart0bdf8372015-10-08 14:30:36 -070055 @Option(name = "-m", aliases = "--mac",
56 description = "MAC address of the interface",
Luca Pretee3879f72015-10-16 11:19:53 +020057 required = false, multiValued = false)
Jonathan Hart0bdf8372015-10-08 14:30:36 -070058 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 Milkeyd84f89b2018-08-17 14:54:17 -070072 protected void doExecute() {
Jonathan Hart0bdf8372015-10-08 14:30:36 -070073 InterfaceAdminService interfaceService = get(InterfaceAdminService.class);
74
Jonathan Hart00cddda2016-02-16 10:30:37 -080075 List<InterfaceIpAddress> ipAddresses = Lists.newArrayList();
Jonathan Hart0bdf8372015-10-08 14:30:36 -070076 if (ips != null) {
77 for (String strIp : ips) {
78 ipAddresses.add(InterfaceIpAddress.valueOf(strIp));
79 }
80 }
81
Luca Pretee3879f72015-10-16 11:19:53 +020082 MacAddress macAddr = mac == null ? null : MacAddress.valueOf(mac);
83
Jonathan Hart0bdf8372015-10-08 14:30:36 -070084 VlanId vlanId = vlan == null ? VlanId.NONE : VlanId.vlanId(Short.parseShort(vlan));
85
Jonathan Hart7dbe6292015-11-10 08:33:17 -080086 Interface intf = new Interface(name,
87 ConnectPoint.deviceConnectPoint(connectPoint),
Luca Pretee3879f72015-10-16 11:19:53 +020088 ipAddresses, macAddr, vlanId);
Jonathan Hart0bdf8372015-10-08 14:30:36 -070089
90 interfaceService.add(intf);
Jonathan Hart7dbe6292015-11-10 08:33:17 -080091
92 print("Interface added");
Jonathan Hart0bdf8372015-10-08 14:30:36 -070093 }
94
95}