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