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