blob: 5fbed6d74184f7944de525cb0afe4a80c966e0a9 [file] [log] [blame]
Sean Condon96b896d2017-12-11 12:44:29 -08001/*
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.cfm.rest;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
20import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
21import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
22import org.onosproject.net.DeviceId;
23import org.onosproject.rest.AbstractWebResource;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.GET;
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;
34import java.util.Collection;
35
36/**
37 * Layer 2 CFM Maintenance Association Endpoint (MEP) by Device web resource.
38 */
39@Path("device")
40public class DeviceMepWebResource extends AbstractWebResource {
41
42 private final Logger log = LoggerFactory.getLogger(getClass());
43
44 /**
45 * Get all MEPs by Device Id. The device should support Meps
46 *
47 * @param deviceId The id of a device.
48 * @return 200 OK with a list of MEPS or 500 on error
49 */
50 @GET
51 @Path("{device_id}")
52 @Produces(MediaType.APPLICATION_JSON)
53 @Consumes(MediaType.APPLICATION_JSON)
54 public Response getAllMepsForDevice(@PathParam("device_id") String deviceId) {
55 DeviceId deviceIdObj = DeviceId.deviceId(deviceId);
56 log.debug("GET all Meps called for Device {}", deviceIdObj);
57 try {
58 Collection<Mep> mepCollection = get(CfmMepService.class)
59 .getAllMepsByDevice(deviceIdObj);
60 ArrayNode an = mapper().createArrayNode();
61 an.add(codec(Mep.class).encode(mepCollection, this));
62 return ok(mapper().createObjectNode().set("meps", an)).build();
63 } catch (CfmConfigException e) {
64 log.error("Get all Meps on device {} failed because of exception",
65 deviceIdObj, e);
66 return Response.serverError().entity("{ \"failure\":\"" + e.toString() + "\" }").build();
67 }
68 }
69}