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