Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.opencord.sadis.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.rest.AbstractWebResource; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import org.opencord.sadis.SubscriberAndDeviceInformationService; |
| 22 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 23 | import org.onlab.util.ItemNotFoundException; |
| 24 | |
| 25 | import java.net.URI; |
| 26 | import java.net.URISyntaxException; |
| 27 | |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.DELETE; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.Consumes; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | |
| 38 | /** |
| 39 | * Subscriber And Device Information Service web resource. |
| 40 | */ |
| 41 | @Path("sadisApp") |
| 42 | public class SadisWebResource extends AbstractWebResource { |
| 43 | private final ObjectNode root = mapper().createObjectNode(); |
| 44 | private final ArrayNode node = root.putArray("entry"); |
| 45 | private static final String SUBSCRIBER_NOT_FOUND = "Subscriber not found"; |
| 46 | private final SubscriberAndDeviceInformationService service = get(SubscriberAndDeviceInformationService.class); |
| 47 | |
| 48 | /** |
| 49 | * Get subscriber object. |
| 50 | * |
| 51 | * @param id |
| 52 | * ID of the subscriber |
| 53 | * |
| 54 | * @return 200 OK |
| 55 | */ |
| 56 | @GET |
| 57 | @Path("/subscriber/{id}") |
| 58 | @Produces(MediaType.APPLICATION_JSON) |
| 59 | public Response getSubscriber(@PathParam("id") String id) { |
| 60 | final SubscriberAndDeviceInformation entry = service.get(id); |
| 61 | if (entry == null) { |
| 62 | throw new ItemNotFoundException(SUBSCRIBER_NOT_FOUND); |
| 63 | } |
| 64 | node.add(codec(SubscriberAndDeviceInformation.class).encode(entry, this)); |
| 65 | return ok(root).build(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get subscriber object from the cache. |
| 70 | * |
| 71 | * @param id |
| 72 | * ID of the subscriber |
| 73 | * |
| 74 | * @return 200 OK |
| 75 | */ |
| 76 | @GET |
| 77 | @Path("/cache/subscriber/{id}") |
| 78 | @Produces(MediaType.APPLICATION_JSON) |
| 79 | public Response getSubscriberCache(@PathParam("id") String id) { |
| 80 | final SubscriberAndDeviceInformation entry = service.getfromCache(id); |
| 81 | if (entry == null) { |
| 82 | throw new ItemNotFoundException(SUBSCRIBER_NOT_FOUND); |
| 83 | } |
| 84 | node.add(codec(SubscriberAndDeviceInformation.class).encode(entry, this)); |
| 85 | return ok(root).build(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Create subscriber object. |
| 90 | * |
| 91 | * @return 201 Created |
| 92 | */ |
| 93 | @POST |
| 94 | @Path("/subscriber") |
| 95 | @Consumes(MediaType.APPLICATION_JSON) |
| 96 | public Response postSubscriber() { |
| 97 | try { |
| 98 | return Response.created(new URI("/subsciber/123")).build(); |
| 99 | } catch (URISyntaxException e) { |
| 100 | return Response.serverError().build(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Delete subscriber object. |
| 106 | * |
| 107 | * @param id |
| 108 | * ID of the subscriber |
| 109 | * @return 204 NoContent |
| 110 | */ |
| 111 | @DELETE |
| 112 | @Path("/cache/subscriber/{id}") |
| 113 | public Response deleteSubscriber(@PathParam("id") String id) { |
| 114 | service.invalidateId(id); |
| 115 | return Response.noContent().build(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Delete all the subscriber objects. |
| 120 | * |
| 121 | * @return 204 NoContent |
| 122 | */ |
| 123 | @DELETE |
| 124 | @Path("/cache/subscriber/") |
| 125 | public Response deleteAllSubscribers() { |
| 126 | service.invalidateAll(); |
| 127 | return Response.noContent().build(); |
| 128 | } |
| 129 | } |