blob: 370755f3f4d021093f3163f42df7ebd11b999d68 [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
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 */
16package org.onosproject.xran.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.Lists;
21import org.apache.commons.lang.exception.ExceptionUtils;
22import org.onosproject.rest.AbstractWebResource;
23import org.onosproject.xran.XranStore;
24import org.onosproject.xran.annotations.Patch;
slowr67d05e42017-08-11 20:37:22 -070025import org.onosproject.xran.controller.XranController;
slowr13fa5b02017-08-08 16:32:31 -070026import org.onosproject.xran.entities.RnibLink;
slowr67d05e42017-08-11 20:37:22 -070027import org.openmuc.jasn1.ber.types.BerInteger;
slowr13fa5b02017-08-08 16:32:31 -070028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import javax.ws.rs.Consumes;
32import javax.ws.rs.DefaultValue;
33import javax.ws.rs.GET;
34import javax.ws.rs.POST;
35import javax.ws.rs.Path;
36import javax.ws.rs.PathParam;
37import javax.ws.rs.Produces;
38import javax.ws.rs.QueryParam;
39import javax.ws.rs.core.MediaType;
40import javax.ws.rs.core.Response;
41import java.io.IOException;
42import java.io.InputStream;
43import java.util.List;
slowr67d05e42017-08-11 20:37:22 -070044import java.util.Optional;
45import java.util.concurrent.SynchronousQueue;
slowr13fa5b02017-08-08 16:32:31 -070046
47/**
48 * Link web resource.
49 */
50@Path("links")
51public 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
slowr13fa5b02017-08-08 16:32:31 -0700110 try {
slowr67d05e42017-08-11 20:37:22 -0700111 RnibLink link = get(XranStore.class).getLinkBetweenCellIdUeId(src, dst);
112 if (link != null) {
slowr13fa5b02017-08-08 16:32:31 -0700113
slowr67d05e42017-08-11 20:37:22 -0700114 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
slowr13fa5b02017-08-08 16:32:31 -0700115
slowr67d05e42017-08-11 20:37:22 -0700116 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 }
slowr13fa5b02017-08-08 16:32:31 -0700131
slowr67d05e42017-08-11 20:37:22 -0700132 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 }
slowr13fa5b02017-08-08 16:32:31 -0700144 }
145 } catch (Exception e) {
146 log.error(ExceptionUtils.getFullStackTrace(e));
147 e.printStackTrace();
slowr67d05e42017-08-11 20:37:22 -0700148 return Response.serverError().entity(ExceptionUtils.getFullStackTrace(e)).build();
slowr13fa5b02017-08-08 16:32:31 -0700149 }
150
slowr67d05e42017-08-11 20:37:22 -0700151 return Response.noContent().build();
slowr13fa5b02017-08-08 16:32:31 -0700152 }
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}