blob: 0c5307fc76ba6c02d2979a0aacd52d0e61e37134 [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 Condon081290d2017-11-02 13:15:08 +000016package org.onosproject.soam.rest;
Sean Condon0e89bda2017-03-21 14:23:19 +000017
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
24import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
25import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
26import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
27import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
28import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
29import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
30import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
31import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException;
32import org.onosproject.incubator.net.l2monitoring.soam.SoamId;
33import org.onosproject.incubator.net.l2monitoring.soam.SoamService;
34import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementCreate;
35import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementEntry;
36import org.onosproject.rest.AbstractWebResource;
37import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import javax.ws.rs.Consumes;
41import javax.ws.rs.DELETE;
42import javax.ws.rs.GET;
43import javax.ws.rs.POST;
44import javax.ws.rs.PUT;
45import javax.ws.rs.Path;
46import javax.ws.rs.PathParam;
47import javax.ws.rs.Produces;
48import javax.ws.rs.core.MediaType;
49import javax.ws.rs.core.Response;
50import java.io.IOException;
51import java.io.InputStream;
52import java.net.URI;
53import java.net.URISyntaxException;
54import java.util.Collection;
55
Ray Milkey86ee5e82018-04-02 15:33:07 -070056import static org.onlab.util.Tools.readTreeFromStream;
57
Sean Condon0e89bda2017-03-21 14:23:19 +000058/**
59 * Layer 2 SOAM Loss Measurement web resource.
60 */
61@Path("md/{md_name}/ma/{ma_name}/mep/{mep_id}/lm")
62public 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 Condon081290d2017-11-02 13:15:08 +0000176 * @onos.rsModel LmCreate
Sean Condon0e89bda2017-03-21 14:23:19 +0000177 * @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 Milkey86ee5e82018-04-02 15:33:07 -0700202 JsonNode cfg = readTreeFromStream(mapper, input);
Sean Condon0e89bda2017-03-21 14:23:19 +0000203 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}