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