blob: 7cae8500de93a25d9b08c9f3f6b5206e5cbc1de4 [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
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070041import static org.onosproject.dhcp.IpAssignment.AssignmentStatus.Option_Requested;
42
samanwita palf28207b2015-09-04 10:41:56 -070043/**
44 * Manage DHCP address assignments.
45 */
46@Path("dhcp")
Jonathan Hartb35540a2015-11-17 09:30:56 -080047public class DhcpWebResource extends AbstractWebResource {
samanwita palf28207b2015-09-04 10:41:56 -070048
Thomas Vachuskaa026be72015-12-07 16:00:37 -080049 private final DhcpService service = get(DhcpService.class);
samanwita palf28207b2015-09-04 10:41:56 -070050
51 /**
52 * Get DHCP server configuration data.
53 * Shows lease, renewal and rebinding times in seconds.
54 *
55 * @return 200 OK
Andrea Campanella10c4adc2015-12-03 15:27:54 -080056 * @onos.rsModel DhcpConfigGet
samanwita palf28207b2015-09-04 10:41:56 -070057 */
58 @GET
59 @Path("config")
60 public Response getConfigs() {
samanwita palf28207b2015-09-04 10:41:56 -070061 ObjectNode node = mapper().createObjectNode()
62 .put("leaseTime", service.getLeaseTime())
63 .put("renewalTime", service.getRenewalTime())
64 .put("rebindingTime", service.getRebindingTime());
Thomas Vachuskaa026be72015-12-07 16:00:37 -080065 return ok(node).build();
samanwita palf28207b2015-09-04 10:41:56 -070066 }
67
68 /**
69 * Get all MAC/IP mappings.
70 * Shows all MAC/IP mappings held by the DHCP server.
71 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -080072 * @onos.rsModel DhcpConfigGetMappings
samanwita palf28207b2015-09-04 10:41:56 -070073 * @return 200 OK
74 */
75 @GET
76 @Path("mappings")
77 public Response listMappings() {
78 ObjectNode root = mapper().createObjectNode();
79
Thomas Vachuskaa026be72015-12-07 16:00:37 -080080 Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -070081 ArrayNode arrayNode = root.putArray("mappings");
82 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -080083 .put("host", i.getKey().toString())
84 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -070085
Thomas Vachuskaa026be72015-12-07 16:00:37 -080086 return ok(root).build();
samanwita palf28207b2015-09-04 10:41:56 -070087 }
88
89
samanwita palf28207b2015-09-04 10:41:56 -070090 /**
91 * Get all available IPs.
92 * Shows all the IPs in the free pool of the DHCP Server.
93 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -080094 * @onos.rsModel DhcpConfigGetAvailable
samanwita palf28207b2015-09-04 10:41:56 -070095 * @return 200 OK
96 */
97 @GET
98 @Path("available")
99 public Response listAvailableIPs() {
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800100 Iterable<Ip4Address> availableIPList = service.getAvailableIPs();
101 ObjectNode root = mapper().createObjectNode();
samanwita palf28207b2015-09-04 10:41:56 -0700102 ArrayNode arrayNode = root.putArray("availableIP");
103 availableIPList.forEach(i -> arrayNode.add(i.toString()));
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800104 return ok(root).build();
samanwita palf28207b2015-09-04 10:41:56 -0700105 }
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 Campanella10c4adc2015-12-03 15:27:54 -0800111 * @onos.rsModel DhcpConfigPut
Ray Milkey9b36d812015-09-09 15:24:54 -0700112 * @param stream JSON stream
samanwita palf28207b2015-09-04 10:41:56 -0700113 * @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 palf28207b2015-09-04 10:41:56 -0700120 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 Moon04b1fe92016-05-18 21:28:06 -0700125 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 palf28207b2015-09-04 10:41:56 -0700131
132 if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()),
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700133 ipAssignment)) {
andreafaa2c4b2015-11-16 13:48:39 -0800134 throw new IllegalArgumentException("Static Mapping Failed. " +
135 "The IP maybe unavailable.");
samanwita palf28207b2015-09-04 10:41:56 -0700136 }
137 }
138
samanwita pal2a313402015-09-14 16:03:22 -0700139 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700140 ArrayNode arrayNode = root.putArray("mappings");
141 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -0800142 .put("host", i.getKey().toString())
143 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700144 } catch (IOException e) {
145 throw new IllegalArgumentException(e.getMessage());
146 }
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800147 return ok(root).build();
samanwita palf28207b2015-09-04 10:41:56 -0700148 }
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 Milkey9b36d812015-09-09 15:24:54 -0700154 * @param macID mac address identifier
samanwita palf28207b2015-09-04 10:41:56 -0700155 * @return 200 OK
156 */
157 @DELETE
158 @Path("mappings/{macID}")
159 public Response deleteMapping(@PathParam("macID") String macID) {
samanwita palf28207b2015-09-04 10:41:56 -0700160 ObjectNode root = mapper().createObjectNode();
161
162 if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
163 throw new IllegalArgumentException("Static Mapping Removal Failed.");
164 }
samanwita pal2a313402015-09-14 16:03:22 -0700165 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700166 ArrayNode arrayNode = root.putArray("mappings");
167 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -0800168 .put("host", i.getKey().toString())
169 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700170
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800171 return ok(root).build();
samanwita palf28207b2015-09-04 10:41:56 -0700172 }
173}