blob: 5878e6bda34fbd953d8be2ded9b3cd8e11c2e205 [file] [log] [blame]
Jonathan Hart443ebed2015-05-05 14:02:12 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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
17package org.onosproject.cordfabric;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.packet.VlanId;
22import org.onosproject.rest.AbstractWebResource;
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.Path;
29import javax.ws.rs.PathParam;
30import javax.ws.rs.Produces;
31import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
33import java.io.IOException;
34import java.io.InputStream;
35import java.util.List;
36
37/**
38 * Web resource for interacting with the fabric.
39 */
40@Path("vlans")
41public class FabricWebResource extends AbstractWebResource {
42
43 private static final FabricVlanCodec VLAN_CODEC = new FabricVlanCodec();
44
45 @GET
46 @Produces(MediaType.APPLICATION_JSON)
47 public Response getVlans() {
48 FabricService fabricService = get(FabricService.class);
49 List<FabricVlan> vlans = fabricService.getVlans();
50 ObjectNode result = new ObjectMapper().createObjectNode();
51 result.set("vlans", new FabricVlanCodec().encode(vlans, this));
52
53 return ok(result.toString()).build();
54 }
55
56 @POST
57 @Path("add")
58 @Consumes(MediaType.APPLICATION_JSON)
59 public Response addVlan(InputStream input) throws IOException {
60 ObjectMapper mapper = new ObjectMapper();
61 ObjectNode vlanJson = (ObjectNode) mapper.readTree(input);
62 FabricService fabricService = get(FabricService.class);
63
64 fabricService.addVlan(VLAN_CODEC.decode(vlanJson, this));
65
66 return Response.ok().build();
67 }
68
69 @DELETE
70 @Path("{vlan}")
71 public Response deleteVlan(@PathParam("vlan") String vlan) throws IOException {
72 VlanId vlanId = VlanId.vlanId(Short.parseShort(vlan));
73
74 FabricService fabricService = get(FabricService.class);
75
76 fabricService.removeVlan(vlanId);
77
78 return Response.ok().build();
79 }
80}