blob: 38df577a311ea1a45fe2e0751015d53de29614ba [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;
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 palf28207b2015-09-04 10:41:56 -070025import org.onosproject.rest.AbstractWebResource;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.GET;
30import javax.ws.rs.POST;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.io.IOException;
36import java.io.InputStream;
37import java.util.Map;
38
39/**
40 * Manage DHCP address assignments.
41 */
42@Path("dhcp")
43public class DHCPWebResource extends AbstractWebResource {
44
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070045 final DhcpService service = get(DhcpService.class);
samanwita palf28207b2015-09-04 10:41:56 -070046
47 /**
48 * Get DHCP server configuration data.
49 * Shows lease, renewal and rebinding times in seconds.
50 *
51 * @return 200 OK
52 */
53 @GET
54 @Path("config")
55 public Response getConfigs() {
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070056 DhcpService service = get(DhcpService.class);
samanwita palf28207b2015-09-04 10:41:56 -070057 ObjectNode node = mapper().createObjectNode()
58 .put("leaseTime", service.getLeaseTime())
59 .put("renewalTime", service.getRenewalTime())
60 .put("rebindingTime", service.getRebindingTime());
61 return ok(node.toString()).build();
62 }
63
64 /**
65 * Get all MAC/IP mappings.
66 * Shows all MAC/IP mappings held by the DHCP server.
67 *
68 * @return 200 OK
69 */
70 @GET
71 @Path("mappings")
72 public Response listMappings() {
73 ObjectNode root = mapper().createObjectNode();
74
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070075 final Map<MacAddress, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -070076 ArrayNode arrayNode = root.putArray("mappings");
77 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
78 .put("mac", i.getKey().toString())
Thomas Vachuska54dc3522015-09-09 00:11:45 -070079 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -070080
81 return ok(root.toString()).build();
82 }
83
84
85
86 /**
87 * Get all available IPs.
88 * Shows all the IPs in the free pool of the DHCP Server.
89 *
90 * @return 200 OK
91 */
92 @GET
93 @Path("available")
94 public Response listAvailableIPs() {
95 final Iterable<Ip4Address> availableIPList = service.getAvailableIPs();
96
97 final ObjectNode root = mapper().createObjectNode();
98 ArrayNode arrayNode = root.putArray("availableIP");
99 availableIPList.forEach(i -> arrayNode.add(i.toString()));
100 return ok(root.toString()).build();
101 }
102
103 /**
104 * Post a new static MAC/IP binding.
105 * Registers a static binding to the DHCP server, and displays the current set of bindings.
106 *
107 * @return 200 OK
108 */
109 @POST
110 @Path("mappings")
111 @Consumes(MediaType.APPLICATION_JSON)
112 public Response setMapping(InputStream stream) {
113 ObjectNode root = mapper().createObjectNode();
114
115 try {
116 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
117 JsonNode macID = jsonTree.get("mac");
118 JsonNode ip = jsonTree.get("ip");
119 if (macID != null && ip != null) {
120
121 if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()),
122 Ip4Address.valueOf(ip.asText()))) {
123 throw new IllegalArgumentException("Static Mapping Failed. The IP maybe unavailable.");
124 }
125 }
126
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700127 final Map<MacAddress, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700128 ArrayNode arrayNode = root.putArray("mappings");
129 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
130 .put("mac", i.getKey().toString())
Thomas Vachuska54dc3522015-09-09 00:11:45 -0700131 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700132 } catch (IOException e) {
133 throw new IllegalArgumentException(e.getMessage());
134 }
135 return ok(root.toString()).build();
136 }
137
138 /**
139 * Delete a static MAC/IP binding.
140 * Removes a static binding from the DHCP Server, and displays the current set of bindings.
141 *
142 * @return 200 OK
143 */
144 @DELETE
145 @Path("mappings/{macID}")
146 public Response deleteMapping(@PathParam("macID") String macID) {
147
148 ObjectNode root = mapper().createObjectNode();
149
150 if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
151 throw new IllegalArgumentException("Static Mapping Removal Failed.");
152 }
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700153 final Map<MacAddress, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700154 ArrayNode arrayNode = root.putArray("mappings");
155 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
156 .put("mac", i.getKey().toString())
Thomas Vachuska54dc3522015-09-09 00:11:45 -0700157 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700158
159 return ok(root.toString()).build();
160 }
161}