psneha | b710909 | 2018-10-10 08:26:02 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.dhcprelay.rest; |
| 19 | |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onosproject.dhcprelay.api.DhcpRelayService; |
| 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.onosproject.routeservice.Route; |
| 25 | import org.onosproject.routeservice.RouteStore; |
| 26 | import org.onosproject.routing.fpm.api.FpmRecord; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import javax.ws.rs.DELETE; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.core.Response; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.util.Optional; |
| 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * DHCP Relay agent REST API. |
| 41 | */ |
| 42 | @Path("fpm-delete") |
| 43 | public 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 | } |