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; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 21 | import org.opencord.sadis.BandwidthProfileInformation; |
| 22 | import org.opencord.sadis.BaseInformationService; |
| 23 | import org.opencord.sadis.SadisService; |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 24 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 25 | import org.onlab.util.ItemNotFoundException; |
| 26 | |
| 27 | import java.net.URI; |
| 28 | import java.net.URISyntaxException; |
| 29 | |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.POST; |
| 32 | import javax.ws.rs.DELETE; |
| 33 | import javax.ws.rs.Path; |
| 34 | import javax.ws.rs.PathParam; |
| 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.Consumes; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | |
| 40 | /** |
| 41 | * Subscriber And Device Information Service web resource. |
| 42 | */ |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 43 | @Path("sadis") |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 44 | public class SadisWebResource extends AbstractWebResource { |
| 45 | private final ObjectNode root = mapper().createObjectNode(); |
| 46 | private final ArrayNode node = root.putArray("entry"); |
| 47 | private static final String SUBSCRIBER_NOT_FOUND = "Subscriber not found"; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 48 | private static final String BP_NOT_FOUND = "Bandwidth Profile not found"; |
| 49 | private final SadisService sadisService = get(SadisService.class); |
| 50 | private final BaseInformationService<SubscriberAndDeviceInformation> subService = |
| 51 | sadisService.getSubscriberInfoService(); |
| 52 | private final BaseInformationService<BandwidthProfileInformation> bpService = |
| 53 | sadisService.getBandwidthProfileService(); |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Get subscriber object. |
| 57 | * |
| 58 | * @param id |
| 59 | * ID of the subscriber |
| 60 | * |
| 61 | * @return 200 OK |
| 62 | */ |
| 63 | @GET |
| 64 | @Path("/subscriber/{id}") |
| 65 | @Produces(MediaType.APPLICATION_JSON) |
| 66 | public Response getSubscriber(@PathParam("id") String id) { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 67 | final SubscriberAndDeviceInformation entry = subService.get(id); |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 68 | if (entry == null) { |
| 69 | throw new ItemNotFoundException(SUBSCRIBER_NOT_FOUND); |
| 70 | } |
| 71 | node.add(codec(SubscriberAndDeviceInformation.class).encode(entry, this)); |
| 72 | return ok(root).build(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get subscriber object from the cache. |
| 77 | * |
| 78 | * @param id |
| 79 | * ID of the subscriber |
| 80 | * |
| 81 | * @return 200 OK |
| 82 | */ |
| 83 | @GET |
| 84 | @Path("/cache/subscriber/{id}") |
| 85 | @Produces(MediaType.APPLICATION_JSON) |
| 86 | public Response getSubscriberCache(@PathParam("id") String id) { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 87 | final SubscriberAndDeviceInformation entry = subService.getfromCache(id); |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 88 | if (entry == null) { |
| 89 | throw new ItemNotFoundException(SUBSCRIBER_NOT_FOUND); |
| 90 | } |
| 91 | node.add(codec(SubscriberAndDeviceInformation.class).encode(entry, this)); |
| 92 | return ok(root).build(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Create subscriber object. |
| 97 | * |
| 98 | * @return 201 Created |
| 99 | */ |
| 100 | @POST |
| 101 | @Path("/subscriber") |
| 102 | @Consumes(MediaType.APPLICATION_JSON) |
| 103 | public Response postSubscriber() { |
| 104 | try { |
| 105 | return Response.created(new URI("/subsciber/123")).build(); |
| 106 | } catch (URISyntaxException e) { |
| 107 | return Response.serverError().build(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Delete subscriber object. |
| 113 | * |
| 114 | * @param id |
| 115 | * ID of the subscriber |
| 116 | * @return 204 NoContent |
| 117 | */ |
| 118 | @DELETE |
| 119 | @Path("/cache/subscriber/{id}") |
| 120 | public Response deleteSubscriber(@PathParam("id") String id) { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 121 | subService.invalidateId(id); |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 122 | return Response.noContent().build(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Delete all the subscriber objects. |
| 127 | * |
| 128 | * @return 204 NoContent |
| 129 | */ |
| 130 | @DELETE |
| 131 | @Path("/cache/subscriber/") |
| 132 | public Response deleteAllSubscribers() { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 133 | subService.invalidateAll(); |
| 134 | return Response.noContent().build(); |
| 135 | } |
| 136 | |
| 137 | @GET |
| 138 | @Path("/bandwidthprofile/{id}") |
| 139 | @Produces(MediaType.APPLICATION_JSON) |
| 140 | public Response getBandwidthProfile(@PathParam("id") String id) { |
| 141 | final BandwidthProfileInformation entry = bpService.get(id); |
| 142 | if (entry == null) { |
| 143 | throw new ItemNotFoundException(BP_NOT_FOUND); |
| 144 | } |
| 145 | node.add(codec(BandwidthProfileInformation.class).encode(entry, this)); |
| 146 | return ok(root).build(); |
| 147 | } |
| 148 | |
| 149 | @GET |
| 150 | @Path("/cache/bandwidthprofile/{id}") |
| 151 | @Produces(MediaType.APPLICATION_JSON) |
| 152 | public Response getBandwidthProfileCache(@PathParam("id") String id) { |
| 153 | final BandwidthProfileInformation entry = bpService.getfromCache(id); |
| 154 | if (entry == null) { |
| 155 | throw new ItemNotFoundException(BP_NOT_FOUND); |
| 156 | } |
| 157 | node.add(codec(BandwidthProfileInformation.class).encode(entry, this)); |
| 158 | return ok(root).build(); |
| 159 | } |
| 160 | |
| 161 | @DELETE |
| 162 | @Path("/cache/bandwidthprofile/{id}") |
| 163 | public Response deleteBandwidthProfile(@PathParam("id") String id) { |
| 164 | bpService.invalidateAll(); |
| 165 | return Response.noContent().build(); |
| 166 | } |
| 167 | |
| 168 | @DELETE |
| 169 | @Path("/cache/bandwidthprofile/") |
| 170 | public Response deleteAllBandwidthProfiles() { |
| 171 | bpService.invalidateAll(); |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 172 | return Response.noContent().build(); |
| 173 | } |
| 174 | } |