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 | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Link web resource. |
| 49 | */ |
| 50 | @Path("links") |
| 51 | public class LinkWebResource extends AbstractWebResource { |
| 52 | |
| 53 | private static final Logger log = |
| 54 | LoggerFactory.getLogger(LinkWebResource.class); |
| 55 | |
| 56 | /** |
| 57 | * test. |
| 58 | * |
| 59 | * @param eciHex test |
| 60 | * @param ue test |
| 61 | * @return test |
| 62 | */ |
| 63 | @GET |
| 64 | @Produces(MediaType.APPLICATION_JSON) |
| 65 | public Response getLinksBetween(@DefaultValue("") @QueryParam("cell") String eciHex, |
| 66 | @DefaultValue("-1") @QueryParam("ue") long ue) { |
| 67 | log.debug("GET LINKS CELL {} AND UE {}", eciHex, ue); |
| 68 | |
| 69 | List<RnibLink> list = Lists.newArrayList(); |
| 70 | if (!eciHex.isEmpty() && ue != -1) { |
| 71 | RnibLink link = get(XranStore.class).getLinkBetweenCellIdUeId(eciHex, ue); |
| 72 | if (link != null) { |
| 73 | list.add(link); |
| 74 | } |
| 75 | } else if (!eciHex.isEmpty()) { |
| 76 | list.addAll(get(XranStore.class).getLinksByCellId(eciHex)); |
| 77 | } else if (ue != -1) { |
| 78 | list.addAll(get(XranStore.class).getLinksByUeId(ue)); |
| 79 | } else { |
| 80 | list.addAll(get(XranStore.class).getLinks()); |
| 81 | } |
| 82 | |
| 83 | ObjectNode rootNode = mapper().createObjectNode(); |
| 84 | |
| 85 | try { |
| 86 | JsonNode jsonNode = mapper().readTree(list.toString()); |
| 87 | rootNode.put("links", jsonNode); |
| 88 | } catch (IOException e) { |
| 89 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 90 | e.printStackTrace(); |
| 91 | } |
| 92 | |
| 93 | return ok(rootNode.toString()).build(); |
| 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) { |
| 108 | log.debug("Patch LINKS FROM {} to {}", src, dst); |
| 109 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 110 | try { |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame^] | 111 | RnibLink link = get(XranStore.class).getLinkBetweenCellIdUeId(src, dst); |
| 112 | if (link != null) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 113 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame^] | 114 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 115 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame^] | 116 | JsonNode type = jsonTree.get("type"); |
| 117 | if (type != null) { |
| 118 | final SynchronousQueue<String>[] queue = new SynchronousQueue[1]; |
| 119 | RnibLink.Type linkType = RnibLink.Type.getEnum(type.asText()); |
| 120 | if (linkType.equals(RnibLink.Type.SERVING_PRIMARY)) { |
| 121 | List<RnibLink> linksByUeId = get(XranStore.class).getLinksByUeId(dst); |
| 122 | Optional<RnibLink> primary = linksByUeId.stream() |
| 123 | .filter(l -> l.getType().equals(RnibLink.Type.SERVING_PRIMARY)) |
| 124 | .findFirst(); |
| 125 | if (primary.isPresent()) { |
| 126 | queue[0] = get(XranController.class).sendHORequest(link, primary.get()); |
| 127 | return Response.ok().entity(queue[0].take()).build(); |
| 128 | } |
| 129 | } |
| 130 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 131 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame^] | 132 | JsonNode trafficpercent = jsonTree.get("trafficpercent"); |
| 133 | if (trafficpercent != null) { |
| 134 | JsonNode jsonNode = trafficpercent.get("traffic-percent-dl"); |
| 135 | if (jsonNode != null) { |
| 136 | link.getTrafficPercent().setTrafficPercentDl(new BerInteger(jsonNode.asInt())); |
| 137 | } |
| 138 | jsonNode = trafficpercent.get("traffic-percent-ul"); |
| 139 | if (jsonNode != null) { |
| 140 | link.getTrafficPercent().setTrafficPercentUl(new BerInteger(jsonNode.asInt())); |
| 141 | } |
| 142 | return Response.ok().build(); |
| 143 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 144 | } |
| 145 | } catch (Exception e) { |
| 146 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 147 | e.printStackTrace(); |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame^] | 148 | return Response.serverError().entity(ExceptionUtils.getFullStackTrace(e)).build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 149 | } |
| 150 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame^] | 151 | return Response.noContent().build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * test. |
| 156 | * |
| 157 | * @param src test |
| 158 | * @param dst test |
| 159 | * @param stream test |
| 160 | * @return test |
| 161 | */ |
| 162 | @POST |
| 163 | @Path("{src},{dst}") |
| 164 | @Consumes(MediaType.APPLICATION_JSON) |
| 165 | public Response postLinks(@PathParam("src") String src, @PathParam("dst") long dst, InputStream stream) { |
| 166 | log.debug("POST LINKS FROM {} to {}", src, dst); |
| 167 | |
| 168 | boolean b = false; |
| 169 | try { |
| 170 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 171 | |
| 172 | JsonNode type = jsonTree.get("type"); |
| 173 | |
| 174 | if (type != null) { |
| 175 | b = get(XranStore.class).createLinkBetweenCellIdUeId(src, dst, type.asText()); |
| 176 | } |
| 177 | } catch (Exception e) { |
| 178 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 179 | e.printStackTrace(); |
| 180 | } |
| 181 | |
| 182 | return ok(b).build(); |
| 183 | } |
| 184 | |
| 185 | } |