blob: fb3e68c2522bd089553b7b191cab73656fb0e012 [file] [log] [blame]
Jonathan Harteb8c9472015-08-05 07:43:13 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.cli.net;
17
18import com.google.common.collect.Lists;
19import org.apache.karaf.shell.commands.Command;
Jonathan Hartff280fa2015-11-10 08:33:17 -080020import org.onlab.packet.MacAddress;
21import org.onlab.packet.VlanId;
Jonathan Harteb8c9472015-08-05 07:43:13 -070022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.cli.Comparators;
24import org.onosproject.incubator.net.intf.Interface;
25import org.onosproject.incubator.net.intf.InterfaceService;
26
27import java.util.Collections;
28import java.util.List;
29
30/**
31 * Lists all configured interfaces.
32 */
33@Command(scope = "onos", name = "interfaces",
34 description = "Lists all configured interfaces.")
35public class InterfacesListCommand extends AbstractShellCommand {
36
Jonathan Hartff280fa2015-11-10 08:33:17 -080037 private static final String FORMAT = "%s: port=%s/%s";
38 private static final String IP_FORMAT = " ips=";
39 private static final String MAC_FORMAT = " mac=";
40 private static final String VLAN_FORMAT = " vlan=";
Jonathan Harteb8c9472015-08-05 07:43:13 -070041
Jonathan Hartff280fa2015-11-10 08:33:17 -080042 private static final String NO_NAME = "(unamed)";
Jonathan Hart7dbe6292015-11-10 08:33:17 -080043
Jonathan Harteb8c9472015-08-05 07:43:13 -070044 @Override
45 protected void execute() {
46 InterfaceService interfaceService = get(InterfaceService.class);
47
48 List<Interface> interfaces = Lists.newArrayList(interfaceService.getInterfaces());
49
50 Collections.sort(interfaces, Comparators.INTERFACES_COMPARATOR);
51
Jonathan Hartff280fa2015-11-10 08:33:17 -080052 interfaces.forEach(this::printInterface);
53 }
54
55 private void printInterface(Interface intf) {
56 StringBuilder formatStringBuilder = new StringBuilder(FORMAT);
57
58 if (!intf.ipAddresses().isEmpty()) {
59 formatStringBuilder.append(IP_FORMAT);
60 formatStringBuilder.append(intf.ipAddresses().toString());
Jonathan Harteb8c9472015-08-05 07:43:13 -070061 }
Jonathan Hartff280fa2015-11-10 08:33:17 -080062
63 if (!intf.mac().equals(MacAddress.NONE)) {
64 formatStringBuilder.append(MAC_FORMAT);
65 formatStringBuilder.append(intf.mac().toString());
66 }
67
68 if (!intf.vlan().equals(VlanId.NONE)) {
69 formatStringBuilder.append(VLAN_FORMAT);
70 formatStringBuilder.append(intf.vlan().toString());
71 }
72
73 String name = (intf.name().equals(Interface.NO_INTERFACE_NAME)) ?
74 NO_NAME : intf.name();
75
76 print(formatStringBuilder.toString(), name, intf.connectPoint().deviceId(),
77 intf.connectPoint().port());
Jonathan Harteb8c9472015-08-05 07:43:13 -070078 }
79
80}