slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 21 | import org.onosproject.rest.AbstractWebResource; |
| 22 | import org.onosproject.xran.XranStore; |
| 23 | import org.onosproject.xran.annotations.Patch; |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 24 | import org.onosproject.xran.controller.XranController; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 25 | import org.onosproject.xran.entities.RnibCell; |
| 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.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 38 | import java.util.concurrent.SynchronousQueue; |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 39 | import java.util.concurrent.TimeUnit; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Cell web resource. |
| 43 | */ |
| 44 | @Path("cell") |
| 45 | public class CellWebResource extends AbstractWebResource { |
| 46 | |
| 47 | private static final Logger log = |
| 48 | LoggerFactory.getLogger(CellWebResource.class); |
| 49 | |
| 50 | /** |
| 51 | * test. |
| 52 | * |
| 53 | * @param eciHex test |
| 54 | * @return test |
| 55 | */ |
| 56 | @GET |
| 57 | @Path("{cellid}") |
| 58 | @Produces(MediaType.APPLICATION_JSON) |
| 59 | public Response getCell(@PathParam("cellid") String eciHex) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 60 | RnibCell cell = get(XranStore.class).getCell(eciHex); |
| 61 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 62 | if (cell != null) { |
| 63 | try { |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 64 | ObjectNode rootNode = mapper().createObjectNode(); |
| 65 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 66 | JsonNode jsonNode = mapper().readTree(cell.toString()); |
| 67 | rootNode.put("cell", jsonNode); |
| 68 | } catch (IOException e) { |
| 69 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 70 | e.printStackTrace(); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 71 | return Response.serverError() |
| 72 | .entity(ExceptionUtils.getFullStackTrace(e)) |
| 73 | .build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 74 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 75 | } |
| 76 | |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 77 | return Response.serverError().entity("cell not found").build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * test. |
| 82 | * |
| 83 | * @param eciHex test |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 84 | * @param stream test (body of request) |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 85 | * @return test |
| 86 | */ |
| 87 | @Patch |
| 88 | @Path("{cellid}") |
| 89 | @Consumes(MediaType.APPLICATION_JSON) |
| 90 | public Response patchCell(@PathParam("cellid") String eciHex, InputStream stream) { |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 91 | RnibCell cell = get(XranStore.class).getCell(eciHex); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 92 | |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 93 | if (cell != null) { |
| 94 | try { |
| 95 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 96 | |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 97 | JsonNode rrmConf = jsonTree.get("RRMConf"); |
| 98 | if (rrmConf != null) { |
| 99 | final SynchronousQueue<String>[] queue = new SynchronousQueue[1]; |
| 100 | get(XranStore.class).modifyCellRrmConf(cell, rrmConf); |
| 101 | |
| 102 | queue[0] = get(XranController.class).sendModifiedRRMConf(cell.getRrmConfig(), |
| 103 | cell.getVersion().equals("3")); |
| 104 | String poll = queue[0].poll(5, TimeUnit.SECONDS); |
| 105 | |
| 106 | if (poll != null) { |
| 107 | return Response.ok() |
| 108 | .entity(poll) |
| 109 | .build(); |
| 110 | } else { |
| 111 | return Response.serverError() |
| 112 | .entity("did not receive response in time") |
| 113 | .build(); |
| 114 | } |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 115 | } |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 116 | } catch (Exception e) { |
| 117 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 118 | e.printStackTrace(); |
| 119 | return Response.serverError() |
| 120 | .entity(ExceptionUtils.getFullStackTrace(e)) |
| 121 | .build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 122 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 123 | } |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 124 | |
| 125 | return Response.serverError().entity("cell not found").build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } |