OpenDaylight Controller functional modules.
[controller.git] / opendaylight / northbound / subnets / src / main / java / org / opendaylight / controller / subnets / northbound / SubnetsNorthboundJAXRS.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.subnets.northbound;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import javax.ws.rs.DELETE;
14 import javax.ws.rs.GET;
15 import javax.ws.rs.POST;
16 import javax.ws.rs.Path;
17 import javax.ws.rs.PathParam;
18 import javax.ws.rs.Produces;
19 import javax.ws.rs.QueryParam;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22
23 import org.codehaus.enunciate.jaxrs.ResponseCode;
24 import org.codehaus.enunciate.jaxrs.StatusCodes;
25 import org.codehaus.enunciate.jaxrs.TypeHint;
26 import org.opendaylight.controller.northbound.commons.RestMessages;
27 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
28 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
29 import org.opendaylight.controller.sal.utils.ServiceHelper;
30 import org.opendaylight.controller.sal.utils.Status;
31 import org.opendaylight.controller.switchmanager.ISwitchManager;
32 import org.opendaylight.controller.switchmanager.SubnetConfig;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Path("/")
37 public class SubnetsNorthboundJAXRS {
38     protected static final Logger logger = LoggerFactory
39             .getLogger(SubnetsNorthboundJAXRS.class);
40
41     /**
42      * List all the subnets in a given container
43      *
44      * @param containerName container in which we want to query the subnets
45      *
46      * @return a List of SubnetConfig
47      */
48     @Path("/{containerName}")
49     @GET
50     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
51     @StatusCodes( { @ResponseCode(code = 404, condition = "The containerName passed was not found") })
52     @TypeHint(SubnetConfigs.class)
53     public SubnetConfigs listSubnets(
54             @PathParam("containerName") String containerName) {
55         ISwitchManager switchManager = null;
56         switchManager = (ISwitchManager) ServiceHelper.getInstance(
57                 ISwitchManager.class, containerName, this);
58         if (switchManager == null) {
59             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
60                     .toString());
61         }
62         return new SubnetConfigs(switchManager.getSubnetsConfigList());
63     }
64
65     /**
66      * List the configuration of a subnet in a given container
67      *
68      * @param containerName container in which we want to query the
69      * subnet
70      * @param subnetName of the subnet being queried
71      *
72      * @return a SubnetConfig
73      */
74     @Path("/{containerName}/{subnetName}")
75     @GET
76     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
77     @StatusCodes( {
78             @ResponseCode(code = 404, condition = "The containerName passed was not found"),
79             @ResponseCode(code = 404, condition = "Subnet does not exist") })
80     @TypeHint(SubnetConfig.class)
81     public SubnetConfig listSubnet(
82             @PathParam("containerName") String containerName,
83             @PathParam("subnetName") String subnetName) {
84         ISwitchManager switchManager = null;
85         switchManager = (ISwitchManager) ServiceHelper.getInstance(
86                 ISwitchManager.class, containerName, this);
87         if (switchManager == null) {
88             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
89                     .toString());
90         }
91         SubnetConfig res = switchManager.getSubnetConfig(subnetName);
92         if (res == null) {
93             throw new ResourceNotFoundException(RestMessages.NOSUBNET
94                     .toString());
95         } else {
96             return res;
97         }
98     }
99
100     /**
101      * Add/Update a subnet to a container
102      *
103      * @param containerName container in which we want to add/update the
104      * subnet
105      * @param subnetName that has to be added
106      * @param subnet pair default gateway IP/mask that identify the
107      * subnet being added modified
108      *
109      */
110     @Path("/{containerName}/{subnetName}")
111     @POST
112     @StatusCodes( {
113             @ResponseCode(code = 404, condition = "The containerName passed was not found"),
114             @ResponseCode(code = 404, condition = "Invalid Data passed"),
115             @ResponseCode(code = 201, condition = "Subnet added/modified"),
116             @ResponseCode(code = 500, condition = "Addition of subnet failed") })
117     public Response addSubnet(@PathParam("containerName") String containerName,
118             @PathParam("subnetName") String subnetName,
119             @QueryParam("subnet") String subnet) {
120         if (subnetName == null) {
121             throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
122         }
123         if (subnet == null) {
124             throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
125         }
126         ISwitchManager switchManager = null;
127         switchManager = (ISwitchManager) ServiceHelper.getInstance(
128                 ISwitchManager.class, containerName, this);
129         if (switchManager == null) {
130             throw new ResourceNotFoundException(RestMessages.NOCONTAINER.toString());
131         }
132
133         SubnetConfig cfgObject = new SubnetConfig(subnetName, subnet,
134                 new ArrayList<String>(0));
135         Status status = switchManager.addSubnet(cfgObject);
136         if (status.isSuccess()) {
137             return Response.status(Response.Status.CREATED).build();
138         }
139         throw new InternalServerErrorException(status.getDescription());
140     }
141
142     /**
143      * Delete a subnet from a container
144      *
145      * @param containerName container in which we want to delete the
146      * subnet by name
147      * @param subnetName of the subnet to be remove.
148      *
149      */
150     @Path("/{containerName}/{subnetName}")
151     @DELETE
152     @StatusCodes( {
153             @ResponseCode(code = 404, condition = "The containerName passed was not found"),
154             @ResponseCode(code = 500, condition = "Removal of subnet failed") })
155     public Response removeSubnet(
156             @PathParam("containerName") String containerName,
157             @PathParam("subnetName") String subnetName) {
158         if (subnetName == null) {
159             throw new ResourceNotFoundException(RestMessages.INVALIDDATA
160                     .toString());
161         }
162         ISwitchManager switchManager = null;
163         switchManager = (ISwitchManager) ServiceHelper.getInstance(
164                 ISwitchManager.class, containerName, this);
165         if (switchManager == null) {
166             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
167                     .toString());
168         }
169         Status status = switchManager.removeSubnet(subnetName);
170         if (status.isSuccess()) {
171             return Response.status(Response.Status.OK).build();
172         }
173         throw new InternalServerErrorException(status.getDescription());
174     }
175
176     /* /\** */
177     /*  * */
178     /*  * Add or remove switch ports to a subnet */
179     /*  * */
180     /*  * POST subnets/green/sw */
181     /*  * */
182     /*  * @param model */
183     /*  * @param containerName */
184     /*  * @param name */
185     /*  * @param subnet: the subnet name name */
186     /*  * @param switchports: datapath ID/port list => xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y */
187     /*  * @return */
188     /*  *\/ */
189     /* @RequestMapping(value = "/{containerName}/{name}", method = RequestMethod.POST) */
190     /* public View addSwitchports(Map<String, Object> model, */
191     /*         @PathVariable(value = "containerName") String containerName, */
192     /*         @PathVariable(value = "name") String name, */
193     /*         @RequestParam(value = "nodeports") String nodePorts, */
194     /*         @RequestParam(value = "action") String action) { */
195
196     /*     checkDefaultDisabled(containerName); */
197
198     /*     ISwitchManager switchManager = null; */
199     /*     try { */
200     /*         BundleContext bCtx = FrameworkUtil.getBundle(this.getClass()) */
201     /*                 .getBundleContext(); */
202
203     /*         ServiceReference[] services = bCtx.getServiceReferences( */
204     /*                 ISwitchManager.class.getName(), "(containerName=" */
205     /*                         + containerName + ")"); */
206
207     /*         if (services != null) { */
208     /*             switchManager = (ISwitchManager) bCtx.getService(services[0]); */
209     /*             logger.debug("Switch manager reference is:" + switchManager); */
210     /*         } */
211     /*     } catch (Exception e) { */
212     /*         logger.error("Switch Manager reference is NULL"); */
213     /*     } */
214
215     /*     checkContainerExists(switchManager); */
216
217     /*     String ret; */
218     /*     if (action.equals("add")) { */
219     /*         ret = switchManager.addPortsToSubnet(name, nodePorts); */
220     /*     } else if (action.equals("remove")) { */
221     /*         ret = switchManager.removePortsFromSubnet(name, nodePorts); */
222     /*     } else { */
223     /*         throw new UnsupportedMediaTypeException(RestMessages.UNKNOWNACTION */
224     /*                 .toString() */
225     /*                 + ": " + action); */
226     /*     } */
227
228     /*     return returnViewOrThrowConflicEx(model, ret); */
229     /* } */
230 }