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; |
| 25 | import org.onosproject.xran.entities.RnibLink; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import javax.ws.rs.Consumes; |
| 30 | import javax.ws.rs.DefaultValue; |
| 31 | import javax.ws.rs.GET; |
| 32 | import javax.ws.rs.POST; |
| 33 | import javax.ws.rs.Path; |
| 34 | import javax.ws.rs.PathParam; |
| 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.QueryParam; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.util.List; |
| 42 | |
| 43 | /** |
| 44 | * Link web resource. |
| 45 | */ |
| 46 | @Path("links") |
| 47 | public class LinkWebResource extends AbstractWebResource { |
| 48 | |
| 49 | private static final Logger log = |
| 50 | LoggerFactory.getLogger(LinkWebResource.class); |
| 51 | |
| 52 | /** |
| 53 | * test. |
| 54 | * |
| 55 | * @param eciHex test |
| 56 | * @param ue test |
| 57 | * @return test |
| 58 | */ |
| 59 | @GET |
| 60 | @Produces(MediaType.APPLICATION_JSON) |
| 61 | public Response getLinksBetween(@DefaultValue("") @QueryParam("cell") String eciHex, |
| 62 | @DefaultValue("-1") @QueryParam("ue") long ue) { |
| 63 | log.debug("GET LINKS CELL {} AND UE {}", eciHex, ue); |
| 64 | |
| 65 | List<RnibLink> list = Lists.newArrayList(); |
| 66 | if (!eciHex.isEmpty() && ue != -1) { |
| 67 | RnibLink link = get(XranStore.class).getLinkBetweenCellIdUeId(eciHex, ue); |
| 68 | if (link != null) { |
| 69 | list.add(link); |
| 70 | } |
| 71 | } else if (!eciHex.isEmpty()) { |
| 72 | list.addAll(get(XranStore.class).getLinksByCellId(eciHex)); |
| 73 | } else if (ue != -1) { |
| 74 | list.addAll(get(XranStore.class).getLinksByUeId(ue)); |
| 75 | } else { |
| 76 | list.addAll(get(XranStore.class).getLinks()); |
| 77 | } |
| 78 | |
| 79 | ObjectNode rootNode = mapper().createObjectNode(); |
| 80 | |
| 81 | try { |
| 82 | JsonNode jsonNode = mapper().readTree(list.toString()); |
| 83 | rootNode.put("links", jsonNode); |
| 84 | } catch (IOException e) { |
| 85 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 86 | e.printStackTrace(); |
| 87 | } |
| 88 | |
| 89 | return ok(rootNode.toString()).build(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * test. |
| 94 | * |
| 95 | * @param src test |
| 96 | * @param dst test |
| 97 | * @param stream test |
| 98 | * @return test |
| 99 | */ |
| 100 | @Patch |
| 101 | @Path("{src},{dst}") |
| 102 | @Consumes(MediaType.APPLICATION_JSON) |
| 103 | public Response patchLinks(@PathParam("src") String src, @PathParam("dst") long dst, InputStream stream) { |
| 104 | log.debug("Patch LINKS FROM {} to {}", src, dst); |
| 105 | |
| 106 | boolean b = false; |
| 107 | try { |
| 108 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 109 | |
| 110 | JsonNode type = jsonTree.get("type"); |
| 111 | if (type != null) { |
| 112 | |
| 113 | } |
| 114 | |
| 115 | JsonNode trafficpercent = jsonTree.get("trafficpercent"); |
| 116 | if (trafficpercent != null) { |
| 117 | |
| 118 | } |
| 119 | } catch (Exception e) { |
| 120 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 121 | e.printStackTrace(); |
| 122 | } |
| 123 | |
| 124 | return ok(b).build(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * test. |
| 129 | * |
| 130 | * @param src test |
| 131 | * @param dst test |
| 132 | * @param stream test |
| 133 | * @return test |
| 134 | */ |
| 135 | @POST |
| 136 | @Path("{src},{dst}") |
| 137 | @Consumes(MediaType.APPLICATION_JSON) |
| 138 | public Response postLinks(@PathParam("src") String src, @PathParam("dst") long dst, InputStream stream) { |
| 139 | log.debug("POST LINKS FROM {} to {}", src, dst); |
| 140 | |
| 141 | boolean b = false; |
| 142 | try { |
| 143 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 144 | |
| 145 | JsonNode type = jsonTree.get("type"); |
| 146 | |
| 147 | if (type != null) { |
| 148 | b = get(XranStore.class).createLinkBetweenCellIdUeId(src, dst, type.asText()); |
| 149 | } |
| 150 | } catch (Exception e) { |
| 151 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 152 | e.printStackTrace(); |
| 153 | } |
| 154 | |
| 155 | return ok(b).build(); |
| 156 | } |
| 157 | |
| 158 | } |