blob: 4758155bd3f95e908112620c11434b2b6d2f63e7 [file] [log] [blame]
Yi Tseng2e5d99e2017-06-06 10:58:46 -07001/*
2 * Copyright 2017-present 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 */
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;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.dhcprelay.DhcpRelayConfig;
28import org.onosproject.dhcprelay.DhcpRelayManager;
29import org.onosproject.dhcprelay.DhcpRelayService;
30import org.onosproject.dhcprelay.store.DhcpRecord;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.Host;
33import org.onosproject.net.HostId;
34import org.onosproject.net.config.NetworkConfigRegistry;
35import org.onosproject.net.host.HostService;
36
37import java.text.DateFormat;
38import java.text.SimpleDateFormat;
39import java.util.Collection;
40import java.util.Date;
41import java.util.Optional;
42
43/**
44 * Prints DHCP server and DHCP relay status.
45 */
46@Command(scope = "onos", name = "dhcp-relay", description = "DHCP relay app cli.")
47public class DhcpRelayCommand extends AbstractShellCommand {
48 private static final String HEADER = "DHCP relay records ([D]: Directly connected):";
49 private static final String NO_RECORDS = "No DHCP relay record found";
50 private static final String BASIC_HOST_D = "%s/%s: %s[D], %s";
51 private static final String BASIC_HOST = "%s/%s: %s, %s";
52 private static final String NO_IP4 = "\tIPv4: N/A";
53 private static final String IP4_INFO_D = "\tIPv4: %s: %s";
54 private static final String IP4_INFO = "\tIPv4: %s via %s: %s";
55 private static final String NO_IP6 = "\tIPv6: N/A";
56 private static final String IP6_INFO_D = "\tIPv6: %s: %s";
57 private static final String IP6_INFO = "\tIPv6: %s via %s: %s";
58 private static final String DHCP_SERVER_GW = "DHCP Server: %s, %s via %s";
59 private static final String DHCP_SERVER = "DHCP Server: %s, %s";
60 private static final String MISSING_SERVER_CFG = "DHCP Server info not available";
61
62 private static final DhcpRelayService DHCP_RELAY_SERVICE = get(DhcpRelayService.class);
63 private static final NetworkConfigRegistry CFG_SERVICE = get(NetworkConfigRegistry.class);
64 private static final HostService HOST_SERVICE = get(HostService.class);
65 private static final CoreService CORE_SERVICE = get(CoreService.class);
66 private static final ApplicationId APP_ID =
67 CORE_SERVICE.getAppId(DhcpRelayManager.DHCP_RELAY_APP);
68
69 @Override
70 protected void execute() {
71 DhcpRelayConfig cfg = CFG_SERVICE.getConfig(APP_ID, DhcpRelayConfig.class);
72 if (cfg == null) {
73 print(MISSING_SERVER_CFG);
74 return;
75 }
76
77 // DHCP server information
78 ConnectPoint connectPoint = cfg.getDhcpServerConnectPoint();
79 Ip4Address gatewayAddress = cfg.getDhcpGatewayIp();
80 Ip4Address serverIp = cfg.getDhcpServerIp();
81 if (gatewayAddress != null) {
82 print(DHCP_SERVER_GW, connectPoint, serverIp, gatewayAddress);
83 } else {
84 print(DHCP_SERVER, connectPoint, serverIp);
85 }
86
87 // DHCP records
88 Collection<DhcpRecord> records = DHCP_RELAY_SERVICE.getDhcpRecords();
89 if (records.isEmpty()) {
90 print(NO_RECORDS);
91 return;
92 }
93 print(HEADER);
94 records.forEach(record -> {
95 DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
96 String lastSeen = df.format(new Date(record.lastSeen()));
97
98 if (record.directlyConnected()) {
99 print(BASIC_HOST_D, record.macAddress(), record.vlanId(),
100 record.locations(), lastSeen);
101 } else {
102 print(BASIC_HOST, record.macAddress(), record.vlanId(),
103 record.locations(), lastSeen);
104 }
105
106 if (record.ip4Status().isPresent()) {
107 if (record.directlyConnected()) {
108 print(IP4_INFO_D,
109 record.ip4Address().orElse(null),
110 record.ip4Status().orElse(null));
111 } else {
112 IpAddress gatewayIp = record.nextHop()
113 .flatMap(mac -> findGatewayIp(mac, record.vlanId()))
114 .orElse(null);
115 print(IP4_INFO,
116 record.ip4Address().orElse(null),
117 gatewayIp,
118 record.ip4Status().orElse(null));
119 }
120 } else {
121 print(NO_IP4);
122 }
123
124 if (record.ip6Status().isPresent()) {
125 if (record.directlyConnected()) {
126 print(IP6_INFO_D,
127 record.ip6Address().orElse(null),
128 record.ip6Status().orElse(null));
129 } else {
130 IpAddress gatewayIp = record.nextHop()
131 .flatMap(mac -> findGatewayIp(mac, record.vlanId()))
132 .orElse(null);
133 print(IP6_INFO,
134 record.ip6Address().orElse(null),
135 gatewayIp,
136 record.ip6Status().orElse(null));
137 }
138 } else {
139 print(NO_IP6);
140 }
141 });
142 }
143
144 private Optional<IpAddress> findGatewayIp(MacAddress macAddress, VlanId vlanId) {
145 Host host = HOST_SERVICE.getHost(HostId.hostId(macAddress, vlanId));
146 return host.ipAddresses().stream().findFirst();
147 }
148}