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 com.google.common.collect.Lists; |
| 21 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import org.onosproject.xran.XranStore; |
| 24 | import org.onosproject.xran.annotations.Patch; |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 25 | import org.onosproject.xran.controller.XranController; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 26 | import org.onosproject.xran.entities.RnibLink; |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 27 | import org.openmuc.jasn1.ber.types.BerInteger; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import javax.ws.rs.Consumes; |
| 32 | import javax.ws.rs.DefaultValue; |
| 33 | import javax.ws.rs.GET; |
| 34 | import javax.ws.rs.POST; |
| 35 | import javax.ws.rs.Path; |
| 36 | import javax.ws.rs.PathParam; |
| 37 | import javax.ws.rs.Produces; |
| 38 | import javax.ws.rs.QueryParam; |
| 39 | import javax.ws.rs.core.MediaType; |
| 40 | import javax.ws.rs.core.Response; |
| 41 | import java.io.IOException; |
| 42 | import java.io.InputStream; |
| 43 | import java.util.List; |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 44 | import java.util.Optional; |
| 45 | import java.util.concurrent.SynchronousQueue; |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 46 | import java.util.concurrent.TimeUnit; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Link web resource. |
| 50 | */ |
| 51 | @Path("links") |
| 52 | public class LinkWebResource extends AbstractWebResource { |
| 53 | |
| 54 | private static final Logger log = |
| 55 | LoggerFactory.getLogger(LinkWebResource.class); |
| 56 | |
| 57 | /** |
| 58 | * test. |
| 59 | * |
| 60 | * @param eciHex test |
| 61 | * @param ue test |
| 62 | * @return test |
| 63 | */ |
| 64 | @GET |
| 65 | @Produces(MediaType.APPLICATION_JSON) |
| 66 | public Response getLinksBetween(@DefaultValue("") @QueryParam("cell") String eciHex, |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 67 | @DefaultValue("-1") @QueryParam("ue") long ue) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 68 | List<RnibLink> list = Lists.newArrayList(); |
| 69 | if (!eciHex.isEmpty() && ue != -1) { |
| 70 | RnibLink link = get(XranStore.class).getLinkBetweenCellIdUeId(eciHex, ue); |
| 71 | if (link != null) { |
| 72 | list.add(link); |
| 73 | } |
| 74 | } else if (!eciHex.isEmpty()) { |
| 75 | list.addAll(get(XranStore.class).getLinksByCellId(eciHex)); |
| 76 | } else if (ue != -1) { |
| 77 | list.addAll(get(XranStore.class).getLinksByUeId(ue)); |
| 78 | } else { |
| 79 | list.addAll(get(XranStore.class).getLinks()); |
| 80 | } |
| 81 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 82 | try { |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 83 | ObjectNode rootNode = mapper().createObjectNode(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 84 | JsonNode jsonNode = mapper().readTree(list.toString()); |
| 85 | rootNode.put("links", jsonNode); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 86 | return Response.ok(rootNode.toString()).build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 87 | } catch (IOException e) { |
| 88 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 89 | e.printStackTrace(); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 90 | return Response.serverError() |
| 91 | .entity(ExceptionUtils.getFullStackTrace(e)) |
| 92 | .build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 93 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * test. |
| 98 | * |
| 99 | * @param src test |
| 100 | * @param dst test |
| 101 | * @param stream test |
| 102 | * @return test |
| 103 | */ |
| 104 | @Patch |
| 105 | @Path("{src},{dst}") |
| 106 | @Consumes(MediaType.APPLICATION_JSON) |
| 107 | public Response patchLinks(@PathParam("src") String src, @PathParam("dst") long dst, InputStream stream) { |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 108 | RnibLink link = get(XranStore.class).getLinkBetweenCellIdUeId(src, dst); |
| 109 | if (link != null) { |
| 110 | try { |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 111 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 112 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 113 | JsonNode type = jsonTree.get("type"); |
| 114 | if (type != null) { |
| 115 | final SynchronousQueue<String>[] queue = new SynchronousQueue[1]; |
| 116 | RnibLink.Type linkType = RnibLink.Type.getEnum(type.asText()); |
| 117 | if (linkType.equals(RnibLink.Type.SERVING_PRIMARY)) { |
| 118 | List<RnibLink> linksByUeId = get(XranStore.class).getLinksByUeId(dst); |
| 119 | Optional<RnibLink> primary = linksByUeId.stream() |
| 120 | .filter(l -> l.getType().equals(RnibLink.Type.SERVING_PRIMARY)) |
| 121 | .findFirst(); |
| 122 | if (primary.isPresent()) { |
| 123 | queue[0] = get(XranController.class).sendHORequest(link, primary.get()); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 124 | String poll = queue[0].poll(5, TimeUnit.SECONDS); |
| 125 | |
| 126 | if (poll != null) { |
| 127 | return Response.ok() |
| 128 | .entity(poll) |
| 129 | .build(); |
| 130 | } else { |
| 131 | return Response.serverError() |
| 132 | .entity("did not receive response in time") |
| 133 | .build(); |
| 134 | } |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 138 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 139 | JsonNode trafficpercent = jsonTree.get("trafficpercent"); |
| 140 | if (trafficpercent != null) { |
| 141 | JsonNode jsonNode = trafficpercent.get("traffic-percent-dl"); |
| 142 | if (jsonNode != null) { |
| 143 | link.getTrafficPercent().setTrafficPercentDl(new BerInteger(jsonNode.asInt())); |
| 144 | } |
| 145 | jsonNode = trafficpercent.get("traffic-percent-ul"); |
| 146 | if (jsonNode != null) { |
| 147 | link.getTrafficPercent().setTrafficPercentUl(new BerInteger(jsonNode.asInt())); |
| 148 | } |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 149 | return Response.ok("trafficpercent changed successfully").build(); |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 150 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 151 | |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 152 | JsonNode rrmConf = jsonTree.get("RRMConf"); |
| 153 | if (rrmConf != null) { |
| 154 | final SynchronousQueue<String>[] queue = new SynchronousQueue[1]; |
| 155 | get(XranStore.class).modifyLinkRrmConf(link, rrmConf); |
| 156 | queue[0] = get(XranController.class).sendModifiedRRMConf(link.getRrmParameters(), |
| 157 | link.getLinkId().getCell().getVersion().equals("3")); |
| 158 | String poll = queue[0].poll(5, TimeUnit.SECONDS); |
| 159 | |
| 160 | if (poll != null) { |
| 161 | return Response.ok() |
| 162 | .entity(poll) |
| 163 | .build(); |
| 164 | } else { |
| 165 | return Response.serverError() |
| 166 | .entity("did not receive response in time") |
| 167 | .build(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | } catch (Exception e) { |
| 172 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 173 | e.printStackTrace(); |
| 174 | return Response.serverError().entity(ExceptionUtils.getFullStackTrace(e)).build(); |
| 175 | } |
| 176 | } |
| 177 | return Response.serverError().entity("link not found").build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * test. |
| 182 | * |
| 183 | * @param src test |
| 184 | * @param dst test |
| 185 | * @param stream test |
| 186 | * @return test |
| 187 | */ |
| 188 | @POST |
| 189 | @Path("{src},{dst}") |
| 190 | @Consumes(MediaType.APPLICATION_JSON) |
| 191 | public Response postLinks(@PathParam("src") String src, @PathParam("dst") long dst, InputStream stream) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 192 | try { |
| 193 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 194 | |
| 195 | JsonNode type = jsonTree.get("type"); |
| 196 | |
| 197 | if (type != null) { |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 198 | boolean b; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 199 | b = get(XranStore.class).createLinkBetweenCellIdUeId(src, dst, type.asText()); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 200 | return ok(b).build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 201 | } |
| 202 | } catch (Exception e) { |
| 203 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 204 | e.printStackTrace(); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 205 | return Response.serverError() |
| 206 | .entity(ExceptionUtils.getFullStackTrace(e)) |
| 207 | .build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 208 | } |
| 209 | |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame^] | 210 | return Response.serverError().entity("unreachable code").build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | } |