blob: 9338a4bfb0cba0d192cf3159469a88694946b8dc [file] [log] [blame]
psnehab7109092018-10-10 08:26:02 -04001/*
2 * Copyright 2018-present Open Networking Foundation
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 *
16 */
17
18package org.onosproject.dhcprelay.rest;
19
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.IpPrefix;
22import org.onosproject.dhcprelay.api.DhcpRelayService;
23import org.onosproject.rest.AbstractWebResource;
24import org.onosproject.routeservice.Route;
25import org.onosproject.routeservice.RouteStore;
26import org.onosproject.routing.fpm.api.FpmRecord;
27import org.slf4j.Logger;
28
29import javax.ws.rs.DELETE;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.core.Response;
33
34import java.io.IOException;
35import java.util.Optional;
36
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * DHCP Relay agent REST API.
41 */
42@Path("fpm-delete")
43public class DhcpRelayWebResource extends AbstractWebResource {
44 private static final Logger LOG = getLogger(DhcpRelayWebResource.class);
45
46 /**
47 * Deletes the fpm route from fpm record.
48 * Corresponding route from the route store
49 *
50 * @param prefix IpPrefix
51 * @return 204 NO CONTENT
52 * @throws IOException to signify bad request
53 */
54 @DELETE
55 @Path("{prefix}")
56 public Response dhcpFpmDelete(@PathParam("prefix") String prefix) {
57 DhcpRelayService dhcpRelayService = get(DhcpRelayService.class);
58 RouteStore routeStore = get(RouteStore.class);
59
60 try {
61 // removes fpm route from fpm record
62 Optional<FpmRecord> fpmRecord = dhcpRelayService.removeFpmRecord(IpPrefix.valueOf(prefix));
63 if (fpmRecord.isPresent()) {
64 IpAddress nextHop = fpmRecord.get().nextHop();
65 Route route = new Route(Route.Source.DHCP, IpPrefix.valueOf(prefix), nextHop);
66 // removes DHCP route from route store
67 routeStore.removeRoute(route);
68 } else {
69 LOG.warn("fpmRecord is not present");
70 }
71 } catch (IllegalArgumentException ex) {
72 throw new IllegalArgumentException(ex);
73 }
74
75 return Response.noContent().build();
76 }
77
78}