blob: 386ed8c0bbe909a966960b06dee0c14cdc483b2b [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
58/**
59 * Layer 2 SOAM Delay Measurement web resource.
60 */
61@Path("md/{md_name}/ma/{ma_name}/mep/{mep_id}/dm")
62public class DmWebResource extends AbstractWebResource {
63 private final Logger log = LoggerFactory.getLogger(getClass());
64
65 /**
66 * Get all DMs 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 DMs or 500 on error
72 */
73 @GET
74 @Produces(MediaType.APPLICATION_JSON)
75 @Consumes(MediaType.APPLICATION_JSON)
76 public Response getAllDmsForMep(@PathParam("md_name") String mdName,
77 @PathParam("ma_name") String maName,
78 @PathParam("mep_id") short mepId) {
79 log.debug("GET all DMs called for MEP {}", mdName + "/" + maName + "/" + mepId);
80 try {
81 MdId mdId = MdIdCharStr.asMdId(mdName);
82 MaIdShort maId = MaIdCharStr.asMaId(maName);
83 MepId mepIdObj = MepId.valueOf(mepId);
84 Collection<DelayMeasurementEntry> dmCollection =
85 get(SoamService.class).getAllDms(mdId, maId, mepIdObj);
86 ArrayNode an = mapper().createArrayNode();
87 an.add(codec(DelayMeasurementEntry.class).encode(dmCollection, this));
88 return ok(mapper().createObjectNode().set("dms", an)).build();
89 } catch (CfmConfigException | SoamConfigException e) {
90 log.error("Get DM {} failed because of exception {}",
91 mdName + "/" + maName + "/" + mepId, e.toString());
92 return Response.serverError().entity("{ \"failure\":\"" + e.toString() + "\" }").build();
93 }
94 }
95
96 /**
97 * Get DM by MD name, MA name, Mep Id and Dm id.
98 *
99 * @param mdName The name of a Maintenance Domain
100 * @param maName The name of a Maintenance Association belonging to the MD
101 * @param mepId The Id of the MEP
102 * @param dmId The Id of the DM
103 * @return 200 OK with details of the DM or 500 on error
104 */
105 @GET
106 @Path("{dm_id}")
107 @Produces(MediaType.APPLICATION_JSON)
108 @Consumes(MediaType.APPLICATION_JSON)
109 public Response getDm(@PathParam("md_name") String mdName,
110 @PathParam("ma_name") String maName,
111 @PathParam("mep_id") short mepId, @PathParam("dm_id") int dmId) {
112 log.debug("GET called for DM {}", mdName + "/" + maName + "/" + mepId + "/" + dmId);
113 try {
114 MdId mdId = MdIdCharStr.asMdId(mdName);
115 MaIdShort maId = MaIdCharStr.asMaId(maName);
116 MepId mepIdObj = MepId.valueOf(mepId);
117 SoamId dmIdObj = SoamId.valueOf(dmId);
118 DelayMeasurementEntry dm = get(SoamService.class)
119 .getDm(mdId, maId, mepIdObj, dmIdObj);
120 if (dm == null) {
121 return Response.serverError().entity("{ \"failure\":\"DM " +
122 mdName + "/" + maName + "/" + mepId + "/" + dmId + " not found\" }").build();
123 }
124 ObjectNode node = mapper().createObjectNode();
125 node.set("dm", codec(DelayMeasurementEntry.class).encode(dm, this));
126 return ok(node).build();
127 } catch (CfmConfigException | SoamConfigException e) {
128 log.error("Get DM {} failed because of exception {}",
129 mdName + "/" + maName + "/" + mepId + "/" + dmId, e.toString());
130 return Response.serverError().entity("{ \"failure\":\"" + e.toString() + "\" }").build();
131 }
132 }
133
134 /**
135 * Abort DM by MD name, MA name, Mep Id and DM Id.
136 * In the API the measurement is aborted, and not truly deleted. It still
137 * remains so that its results may be read. Depending on the device it will
138 * get overwritten on the creation of subsequent measurements.
139 * Use clear stats to delete old results
140 * 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 dmId The Id of the DM
146 * @return 200 OK or 304 if not found, or 500 on error
147 */
148 @DELETE
149 @Path("{dm_id}")
150 @Consumes(MediaType.APPLICATION_JSON)
151 @Produces(MediaType.APPLICATION_JSON)
152 public Response abortDm(@PathParam("md_name") String mdName,
153 @PathParam("ma_name") String maName,
154 @PathParam("mep_id") short mepId,
155 @PathParam("dm_id") int dmId) {
156 log.debug("DELETE called for DM {}", mdName + "/" + maName + "/" + mepId + "/" + dmId);
157 try {
158 MdId mdId = MdIdCharStr.asMdId(mdName);
159 MaIdShort maId = MaIdCharStr.asMaId(maName);
160 MepId mepIdObj = MepId.valueOf(mepId);
161 SoamId dmIdObj = SoamId.valueOf(dmId);
162
163 get(SoamService.class).abortDm(mdId, maId, mepIdObj, dmIdObj);
164 return ok("{ \"success\":\"deleted (aborted) " + mdName + "/" + maName +
165 "/" + mepId + "/" + dmId + "\" }").build();
166 } catch (CfmConfigException e) {
167 log.error("Delete (abort) DM {} failed because of exception {}",
168 mdName + "/" + maName + "/" + mepId + "/" + dmId, e.toString());
169 return Response.serverError().entity("{ \"failure\":\"" + e.toString() + "\" }").build();
170 }
171 }
172
173 /**
174 * Create DM with MD name, MA name, Mep id and DM Json.
175 *
Sean Condon081290d2017-11-02 13:15:08 +0000176 * @onos.rsModel DmCreate
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 DM 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 createDm(@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 Dm");
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();
202 JsonNode cfg = mapper.readTree(input);
203 JsonCodec<DelayMeasurementCreate> dmCodec = codec(DelayMeasurementCreate.class);
204
205 DelayMeasurementCreate dm = dmCodec.decode((ObjectNode) cfg, this);
206 get(SoamService.class).createDm(mdId, maId, mepIdObj, dm);
207 return Response
208 .created(new URI("md/" + mdName + "/ma/" + maName + "/mep/" +
209 mepId + "/dm"))
210 .entity("{ \"success\":\"dm " + mdName + "/" + maName + "/" +
211 mepId + " created\" }")
212 .build();
213 } catch (CfmConfigException | SoamConfigException | IllegalArgumentException |
214 IOException | URISyntaxException e) {
215 log.error("Create DM 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 DM history stats by MD name, MA name, Mep Id and DM 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 dmId The Id of the DM
230 * @return 200 OK or 304 if not found, or 500 on error
231 */
232 @PUT
233 @Path("{dm_id}/clear-history")
234 @Consumes(MediaType.APPLICATION_JSON)
235 @Produces(MediaType.APPLICATION_JSON)
236 public Response clearDmHistory(@PathParam("md_name") String mdName,
237 @PathParam("ma_name") String maName,
238 @PathParam("mep_id") short mepId,
239 @PathParam("dm_id") int dmId) {
240 log.debug("clear-history called for DM {}", mdName + "/" + maName +
241 "/" + mepId + "/" + dmId);
242 try {
243 MdId mdId = MdIdCharStr.asMdId(mdName);
244 MaIdShort maId = MaIdCharStr.asMaId(maName);
245 MepId mepIdObj = MepId.valueOf(mepId);
246 SoamId dmIdObj = SoamId.valueOf(dmId);
247
248 get(SoamService.class).clearDelayHistoryStats(mdId, maId, mepIdObj, dmIdObj);
249 return ok("{ \"success\":\"cleared DM history stats for " +
250 mdName + "/" + maName + "/" + mepId + "/" + dmId + "\" }").build();
251 } catch (CfmConfigException e) {
252 log.error("Clear history stats for DM {} failed because of exception {}",
253 mdName + "/" + maName + "/" + mepId + "/" + dmId, e.toString());
254 return Response.serverError().entity("{ \"failure\":\"" +
255 e.toString() + "\" }").build();
256 }
257 }
258}