blob: 160874a770ebe7ddbaa008edba2f17baf5118114 [file] [log] [blame]
Deepa vaddireddy386f38b2017-08-02 06:24:01 +00001/*
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 */
16package org.opencord.sadis.rest;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.rest.AbstractWebResource;
20import com.fasterxml.jackson.databind.node.ArrayNode;
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000021import org.opencord.sadis.BandwidthProfileInformation;
22import org.opencord.sadis.BaseInformationService;
23import org.opencord.sadis.SadisService;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000024import org.opencord.sadis.SubscriberAndDeviceInformation;
25import org.onlab.util.ItemNotFoundException;
26
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000027import java.net.URI;
28import java.net.URISyntaxException;
29
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000030import javax.ws.rs.GET;
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000031import javax.ws.rs.POST;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000032import javax.ws.rs.DELETE;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000036import javax.ws.rs.Consumes;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000037import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39
40/**
41 * Subscriber And Device Information Service web resource.
42 */
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000043@Path("sadis")
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000044public 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 Abaka1e5ccf52018-07-02 11:59:03 +000048 private static final String BP_NOT_FOUND = "Bandwidth Profile not found";
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000049 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 vaddireddy386f38b2017-08-02 06:24:01 +000054
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 Abaka1e5ccf52018-07-02 11:59:03 +000067 final SubscriberAndDeviceInformation entry = subService.get(id);
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000068 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 Abaka1e5ccf52018-07-02 11:59:03 +000087 final SubscriberAndDeviceInformation entry = subService.getfromCache(id);
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000088 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 /**
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000096 * 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 /**
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000112 * 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 Abaka1e5ccf52018-07-02 11:59:03 +0000121 subService.invalidateId(id);
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000122 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 Abaka1e5ccf52018-07-02 11:59:03 +0000133 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) {
Matteo Scandoloe4f4b632020-01-07 23:54:35 +0000164 bpService.invalidateAll();
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000165 return Response.noContent().build();
166 }
167
168 @DELETE
169 @Path("/cache/bandwidthprofile/")
170 public Response deleteAllBandwidthProfiles() {
171 bpService.invalidateAll();
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000172 return Response.noContent().build();
173 }
174}