Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -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.dhcpserver.rest; |
| 17 | |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.MacAddress; |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 23 | import org.onosproject.dhcpserver.DHCPService; |
| 24 | import org.onosproject.rest.AbstractWebResource; |
| 25 | |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 28 | import javax.ws.rs.GET; |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 29 | import javax.ws.rs.POST; |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 30 | import javax.ws.rs.Path; |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.core.MediaType; |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 33 | import javax.ws.rs.core.Response; |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.Map; |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Manage DHCP address assignments. |
| 40 | */ |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 41 | @Path("dhcp") |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 42 | public class DHCPWebResource extends AbstractWebResource { |
| 43 | |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 44 | final DHCPService service = get(DHCPService.class); |
| 45 | |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Get DHCP server configuration data. |
| 48 | * Shows lease, renewal and rebinding times in seconds. |
| 49 | * |
| 50 | * @return 200 OK |
| 51 | */ |
| 52 | @GET |
| 53 | @Path("config") |
| 54 | public Response getConfigs() { |
| 55 | DHCPService service = get(DHCPService.class); |
| 56 | ObjectNode node = mapper().createObjectNode() |
| 57 | .put("leaseTime", service.getLeaseTime()) |
| 58 | .put("renewalTime", service.getRenewalTime()) |
| 59 | .put("rebindingTime", service.getRebindingTime()); |
| 60 | return ok(node.toString()).build(); |
| 61 | } |
| 62 | |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 63 | /** |
| 64 | * Get all MAC/IP mappings. |
| 65 | * Shows all MAC/IP mappings held by the DHCP server. |
| 66 | * |
| 67 | * @return 200 OK |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 68 | */ |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 69 | @GET |
| 70 | @Path("mappings") |
| 71 | public Response listMappings() { |
| 72 | ObjectNode root = mapper().createObjectNode(); |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 73 | |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 74 | final Map<MacAddress, Ip4Address> intents = service.listMapping(); |
| 75 | ArrayNode arrayNode = root.putArray("mappings"); |
| 76 | intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() |
| 77 | .put("mac", i.getKey().toString()) |
| 78 | .put("ip", i.getValue().toString()))); |
| 79 | |
| 80 | return ok(root.toString()).build(); |
| 81 | } |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 82 | |
| 83 | |
| 84 | |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 85 | /** |
| 86 | * Get all available IPs. |
| 87 | * Shows all the IPs in the free pool of the DHCP Server. |
| 88 | * |
| 89 | * @return 200 OK |
| 90 | */ |
| 91 | @GET |
| 92 | @Path("available") |
| 93 | public Response listAvailableIPs() { |
| 94 | final Iterable<Ip4Address> availableIPList = service.getAvailableIPs(); |
| 95 | |
| 96 | final ObjectNode root = mapper().createObjectNode(); |
| 97 | ArrayNode arrayNode = root.putArray("availableIP"); |
| 98 | availableIPList.forEach(i -> arrayNode.add(i.toString())); |
| 99 | return ok(root.toString()).build(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Post a new static MAC/IP binding. |
| 104 | * Registers a static binding to the DHCP server, and displays the current set of bindings. |
| 105 | * |
| 106 | * @return 200 OK |
| 107 | */ |
| 108 | @POST |
| 109 | @Path("mappings") |
| 110 | @Consumes(MediaType.APPLICATION_JSON) |
| 111 | public Response setMapping(InputStream stream) { |
| 112 | ObjectNode root = mapper().createObjectNode(); |
| 113 | |
| 114 | try { |
| 115 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 116 | JsonNode macID = jsonTree.get("mac"); |
| 117 | JsonNode ip = jsonTree.get("ip"); |
| 118 | if (macID != null && ip != null) { |
| 119 | |
| 120 | if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()), |
| 121 | Ip4Address.valueOf(ip.asText()))) { |
| 122 | throw new IllegalArgumentException("Static Mapping Failed. The IP maybe unavailable."); |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 123 | } |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 124 | } |
| 125 | |
| 126 | final Map<MacAddress, Ip4Address> intents = service.listMapping(); |
| 127 | ArrayNode arrayNode = root.putArray("mappings"); |
| 128 | intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() |
| 129 | .put("mac", i.getKey().toString()) |
| 130 | .put("ip", i.getValue().toString()))); |
| 131 | } catch (IOException e) { |
| 132 | throw new IllegalArgumentException(e.getMessage()); |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 133 | } |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 134 | return ok(root.toString()).build(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Delete a static MAC/IP binding. |
| 139 | * Removes a static binding from the DHCP Server, and displays the current set of bindings. |
| 140 | * |
| 141 | * @return 200 OK |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 142 | */ |
samanwita pal | ab127d0 | 2015-08-25 13:26:51 -0700 | [diff] [blame^] | 143 | @DELETE |
| 144 | @Path("mappings/{macID}") |
| 145 | public Response deleteMapping(@PathParam("macID") String macID) { |
| 146 | |
| 147 | ObjectNode root = mapper().createObjectNode(); |
| 148 | |
| 149 | if (!service.removeStaticMapping(MacAddress.valueOf(macID))) { |
| 150 | throw new IllegalArgumentException("Static Mapping Removal Failed."); |
| 151 | } |
| 152 | final Map<MacAddress, Ip4Address> intents = service.listMapping(); |
| 153 | ArrayNode arrayNode = root.putArray("mappings"); |
| 154 | intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() |
| 155 | .put("mac", i.getKey().toString()) |
| 156 | .put("ip", i.getValue().toString()))); |
| 157 | |
| 158 | return ok(root.toString()).build(); |
| 159 | } |
Thomas Vachuska | b81c278 | 2015-08-21 14:21:50 -0700 | [diff] [blame] | 160 | } |