Jonathan Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.cordfabric; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.packet.VlanId; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | |
| 24 | import javax.ws.rs.Consumes; |
| 25 | import javax.ws.rs.DELETE; |
| 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.Path; |
| 29 | import javax.ws.rs.PathParam; |
| 30 | import javax.ws.rs.Produces; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | import java.util.List; |
| 36 | |
| 37 | /** |
| 38 | * Web resource for interacting with the fabric. |
| 39 | */ |
| 40 | @Path("vlans") |
| 41 | public class FabricWebResource extends AbstractWebResource { |
| 42 | |
| 43 | private static final FabricVlanCodec VLAN_CODEC = new FabricVlanCodec(); |
| 44 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Get all CORD fabric VLANs. |
| 47 | * |
| 48 | * @return array of cord VLANs in the system. |
| 49 | */ |
Jonathan Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 50 | @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 Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 61 | /** |
| 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 Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 69 | @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 Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 82 | /** |
| 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 Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 88 | @DELETE |
| 89 | @Path("{vlan}") |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 90 | public Response deleteVlan(@PathParam("vlan") String vlan) { |
Jonathan Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 91 | 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 | } |