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