blob: 3640a1d3cd2f19db240b3df4ad21c8b6e241abfe [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;
slowr8ddc2b12017-08-14 14:13:38 -070046import java.util.concurrent.TimeUnit;
slowr13fa5b02017-08-08 16:32:31 -070047
48/**
49 * Link web resource.
50 */
51@Path("links")
52public 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,
slowr8ddc2b12017-08-14 14:13:38 -070067 @DefaultValue("-1") @QueryParam("ue") long ue) {
slowr13fa5b02017-08-08 16:32:31 -070068 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
slowr13fa5b02017-08-08 16:32:31 -070082 try {
slowr8ddc2b12017-08-14 14:13:38 -070083 ObjectNode rootNode = mapper().createObjectNode();
slowr13fa5b02017-08-08 16:32:31 -070084 JsonNode jsonNode = mapper().readTree(list.toString());
85 rootNode.put("links", jsonNode);
slowr8ddc2b12017-08-14 14:13:38 -070086 return Response.ok(rootNode.toString()).build();
slowr13fa5b02017-08-08 16:32:31 -070087 } catch (IOException e) {
88 log.error(ExceptionUtils.getFullStackTrace(e));
89 e.printStackTrace();
slowr8ddc2b12017-08-14 14:13:38 -070090 return Response.serverError()
91 .entity(ExceptionUtils.getFullStackTrace(e))
92 .build();
slowr13fa5b02017-08-08 16:32:31 -070093 }
slowr13fa5b02017-08-08 16:32:31 -070094 }
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) {
slowr8ddc2b12017-08-14 14:13:38 -0700108 RnibLink link = get(XranStore.class).getLinkBetweenCellIdUeId(src, dst);
109 if (link != null) {
110 try {
slowr67d05e42017-08-11 20:37:22 -0700111 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
slowr13fa5b02017-08-08 16:32:31 -0700112
slowr67d05e42017-08-11 20:37:22 -0700113 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());
slowr8ddc2b12017-08-14 14:13:38 -0700124 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 }
slowr67d05e42017-08-11 20:37:22 -0700135 }
136 }
137 }
slowr13fa5b02017-08-08 16:32:31 -0700138
slowr67d05e42017-08-11 20:37:22 -0700139 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 }
slowr8ddc2b12017-08-14 14:13:38 -0700149 return Response.ok("trafficpercent changed successfully").build();
slowr67d05e42017-08-11 20:37:22 -0700150 }
slowr13fa5b02017-08-08 16:32:31 -0700151
slowr8ddc2b12017-08-14 14:13:38 -0700152 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();
slowr13fa5b02017-08-08 16:32:31 -0700178 }
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) {
slowr13fa5b02017-08-08 16:32:31 -0700192 try {
193 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
194
195 JsonNode type = jsonTree.get("type");
196
197 if (type != null) {
slowr8ddc2b12017-08-14 14:13:38 -0700198 boolean b;
slowr13fa5b02017-08-08 16:32:31 -0700199 b = get(XranStore.class).createLinkBetweenCellIdUeId(src, dst, type.asText());
slowr8ddc2b12017-08-14 14:13:38 -0700200 return ok(b).build();
slowr13fa5b02017-08-08 16:32:31 -0700201 }
202 } catch (Exception e) {
203 log.error(ExceptionUtils.getFullStackTrace(e));
204 e.printStackTrace();
slowr8ddc2b12017-08-14 14:13:38 -0700205 return Response.serverError()
206 .entity(ExceptionUtils.getFullStackTrace(e))
207 .build();
slowr13fa5b02017-08-08 16:32:31 -0700208 }
209
slowr8ddc2b12017-08-14 14:13:38 -0700210 return Response.serverError().entity("unreachable code").build();
slowr13fa5b02017-08-08 16:32:31 -0700211 }
212
213}