blob: c35d975b95b5bafa0003f8c47c5d972a5f35f930 [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
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070045 /**
46 * Get all CORD fabric VLANs.
47 *
48 * @return array of cord VLANs in the system.
49 */
Jonathan Hart443ebed2015-05-05 14:02:12 -070050 @GET
51 @Produces(MediaType.APPLICATION_JSON)
52 public Response getVlans() {
53 FabricService fabricService = get(FabricService.class);
54 List<FabricVlan> vlans = fabricService.getVlans();
55 ObjectNode result = new ObjectMapper().createObjectNode();
56 result.set("vlans", new FabricVlanCodec().encode(vlans, this));
57
58 return ok(result.toString()).build();
59 }
60
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070061 /**
62 * Create a CORD fabric VLAN.
63 *
64 * @param input JSON stream describing new VLAN
65 * @return status of the request - CREATED if the JSON is correct,
66 * INTERNAL_SERVER_ERROR if the JSON is invalid
67 * @throws IOException if the JSON is invalid
68 */
Jonathan Hart443ebed2015-05-05 14:02:12 -070069 @POST
70 @Path("add")
71 @Consumes(MediaType.APPLICATION_JSON)
72 public Response addVlan(InputStream input) throws IOException {
73 ObjectMapper mapper = new ObjectMapper();
74 ObjectNode vlanJson = (ObjectNode) mapper.readTree(input);
75 FabricService fabricService = get(FabricService.class);
76
77 fabricService.addVlan(VLAN_CODEC.decode(vlanJson, this));
78
79 return Response.ok().build();
80 }
81
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070082 /**
83 * Delete a CORD fabric VLAN.
84 *
85 * @param vlan identifier of the VLAN to remove
86 * @return status of the request - OK
87 */
Jonathan Hart443ebed2015-05-05 14:02:12 -070088 @DELETE
89 @Path("{vlan}")
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070090 public Response deleteVlan(@PathParam("vlan") String vlan) {
Jonathan Hart443ebed2015-05-05 14:02:12 -070091 VlanId vlanId = VlanId.vlanId(Short.parseShort(vlan));
92
93 FabricService fabricService = get(FabricService.class);
94
95 fabricService.removeVlan(vlanId);
96
97 return Response.ok().build();
98 }
99}