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