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;
12 import javax.ws.rs.DELETE;
13 import javax.ws.rs.GET;
14 import javax.ws.rs.POST;
15 import javax.ws.rs.Path;
16 import javax.ws.rs.PathParam;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.QueryParam;
19 import javax.ws.rs.core.Context;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.SecurityContext;
24 import org.codehaus.enunciate.jaxrs.ResponseCode;
25 import org.codehaus.enunciate.jaxrs.StatusCodes;
26 import org.codehaus.enunciate.jaxrs.TypeHint;
27 import org.opendaylight.controller.northbound.commons.RestMessages;
28 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
29 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
30 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
31 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
32 import org.opendaylight.controller.sal.authorization.Privilege;
33 import org.opendaylight.controller.sal.utils.ServiceHelper;
34 import org.opendaylight.controller.sal.utils.Status;
35 import org.opendaylight.controller.switchmanager.ISwitchManager;
36 import org.opendaylight.controller.switchmanager.SubnetConfig;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 public class SubnetsNorthboundJAXRS {
42 protected static final Logger logger = LoggerFactory
43 .getLogger(SubnetsNorthboundJAXRS.class);
45 private String username;
48 public void setSecurityContext(SecurityContext context) {
49 username = context.getUserPrincipal().getName();
52 protected String getUserName() {
57 * List all the subnets in a given container
59 * @param containerName
60 * container in which we want to query the subnets
62 * @return a List of SubnetConfig
64 @Path("/{containerName}")
66 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
67 @StatusCodes({ @ResponseCode(code = 404, condition = "The containerName passed was not found") })
68 @TypeHint(SubnetConfigs.class)
69 public SubnetConfigs listSubnets(
70 @PathParam("containerName") String containerName) {
71 if (!NorthboundUtils.isAuthorized(
72 getUserName(), containerName, Privilege.READ, this)) {
73 throw new UnauthorizedException(
74 "User is not authorized to perform this operation on container "
77 ISwitchManager switchManager = null;
78 switchManager = (ISwitchManager) ServiceHelper.getInstance(
79 ISwitchManager.class, containerName, this);
80 if (switchManager == null) {
81 throw new ResourceNotFoundException(
82 RestMessages.NOCONTAINER.toString());
84 return new SubnetConfigs(switchManager.getSubnetsConfigList());
88 * List the configuration of a subnet in a given container
90 * @param containerName
91 * container in which we want to query the subnet
93 * of the subnet being queried
95 * @return a SubnetConfig
97 @Path("/{containerName}/{subnetName}")
99 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
101 @ResponseCode(code = 404, condition = "The containerName passed was not found"),
102 @ResponseCode(code = 404, condition = "Subnet does not exist") })
103 @TypeHint(SubnetConfig.class)
104 public SubnetConfig listSubnet(
105 @PathParam("containerName") String containerName,
106 @PathParam("subnetName") String subnetName) {
108 if (!NorthboundUtils.isAuthorized(
109 getUserName(), containerName, Privilege.READ, this)) {
110 throw new UnauthorizedException(
111 "User is not authorized to perform this operation on container "
114 ISwitchManager switchManager = null;
115 switchManager = (ISwitchManager) ServiceHelper.getInstance(
116 ISwitchManager.class, containerName, this);
117 if (switchManager == null) {
118 throw new ResourceNotFoundException(
119 RestMessages.NOCONTAINER.toString());
121 SubnetConfig res = switchManager.getSubnetConfig(subnetName);
123 throw new ResourceNotFoundException(
124 RestMessages.NOSUBNET.toString());
131 * Add/Update a subnet to a container
133 * @param containerName
134 * container in which we want to add/update the subnet
136 * that has to be added
138 * pair default gateway IP/mask that identify the subnet being
142 @Path("/{containerName}/{subnetName}")
145 @ResponseCode(code = 404, condition = "The containerName passed was not found"),
146 @ResponseCode(code = 404, condition = "Invalid Data passed"),
147 @ResponseCode(code = 201, condition = "Subnet added/modified"),
148 @ResponseCode(code = 500, condition = "Addition of subnet failed") })
149 public Response addSubnet(@PathParam("containerName") String containerName,
150 @PathParam("subnetName") String subnetName,
151 @QueryParam("subnet") String subnet) {
153 if (!NorthboundUtils.isAuthorized(
154 getUserName(), containerName, Privilege.WRITE, this)) {
155 throw new UnauthorizedException(
156 "User is not authorized to perform this operation on container "
159 if (subnetName == null) {
160 throw new ResourceNotFoundException(
161 RestMessages.INVALIDDATA.toString());
163 if (subnet == null) {
164 throw new ResourceNotFoundException(
165 RestMessages.INVALIDDATA.toString());
167 ISwitchManager switchManager = null;
168 switchManager = (ISwitchManager) ServiceHelper.getInstance(
169 ISwitchManager.class, containerName, this);
170 if (switchManager == null) {
171 throw new ResourceNotFoundException(
172 RestMessages.NOCONTAINER.toString());
175 SubnetConfig cfgObject = new SubnetConfig(subnetName, subnet,
176 new ArrayList<String>(0));
177 Status status = switchManager.addSubnet(cfgObject);
178 if (status.isSuccess()) {
179 return Response.status(Response.Status.CREATED).build();
181 throw new InternalServerErrorException(status.getDescription());
185 * Delete a subnet from a container
187 * @param containerName
188 * container in which we want to delete the subnet by name
190 * of the subnet to be remove.
193 @Path("/{containerName}/{subnetName}")
196 @ResponseCode(code = 404, condition = "The containerName passed was not found"),
197 @ResponseCode(code = 500, condition = "Removal of subnet failed") })
198 public Response removeSubnet(
199 @PathParam("containerName") String containerName,
200 @PathParam("subnetName") String subnetName) {
201 if (subnetName == null) {
202 throw new ResourceNotFoundException(
203 RestMessages.INVALIDDATA.toString());
206 if (!NorthboundUtils.isAuthorized(
207 getUserName(), containerName, Privilege.WRITE, this)) {
208 throw new UnauthorizedException(
209 "User is not authorized to perform this operation on container "
213 ISwitchManager switchManager = null;
214 switchManager = (ISwitchManager) ServiceHelper.getInstance(
215 ISwitchManager.class, containerName, this);
216 if (switchManager == null) {
217 throw new ResourceNotFoundException(
218 RestMessages.NOCONTAINER.toString());
220 Status status = switchManager.removeSubnet(subnetName);
221 if (status.isSuccess()) {
222 return Response.status(Response.Status.OK).build();
224 throw new InternalServerErrorException(status.getDescription());
229 * Add or remove switch ports to a subnet POST subnets/green/sw
233 * @param containerName
237 * @param subnet: the subnet name name
239 * @param switchports: datapath ID/port list =>
240 * xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
244 * @RequestMapping(value = "/{containerName}/{name}", method =
245 * RequestMethod.POST)
247 * public View addSwitchports(Map<String, Object> model,
249 * @PathVariable(value = "containerName") String containerName,
251 * @PathVariable(value = "name") String name,
253 * @RequestParam(value = "nodeports") String nodePorts,
255 * @RequestParam(value = "action") String action) {
257 * checkDefaultDisabled(containerName); ISwitchManager switchManager = null;
258 * try { BundleContext bCtx = FrameworkUtil.getBundle(this.getClass())
259 * .getBundleContext();
261 * ServiceReference[] services = bCtx.getServiceReferences(
262 * ISwitchManager.class.getName(), "(containerName=" + containerName + ")");
264 * if (services != null) { switchManager = (ISwitchManager)
265 * bCtx.getService(services[0]); logger.debug("Switch manager reference is:"
266 * + switchManager); } } catch (Exception e) {
267 * logger.error("Switch Manager reference is NULL"); }
269 * checkContainerExists(switchManager);
271 * String ret; if (action.equals("add")) { ret =
272 * switchManager.addPortsToSubnet(name, nodePorts); } else if
273 * (action.equals("remove")) { ret =
274 * switchManager.removePortsFromSubnet(name, nodePorts); } else { throw new
275 * UnsupportedMediaTypeException(RestMessages.UNKNOWNACTION .toString() +
278 * return returnViewOrThrowConflicEx(model, ret); }