alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Jonathan Hart | 61d4ebc | 2014-10-29 11:08:26 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Collections; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.cli.Comparators; |
| 25 | import org.onosproject.net.host.HostService; |
| 26 | import org.onosproject.net.host.InterfaceIpAddress; |
| 27 | import org.onosproject.net.host.PortAddresses; |
Jonathan Hart | 61d4ebc | 2014-10-29 11:08:26 -0700 | [diff] [blame] | 28 | |
| 29 | import com.google.common.collect.Lists; |
| 30 | |
| 31 | /** |
| 32 | * Lists all configured address port bindings. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "address-bindings", |
| 35 | description = "Lists all configured address port bindings.") |
| 36 | public class AddressBindingsListCommand extends AbstractShellCommand { |
| 37 | |
| 38 | private static final String FORMAT = |
| 39 | "port=%s/%s, ip(s)=%s, mac=%s"; |
| 40 | |
| 41 | @Override |
| 42 | protected void execute() { |
| 43 | HostService hostService = get(HostService.class); |
| 44 | |
| 45 | List<PortAddresses> addresses = |
| 46 | Lists.newArrayList(hostService.getAddressBindings()); |
| 47 | |
| 48 | Collections.sort(addresses, Comparators.ADDRESSES_COMPARATOR); |
| 49 | |
| 50 | for (PortAddresses pa : addresses) { |
| 51 | print(FORMAT, pa.connectPoint().deviceId(), pa.connectPoint().port(), |
| 52 | printIpAddresses(pa.ipAddresses()), pa.mac()); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | private String printIpAddresses(Set<InterfaceIpAddress> addresses) { |
| 57 | StringBuilder output = new StringBuilder("["); |
| 58 | for (InterfaceIpAddress address : addresses) { |
| 59 | output.append(address.ipAddress().toString()); |
| 60 | output.append("/"); |
| 61 | output.append(address.subnetAddress().prefixLength()); |
| 62 | output.append(", "); |
| 63 | } |
| 64 | // Remove the last comma |
| 65 | output.delete(output.length() - 2 , output.length()); |
| 66 | output.append("]"); |
| 67 | return output.toString(); |
| 68 | } |
| 69 | |
| 70 | } |