blob: 3b5baf964e51a2f1aea81cb89385bc56d5c4ba3f [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
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 */
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;
danielcd9deed2015-10-30 17:16:16 +090021import com.google.common.collect.Lists;
samanwita palf28207b2015-09-04 10:41:56 -070022import org.onlab.packet.Ip4Address;
23import org.onlab.packet.MacAddress;
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070024import org.onosproject.dhcp.DhcpService;
25import org.onosproject.dhcp.IpAssignment;
samanwita pal2a313402015-09-14 16:03:22 -070026import org.onosproject.net.HostId;
samanwita palf28207b2015-09-04 10:41:56 -070027import org.onosproject.rest.AbstractWebResource;
28
29import javax.ws.rs.Consumes;
30import javax.ws.rs.DELETE;
31import javax.ws.rs.GET;
32import javax.ws.rs.POST;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37import java.io.IOException;
38import java.io.InputStream;
39import java.util.Map;
40
41/**
42 * Manage DHCP address assignments.
43 */
44@Path("dhcp")
Jonathan Hartb35540a2015-11-17 09:30:56 -080045public class DhcpWebResource extends AbstractWebResource {
samanwita palf28207b2015-09-04 10:41:56 -070046
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070047 final DhcpService service = get(DhcpService.class);
samanwita palf28207b2015-09-04 10:41:56 -070048
49 /**
50 * Get DHCP server configuration data.
51 * Shows lease, renewal and rebinding times in seconds.
52 *
53 * @return 200 OK
andreafaa2c4b2015-11-16 13:48:39 -080054 * @rsModel DhcpConfigGet
samanwita palf28207b2015-09-04 10:41:56 -070055 */
56 @GET
57 @Path("config")
58 public Response getConfigs() {
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070059 DhcpService service = get(DhcpService.class);
samanwita palf28207b2015-09-04 10:41:56 -070060 ObjectNode node = mapper().createObjectNode()
61 .put("leaseTime", service.getLeaseTime())
62 .put("renewalTime", service.getRenewalTime())
63 .put("rebindingTime", service.getRebindingTime());
64 return ok(node.toString()).build();
65 }
66
67 /**
68 * Get all MAC/IP mappings.
69 * Shows all MAC/IP mappings held by the DHCP server.
70 *
71 * @return 200 OK
72 */
73 @GET
74 @Path("mappings")
75 public Response listMappings() {
76 ObjectNode root = mapper().createObjectNode();
77
samanwita pal2a313402015-09-14 16:03:22 -070078 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -070079 ArrayNode arrayNode = root.putArray("mappings");
80 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -080081 .put("host", i.getKey().toString())
82 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -070083
84 return ok(root.toString()).build();
85 }
86
87
samanwita palf28207b2015-09-04 10:41:56 -070088 /**
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 *
andreafaa2c4b2015-11-16 13:48:39 -0800109 * @rsModel DhcpConfigPut
Ray Milkey9b36d812015-09-09 15:24:54 -0700110 * @param stream JSON stream
samanwita palf28207b2015-09-04 10:41:56 -0700111 * @return 200 OK
112 */
113 @POST
114 @Path("mappings")
115 @Consumes(MediaType.APPLICATION_JSON)
116 public Response setMapping(InputStream stream) {
117 ObjectNode root = mapper().createObjectNode();
samanwita palf28207b2015-09-04 10:41:56 -0700118 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()),
andreafaa2c4b2015-11-16 13:48:39 -0800125 Ip4Address.valueOf(ip.asText()),
126 false, Lists.newArrayList())) {
127 throw new IllegalArgumentException("Static Mapping Failed. " +
128 "The IP maybe unavailable.");
samanwita palf28207b2015-09-04 10:41:56 -0700129 }
130 }
131
samanwita pal2a313402015-09-14 16:03:22 -0700132 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700133 ArrayNode arrayNode = root.putArray("mappings");
134 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -0800135 .put("host", i.getKey().toString())
136 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700137 } catch (IOException e) {
138 throw new IllegalArgumentException(e.getMessage());
139 }
140 return ok(root.toString()).build();
141 }
142
143 /**
144 * Delete a static MAC/IP binding.
145 * Removes a static binding from the DHCP Server, and displays the current set of bindings.
146 *
Ray Milkey9b36d812015-09-09 15:24:54 -0700147 * @param macID mac address identifier
samanwita palf28207b2015-09-04 10:41:56 -0700148 * @return 200 OK
149 */
150 @DELETE
151 @Path("mappings/{macID}")
152 public Response deleteMapping(@PathParam("macID") String macID) {
153
154 ObjectNode root = mapper().createObjectNode();
155
156 if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
157 throw new IllegalArgumentException("Static Mapping Removal Failed.");
158 }
samanwita pal2a313402015-09-14 16:03:22 -0700159 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700160 ArrayNode arrayNode = root.putArray("mappings");
161 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -0800162 .put("host", i.getKey().toString())
163 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700164
165 return ok(root.toString()).build();
166 }
167}