daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [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 | package org.onosproject.openstackswitching.web; |
| 17 | |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.openstackswitching.OpenstackSubnet; |
| 22 | import org.onosproject.openstackswitching.OpenstackSwitchingService; |
| 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.POST; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.Produces; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
| 33 | import java.io.InputStream; |
| 34 | |
| 35 | @Path("subnets") |
| 36 | public class OpenstackSubnetWebResource extends AbstractWebResource { |
| 37 | protected static final Logger log = LoggerFactory |
| 38 | .getLogger(OpenstackSubnetWebResource.class); |
| 39 | |
| 40 | private static final OpenstackSubnetCodec SUBNET_CODEC = new OpenstackSubnetCodec(); |
| 41 | |
| 42 | @POST |
| 43 | @Consumes(MediaType.APPLICATION_JSON) |
| 44 | @Produces(MediaType.APPLICATION_JSON) |
| 45 | public Response createSubnet(InputStream input) { |
| 46 | try { |
| 47 | ObjectMapper mapper = new ObjectMapper(); |
| 48 | ObjectNode subnetNode = (ObjectNode) mapper.readTree(input); |
| 49 | |
| 50 | OpenstackSubnet openstackSubnet = SUBNET_CODEC.decode(subnetNode, this); |
| 51 | |
| 52 | OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class); |
| 53 | switchingService.createSubnet(openstackSubnet); |
| 54 | log.info("REST API subnets is called with {}", subnetNode.toString()); |
| 55 | return Response.status(Response.Status.OK).build(); |
| 56 | } catch (Exception e) { |
| 57 | log.error("Creates VirtualSubnet failed because of exception {}", |
| 58 | e.toString()); |
| 59 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 60 | .build(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | } |