blob: be4b75c76254bd1c03f859cb9799fbcfbb66fe73 [file] [log] [blame]
Yi Tseng2e5d99e2017-06-06 10:58:46 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tseng2e5d99e2017-06-06 10:58:46 -07003 *
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
17package org.onosproject.dhcprelay.cli;
18
19import org.apache.karaf.shell.commands.Command;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Yi Tseng13a41a12017-07-26 13:45:01 -070024import org.onlab.util.Tools;
Yi Tseng2e5d99e2017-06-06 10:58:46 -070025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.dhcprelay.DhcpRelayConfig;
29import org.onosproject.dhcprelay.DhcpRelayManager;
30import org.onosproject.dhcprelay.DhcpRelayService;
31import org.onosproject.dhcprelay.store.DhcpRecord;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.Host;
34import org.onosproject.net.HostId;
35import org.onosproject.net.config.NetworkConfigRegistry;
36import org.onosproject.net.host.HostService;
37
Yi Tseng2e5d99e2017-06-06 10:58:46 -070038import java.util.Collection;
Yi Tseng13a41a12017-07-26 13:45:01 -070039import java.util.function.Predicate;
Yi Tseng2e5d99e2017-06-06 10:58:46 -070040
41/**
42 * Prints DHCP server and DHCP relay status.
43 */
44@Command(scope = "onos", name = "dhcp-relay", description = "DHCP relay app cli.")
45public 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 Tseng13a41a12017-07-26 13:45:01 -070048 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 Tseng2e5d99e2017-06-06 10:58:46 -070051 private static final String MISSING_SERVER_CFG = "DHCP Server info not available";
Yi Tseng13a41a12017-07-26 13:45:01 -070052 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 Tseng2e5d99e2017-06-06 10:58:46 -070057
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 Tseng13a41a12017-07-26 13:45:01 -070065
Yi Tseng2e5d99e2017-06-06 10:58:46 -070066 @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 Tseng13a41a12017-07-26 13:45:01 -070078 String serverMac = DHCP_RELAY_SERVICE.getDhcpServerMacAddress()
79 .map(MacAddress::toString).orElse(NA);
Yi Tseng2e5d99e2017-06-06 10:58:46 -070080 if (gatewayAddress != null) {
Yi Tseng13a41a12017-07-26 13:45:01 -070081 print(DHCP_SERVER_GW, connectPoint, serverIp, gatewayAddress, serverMac);
Yi Tseng2e5d99e2017-06-06 10:58:46 -070082 } else {
Yi Tseng13a41a12017-07-26 13:45:01 -070083 print(DHCP_SERVER, connectPoint, serverIp, serverMac);
Yi Tseng2e5d99e2017-06-06 10:58:46 -070084 }
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 Tseng13a41a12017-07-26 13:45:01 -070093 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 Tseng2e5d99e2017-06-06 10:58:46 -0700101 }
102
Yi Tseng13a41a12017-07-26 13:45:01 -0700103 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 Tseng2e5d99e2017-06-06 10:58:46 -0700147 }
148}