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; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 25 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 26 | import org.onosproject.dhcprelay.api.DhcpRelayService; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 27 | import org.onosproject.dhcprelay.store.DhcpRecord; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 30 | import org.onosproject.net.host.HostService; |
| 31 | |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 32 | import java.util.Collection; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 33 | import java.util.List; |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 34 | import java.util.function.Predicate; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Prints DHCP server and DHCP relay status. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "dhcp-relay", description = "DHCP relay app cli.") |
| 40 | public class DhcpRelayCommand extends AbstractShellCommand { |
| 41 | private static final String HEADER = "DHCP relay records ([D]: Directly connected):"; |
| 42 | private static final String NO_RECORDS = "No DHCP relay record found"; |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 43 | private static final String HOST = "id=%s/%s, locations=%s%s, last-seen=%s, IPv4=%s, IPv6=%s"; |
Yi Tseng | beea16e | 2017-11-15 13:39:01 -0800 | [diff] [blame] | 44 | private static final String DHCP_SERVER_GW = "DHCP Server: %s, %s via %s (Mac: %s)"; |
| 45 | private static final String DHCP_SERVER = "DHCP Server: %s, %s (Mac: %s)"; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 46 | private static final String MISSING_SERVER_CFG = "DHCP Server info not available"; |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 47 | private static final String DIRECTLY = "[D]"; |
| 48 | private static final String EMPTY = ""; |
| 49 | private static final String NA = "N/A"; |
| 50 | private static final String STATUS_FMT = "[%s, %s]"; |
| 51 | private static final String STATUS_FMT_NH = "[%s via %s, %s]"; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame^] | 52 | private static final String STATUS_FMT_V6 = "[%s, %s, %s]"; |
| 53 | private static final String STATUS_FMT_V6_NH = "[%s, %s via %s, %s]"; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 54 | private static final String DEFAULT_SERVERS = "Default DHCP servers:"; |
| 55 | private static final String INDIRECT_SERVERS = "Indirect DHCP servers:"; |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 56 | |
| 57 | private static final DhcpRelayService DHCP_RELAY_SERVICE = get(DhcpRelayService.class); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 58 | private static final HostService HOST_SERVICE = get(HostService.class); |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 59 | |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 60 | @Override |
| 61 | protected void execute() { |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 62 | List<DhcpServerInfo> defaultDhcpServerInfoList = DHCP_RELAY_SERVICE.getDefaultDhcpServerInfoList(); |
| 63 | List<DhcpServerInfo> indirectDhcpServerInfoList = DHCP_RELAY_SERVICE.getIndirectDhcpServerInfoList(); |
| 64 | |
| 65 | if (defaultDhcpServerInfoList.isEmpty() && indirectDhcpServerInfoList.isEmpty()) { |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 66 | print(MISSING_SERVER_CFG); |
| 67 | return; |
| 68 | } |
| 69 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 70 | if (!defaultDhcpServerInfoList.isEmpty()) { |
| 71 | print(DEFAULT_SERVERS); |
| 72 | listServers(defaultDhcpServerInfoList); |
| 73 | } |
| 74 | if (!indirectDhcpServerInfoList.isEmpty()) { |
| 75 | print(INDIRECT_SERVERS); |
| 76 | listServers(indirectDhcpServerInfoList); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // DHCP records |
| 80 | Collection<DhcpRecord> records = DHCP_RELAY_SERVICE.getDhcpRecords(); |
| 81 | if (records.isEmpty()) { |
| 82 | print(NO_RECORDS); |
| 83 | return; |
| 84 | } |
| 85 | print(HEADER); |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 86 | records.forEach(record -> print(HOST, |
| 87 | record.macAddress(), |
| 88 | record.vlanId(), |
| 89 | record.locations(), |
| 90 | record.directlyConnected() ? DIRECTLY : EMPTY, |
| 91 | Tools.timeAgo(record.lastSeen()), |
| 92 | ip4State(record), |
| 93 | ip6State(record))); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 96 | private void listServers(List<DhcpServerInfo> dhcpServerInfoList) { |
| 97 | dhcpServerInfoList.forEach(dhcpServerInfo -> { |
| 98 | String connectPoint = dhcpServerInfo.getDhcpServerConnectPoint() |
| 99 | .map(Object::toString).orElse(NA); |
| 100 | String serverMac = dhcpServerInfo.getDhcpConnectMac() |
| 101 | .map(Object::toString).orElse(NA); |
| 102 | String gatewayAddress; |
| 103 | String serverIp; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 104 | |
| 105 | switch (dhcpServerInfo.getVersion()) { |
| 106 | case DHCP_V4: |
| 107 | gatewayAddress = dhcpServerInfo.getDhcpGatewayIp4() |
| 108 | .map(Object::toString).orElse(null); |
| 109 | serverIp = dhcpServerInfo.getDhcpServerIp4() |
| 110 | .map(Object::toString).orElse(NA); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 111 | break; |
| 112 | case DHCP_V6: |
| 113 | gatewayAddress = dhcpServerInfo.getDhcpGatewayIp6() |
| 114 | .map(Object::toString).orElse(null); |
| 115 | serverIp = dhcpServerInfo.getDhcpServerIp6() |
| 116 | .map(Object::toString).orElse(NA); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 117 | break; |
| 118 | default: |
| 119 | return; |
| 120 | } |
| 121 | if (gatewayAddress != null) { |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 122 | print(DHCP_SERVER_GW, connectPoint, serverIp, gatewayAddress, serverMac); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 123 | } else { |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 124 | print(DHCP_SERVER, connectPoint, serverIp, serverMac); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 125 | } |
| 126 | }); |
| 127 | } |
| 128 | |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 129 | private String ip4State(DhcpRecord record) { |
| 130 | String nextHopIp = findNextHopIp(IpAddress::isIp4, |
| 131 | record.nextHop().orElse(null), |
| 132 | record.vlanId()); |
| 133 | return ipState(record.ip4Address().map(Object::toString).orElse(NA), |
| 134 | record.ip4Status().map(Object::toString).orElse(NA), |
| 135 | record.directlyConnected(), |
| 136 | nextHopIp); |
| 137 | } |
| 138 | |
| 139 | private String ip6State(DhcpRecord record) { |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame^] | 140 | String nextHopIp = findNextHopIp6(IpAddress::isIp6, |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 141 | record.nextHop().orElse(null), |
| 142 | record.vlanId()); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame^] | 143 | |
| 144 | if (record.directlyConnected()) { |
| 145 | return String.format(STATUS_FMT_V6, |
| 146 | record.ip6Address().map(Object::toString).orElse(NA), |
| 147 | record.pdPrefix().map(Object::toString).orElse(NA), |
| 148 | record.ip6Status().map(Object::toString).orElse(NA)); |
| 149 | } else { |
| 150 | return String.format(STATUS_FMT_V6_NH, |
| 151 | record.ip6Address().map(Object::toString).orElse(NA), |
| 152 | record.pdPrefix().map(Object::toString).orElse(NA), |
| 153 | nextHopIp, |
| 154 | record.ip6Status().map(Object::toString).orElse(NA)); |
| 155 | } |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | private String ipState(String ipAddress, String status, |
| 159 | boolean directlyConnected, |
| 160 | String nextHopIp) { |
| 161 | if (directlyConnected) { |
| 162 | return String.format(STATUS_FMT, ipAddress, status); |
| 163 | } else { |
| 164 | return String.format(STATUS_FMT_NH, ipAddress, nextHopIp, status); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private String findNextHopIp(Predicate<IpAddress> ipFilter, MacAddress nextHopMac, VlanId vlanId) { |
| 169 | if (ipFilter == null || nextHopMac == null || vlanId == null) { |
| 170 | return NA; |
| 171 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame^] | 172 | |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 173 | Host host = HOST_SERVICE.getHost(HostId.hostId(nextHopMac, vlanId)); |
| 174 | if (host == null) { |
| 175 | return NA; |
| 176 | } |
| 177 | return host.ipAddresses().stream() |
| 178 | .filter(ipFilter) |
| 179 | .filter(ip -> !ip.isLinkLocal()) |
| 180 | .map(Object::toString) |
| 181 | .findFirst() |
| 182 | .orElse(NA); |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 183 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame^] | 184 | |
| 185 | private String findNextHopIp6(Predicate<IpAddress> ipFilter, MacAddress nextHopMac, VlanId vlanId) { |
| 186 | if (ipFilter == null || nextHopMac == null || vlanId == null) { |
| 187 | return NA; |
| 188 | } |
| 189 | |
| 190 | Host host = HOST_SERVICE.getHost(HostId.hostId(nextHopMac, vlanId)); |
| 191 | if (host == null) { |
| 192 | return NA; |
| 193 | } |
| 194 | return host.ipAddresses().stream() |
| 195 | .filter(ipFilter) |
| 196 | .filter(ip -> ip.isLinkLocal()) |
| 197 | .map(Object::toString) |
| 198 | .findFirst() |
| 199 | .orElse(NA); |
| 200 | } |
Yi Tseng | 2e5d99e | 2017-06-06 10:58:46 -0700 | [diff] [blame] | 201 | } |