blob: 7f5b5c3f7ad41ec0755a8cd9ea810c14eb861190 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Jonathan Hart61d4ebc2014-10-29 11:08:26 -070017
18import java.util.Collections;
19import java.util.List;
20import java.util.Set;
21
22import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cli.Comparators;
25import org.onosproject.net.host.HostService;
26import org.onosproject.net.host.InterfaceIpAddress;
27import org.onosproject.net.host.PortAddresses;
Jonathan Hart61d4ebc2014-10-29 11:08:26 -070028
29import 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.")
36public 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}