blob: bfa2767d89f53e4c0708d29f7b4a6bbe55a5dee6 [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 *
Ray Milkey9b36d812015-09-09 15:24:54 -0700107 * @param stream JSON stream
samanwita palf28207b2015-09-04 10:41:56 -0700108 * @return 200 OK
109 */
110 @POST
111 @Path("mappings")
112 @Consumes(MediaType.APPLICATION_JSON)
113 public Response setMapping(InputStream stream) {
114 ObjectNode root = mapper().createObjectNode();
115
116 try {
117 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
118 JsonNode macID = jsonTree.get("mac");
119 JsonNode ip = jsonTree.get("ip");
120 if (macID != null && ip != null) {
121
122 if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()),
123 Ip4Address.valueOf(ip.asText()))) {
124 throw new IllegalArgumentException("Static Mapping Failed. The IP maybe unavailable.");
125 }
126 }
127
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700128 final Map<MacAddress, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700129 ArrayNode arrayNode = root.putArray("mappings");
130 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
131 .put("mac", i.getKey().toString())
Thomas Vachuska54dc3522015-09-09 00:11:45 -0700132 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700133 } catch (IOException e) {
134 throw new IllegalArgumentException(e.getMessage());
135 }
136 return ok(root.toString()).build();
137 }
138
139 /**
140 * Delete a static MAC/IP binding.
141 * Removes a static binding from the DHCP Server, and displays the current set of bindings.
142 *
Ray Milkey9b36d812015-09-09 15:24:54 -0700143 * @param macID mac address identifier
samanwita palf28207b2015-09-04 10:41:56 -0700144 * @return 200 OK
145 */
146 @DELETE
147 @Path("mappings/{macID}")
148 public Response deleteMapping(@PathParam("macID") String macID) {
149
150 ObjectNode root = mapper().createObjectNode();
151
152 if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
153 throw new IllegalArgumentException("Static Mapping Removal Failed.");
154 }
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700155 final Map<MacAddress, IpAssignment> intents = service.listMapping();
samanwita palf28207b2015-09-04 10:41:56 -0700156 ArrayNode arrayNode = root.putArray("mappings");
157 intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
158 .put("mac", i.getKey().toString())
Thomas Vachuska54dc3522015-09-09 00:11:45 -0700159 .put("ip", i.getValue().ipAddress().toString())));
samanwita palf28207b2015-09-04 10:41:56 -0700160
161 return ok(root.toString()).build();
162 }
163}