blob: c6210c731d34efa4e06888ec1cd729ddd5bc3048 [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;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Option;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070024import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyfacf2862017-08-03 11:58:29 -070027import org.onosproject.net.intf.Interface;
28import org.onosproject.net.intf.InterfaceAdminService;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.host.InterfaceIpAddress;
31
Jonathan Hart00cddda2016-02-16 10:30:37 -080032import java.util.List;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070033
34/**
35 * Adds a new interface configuration.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010038@Command(scope = "onos", name = "interface-add",
Jonathan Hart0bdf8372015-10-08 14:30:36 -070039 description = "Adds a new configured interface")
40public class InterfaceAddCommand extends AbstractShellCommand {
41
Jonathan Hart7dbe6292015-11-10 08:33:17 -080042 @Argument(index = 0, name = "port",
Jonathan Hart0bdf8372015-10-08 14:30:36 -070043 description = "Device port that the interface is associated with",
44 required = true, multiValued = false)
45 private String connectPoint = null;
46
Jonathan Hart7dbe6292015-11-10 08:33:17 -080047 @Argument(index = 1, name = "name", description = "Interface name",
48 required = true, multiValued = false)
49 private String name = null;
50
Jonathan Hart0bdf8372015-10-08 14:30:36 -070051 @Option(name = "-m", aliases = "--mac",
52 description = "MAC address of the interface",
Luca Pretee3879f72015-10-16 11:19:53 +020053 required = false, multiValued = false)
Jonathan Hart0bdf8372015-10-08 14:30:36 -070054 private String mac = null;
55
56 @Option(name = "-i", aliases = "--ip",
57 description = "IP address configured on the interface\n" +
58 "(e.g. 10.0.1.1/24). Can be specified multiple times.",
59 required = false, multiValued = true)
60 private String[] ips = null;
61
62 @Option(name = "-v", aliases = "--vlan",
63 description = "VLAN configured on the interface",
64 required = false, multiValued = false)
65 private String vlan = null;
66
67 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 protected void doExecute() {
Jonathan Hart0bdf8372015-10-08 14:30:36 -070069 InterfaceAdminService interfaceService = get(InterfaceAdminService.class);
70
Jonathan Hart00cddda2016-02-16 10:30:37 -080071 List<InterfaceIpAddress> ipAddresses = Lists.newArrayList();
Jonathan Hart0bdf8372015-10-08 14:30:36 -070072 if (ips != null) {
73 for (String strIp : ips) {
74 ipAddresses.add(InterfaceIpAddress.valueOf(strIp));
75 }
76 }
77
Luca Pretee3879f72015-10-16 11:19:53 +020078 MacAddress macAddr = mac == null ? null : MacAddress.valueOf(mac);
79
Jonathan Hart0bdf8372015-10-08 14:30:36 -070080 VlanId vlanId = vlan == null ? VlanId.NONE : VlanId.vlanId(Short.parseShort(vlan));
81
Jonathan Hart7dbe6292015-11-10 08:33:17 -080082 Interface intf = new Interface(name,
83 ConnectPoint.deviceConnectPoint(connectPoint),
Luca Pretee3879f72015-10-16 11:19:53 +020084 ipAddresses, macAddr, vlanId);
Jonathan Hart0bdf8372015-10-08 14:30:36 -070085
86 interfaceService.add(intf);
Jonathan Hart7dbe6292015-11-10 08:33:17 -080087
88 print("Interface added");
Jonathan Hart0bdf8372015-10-08 14:30:36 -070089 }
90
91}