f4c401fb639b2bdd4be6bc6f8efc5dc1b959dc3e
[onosfw.git] /
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 import com.fasterxml.jackson.databind.ObjectMapper;
19 import com.fasterxml.jackson.databind.node.ObjectNode;
20 import org.onosproject.openstackswitching.OpenstackNetwork;
21 import org.onosproject.openstackswitching.OpenstackSwitchingService;
22 import org.onosproject.rest.AbstractWebResource;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import java.io.InputStream;
33
34 @Path("networks")
35 public class OpenstackNetworkWebResource extends AbstractWebResource {
36
37     protected static final Logger log = LoggerFactory
38             .getLogger(OpenstackNetworkWebResource.class);
39
40     private static final OpenstackNetworkCodec NETWORK_CODEC = new OpenstackNetworkCodec();
41
42     @POST
43     @Consumes(MediaType.APPLICATION_JSON)
44     @Produces(MediaType.APPLICATION_JSON)
45     public Response createNetwork(InputStream input) {
46         try {
47             ObjectMapper mapper = new ObjectMapper();
48             ObjectNode networkNode = (ObjectNode) mapper.readTree(input);
49
50             OpenstackNetwork openstackNetwork = NETWORK_CODEC.decode(networkNode, this);
51
52             OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class);
53             switchingService.createNetwork(openstackNetwork);
54             return Response.status(Response.Status.OK).build();
55         } catch (Exception e) {
56             log.error("Creates VirtualPort failed because of exception {}",
57                     e.toString());
58             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
59                     .build();
60         }
61     }
62 }