Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [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.onosproject.xran.impl.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import io.swagger.annotations.ApiResponse; |
| 21 | import io.swagger.annotations.ApiResponses; |
| 22 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.onosproject.xran.XranService; |
| 25 | import org.onosproject.xran.XranStore; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import javax.ws.rs.Consumes; |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.PATCH; |
| 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.PathParam; |
| 34 | import javax.ws.rs.Produces; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | import java.io.InputStream; |
| 38 | import java.net.HttpURLConnection; |
| 39 | import java.util.Optional; |
| 40 | import java.util.concurrent.TimeUnit; |
| 41 | |
| 42 | /** |
| 43 | * Cell web resource. |
| 44 | */ |
| 45 | @Path("cell") |
| 46 | public class CellWebResource extends AbstractWebResource { |
| 47 | |
| 48 | private static final Logger log = |
| 49 | LoggerFactory.getLogger(CellWebResource.class); |
| 50 | |
| 51 | private XranStore xranStore; |
| 52 | private XranService xranService; |
| 53 | |
| 54 | public CellWebResource() { |
| 55 | xranStore = get(XranStore.class); |
| 56 | xranService = get(XranService.class); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Lists the cell with {cellid}. |
| 61 | * |
| 62 | * @param eciHex EutranCellIdentifier in binary |
| 63 | * @return Response |
| 64 | */ |
| 65 | @GET |
| 66 | @Path("{cellid}") |
| 67 | @Produces(MediaType.APPLICATION_JSON) |
| 68 | @ApiResponses(value = { |
| 69 | @ApiResponse(code = 200, message = "HTTP_OK"), |
| 70 | @ApiResponse(code = 500, message = "HTTP_INTERNAL_ERROR"), |
| 71 | @ApiResponse(code = 404, message = "HTTP_NOT_FOUND") |
| 72 | }) |
| 73 | public Response getCell(@PathParam("cellid") String eciHex) { |
| 74 | return xranStore.getCell(eciHex).map(cell -> { |
| 75 | try { |
| 76 | JsonNode jsonNode = mapper().valueToTree(cell); |
| 77 | |
| 78 | return ResponseHelper.getResponse( |
| 79 | mapper(), |
| 80 | HttpURLConnection.HTTP_OK, |
| 81 | jsonNode |
| 82 | ); |
| 83 | |
| 84 | } catch (Exception e) { |
| 85 | String fullStackTrace = ExceptionUtils.getFullStackTrace(e); |
| 86 | log.error(fullStackTrace); |
| 87 | e.printStackTrace(); |
| 88 | |
| 89 | return ResponseHelper.getResponse( |
| 90 | mapper(), |
| 91 | HttpURLConnection.HTTP_INTERNAL_ERROR, |
| 92 | "Exception", |
| 93 | fullStackTrace |
| 94 | ); |
| 95 | } |
| 96 | }).orElse(ResponseHelper.getResponse( |
| 97 | mapper(), |
| 98 | HttpURLConnection.HTTP_NOT_FOUND, |
| 99 | "Not Found", |
| 100 | "Cell with " + eciHex + " was not found" |
| 101 | )); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Modify the RRMConfig parameters of the cell. |
| 106 | * |
| 107 | * @param eciHex EutranCellIdentifier in binary |
| 108 | * @param stream Parameters that you want to modify |
| 109 | * @return Response |
| 110 | */ |
| 111 | @PATCH |
| 112 | @Path("{cellid}") |
| 113 | @Consumes(MediaType.APPLICATION_JSON) |
| 114 | @Produces(MediaType.APPLICATION_JSON) |
| 115 | @ApiResponses(value = { |
| 116 | @ApiResponse(code = 200, message = "HTTP_OK"), |
| 117 | @ApiResponse(code = 408, message = "HTTP_CLIENT_TIMEOUT"), |
| 118 | @ApiResponse(code = 400, message = "HTTP_BAD_REQUEST"), |
| 119 | @ApiResponse(code = 500, message = "HTTP_INTERNAL_ERROR"), |
| 120 | @ApiResponse(code = 404, message = "HTTP_NOT_FOUND") |
| 121 | |
| 122 | }) |
| 123 | public Response patchCell(@PathParam("cellid") String eciHex, InputStream stream) { |
| 124 | return xranStore.getCell(eciHex).map(cell -> { |
| 125 | try { |
| 126 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 127 | |
| 128 | JsonNode rrmConf = jsonTree.path("RRMConf"); |
| 129 | if (!rrmConf.isMissingNode()) { |
| 130 | xranStore.modifyCellRrmConf(cell, rrmConf); |
| 131 | |
| 132 | return xranService.sendModifiedRrm(cell.getRrmConfig()) |
| 133 | .flatMap(queue -> { |
| 134 | try { |
| 135 | return Optional.ofNullable(queue.poll(xranService |
| 136 | .getNorthboundTimeout(), TimeUnit.MILLISECONDS)); |
| 137 | } catch (InterruptedException e) { |
| 138 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 139 | return Optional.empty(); |
| 140 | } |
| 141 | }).map(p -> |
| 142 | ResponseHelper.getResponse( |
| 143 | mapper(), |
| 144 | HttpURLConnection.HTTP_OK, |
| 145 | "Handoff Response", |
| 146 | p |
| 147 | ) |
| 148 | ).orElse( |
| 149 | ResponseHelper.getResponse( |
| 150 | mapper(), |
| 151 | HttpURLConnection.HTTP_CLIENT_TIMEOUT, |
| 152 | "Handoff Timeout", |
| 153 | "eNodeB did not send a HOComplete/HOFailure on time" |
| 154 | ) |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | return ResponseHelper.getResponse( |
| 159 | mapper(), |
| 160 | HttpURLConnection.HTTP_BAD_REQUEST, |
| 161 | "Bad Request", |
| 162 | "The command you specified is not implemented or doesn't exist. We support " + |
| 163 | "RRMConf commands." |
| 164 | ); |
| 165 | } catch (Exception e) { |
| 166 | String fullStackTrace = ExceptionUtils.getFullStackTrace(e); |
| 167 | log.error(fullStackTrace); |
| 168 | |
| 169 | return ResponseHelper.getResponse( |
| 170 | mapper(), |
| 171 | HttpURLConnection.HTTP_INTERNAL_ERROR, |
| 172 | "Exception", |
| 173 | fullStackTrace |
| 174 | ); |
| 175 | } |
| 176 | }).orElse(ResponseHelper.getResponse( |
| 177 | mapper(), |
| 178 | HttpURLConnection.HTTP_NOT_FOUND, |
| 179 | "Not Found", |
| 180 | "Cell " + eciHex + " was not found" |
| 181 | )); |
| 182 | } |
| 183 | |
| 184 | } |