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