blob: fb4ee79206deafb3d50d4ea6a87d4d63a0add4c3 [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
samanwita palf28207b2015-09-04 10:41:56 -07003 *
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.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.MacAddress;
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070023import org.onosproject.dhcp.DhcpService;
24import org.onosproject.dhcp.IpAssignment;
samanwita pal2a313402015-09-14 16:03:22 -070025import org.onosproject.net.HostId;
samanwita palf28207b2015-09-04 10:41:56 -070026import org.onosproject.rest.AbstractWebResource;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.DELETE;
30import javax.ws.rs.GET;
31import javax.ws.rs.POST;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import java.io.IOException;
37import java.io.InputStream;
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070038import java.util.Date;
samanwita palf28207b2015-09-04 10:41:56 -070039import java.util.Map;
40
Ray Milkey86ee5e82018-04-02 15:33:07 -070041import static org.onlab.util.Tools.readTreeFromStream;
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070042import static org.onosproject.dhcp.IpAssignment.AssignmentStatus.Option_Requested;
43
samanwita palf28207b2015-09-04 10:41:56 -070044/**
45 * Manage DHCP address assignments.
46 */
47@Path("dhcp")
Jonathan Hartb35540a2015-11-17 09:30:56 -080048public class DhcpWebResource extends AbstractWebResource {
samanwita palf28207b2015-09-04 10:41:56 -070049
Thomas Vachuskaa026be72015-12-07 16:00:37 -080050 private final DhcpService service = get(DhcpService.class);
samanwita palf28207b2015-09-04 10:41:56 -070051
52 /**
53 * Get DHCP server configuration data.
54 * Shows lease, renewal and rebinding times in seconds.
55 *
56 * @return 200 OK
Andrea Campanella10c4adc2015-12-03 15:27:54 -080057 * @onos.rsModel DhcpConfigGet
samanwita palf28207b2015-09-04 10:41:56 -070058 */
59 @GET
60 @Path("config")
61 public Response getConfigs() {
samanwita palf28207b2015-09-04 10:41:56 -070062 ObjectNode node = mapper().createObjectNode()
63 .put("leaseTime", service.getLeaseTime())
64 .put("renewalTime", service.getRenewalTime())
65 .put("rebindingTime", service.getRebindingTime());
Thomas Vachuskaa026be72015-12-07 16:00:37 -080066 return ok(node).build();
samanwita palf28207b2015-09-04 10:41:56 -070067 }
68
69 /**
70 * Get all MAC/IP mappings.
71 * Shows all MAC/IP mappings held by the DHCP server.
72 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -080073 * @onos.rsModel DhcpConfigGetMappings
samanwita palf28207b2015-09-04 10:41:56 -070074 * @return 200 OK
75 */
76 @GET
77 @Path("mappings")
78 public Response listMappings() {
79 ObjectNode root = mapper().createObjectNode();
80
Thomas Vachuskaa026be72015-12-07 16:00:37 -080081 Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -070082 ArrayNode arrayNode = root.putArray("mappings");
83 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -080084 .put("host", i.getKey().toString())
85 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -070086
Thomas Vachuskaa026be72015-12-07 16:00:37 -080087 return ok(root).build();
samanwita palf28207b2015-09-04 10:41:56 -070088 }
89
90
samanwita palf28207b2015-09-04 10:41:56 -070091 /**
92 * Get all available IPs.
93 * Shows all the IPs in the free pool of the DHCP Server.
94 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -080095 * @onos.rsModel DhcpConfigGetAvailable
samanwita palf28207b2015-09-04 10:41:56 -070096 * @return 200 OK
97 */
98 @GET
99 @Path("available")
100 public Response listAvailableIPs() {
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800101 Iterable<Ip4Address> availableIPList = service.getAvailableIPs();
102 ObjectNode root = mapper().createObjectNode();
samanwita palf28207b2015-09-04 10:41:56 -0700103 ArrayNode arrayNode = root.putArray("availableIP");
104 availableIPList.forEach(i -> arrayNode.add(i.toString()));
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800105 return ok(root).build();
samanwita palf28207b2015-09-04 10:41:56 -0700106 }
107
108 /**
109 * Post a new static MAC/IP binding.
110 * Registers a static binding to the DHCP server, and displays the current set of bindings.
111 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800112 * @onos.rsModel DhcpConfigPut
Ray Milkey9b36d812015-09-09 15:24:54 -0700113 * @param stream JSON stream
samanwita palf28207b2015-09-04 10:41:56 -0700114 * @return 200 OK
115 */
116 @POST
117 @Path("mappings")
118 @Consumes(MediaType.APPLICATION_JSON)
119 public Response setMapping(InputStream stream) {
120 ObjectNode root = mapper().createObjectNode();
samanwita palf28207b2015-09-04 10:41:56 -0700121 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700122 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
samanwita palf28207b2015-09-04 10:41:56 -0700123 JsonNode macID = jsonTree.get("mac");
124 JsonNode ip = jsonTree.get("ip");
125 if (macID != null && ip != null) {
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700126 IpAssignment ipAssignment = IpAssignment.builder()
127 .ipAddress(Ip4Address.valueOf(ip.asText()))
128 .leasePeriod(service.getLeaseTime())
129 .timestamp(new Date())
130 .assignmentStatus(Option_Requested)
131 .build();
samanwita palf28207b2015-09-04 10:41:56 -0700132
133 if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()),
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700134 ipAssignment)) {
andreafaa2c4b2015-11-16 13:48:39 -0800135 throw new IllegalArgumentException("Static Mapping Failed. " +
136 "The IP maybe unavailable.");
samanwita palf28207b2015-09-04 10:41:56 -0700137 }
138 }
139
samanwita pal2a313402015-09-14 16:03:22 -0700140 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700141 ArrayNode arrayNode = root.putArray("mappings");
142 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -0800143 .put("host", i.getKey().toString())
144 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700145 } catch (IOException e) {
146 throw new IllegalArgumentException(e.getMessage());
147 }
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800148 return ok(root).build();
samanwita palf28207b2015-09-04 10:41:56 -0700149 }
150
151 /**
152 * Delete a static MAC/IP binding.
153 * Removes a static binding from the DHCP Server, and displays the current set of bindings.
154 *
Ray Milkey9b36d812015-09-09 15:24:54 -0700155 * @param macID mac address identifier
samanwita palf28207b2015-09-04 10:41:56 -0700156 * @return 200 OK
157 */
158 @DELETE
159 @Path("mappings/{macID}")
160 public Response deleteMapping(@PathParam("macID") String macID) {
samanwita palf28207b2015-09-04 10:41:56 -0700161 ObjectNode root = mapper().createObjectNode();
162
163 if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
164 throw new IllegalArgumentException("Static Mapping Removal Failed.");
165 }
samanwita pal2a313402015-09-14 16:03:22 -0700166 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700167 ArrayNode arrayNode = root.putArray("mappings");
168 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -0800169 .put("host", i.getKey().toString())
170 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700171
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800172 return ok(root).build();
samanwita palf28207b2015-09-04 10:41:56 -0700173 }
174}