blob: 028087dabd9f9b06caae7759c16f0356ab8c6975 [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;
Jonathan Hart7dbe6292015-11-10 08:33:17 -080020import org.apache.karaf.shell.commands.Argument;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070021import org.apache.karaf.shell.commands.Command;
22import org.apache.karaf.shell.commands.Option;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.incubator.net.intf.Interface;
27import org.onosproject.incubator.net.intf.InterfaceAdminService;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.host.InterfaceIpAddress;
30
Jonathan Hart00cddda2016-02-16 10:30:37 -080031import java.util.List;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070032
33/**
34 * Adds a new interface configuration.
35 */
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010036@Command(scope = "onos", name = "interface-add",
Jonathan Hart0bdf8372015-10-08 14:30:36 -070037 description = "Adds a new configured interface")
38public class InterfaceAddCommand extends AbstractShellCommand {
39
Jonathan Hart7dbe6292015-11-10 08:33:17 -080040 @Argument(index = 0, name = "port",
Jonathan Hart0bdf8372015-10-08 14:30:36 -070041 description = "Device port that the interface is associated with",
42 required = true, multiValued = false)
43 private String connectPoint = null;
44
Jonathan Hart7dbe6292015-11-10 08:33:17 -080045 @Argument(index = 1, name = "name", description = "Interface name",
46 required = true, multiValued = false)
47 private String name = null;
48
Jonathan Hart0bdf8372015-10-08 14:30:36 -070049 @Option(name = "-m", aliases = "--mac",
50 description = "MAC address of the interface",
Luca Pretee3879f72015-10-16 11:19:53 +020051 required = false, multiValued = false)
Jonathan Hart0bdf8372015-10-08 14:30:36 -070052 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 Hart00cddda2016-02-16 10:30:37 -080069 List<InterfaceIpAddress> ipAddresses = Lists.newArrayList();
Jonathan Hart0bdf8372015-10-08 14:30:36 -070070 if (ips != null) {
71 for (String strIp : ips) {
72 ipAddresses.add(InterfaceIpAddress.valueOf(strIp));
73 }
74 }
75
Luca Pretee3879f72015-10-16 11:19:53 +020076 MacAddress macAddr = mac == null ? null : MacAddress.valueOf(mac);
77
Jonathan Hart0bdf8372015-10-08 14:30:36 -070078 VlanId vlanId = vlan == null ? VlanId.NONE : VlanId.vlanId(Short.parseShort(vlan));
79
Jonathan Hart7dbe6292015-11-10 08:33:17 -080080 Interface intf = new Interface(name,
81 ConnectPoint.deviceConnectPoint(connectPoint),
Luca Pretee3879f72015-10-16 11:19:53 +020082 ipAddresses, macAddr, vlanId);
Jonathan Hart0bdf8372015-10-08 14:30:36 -070083
84 interfaceService.add(intf);
Jonathan Hart7dbe6292015-11-10 08:33:17 -080085
86 print("Interface added");
Jonathan Hart0bdf8372015-10-08 14:30:36 -070087 }
88
89}