blob: 6265fee0faee068aecd79818c3e10098f0e7f557 [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
Andrea Campanella10c4adc2015-12-03 15:27:54 -080054 * @onos.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 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -080071 * @onos.rsModel DhcpConfigGetMappings
samanwita palf28207b2015-09-04 10:41:56 -070072 * @return 200 OK
73 */
74 @GET
75 @Path("mappings")
76 public Response listMappings() {
77 ObjectNode root = mapper().createObjectNode();
78
samanwita pal2a313402015-09-14 16:03:22 -070079 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -070080 ArrayNode arrayNode = root.putArray("mappings");
81 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -080082 .put("host", i.getKey().toString())
83 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -070084
85 return ok(root.toString()).build();
86 }
87
88
samanwita palf28207b2015-09-04 10:41:56 -070089 /**
90 * Get all available IPs.
91 * Shows all the IPs in the free pool of the DHCP Server.
92 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -080093 * @onos.rsModel DhcpConfigGetAvailable
samanwita palf28207b2015-09-04 10:41:56 -070094 * @return 200 OK
95 */
96 @GET
97 @Path("available")
98 public Response listAvailableIPs() {
99 final Iterable<Ip4Address> availableIPList = service.getAvailableIPs();
100
101 final ObjectNode root = mapper().createObjectNode();
102 ArrayNode arrayNode = root.putArray("availableIP");
103 availableIPList.forEach(i -> arrayNode.add(i.toString()));
104 return ok(root.toString()).build();
105 }
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) {
125
126 if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()),
andreafaa2c4b2015-11-16 13:48:39 -0800127 Ip4Address.valueOf(ip.asText()),
128 false, Lists.newArrayList())) {
129 throw new IllegalArgumentException("Static Mapping Failed. " +
130 "The IP maybe unavailable.");
samanwita palf28207b2015-09-04 10:41:56 -0700131 }
132 }
133
samanwita pal2a313402015-09-14 16:03:22 -0700134 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700135 ArrayNode arrayNode = root.putArray("mappings");
136 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -0800137 .put("host", i.getKey().toString())
138 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700139 } catch (IOException e) {
140 throw new IllegalArgumentException(e.getMessage());
141 }
142 return ok(root.toString()).build();
143 }
144
145 /**
146 * Delete a static MAC/IP binding.
147 * Removes a static binding from the DHCP Server, and displays the current set of bindings.
148 *
Ray Milkey9b36d812015-09-09 15:24:54 -0700149 * @param macID mac address identifier
samanwita palf28207b2015-09-04 10:41:56 -0700150 * @return 200 OK
151 */
152 @DELETE
153 @Path("mappings/{macID}")
154 public Response deleteMapping(@PathParam("macID") String macID) {
155
156 ObjectNode root = mapper().createObjectNode();
157
158 if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
159 throw new IllegalArgumentException("Static Mapping Removal Failed.");
160 }
samanwita pal2a313402015-09-14 16:03:22 -0700161 final Map<HostId, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700162 ArrayNode arrayNode = root.putArray("mappings");
163 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
andreafaa2c4b2015-11-16 13:48:39 -0800164 .put("host", i.getKey().toString())
165 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700166
167 return ok(root.toString()).build();
168 }
169}