2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.subnets.northbound;
10 import java.util.ArrayList;
11 import java.util.List;
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;
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;
37 public class SubnetsNorthboundJAXRS {
38 protected static final Logger logger = LoggerFactory
39 .getLogger(SubnetsNorthboundJAXRS.class);
42 * List all the subnets in a given container
44 * @param containerName container in which we want to query the subnets
46 * @return a List of SubnetConfig
48 @Path("/{containerName}")
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
62 return new SubnetConfigs(switchManager.getSubnetsConfigList());
66 * List the configuration of a subnet in a given container
68 * @param containerName container in which we want to query the
70 * @param subnetName of the subnet being queried
72 * @return a SubnetConfig
74 @Path("/{containerName}/{subnetName}")
76 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
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
91 SubnetConfig res = switchManager.getSubnetConfig(subnetName);
93 throw new ResourceNotFoundException(RestMessages.NOSUBNET
101 * Add/Update a subnet to a container
103 * @param containerName container in which we want to add/update the
105 * @param subnetName that has to be added
106 * @param subnet pair default gateway IP/mask that identify the
107 * subnet being added modified
110 @Path("/{containerName}/{subnetName}")
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());
123 if (subnet == null) {
124 throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
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());
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();
139 throw new InternalServerErrorException(status.getDescription());
143 * Delete a subnet from a container
145 * @param containerName container in which we want to delete the
147 * @param subnetName of the subnet to be remove.
150 @Path("/{containerName}/{subnetName}")
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
162 ISwitchManager switchManager = null;
163 switchManager = (ISwitchManager) ServiceHelper.getInstance(
164 ISwitchManager.class, containerName, this);
165 if (switchManager == null) {
166 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
169 Status status = switchManager.removeSubnet(subnetName);
170 if (status.isSuccess()) {
171 return Response.status(Response.Status.OK).build();
173 throw new InternalServerErrorException(status.getDescription());
178 /* * Add or remove switch ports to a subnet */
180 /* * POST subnets/green/sw */
183 /* * @param containerName */
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 */
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) { */
196 /* checkDefaultDisabled(containerName); */
198 /* ISwitchManager switchManager = null; */
200 /* BundleContext bCtx = FrameworkUtil.getBundle(this.getClass()) */
201 /* .getBundleContext(); */
203 /* ServiceReference[] services = bCtx.getServiceReferences( */
204 /* ISwitchManager.class.getName(), "(containerName=" */
205 /* + containerName + ")"); */
207 /* if (services != null) { */
208 /* switchManager = (ISwitchManager) bCtx.getService(services[0]); */
209 /* logger.debug("Switch manager reference is:" + switchManager); */
211 /* } catch (Exception e) { */
212 /* logger.error("Switch Manager reference is NULL"); */
215 /* checkContainerExists(switchManager); */
218 /* if (action.equals("add")) { */
219 /* ret = switchManager.addPortsToSubnet(name, nodePorts); */
220 /* } else if (action.equals("remove")) { */
221 /* ret = switchManager.removePortsFromSubnet(name, nodePorts); */
223 /* throw new UnsupportedMediaTypeException(RestMessages.UNKNOWNACTION */
225 /* + ": " + action); */
228 /* return returnViewOrThrowConflicEx(model, ret); */