Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
Sean Condon | 081290d | 2017-11-02 13:15:08 +0000 | [diff] [blame] | 16 | package org.onosproject.soam.rest; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService; |
| 31 | import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException; |
| 32 | import org.onosproject.incubator.net.l2monitoring.soam.SoamId; |
| 33 | import org.onosproject.incubator.net.l2monitoring.soam.SoamService; |
| 34 | import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementCreate; |
| 35 | import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementEntry; |
| 36 | import org.onosproject.rest.AbstractWebResource; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import javax.ws.rs.Consumes; |
| 41 | import javax.ws.rs.DELETE; |
| 42 | import javax.ws.rs.GET; |
| 43 | import javax.ws.rs.POST; |
| 44 | import javax.ws.rs.PUT; |
| 45 | import javax.ws.rs.Path; |
| 46 | import javax.ws.rs.PathParam; |
| 47 | import javax.ws.rs.Produces; |
| 48 | import javax.ws.rs.core.MediaType; |
| 49 | import javax.ws.rs.core.Response; |
| 50 | import java.io.IOException; |
| 51 | import java.io.InputStream; |
| 52 | import java.net.URI; |
| 53 | import java.net.URISyntaxException; |
| 54 | import java.util.Collection; |
| 55 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 56 | import static org.onlab.util.Tools.readTreeFromStream; |
| 57 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 58 | /** |
| 59 | * Layer 2 SOAM Loss Measurement web resource. |
| 60 | */ |
| 61 | @Path("md/{md_name}/ma/{ma_name}/mep/{mep_id}/lm") |
| 62 | public class LmWebResource extends AbstractWebResource { |
| 63 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 64 | |
| 65 | /** |
| 66 | * Get all LMs for a Mep. |
| 67 | * |
| 68 | * @param mdName The name of a Maintenance Domain |
| 69 | * @param maName The name of a Maintenance Association belonging to the MD |
| 70 | * @param mepId The ID of a Mep belonging to the MA |
| 71 | * @return 200 OK with a list of LMs or 500 on error |
| 72 | */ |
| 73 | @GET |
| 74 | @Produces(MediaType.APPLICATION_JSON) |
| 75 | @Consumes(MediaType.APPLICATION_JSON) |
| 76 | public Response getAllLmsForMep(@PathParam("md_name") String mdName, |
| 77 | @PathParam("ma_name") String maName, |
| 78 | @PathParam("mep_id") short mepId) { |
| 79 | |
| 80 | log.debug("GET all LMs called for MEP {}", mdName + "/" + maName + "/" + mepId); |
| 81 | try { |
| 82 | MdId mdId = MdIdCharStr.asMdId(mdName); |
| 83 | MaIdShort maId = MaIdCharStr.asMaId(maName); |
| 84 | MepId mepIdObj = MepId.valueOf(mepId); |
| 85 | Collection<LossMeasurementEntry> lmCollection = |
| 86 | get(SoamService.class).getAllLms(mdId, maId, mepIdObj); |
| 87 | ArrayNode an = mapper().createArrayNode(); |
| 88 | an.add(codec(LossMeasurementEntry.class).encode(lmCollection, this)); |
| 89 | return ok(mapper().createObjectNode().set("lms", an)).build(); |
| 90 | } catch (CfmConfigException | SoamConfigException e) { |
| 91 | log.error("Get LM {} failed because of exception {}", |
| 92 | mdName + "/" + maName + "/" + mepId, e.toString()); |
| 93 | return Response.serverError().entity("{ \"failure\":\"" + e.toString() + "\" }").build(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get LM by MD name, MA name, Mep Id and Dm id. |
| 99 | * |
| 100 | * @param mdName The name of a Maintenance Domain |
| 101 | * @param maName The name of a Maintenance Association belonging to the MD |
| 102 | * @param mepId The Id of the MEP |
| 103 | * @param lmId The Id of the LM |
| 104 | * @return 200 OK with details of the LM or 500 on error |
| 105 | */ |
| 106 | @GET |
| 107 | @Path("{lm_id}") |
| 108 | @Produces(MediaType.APPLICATION_JSON) |
| 109 | @Consumes(MediaType.APPLICATION_JSON) |
| 110 | public Response getLm(@PathParam("md_name") String mdName, |
| 111 | @PathParam("ma_name") String maName, |
| 112 | @PathParam("mep_id") short mepId, @PathParam("lm_id") int lmId) { |
| 113 | log.debug("GET called for LM {}", mdName + "/" + maName + "/" + mepId + "/" + lmId); |
| 114 | try { |
| 115 | MdId mdId = MdIdCharStr.asMdId(mdName); |
| 116 | MaIdShort maId = MaIdCharStr.asMaId(maName); |
| 117 | MepId mepIdObj = MepId.valueOf(mepId); |
| 118 | SoamId lmIdObj = SoamId.valueOf(lmId); |
| 119 | LossMeasurementEntry lm = get(SoamService.class) |
| 120 | .getLm(mdId, maId, mepIdObj, lmIdObj); |
| 121 | if (lm == null) { |
| 122 | return Response.serverError().entity("{ \"failure\":\"LM " + |
| 123 | mdName + "/" + maName + "/" + mepId + "/" + lmId + " not found\" }").build(); |
| 124 | } |
| 125 | ObjectNode node = mapper().createObjectNode(); |
| 126 | node.set("lm", codec(LossMeasurementEntry.class).encode(lm, this)); |
| 127 | return ok(node).build(); |
| 128 | } catch (CfmConfigException | SoamConfigException e) { |
| 129 | log.error("Get LM {} failed because of exception {}", |
| 130 | mdName + "/" + maName + "/" + mepId + "/" + lmId, e.toString()); |
| 131 | return Response.serverError().entity("{ \"failure\":\"" + e.toString() + "\" }").build(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Abort LM by MD name, MA name, Mep Id and LM Id. |
| 137 | * In the API the measurement is aborted, and not truly deleted. It still |
| 138 | * remains so that its results may be read. Depending on the device it will |
| 139 | * get overwritten on the creation of subsequent measurements. |
| 140 | * Use clear stats to delete old results measurements. |
| 141 | * |
| 142 | * @param mdName The name of a Maintenance Domain |
| 143 | * @param maName The name of a Maintenance Association belonging to the MD |
| 144 | * @param mepId The Id of the MEP |
| 145 | * @param lmId The Id of the LM |
| 146 | * @return 200 OK or 304 if not found, or 500 on error |
| 147 | */ |
| 148 | @DELETE |
| 149 | @Path("{lm_id}") |
| 150 | @Consumes(MediaType.APPLICATION_JSON) |
| 151 | @Produces(MediaType.APPLICATION_JSON) |
| 152 | public Response abortLm(@PathParam("md_name") String mdName, |
| 153 | @PathParam("ma_name") String maName, |
| 154 | @PathParam("mep_id") short mepId, |
| 155 | @PathParam("lm_id") int lmId) { |
| 156 | log.debug("DELETE called for LM {}", mdName + "/" + maName + "/" + mepId + "/" + lmId); |
| 157 | try { |
| 158 | MdId mdId = MdIdCharStr.asMdId(mdName); |
| 159 | MaIdShort maId = MaIdCharStr.asMaId(maName); |
| 160 | MepId mepIdObj = MepId.valueOf(mepId); |
| 161 | SoamId lmIdObj = SoamId.valueOf(lmId); |
| 162 | |
| 163 | get(SoamService.class).abortLm(mdId, maId, mepIdObj, lmIdObj); |
| 164 | return ok("{ \"success\":\"deleted (aborted) " + mdName + "/" + maName + |
| 165 | "/" + mepId + "/" + lmId + "\" }").build(); |
| 166 | } catch (CfmConfigException e) { |
| 167 | log.error("Delete (abort) LM {} failed because of exception {}", |
| 168 | mdName + "/" + maName + "/" + mepId + "/" + lmId, e.toString()); |
| 169 | return Response.serverError().entity("{ \"failure\":\"" + e.toString() + "\" }").build(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Create LM with MD name, MA name, Mep id and LM Json. |
| 175 | * |
Sean Condon | 081290d | 2017-11-02 13:15:08 +0000 | [diff] [blame] | 176 | * @onos.rsModel LmCreate |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 177 | * @param mdName The name of a Maintenance Domain |
| 178 | * @param maName The name of a Maintenance Association belonging to the MD |
| 179 | * @param mepId The Id of the MEP belonging to the MEP |
| 180 | * @param input A JSON formatted input stream specifying the LM parameters |
| 181 | * @return 201 Created or 304 if already exists or 500 on error |
| 182 | */ |
| 183 | @POST |
| 184 | @Consumes(MediaType.APPLICATION_JSON) |
| 185 | @Produces(MediaType.APPLICATION_JSON) |
| 186 | public Response createLm(@PathParam("md_name") String mdName, |
| 187 | @PathParam("ma_name") String maName, |
| 188 | @PathParam("mep_id") short mepId, InputStream input) { |
| 189 | log.debug("POST called to Create Lm"); |
| 190 | try { |
| 191 | MdId mdId = MdIdCharStr.asMdId(mdName); |
| 192 | MaIdShort maId = MaIdCharStr.asMaId(maName); |
| 193 | MepId mepIdObj = MepId.valueOf(mepId); |
| 194 | |
| 195 | Mep mep = get(CfmMepService.class).getMep(mdId, maId, mepIdObj); |
| 196 | if (mep == null) { |
| 197 | return Response.serverError().entity("{ \"failure\":\"mep " + |
| 198 | mdName + "/" + maName + "/" + mepId + " does not exist\" }").build(); |
| 199 | } |
| 200 | |
| 201 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 202 | JsonNode cfg = readTreeFromStream(mapper, input); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 203 | JsonCodec<LossMeasurementCreate> lmCodec = codec(LossMeasurementCreate.class); |
| 204 | |
| 205 | LossMeasurementCreate lm = lmCodec.decode((ObjectNode) cfg, this); |
| 206 | get(SoamService.class).createLm(mdId, maId, mepIdObj, lm); |
| 207 | return Response |
| 208 | .created(new URI("md/" + mdName + "/ma/" + maName + "/mep/" + |
| 209 | mepId + "/lm")) |
| 210 | .entity("{ \"success\":\"lm " + mdName + "/" + maName + "/" + |
| 211 | mepId + " created\" }") |
| 212 | .build(); |
| 213 | } catch (CfmConfigException | SoamConfigException | IllegalArgumentException | |
| 214 | IOException | URISyntaxException e) { |
| 215 | log.error("Create LM on " + mdName + "/" + maName + "/" + mepId + |
| 216 | " failed because of exception {}", e.toString()); |
| 217 | return Response.serverError() |
| 218 | .entity("{ \"failure\":\"" + e.toString() + "\" }") |
| 219 | .build(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Clear LM history stats by MD name, MA name, Mep Id and LM Id. |
| 225 | * |
| 226 | * @param mdName The name of a Maintenance Domain |
| 227 | * @param maName The name of a Maintenance Association belonging to the MD |
| 228 | * @param mepId The Id of the MEP |
| 229 | * @param lmId The Id of the LM |
| 230 | * @return 200 OK or 304 if not found, or 500 on error |
| 231 | */ |
| 232 | @PUT |
| 233 | @Path("{lm_id}/clear-history") |
| 234 | @Consumes(MediaType.APPLICATION_JSON) |
| 235 | @Produces(MediaType.APPLICATION_JSON) |
| 236 | public Response clearLmHistory(@PathParam("md_name") String mdName, |
| 237 | @PathParam("ma_name") String maName, |
| 238 | @PathParam("mep_id") short mepId, |
| 239 | @PathParam("lm_id") int lmId) { |
| 240 | log.debug("clear-history called for LM {}", mdName + "/" + maName + |
| 241 | "/" + mepId + "/" + lmId); |
| 242 | try { |
| 243 | MdId mdId = MdIdCharStr.asMdId(mdName); |
| 244 | MaIdShort maId = MaIdCharStr.asMaId(maName); |
| 245 | MepId mepIdObj = MepId.valueOf(mepId); |
| 246 | SoamId lmIdObj = SoamId.valueOf(lmId); |
| 247 | |
| 248 | get(SoamService.class).clearDelayHistoryStats(mdId, maId, mepIdObj, lmIdObj); |
| 249 | return ok("{ \"success\":\"cleared LM history stats for " + |
| 250 | mdName + "/" + maName + "/" + mepId + "/" + lmId + "\" }").build(); |
| 251 | } catch (CfmConfigException e) { |
| 252 | log.error("Clear history stats for LM {} failed because of exception {}", |
| 253 | mdName + "/" + maName + "/" + mepId + "/" + lmId, e.toString()); |
| 254 | return Response.serverError().entity("{ \"failure\":\"" + |
| 255 | e.toString() + "\" }").build(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | } |