blob: d88966888ec2b7064ca16e601edf1e09a925093a [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 */
16package org.onosproject.soam.impl;
17
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
56/**
57 * Layer 2 SOAM Loss Measurement web resource.
58 */
59@Path("md/{md_name}/ma/{ma_name}/mep/{mep_id}/lm")
60public 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 *
174 * @param mdName The name of a Maintenance Domain
175 * @param maName The name of a Maintenance Association belonging to the MD
176 * @param mepId The Id of the MEP belonging to the MEP
177 * @param input A JSON formatted input stream specifying the LM parameters
178 * @return 201 Created or 304 if already exists or 500 on error
179 */
180 @POST
181 @Consumes(MediaType.APPLICATION_JSON)
182 @Produces(MediaType.APPLICATION_JSON)
183 public Response createLm(@PathParam("md_name") String mdName,
184 @PathParam("ma_name") String maName,
185 @PathParam("mep_id") short mepId, InputStream input) {
186 log.debug("POST called to Create Lm");
187 try {
188 MdId mdId = MdIdCharStr.asMdId(mdName);
189 MaIdShort maId = MaIdCharStr.asMaId(maName);
190 MepId mepIdObj = MepId.valueOf(mepId);
191
192 Mep mep = get(CfmMepService.class).getMep(mdId, maId, mepIdObj);
193 if (mep == null) {
194 return Response.serverError().entity("{ \"failure\":\"mep " +
195 mdName + "/" + maName + "/" + mepId + " does not exist\" }").build();
196 }
197
198 ObjectMapper mapper = new ObjectMapper();
199 JsonNode cfg = mapper.readTree(input);
200 JsonCodec<LossMeasurementCreate> lmCodec = codec(LossMeasurementCreate.class);
201
202 LossMeasurementCreate lm = lmCodec.decode((ObjectNode) cfg, this);
203 get(SoamService.class).createLm(mdId, maId, mepIdObj, lm);
204 return Response
205 .created(new URI("md/" + mdName + "/ma/" + maName + "/mep/" +
206 mepId + "/lm"))
207 .entity("{ \"success\":\"lm " + mdName + "/" + maName + "/" +
208 mepId + " created\" }")
209 .build();
210 } catch (CfmConfigException | SoamConfigException | IllegalArgumentException |
211 IOException | URISyntaxException e) {
212 log.error("Create LM on " + mdName + "/" + maName + "/" + mepId +
213 " failed because of exception {}", e.toString());
214 return Response.serverError()
215 .entity("{ \"failure\":\"" + e.toString() + "\" }")
216 .build();
217 }
218 }
219
220 /**
221 * Clear LM history stats by MD name, MA name, Mep Id and LM Id.
222 *
223 * @param mdName The name of a Maintenance Domain
224 * @param maName The name of a Maintenance Association belonging to the MD
225 * @param mepId The Id of the MEP
226 * @param lmId The Id of the LM
227 * @return 200 OK or 304 if not found, or 500 on error
228 */
229 @PUT
230 @Path("{lm_id}/clear-history")
231 @Consumes(MediaType.APPLICATION_JSON)
232 @Produces(MediaType.APPLICATION_JSON)
233 public Response clearLmHistory(@PathParam("md_name") String mdName,
234 @PathParam("ma_name") String maName,
235 @PathParam("mep_id") short mepId,
236 @PathParam("lm_id") int lmId) {
237 log.debug("clear-history called for LM {}", mdName + "/" + maName +
238 "/" + mepId + "/" + lmId);
239 try {
240 MdId mdId = MdIdCharStr.asMdId(mdName);
241 MaIdShort maId = MaIdCharStr.asMaId(maName);
242 MepId mepIdObj = MepId.valueOf(mepId);
243 SoamId lmIdObj = SoamId.valueOf(lmId);
244
245 get(SoamService.class).clearDelayHistoryStats(mdId, maId, mepIdObj, lmIdObj);
246 return ok("{ \"success\":\"cleared LM history stats for " +
247 mdName + "/" + maName + "/" + mepId + "/" + lmId + "\" }").build();
248 } catch (CfmConfigException e) {
249 log.error("Clear history stats for LM {} failed because of exception {}",
250 mdName + "/" + maName + "/" + mepId + "/" + lmId, e.toString());
251 return Response.serverError().entity("{ \"failure\":\"" +
252 e.toString() + "\" }").build();
253 }
254 }
255
256}