Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cfm.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep; |
| 20 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 21 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.util.Collection; |
| 35 | |
| 36 | /** |
| 37 | * Layer 2 CFM Maintenance Association Endpoint (MEP) by Device web resource. |
| 38 | */ |
| 39 | @Path("device") |
| 40 | public 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 | } |