Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 1 | /* |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 2 | * Copyright 2021-present Open Networking Foundation |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 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 | */ |
alshabib | 36a4d73 | 2016-06-01 16:03:59 -0700 | [diff] [blame] | 16 | package org.opencord.olt.rest; |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 17 | |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 18 | import org.onlab.packet.VlanId; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 19 | import org.onosproject.net.ConnectPoint; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import org.opencord.olt.AccessDeviceService; |
Andrea Campanella | 5111823 | 2021-07-01 17:18:02 +0200 | [diff] [blame] | 24 | import org.slf4j.Logger; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 25 | |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 26 | import javax.ws.rs.Consumes; |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 27 | import javax.ws.rs.DELETE; |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 28 | import javax.ws.rs.GET; |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
| 35 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 36 | import java.util.Optional; |
| 37 | |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 38 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
Andrea Campanella | 5111823 | 2021-07-01 17:18:02 +0200 | [diff] [blame] | 39 | import static org.slf4j.LoggerFactory.getLogger; |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 40 | |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 41 | /** |
| 42 | * OLT REST APIs. |
| 43 | */ |
| 44 | |
| 45 | @Path("oltapp") |
| 46 | public class OltWebResource extends AbstractWebResource { |
Andrea Campanella | 5111823 | 2021-07-01 17:18:02 +0200 | [diff] [blame] | 47 | private final Logger log = getLogger(getClass()); |
| 48 | |
| 49 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 50 | @GET |
| 51 | @Produces(MediaType.APPLICATION_JSON) |
| 52 | @Path("status") |
| 53 | public Response status() { |
| 54 | return Response.ok().build(); |
| 55 | } |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Provision a subscriber. |
| 58 | * |
Jian Li | d8bca08 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 59 | * @param device device id |
| 60 | * @param port port number |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 61 | * @return 200 OK or 500 Internal Server Error |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 62 | */ |
| 63 | @POST |
| 64 | @Produces(MediaType.APPLICATION_JSON) |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 65 | @Path("{device}/{port}") |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 66 | public Response provisionSubscriber( |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 67 | @PathParam("device") String device, |
| 68 | @PathParam("port") long port) { |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 69 | AccessDeviceService service = get(AccessDeviceService.class); |
| 70 | DeviceId deviceId = DeviceId.deviceId(device); |
| 71 | PortNumber portNumber = PortNumber.portNumber(port); |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 72 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 73 | |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 74 | try { |
| 75 | service.provisionSubscriber(connectPoint); |
| 76 | } catch (Exception e) { |
Andrea Campanella | 5111823 | 2021-07-01 17:18:02 +0200 | [diff] [blame] | 77 | log.error("Can't provision subscriber {} due to exception", connectPoint, e); |
| 78 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 79 | } |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 80 | return ok("").build(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Remove the provisioning for a subscriber. |
Jian Li | d8bca08 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 85 | * |
| 86 | * @param device device id |
| 87 | * @param port port number |
Jian Li | c39f08d | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 88 | * @return 204 NO CONTENT |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 89 | */ |
| 90 | @DELETE |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 91 | @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 Campanella | 5111823 | 2021-07-01 17:18:02 +0200 | [diff] [blame] | 99 | 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 Li | c39f08d | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 105 | return Response.noContent().build(); |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 106 | } |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Provision service for a subscriber. |
| 110 | * |
| 111 | * @param portName Name of the port on which the subscriber is connected |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 112 | * @return 200 OK or 204 NO CONTENT |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 113 | */ |
| 114 | @POST |
| 115 | @Produces(MediaType.APPLICATION_JSON) |
| 116 | @Path("services/{portName}") |
| 117 | public Response provisionServices( |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 118 | @PathParam("portName") String portName) { |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 119 | AccessDeviceService service = get(AccessDeviceService.class); |
| 120 | |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 121 | Optional<VlanId> emptyVlan = Optional.empty(); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 122 | Optional<Integer> emptyTpId = Optional.empty(); |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 123 | // 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 Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 156 | return ok("").build(); |
| 157 | } |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 158 | return Response.noContent().build(); |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 159 | } |
| 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 Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 165 | * @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 Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 169 | */ |
| 170 | @POST |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 171 | @Consumes(MediaType.APPLICATION_JSON) |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 172 | @Produces(MediaType.APPLICATION_JSON) |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 173 | @Path("services/{portName}/{sTag}/{cTag}/{tpId}") |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 174 | public Response provisionAdditionalVlans( |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 175 | @PathParam("portName") String portName, |
| 176 | @PathParam("sTag") String sTagVal, |
| 177 | @PathParam("cTag") String cTagVal, |
| 178 | @PathParam("tpId") String tpIdVal) { |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 179 | AccessDeviceService service = get(AccessDeviceService.class); |
| 180 | VlanId cTag = VlanId.vlanId(cTagVal); |
| 181 | VlanId sTag = VlanId.vlanId(sTagVal); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 182 | Integer tpId = Integer.valueOf(tpIdVal); |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 183 | // 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 Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 191 | } |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 192 | if (service.provisionSubscriber(cp, cTag, sTag, tpId)) { |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 193 | return ok("").build(); |
| 194 | } |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 195 | return Response.noContent().build(); |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 196 | } |
| 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 Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 202 | * @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 Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 206 | */ |
| 207 | @DELETE |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 208 | @Consumes(MediaType.APPLICATION_JSON) |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 209 | @Produces(MediaType.APPLICATION_JSON) |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 210 | @Path("services/{portName}/{sTag}/{cTag}/{tpId}") |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 211 | public Response removeAdditionalVlans( |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 212 | @PathParam("portName") String portName, |
| 213 | @PathParam("sTag") String sTagVal, |
| 214 | @PathParam("cTag") String cTagVal, |
| 215 | @PathParam("tpId") String tpIdVal) { |
Amit Ghosh | e1d3f09 | 2018-10-09 19:44:33 +0100 | [diff] [blame] | 216 | AccessDeviceService service = get(AccessDeviceService.class); |
| 217 | VlanId cTag = VlanId.vlanId(cTagVal); |
| 218 | VlanId sTag = VlanId.vlanId(sTagVal); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 219 | Integer tpId = Integer.valueOf(tpIdVal); |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 220 | // 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 Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 230 | return ok("").build(); |
| 231 | } |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 232 | return Response.noContent().build(); |
Amit Ghosh | 3193952 | 2018-08-16 13:28:21 +0100 | [diff] [blame] | 233 | } |
Ray Milkey | 1748141 | 2015-12-09 09:16:26 -0800 | [diff] [blame] | 234 | } |