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.Path;
17 import javax.ws.rs.PathParam;
18 import javax.ws.rs.core.Context;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.SecurityContext;
22 import org.codehaus.enunciate.jaxrs.ResponseCode;
23 import org.codehaus.enunciate.jaxrs.StatusCodes;
24 import org.opendaylight.controller.connectionmanager.IConnectionManager;
25 import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException;
26 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
27 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
28 import org.opendaylight.controller.sal.core.Node;
29 import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
30 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IBridgeDomainConfigService;
31 import org.opendaylight.controller.sal.utils.ServiceHelper;
32 import org.opendaylight.controller.sal.utils.Status;
33 import org.opendaylight.controller.sal.utils.StatusCode;
36 * BridgeDomain Configuration Northbound APIs
39 * Authentication scheme : <b>HTTP Basic</b><br>
40 * Authentication realm : <b>opendaylight</b><br>
41 * Transport : <b>HTTP and HTTPS</b><br>
43 * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding
44 * a proper keystore / SSL certificate from a trusted authority.<br>
45 * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
48 public class BridgeDomainNorthbound {
49 private String username;
52 public void setSecurityContext(SecurityContext context) {
53 if (context != null && context.getUserPrincipal() != null) username = context.getUserPrincipal().getName();
55 protected String getUserName() {
59 private IBridgeDomainConfigService getConfigurationService() {
60 return (IBridgeDomainConfigService) ServiceHelper
61 .getGlobalInstance(IBridgeDomainConfigService.class, this);
64 private IConnectionManager getConnectionManager() {
65 return (IConnectionManager) ServiceHelper
66 .getGlobalInstance(IConnectionManager.class, this);
76 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/bridge/STUB/mgmt1/bridge1
79 * @param nodeType Node Type of the node with the management session.
80 * @param nodeId Node Identifier of the node with the management session.
81 * @param bridgeName Name / Identifier for a bridge to be created.
84 @Path("/bridge/{nodeType}/{nodeId}/{bridgeName}")
86 @StatusCodes( { @ResponseCode(code = 201, condition = "Bridge created successfully"),
87 @ResponseCode(code = 404, condition = "Could not create Bridge"),
88 @ResponseCode(code = 412, condition = "Failed to create Bridge due to an exception"),
89 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
91 public Response createBridge(
92 @PathParam(value = "nodeType") String nodeType,
93 @PathParam(value = "nodeId") String nodeId,
94 @PathParam(value = "bridgeName") String name) {
96 IBridgeDomainConfigService configurationService = getConfigurationService();
97 if (configurationService == null) {
98 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
101 Node node = Node.fromString(nodeType, nodeId);
102 Status status = null;
104 status = configurationService.createBridgeDomain(node, name, null);
105 if (status.getCode().equals(StatusCode.SUCCESS)) {
106 return Response.status(Response.Status.CREATED).build();
108 } catch (Throwable t) {
109 return Response.status(Response.Status.PRECONDITION_FAILED).build();
111 throw new ResourceNotFoundException(status.getDescription());
115 * Add a Port to a Bridge
121 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/port/STUB/mgmt1/bridge1/port1
124 * @param nodeType Node Type of the node with the management session.
125 * @param nodeId Node Identifier of the node with the management session.
126 * @param bridgeName Name / Identifier of the bridge to which a Port is being added.
127 * @param portName Name / Identifier of a Port that is being added to a bridge.
130 @Path("/port/{nodeType}/{nodeId}/{bridgeName}/{portName}")
132 @StatusCodes( { @ResponseCode(code = 201, condition = "Port added successfully"),
133 @ResponseCode(code = 404, condition = "Could not add Port to the Bridge"),
134 @ResponseCode(code = 412, condition = "Failed to add Port due to an exception"),
135 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
137 public Response addPort(
138 @PathParam(value = "nodeType") String nodeType,
139 @PathParam(value = "nodeId") String nodeId,
140 @PathParam(value = "bridgeName") String bridge,
141 @PathParam(value = "portName") String port) {
143 IBridgeDomainConfigService configurationService = getConfigurationService();
144 if (configurationService == null) {
145 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
148 Node node = Node.fromString(nodeType, nodeId);
149 Status status = null;
151 status = configurationService.addPort(node, bridge, port, null);
152 if (status.getCode().equals(StatusCode.SUCCESS)) {
153 return Response.status(Response.Status.CREATED).build();
155 } catch (Throwable t) {
156 return Response.status(Response.Status.PRECONDITION_FAILED).build();
158 throw new ResourceNotFoundException(status.getDescription());
162 * Add a Port,Vlan to a Bridge
167 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/port/STUB/mgmt1/bridge1/port2/200
170 * @param nodeType Node Type of the node with the management session.
171 * @param nodeId Node Identifier of the node with the management session.
172 * @param bridgeName Name / Identifier of the bridge to which a Port is being added.
173 * @param portName Name / Identifier of a Port that is being added to a bridge.
174 * @param vlan Vlan Id.
177 @Path("/port/{nodeType}/{nodeId}/{bridgeName}/{portName}/{vlan}")
179 @StatusCodes( { @ResponseCode(code = 201, condition = "Created Port with Vlan tag successfully"),
180 @ResponseCode(code = 404, condition = "Could not add Port,Vlan to the Bridge"),
181 @ResponseCode(code = 406, condition = "Invalid Vlan parameter passed."),
182 @ResponseCode(code = 412, condition = "Failed to add Port,Vlan due to an exception"),
183 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
185 public Response addPort(
186 @PathParam(value = "nodeType") String nodeType,
187 @PathParam(value = "nodeId") String nodeId,
188 @PathParam(value = "bridgeName") String bridge,
189 @PathParam(value = "portName") String port,
190 @PathParam(value = "vlan") String vlan) {
192 IBridgeDomainConfigService configurationService = getConfigurationService();
193 if (configurationService == null) {
194 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
197 Integer.parseInt(vlan);
198 } catch (Exception e) {
199 throw new NotAcceptableException("Incorrect Vlan :"+vlan);
202 Node node = Node.fromString(nodeType, nodeId);
203 Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
204 configs.put(ConfigConstants.TYPE, ConfigConstants.VLAN.name());
205 configs.put(ConfigConstants.VLAN, vlan);
207 Status status = null;
209 status = configurationService.addPort(node, bridge, port, configs);
210 if (status.getCode().equals(StatusCode.SUCCESS)) {
211 return Response.status(Response.Status.CREATED).build();
213 } catch (Exception e) {
214 return Response.status(Response.Status.PRECONDITION_FAILED).build();
216 throw new ResourceNotFoundException(status.getDescription());