3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.networkconfig.bridgedomain.northbound;
12 import java.util.HashMap;
15 import javax.ws.rs.POST;
16 import javax.ws.rs.PUT;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.PathParam;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.core.Context;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
23 import javax.ws.rs.core.SecurityContext;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.codehaus.enunciate.jaxrs.TypeHint;
28 import org.opendaylight.controller.connectionmanager.IConnectionManager;
29 import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException;
30 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
31 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
32 import org.opendaylight.controller.sal.connection.ConnectionConstants;
33 import org.opendaylight.controller.sal.core.Node;
34 import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
35 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IBridgeDomainConfigService;
36 import org.opendaylight.controller.sal.utils.NetUtils;
37 import org.opendaylight.controller.sal.utils.ServiceHelper;
38 import org.opendaylight.controller.sal.utils.Status;
39 import org.opendaylight.controller.sal.utils.StatusCode;
42 * BridgeDomain Configuration Northbound APIs
45 * Authentication scheme : <b>HTTP Basic</b><br>
46 * Authentication realm : <b>opendaylight</b><br>
47 * Transport : <b>HTTP and HTTPS</b><br>
49 * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding
50 * a proper keystore / SSL certificate from a trusted authority.<br>
51 * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
54 public class BridgeDomainNorthbound {
55 private String username;
58 public void setSecurityContext(SecurityContext context) {
59 if (context != null && context.getUserPrincipal() != null) username = context.getUserPrincipal().getName();
61 protected String getUserName() {
65 private IBridgeDomainConfigService getConfigurationService() {
66 return (IBridgeDomainConfigService) ServiceHelper
67 .getGlobalInstance(IBridgeDomainConfigService.class, this);
70 private IConnectionManager getConnectionManager() {
71 return (IConnectionManager) ServiceHelper
72 .getGlobalInstance(IConnectionManager.class, this);
82 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/bridge/STUB/mgmt1/bridge1
85 * @param nodeType Node Type of the node with the management session.
86 * @param nodeId Node Identifier of the node with the management session.
87 * @param bridgeName Name / Identifier for a bridge to be created.
90 @Path("/bridge/{nodeType}/{nodeId}/{bridgeName}")
92 @StatusCodes( { @ResponseCode(code = 201, condition = "Bridge created successfully"),
93 @ResponseCode(code = 404, condition = "Could not create Bridge"),
94 @ResponseCode(code = 412, condition = "Failed to create Bridge due to an exception"),
95 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
97 public Response createBridge(
98 @PathParam(value = "nodeType") String nodeType,
99 @PathParam(value = "nodeId") String nodeId,
100 @PathParam(value = "bridgeName") String name) {
102 IBridgeDomainConfigService configurationService = getConfigurationService();
103 if (configurationService == null) {
104 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
107 Node node = Node.fromString(nodeType, nodeId);
108 Status status = null;
110 status = configurationService.createBridgeDomain(node, name, null);
111 if (status.getCode().equals(StatusCode.SUCCESS)) {
112 return Response.status(Response.Status.CREATED).build();
114 } catch (Throwable t) {
115 return Response.status(Response.Status.PRECONDITION_FAILED).build();
117 throw new ResourceNotFoundException(status.getDescription());
121 * Add a Port to a Bridge
127 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/port/STUB/mgmt1/bridge1/port1
130 * @param nodeType Node Type of the node with the management session.
131 * @param nodeId Node Identifier of the node with the management session.
132 * @param bridgeName Name / Identifier of the bridge to which a Port is being added.
133 * @param portName Name / Identifier of a Port that is being added to a bridge.
136 @Path("/port/{nodeType}/{nodeId}/{bridgeName}/{portName}")
138 @StatusCodes( { @ResponseCode(code = 201, condition = "Port added successfully"),
139 @ResponseCode(code = 404, condition = "Could not add Port to the Bridge"),
140 @ResponseCode(code = 412, condition = "Failed to add Port due to an exception"),
141 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
143 public Response addPort(
144 @PathParam(value = "nodeType") String nodeType,
145 @PathParam(value = "nodeId") String nodeId,
146 @PathParam(value = "bridgeName") String bridge,
147 @PathParam(value = "portName") String port) {
149 IBridgeDomainConfigService configurationService = getConfigurationService();
150 if (configurationService == null) {
151 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
154 Node node = Node.fromString(nodeType, nodeId);
155 Status status = null;
157 status = configurationService.addPort(node, bridge, port, null);
158 if (status.getCode().equals(StatusCode.SUCCESS)) {
159 return Response.status(Response.Status.CREATED).build();
161 } catch (Throwable t) {
162 return Response.status(Response.Status.PRECONDITION_FAILED).build();
164 throw new ResourceNotFoundException(status.getDescription());
168 * Add a Port,Vlan to a Bridge
173 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/port/STUB/mgmt1/bridge1/port2/200
176 * @param nodeType Node Type of the node with the management session.
177 * @param nodeId Node Identifier of the node with the management session.
178 * @param bridgeName Name / Identifier of the bridge to which a Port is being added.
179 * @param portName Name / Identifier of a Port that is being added to a bridge.
180 * @param vlan Vlan Id.
183 @Path("/port/{nodeType}/{nodeId}/{bridgeName}/{portName}/{vlan}")
185 @StatusCodes( { @ResponseCode(code = 201, condition = "Created Port with Vlan tag successfully"),
186 @ResponseCode(code = 404, condition = "Could not add Port,Vlan to the Bridge"),
187 @ResponseCode(code = 406, condition = "Invalid Vlan parameter passed."),
188 @ResponseCode(code = 412, condition = "Failed to add Port,Vlan due to an exception"),
189 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
191 public Response addPort(
192 @PathParam(value = "nodeType") String nodeType,
193 @PathParam(value = "nodeId") String nodeId,
194 @PathParam(value = "bridgeName") String bridge,
195 @PathParam(value = "portName") String port,
196 @PathParam(value = "vlan") String vlan) {
198 IBridgeDomainConfigService configurationService = getConfigurationService();
199 if (configurationService == null) {
200 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
203 Integer.parseInt(vlan);
204 } catch (Exception e) {
205 throw new NotAcceptableException("Incorrect Vlan :"+vlan);
208 Node node = Node.fromString(nodeType, nodeId);
209 Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
210 configs.put(ConfigConstants.TYPE, ConfigConstants.VLAN.name());
211 configs.put(ConfigConstants.VLAN, vlan);
213 Status status = null;
215 status = configurationService.addPort(node, bridge, port, configs);
216 if (status.getCode().equals(StatusCode.SUCCESS)) {
217 return Response.status(Response.Status.CREATED).build();
219 } catch (Exception e) {
220 return Response.status(Response.Status.PRECONDITION_FAILED).build();
222 throw new ResourceNotFoundException(status.getDescription());