Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.dhcprelay.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 24 | import org.onlab.util.Tools; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.dhcprelay.DhcpRelayConfig; |
| 29 | import org.onosproject.dhcprelay.DhcpRelayManager; |
| 30 | import org.onosproject.dhcprelay.DhcpRelayService; |
| 31 | import org.onosproject.dhcprelay.store.DhcpRecord; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.Host; |
| 34 | import org.onosproject.net.HostId; |
| 35 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 36 | import org.onosproject.net.host.HostService; |
| 37 | |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 38 | import java.util.Collection; |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 39 | import java.util.function.Predicate; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Prints DHCP server and DHCP relay status. |
| 43 | */ |
| 44 | @Command(scope = "onos", name = "dhcp-relay", description = "DHCP relay app cli.") |
| 45 | public class DhcpRelayCommand extends AbstractShellCommand { |
| 46 | private static final String HEADER = "DHCP relay records ([D]: Directly connected):"; |
| 47 | private static final String NO_RECORDS = "No DHCP relay record found"; |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 48 | private static final String HOST = "id=%s/%s, locations=%s%s, last-seen=%s, IPv4=%s, IPv6=%s"; |
| 49 | private static final String DHCP_SERVER_GW = "DHCP Server: %s, %s via %s (Mac: %s)"; |
| 50 | private static final String DHCP_SERVER = "DHCP Server: %s, %s (Mac: %s)"; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 51 | private static final String MISSING_SERVER_CFG = "DHCP Server info not available"; |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 52 | private static final String DIRECTLY = "[D]"; |
| 53 | private static final String EMPTY = ""; |
| 54 | private static final String NA = "N/A"; |
| 55 | private static final String STATUS_FMT = "[%s, %s]"; |
| 56 | private static final String STATUS_FMT_NH = "[%s via %s, %s]"; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 57 | |
| 58 | private static final DhcpRelayService DHCP_RELAY_SERVICE = get(DhcpRelayService.class); |
| 59 | private static final NetworkConfigRegistry CFG_SERVICE = get(NetworkConfigRegistry.class); |
| 60 | private static final HostService HOST_SERVICE = get(HostService.class); |
| 61 | private static final CoreService CORE_SERVICE = get(CoreService.class); |
| 62 | private static final ApplicationId APP_ID = |
| 63 | CORE_SERVICE.getAppId(DhcpRelayManager.DHCP_RELAY_APP); |
| 64 | |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 65 | |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 66 | @Override |
| 67 | protected void execute() { |
| 68 | DhcpRelayConfig cfg = CFG_SERVICE.getConfig(APP_ID, DhcpRelayConfig.class); |
| 69 | if (cfg == null) { |
| 70 | print(MISSING_SERVER_CFG); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // DHCP server information |
| 75 | ConnectPoint connectPoint = cfg.getDhcpServerConnectPoint(); |
| 76 | Ip4Address gatewayAddress = cfg.getDhcpGatewayIp(); |
| 77 | Ip4Address serverIp = cfg.getDhcpServerIp(); |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 78 | String serverMac = DHCP_RELAY_SERVICE.getDhcpServerMacAddress() |
| 79 | .map(MacAddress::toString).orElse(NA); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 80 | if (gatewayAddress != null) { |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 81 | print(DHCP_SERVER_GW, connectPoint, serverIp, gatewayAddress, serverMac); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 82 | } else { |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 83 | print(DHCP_SERVER, connectPoint, serverIp, serverMac); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // DHCP records |
| 87 | Collection<DhcpRecord> records = DHCP_RELAY_SERVICE.getDhcpRecords(); |
| 88 | if (records.isEmpty()) { |
| 89 | print(NO_RECORDS); |
| 90 | return; |
| 91 | } |
| 92 | print(HEADER); |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 93 | records.forEach(record -> print(HOST, |
| 94 | record.macAddress(), |
| 95 | record.vlanId(), |
| 96 | record.locations(), |
| 97 | record.directlyConnected() ? DIRECTLY : EMPTY, |
| 98 | Tools.timeAgo(record.lastSeen()), |
| 99 | ip4State(record), |
| 100 | ip6State(record))); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 103 | private String ip4State(DhcpRecord record) { |
| 104 | String nextHopIp = findNextHopIp(IpAddress::isIp4, |
| 105 | record.nextHop().orElse(null), |
| 106 | record.vlanId()); |
| 107 | return ipState(record.ip4Address().map(Object::toString).orElse(NA), |
| 108 | record.ip4Status().map(Object::toString).orElse(NA), |
| 109 | record.directlyConnected(), |
| 110 | nextHopIp); |
| 111 | } |
| 112 | |
| 113 | private String ip6State(DhcpRecord record) { |
| 114 | String nextHopIp = findNextHopIp(IpAddress::isIp6, |
| 115 | record.nextHop().orElse(null), |
| 116 | record.vlanId()); |
| 117 | return ipState(record.ip6Address().map(Object::toString).orElse(NA), |
| 118 | record.ip6Status().map(Object::toString).orElse(NA), |
| 119 | record.directlyConnected(), |
| 120 | nextHopIp); |
| 121 | } |
| 122 | |
| 123 | private String ipState(String ipAddress, String status, |
| 124 | boolean directlyConnected, |
| 125 | String nextHopIp) { |
| 126 | if (directlyConnected) { |
| 127 | return String.format(STATUS_FMT, ipAddress, status); |
| 128 | } else { |
| 129 | return String.format(STATUS_FMT_NH, ipAddress, nextHopIp, status); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private String findNextHopIp(Predicate<IpAddress> ipFilter, MacAddress nextHopMac, VlanId vlanId) { |
| 134 | if (ipFilter == null || nextHopMac == null || vlanId == null) { |
| 135 | return NA; |
| 136 | } |
| 137 | Host host = HOST_SERVICE.getHost(HostId.hostId(nextHopMac, vlanId)); |
| 138 | if (host == null) { |
| 139 | return NA; |
| 140 | } |
| 141 | return host.ipAddresses().stream() |
| 142 | .filter(ipFilter) |
| 143 | .filter(ip -> !ip.isLinkLocal()) |
| 144 | .map(Object::toString) |
| 145 | .findFirst() |
| 146 | .orElse(NA); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 147 | } |
| 148 | } |