Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Jonathan Hart | ff280fa | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.cli.Comparators; |
| 24 | import org.onosproject.incubator.net.intf.Interface; |
| 25 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 26 | |
| 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Lists all configured interfaces. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "interfaces", |
| 34 | description = "Lists all configured interfaces.") |
| 35 | public class InterfacesListCommand extends AbstractShellCommand { |
| 36 | |
Jonathan Hart | ff280fa | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 37 | 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 Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 41 | |
Jonathan Hart | ff280fa | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 42 | private static final String NO_NAME = "(unamed)"; |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 43 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 44 | @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 Hart | ff280fa | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 52 | 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 Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 61 | } |
Jonathan Hart | ff280fa | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 62 | |
| 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 Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } |