blob: c5123047a502fe75cc3f3fb344b3f8c47ea4dde9 [file] [log] [blame]
Ray Milkey17481412015-12-09 09:16:26 -08001/*
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07002 * Copyright 2021-present Open Networking Foundation
Ray Milkey17481412015-12-09 09:16:26 -08003 *
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 */
alshabib36a4d732016-06-01 16:03:59 -070016package org.opencord.olt.rest;
Ray Milkey17481412015-12-09 09:16:26 -080017
Amit Ghoshe1d3f092018-10-09 19:44:33 +010018import org.onlab.packet.VlanId;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.PortNumber;
22import org.onosproject.rest.AbstractWebResource;
23import org.opencord.olt.AccessDeviceService;
Andrea Campanella51118232021-07-01 17:18:02 +020024import org.slf4j.Logger;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010025
Andrea Campanellacbbb7952019-11-25 06:38:41 +000026import javax.ws.rs.Consumes;
Ray Milkey17481412015-12-09 09:16:26 -080027import javax.ws.rs.DELETE;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070028import javax.ws.rs.GET;
Ray Milkey17481412015-12-09 09:16:26 -080029import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070036import java.util.Optional;
37
Andrea Campanellacbbb7952019-11-25 06:38:41 +000038import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
Andrea Campanella51118232021-07-01 17:18:02 +020039import static org.slf4j.LoggerFactory.getLogger;
Amit Ghosh31939522018-08-16 13:28:21 +010040
Ray Milkey17481412015-12-09 09:16:26 -080041/**
42 * OLT REST APIs.
43 */
44
45@Path("oltapp")
46public class OltWebResource extends AbstractWebResource {
Andrea Campanella51118232021-07-01 17:18:02 +020047 private final Logger log = getLogger(getClass());
48
49
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070050 @GET
51 @Produces(MediaType.APPLICATION_JSON)
52 @Path("status")
53 public Response status() {
54 return Response.ok().build();
55 }
Ray Milkey17481412015-12-09 09:16:26 -080056 /**
57 * Provision a subscriber.
58 *
Jian Lid8bca082016-01-22 16:46:58 -080059 * @param device device id
60 * @param port port number
Andrea Campanellacbbb7952019-11-25 06:38:41 +000061 * @return 200 OK or 500 Internal Server Error
Ray Milkey17481412015-12-09 09:16:26 -080062 */
63 @POST
64 @Produces(MediaType.APPLICATION_JSON)
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010065 @Path("{device}/{port}")
Ray Milkey17481412015-12-09 09:16:26 -080066 public Response provisionSubscriber(
Andrea Campanellacbbb7952019-11-25 06:38:41 +000067 @PathParam("device") String device,
68 @PathParam("port") long port) {
Ray Milkey17481412015-12-09 09:16:26 -080069 AccessDeviceService service = get(AccessDeviceService.class);
70 DeviceId deviceId = DeviceId.deviceId(device);
71 PortNumber portNumber = PortNumber.portNumber(port);
Ray Milkey17481412015-12-09 09:16:26 -080072 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070073
Andrea Campanellacbbb7952019-11-25 06:38:41 +000074 try {
75 service.provisionSubscriber(connectPoint);
76 } catch (Exception e) {
Andrea Campanella51118232021-07-01 17:18:02 +020077 log.error("Can't provision subscriber {} due to exception", connectPoint, e);
78 return Response.status(INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
Andrea Campanellacbbb7952019-11-25 06:38:41 +000079 }
Ray Milkey17481412015-12-09 09:16:26 -080080 return ok("").build();
81 }
82
83 /**
84 * Remove the provisioning for a subscriber.
Jian Lid8bca082016-01-22 16:46:58 -080085 *
86 * @param device device id
87 * @param port port number
Jian Lic39f08d2016-05-10 11:48:19 -070088 * @return 204 NO CONTENT
Ray Milkey17481412015-12-09 09:16:26 -080089 */
90 @DELETE
Ray Milkey17481412015-12-09 09:16:26 -080091 @Path("{device}/{port}")
92 public Response removeSubscriber(
93 @PathParam("device")String device,
94 @PathParam("port")long port) {
95 AccessDeviceService service = get(AccessDeviceService.class);
96 DeviceId deviceId = DeviceId.deviceId(device);
97 PortNumber portNumber = PortNumber.portNumber(port);
98 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
Andrea Campanella51118232021-07-01 17:18:02 +020099 try {
100 service.removeSubscriber(connectPoint);
101 } catch (Exception e) {
102 log.error("Can't remove subscriber {} due to exception", connectPoint, e);
103 return Response.status(INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
104 }
Jian Lic39f08d2016-05-10 11:48:19 -0700105 return Response.noContent().build();
Ray Milkey17481412015-12-09 09:16:26 -0800106 }
Amit Ghosh31939522018-08-16 13:28:21 +0100107
108 /**
109 * Provision service for a subscriber.
110 *
111 * @param portName Name of the port on which the subscriber is connected
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000112 * @return 200 OK or 204 NO CONTENT
Amit Ghosh31939522018-08-16 13:28:21 +0100113 */
114 @POST
115 @Produces(MediaType.APPLICATION_JSON)
116 @Path("services/{portName}")
117 public Response provisionServices(
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000118 @PathParam("portName") String portName) {
Amit Ghosh31939522018-08-16 13:28:21 +0100119 AccessDeviceService service = get(AccessDeviceService.class);
120
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100121 Optional<VlanId> emptyVlan = Optional.empty();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000122 Optional<Integer> emptyTpId = Optional.empty();
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700123 // Check if we can find the connect point to which this subscriber is connected
124 ConnectPoint cp = service.findSubscriberConnectPoint(portName);
125 if (cp == null) {
126 log.warn("ConnectPoint not found for {}", portName);
127 return Response.status(INTERNAL_SERVER_ERROR)
128 .entity("ConnectPoint not found for " + portName).build();
129 }
130 if (service.provisionSubscriber(cp)) {
131 return ok("").build();
132 }
133 return Response.noContent().build();
134 }
135
136 /**
137 * Removes services for a subscriber.
138 *
139 * @param portName Name of the port on which the subscriber is connected
140 * @return 200 OK or 204 NO CONTENT
141 */
142 @DELETE
143 @Produces(MediaType.APPLICATION_JSON)
144 @Path("services/{portName}")
145 public Response deleteServices(
146 @PathParam("portName") String portName) {
147 AccessDeviceService service = get(AccessDeviceService.class);
148
149 ConnectPoint cp = service.findSubscriberConnectPoint(portName);
150 if (cp == null) {
151 log.warn("ConnectPoint not found for {}", portName);
152 return Response.status(INTERNAL_SERVER_ERROR)
153 .entity("ConnectPoint not found for " + portName).build();
154 }
155 if (service.removeSubscriber(cp)) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100156 return ok("").build();
157 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000158 return Response.noContent().build();
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100159 }
160
161 /**
162 * Provision service with particular tags for a subscriber.
163 *
164 * @param portName Name of the port on which the subscriber is connected
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000165 * @param sTagVal additional outer tag on this port
166 * @param cTagVal additional innter tag on this port
167 * @param tpIdVal technology profile id
168 * @return 200 OK or 204 NO CONTENT
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100169 */
170 @POST
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000171 @Consumes(MediaType.APPLICATION_JSON)
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100172 @Produces(MediaType.APPLICATION_JSON)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000173 @Path("services/{portName}/{sTag}/{cTag}/{tpId}")
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100174 public Response provisionAdditionalVlans(
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000175 @PathParam("portName") String portName,
176 @PathParam("sTag") String sTagVal,
177 @PathParam("cTag") String cTagVal,
178 @PathParam("tpId") String tpIdVal) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100179 AccessDeviceService service = get(AccessDeviceService.class);
180 VlanId cTag = VlanId.vlanId(cTagVal);
181 VlanId sTag = VlanId.vlanId(sTagVal);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000182 Integer tpId = Integer.valueOf(tpIdVal);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700183 // TODO this is not optimal, because we call device service 2 times here and
184 // 2 times in the provisionSubscriber call, optimize byu having 2 more methods
185 // in the OltService that allow provisioning with portName.
186 ConnectPoint cp = service.findSubscriberConnectPoint(portName);
187 if (cp == null) {
188 log.warn("ConnectPoint not found for {}", portName);
189 return Response.status(INTERNAL_SERVER_ERROR)
190 .entity("ConnectPoint not found for " + portName).build();
Amit Ghosh31939522018-08-16 13:28:21 +0100191 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700192 if (service.provisionSubscriber(cp, cTag, sTag, tpId)) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100193 return ok("").build();
194 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000195 return Response.noContent().build();
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100196 }
197
198 /**
199 * Removes additional vlans of a particular subscriber.
200 *
201 * @param portName Name of the port on which the subscriber is connected
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000202 * @param sTagVal additional outer tag on this port which needs to be removed
203 * @param cTagVal additional inner tag on this port which needs to be removed
204 * @param tpIdVal additional technology profile id
205 * @return 200 OK or 204 NO CONTENT
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100206 */
207 @DELETE
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000208 @Consumes(MediaType.APPLICATION_JSON)
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100209 @Produces(MediaType.APPLICATION_JSON)
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000210 @Path("services/{portName}/{sTag}/{cTag}/{tpId}")
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100211 public Response removeAdditionalVlans(
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000212 @PathParam("portName") String portName,
213 @PathParam("sTag") String sTagVal,
214 @PathParam("cTag") String cTagVal,
215 @PathParam("tpId") String tpIdVal) {
Amit Ghoshe1d3f092018-10-09 19:44:33 +0100216 AccessDeviceService service = get(AccessDeviceService.class);
217 VlanId cTag = VlanId.vlanId(cTagVal);
218 VlanId sTag = VlanId.vlanId(sTagVal);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000219 Integer tpId = Integer.valueOf(tpIdVal);
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700220 // TODO this is not optimal, because we call device service 2 times here and
221 // 2 times in the provisionSubscriber call, optimize byu having 2 more methods
222 // in the OltService that allow provisioning with portName.
223 ConnectPoint cp = service.findSubscriberConnectPoint(portName);
224 if (cp == null) {
225 log.warn("ConnectPoint not found for {}", portName);
226 return Response.status(INTERNAL_SERVER_ERROR)
227 .entity("ConnectPoint not found for " + portName).build();
228 }
229 if (service.removeSubscriber(cp, cTag, sTag, tpId)) {
Amit Ghosh31939522018-08-16 13:28:21 +0100230 return ok("").build();
231 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000232 return Response.noContent().build();
Amit Ghosh31939522018-08-16 13:28:21 +0100233 }
Ray Milkey17481412015-12-09 09:16:26 -0800234}