Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.dhcp.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 20 | import org.onosproject.cli.AbstractShellCommand; |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 21 | import org.onosproject.dhcp.DhcpService; |
| 22 | import org.onosproject.dhcp.IpAssignment; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 23 | import org.onosproject.net.HostId; |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.RequestHandler; |
| 25 | import org.onosproject.ui.UiMessageHandler; |
| 26 | import org.onosproject.ui.table.TableModel; |
| 27 | import org.onosproject.ui.table.TableRequestHandler; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | import java.util.Date; |
| 31 | import java.util.Map; |
| 32 | |
| 33 | /** |
| 34 | * DHCPViewMessageHandler class implementation. |
| 35 | */ |
| 36 | public 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 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 42 | private static final String HOST = "host"; |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 43 | private static final String IP = "ip"; |
| 44 | private static final String LEASE = "lease"; |
| 45 | |
| 46 | private static final String[] COL_IDS = { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 47 | HOST, IP, LEASE |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | @Override |
| 51 | protected Collection<RequestHandler> createRequestHandlers() { |
| 52 | return ImmutableSet.of( |
| 53 | new DataRequestHandler() |
| 54 | ); |
| 55 | } |
| 56 | |
samanwita pal | f321e6b | 2015-09-10 10:46:46 -0700 | [diff] [blame] | 57 | // handler for dhcp table requests |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 58 | private final class DataRequestHandler extends TableRequestHandler { |
| 59 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame^] | 60 | private static final String NO_ROWS_MESSAGE = "No mappings found"; |
| 61 | |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 62 | private DataRequestHandler() { |
| 63 | super(DHCP_DATA_REQ, DHCP_DATA_RESP, DHCP); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected String defaultColumnId() { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 68 | return HOST; |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected String[] getColumnIds() { |
| 73 | return COL_IDS; |
| 74 | } |
| 75 | |
| 76 | @Override |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame^] | 77 | protected String noRowsMessage() { |
| 78 | return NO_ROWS_MESSAGE; |
| 79 | } |
| 80 | |
| 81 | @Override |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 82 | protected void populateTable(TableModel tm, ObjectNode payload) { |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 83 | DhcpService dhcpService = AbstractShellCommand.get(DhcpService.class); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 84 | Map<HostId, IpAssignment> allocationMap = dhcpService.listMapping(); |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 85 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 86 | for (Map.Entry<HostId, IpAssignment> entry : allocationMap.entrySet()) { |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 87 | populateRow(tm.addRow(), entry); |
| 88 | } |
| 89 | } |
| 90 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 91 | private void populateRow(TableModel.Row row, Map.Entry<HostId, IpAssignment> entry) { |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 92 | if (entry.getValue().leasePeriod() > 0) { |
| 93 | Date now = new Date(entry.getValue().timestamp().getTime() + entry.getValue().leasePeriod()); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 94 | row.cell(HOST, entry.getKey()) |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 95 | .cell(IP, entry.getValue().ipAddress()) |
| 96 | .cell(LEASE, now.toString()); |
| 97 | } else { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 98 | row.cell(HOST, entry.getKey()) |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 99 | .cell(IP, entry.getValue().ipAddress()) |
| 100 | .cell(LEASE, "Infinite Static Lease"); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |