blob: 1d4174b69363b06991b7e056117b53d7b376f54b [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
54 */
55 @GET
56 @Path("config")
57 public Response getConfigs() {
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070058 DhcpService service = get(DhcpService.class);
samanwita palf28207b2015-09-04 10:41:56 -070059 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 pal2a313402015-09-14 16:03:22 -070077 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -070078 ArrayNode arrayNode = root.putArray("mappings");
79 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
samanwita pal2a313402015-09-14 16:03:22 -070080 .put("host", i.getKey().toString())
Thomas Vachuska54dc3522015-09-09 00:11:45 -070081 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -070082
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 Milkey9b36d812015-09-09 15:24:54 -0700109 * @param stream JSON stream
samanwita palf28207b2015-09-04 10:41:56 -0700110 * @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()),
danielcd9deed2015-10-30 17:16:16 +0900125 Ip4Address.valueOf(ip.asText()), false, Lists.newArrayList())) {
samanwita palf28207b2015-09-04 10:41:56 -0700126 throw new IllegalArgumentException("Static Mapping Failed. The IP maybe unavailable.");
127 }
128 }
129
samanwita pal2a313402015-09-14 16:03:22 -0700130 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700131 ArrayNode arrayNode = root.putArray("mappings");
132 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
samanwita pal2a313402015-09-14 16:03:22 -0700133 .put("host", i.getKey().toString())
Thomas Vachuska54dc3522015-09-09 00:11:45 -0700134 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700135 } 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 Milkey9b36d812015-09-09 15:24:54 -0700145 * @param macID mac address identifier
samanwita palf28207b2015-09-04 10:41:56 -0700146 * @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 pal2a313402015-09-14 16:03:22 -0700157 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700158 ArrayNode arrayNode = root.putArray("mappings");
159 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
samanwita pal2a313402015-09-14 16:03:22 -0700160 .put("host", i.getKey().toString())
Thomas Vachuska54dc3522015-09-09 00:11:45 -0700161 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700162
163 return ok(root.toString()).build();
164 }
165}