samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
samanwita pal | f28207b | 2015-09-04 10:41:56 -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 | package org.onosproject.dhcp.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.MacAddress; |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 23 | import org.onosproject.dhcp.DhcpService; |
| 24 | import org.onosproject.dhcp.IpAssignment; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 25 | import org.onosproject.net.HostId; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 26 | import org.onosproject.rest.AbstractWebResource; |
| 27 | |
| 28 | import javax.ws.rs.Consumes; |
| 29 | import javax.ws.rs.DELETE; |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.POST; |
| 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.PathParam; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 38 | import java.util.Date; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 39 | import java.util.Map; |
| 40 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 41 | import static org.onosproject.dhcp.IpAssignment.AssignmentStatus.Option_Requested; |
| 42 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Manage DHCP address assignments. |
| 45 | */ |
| 46 | @Path("dhcp") |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 47 | public class DhcpWebResource extends AbstractWebResource { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 48 | |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 49 | private final DhcpService service = get(DhcpService.class); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Get DHCP server configuration data. |
| 53 | * Shows lease, renewal and rebinding times in seconds. |
| 54 | * |
| 55 | * @return 200 OK |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 56 | * @onos.rsModel DhcpConfigGet |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 57 | */ |
| 58 | @GET |
| 59 | @Path("config") |
| 60 | public Response getConfigs() { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 61 | ObjectNode node = mapper().createObjectNode() |
| 62 | .put("leaseTime", service.getLeaseTime()) |
| 63 | .put("renewalTime", service.getRenewalTime()) |
| 64 | .put("rebindingTime", service.getRebindingTime()); |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 65 | return ok(node).build(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get all MAC/IP mappings. |
| 70 | * Shows all MAC/IP mappings held by the DHCP server. |
| 71 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 72 | * @onos.rsModel DhcpConfigGetMappings |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 73 | * @return 200 OK |
| 74 | */ |
| 75 | @GET |
| 76 | @Path("mappings") |
| 77 | public Response listMappings() { |
| 78 | ObjectNode root = mapper().createObjectNode(); |
| 79 | |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 80 | Map<HostId, IpAssignment> intents = service.listMapping(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 81 | ArrayNode arrayNode = root.putArray("mappings"); |
| 82 | intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 83 | .put("host", i.getKey().toString()) |
| 84 | .put("ip", i.getValue().ipAddress().toString()))); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 85 | |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 86 | return ok(root).build(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Get all available IPs. |
| 92 | * Shows all the IPs in the free pool of the DHCP Server. |
| 93 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 94 | * @onos.rsModel DhcpConfigGetAvailable |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 95 | * @return 200 OK |
| 96 | */ |
| 97 | @GET |
| 98 | @Path("available") |
| 99 | public Response listAvailableIPs() { |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 100 | Iterable<Ip4Address> availableIPList = service.getAvailableIPs(); |
| 101 | ObjectNode root = mapper().createObjectNode(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 102 | ArrayNode arrayNode = root.putArray("availableIP"); |
| 103 | availableIPList.forEach(i -> arrayNode.add(i.toString())); |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 104 | return ok(root).build(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Post a new static MAC/IP binding. |
| 109 | * Registers a static binding to the DHCP server, and displays the current set of bindings. |
| 110 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 111 | * @onos.rsModel DhcpConfigPut |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 112 | * @param stream JSON stream |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 113 | * @return 200 OK |
| 114 | */ |
| 115 | @POST |
| 116 | @Path("mappings") |
| 117 | @Consumes(MediaType.APPLICATION_JSON) |
| 118 | public Response setMapping(InputStream stream) { |
| 119 | ObjectNode root = mapper().createObjectNode(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 120 | try { |
| 121 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 122 | JsonNode macID = jsonTree.get("mac"); |
| 123 | JsonNode ip = jsonTree.get("ip"); |
| 124 | if (macID != null && ip != null) { |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 125 | IpAssignment ipAssignment = IpAssignment.builder() |
| 126 | .ipAddress(Ip4Address.valueOf(ip.asText())) |
| 127 | .leasePeriod(service.getLeaseTime()) |
| 128 | .timestamp(new Date()) |
| 129 | .assignmentStatus(Option_Requested) |
| 130 | .build(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 131 | |
| 132 | if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()), |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 133 | ipAssignment)) { |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 134 | throw new IllegalArgumentException("Static Mapping Failed. " + |
| 135 | "The IP maybe unavailable."); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 139 | final Map<HostId, IpAssignment> intents = service.listMapping(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 140 | ArrayNode arrayNode = root.putArray("mappings"); |
| 141 | intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 142 | .put("host", i.getKey().toString()) |
| 143 | .put("ip", i.getValue().ipAddress().toString()))); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 144 | } catch (IOException e) { |
| 145 | throw new IllegalArgumentException(e.getMessage()); |
| 146 | } |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 147 | return ok(root).build(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Delete a static MAC/IP binding. |
| 152 | * Removes a static binding from the DHCP Server, and displays the current set of bindings. |
| 153 | * |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 154 | * @param macID mac address identifier |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 155 | * @return 200 OK |
| 156 | */ |
| 157 | @DELETE |
| 158 | @Path("mappings/{macID}") |
| 159 | public Response deleteMapping(@PathParam("macID") String macID) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 160 | ObjectNode root = mapper().createObjectNode(); |
| 161 | |
| 162 | if (!service.removeStaticMapping(MacAddress.valueOf(macID))) { |
| 163 | throw new IllegalArgumentException("Static Mapping Removal Failed."); |
| 164 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 165 | final Map<HostId, IpAssignment> intents = service.listMapping(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 166 | ArrayNode arrayNode = root.putArray("mappings"); |
| 167 | intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 168 | .put("host", i.getKey().toString()) |
| 169 | .put("ip", i.getValue().ipAddress().toString()))); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 170 | |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 171 | return ok(root).build(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 172 | } |
| 173 | } |