blob: 50a6802c612f03cf0f2f993594902bf3ec500247 [file] [log] [blame]
Thomas Vachuska54dc3522015-09-09 00:11:45 -07001/*
2 * Copyright 2015 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 */
16package org.onosproject.dhcp.impl;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableSet;
20import org.onlab.packet.MacAddress;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.dhcp.DHCPService;
23import org.onosproject.dhcp.IPAssignment;
24import org.onosproject.ui.RequestHandler;
25import org.onosproject.ui.UiMessageHandler;
26import org.onosproject.ui.table.TableModel;
27import org.onosproject.ui.table.TableRequestHandler;
28
29import java.util.Collection;
30import java.util.Date;
31import java.util.Map;
32
33/**
34 * DHCPViewMessageHandler class implementation.
35 */
36public class DhcpViewMessageHandler extends UiMessageHandler {
37
38 private static final String DHCP_DATA_REQ = "dhcpDataRequest";
39 private static final String DHCP_DATA_RESP = "dhcpDataResponse";
40 private static final String DHCP = "dhcps";
41
42 private static final String MAC = "mac";
43 private static final String IP = "ip";
44 private static final String LEASE = "lease";
45
46 private static final String[] COL_IDS = {
47 MAC, IP, LEASE
48 };
49
50 @Override
51 protected Collection<RequestHandler> createRequestHandlers() {
52 return ImmutableSet.of(
53 new DataRequestHandler()
54 );
55 }
56
57 private final class DataRequestHandler extends TableRequestHandler {
58
59 private DataRequestHandler() {
60 super(DHCP_DATA_REQ, DHCP_DATA_RESP, DHCP);
61 }
62
63 @Override
64 protected String defaultColumnId() {
65 return MAC;
66 }
67
68 @Override
69 protected String[] getColumnIds() {
70 return COL_IDS;
71 }
72
73 @Override
74 protected void populateTable(TableModel tm, ObjectNode payload) {
75 DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class);
76 Map<MacAddress, IPAssignment> allocationMap = dhcpService.listMapping();
77
78 for (Map.Entry<MacAddress, IPAssignment> entry : allocationMap.entrySet()) {
79 populateRow(tm.addRow(), entry);
80 }
81 }
82
83 private void populateRow(TableModel.Row row, Map.Entry<MacAddress, IPAssignment> entry) {
84 if (entry.getValue().leasePeriod() > 0) {
85 Date now = new Date(entry.getValue().timestamp().getTime() + entry.getValue().leasePeriod());
86 row.cell(MAC, entry.getKey())
87 .cell(IP, entry.getValue().ipAddress())
88 .cell(LEASE, now.toString());
89 } else {
90 row.cell(MAC, entry.getKey())
91 .cell(IP, entry.getValue().ipAddress())
92 .cell(LEASE, "Infinite Static Lease");
93 }
94 }
95 }
96}