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.Consumes;
16 import javax.ws.rs.POST;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.PathParam;
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.opendaylight.controller.connectionmanager.IConnectionManager;
27 import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException;
28 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
29 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
30 import org.opendaylight.controller.sal.core.Node;
31 import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
32 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IBridgeDomainConfigService;
33 import org.opendaylight.controller.sal.utils.ServiceHelper;
34 import org.opendaylight.controller.sal.utils.Status;
35 import org.opendaylight.controller.sal.utils.StatusCode;
38 * BridgeDomain Configuration Northbound APIs
41 * Authentication scheme : <b>HTTP Basic</b><br>
42 * Authentication realm : <b>opendaylight</b><br>
43 * Transport : <b>HTTP and HTTPS</b><br>
45 * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding
46 * a proper keystore / SSL certificate from a trusted authority.<br>
47 * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
50 public class BridgeDomainNorthbound {
51 private String username;
54 public void setSecurityContext(SecurityContext context) {
55 if (context != null && context.getUserPrincipal() != null) username = context.getUserPrincipal().getName();
57 protected String getUserName() {
61 private IBridgeDomainConfigService getConfigurationService() {
62 return (IBridgeDomainConfigService) ServiceHelper
63 .getGlobalInstance(IBridgeDomainConfigService.class, this);
66 private IConnectionManager getConnectionManager() {
67 return (IConnectionManager) ServiceHelper
68 .getGlobalInstance(IConnectionManager.class, this);
78 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/bridge/STUB/mgmt1/bridge1
81 * @param nodeType Node Type of the node with the management session.
82 * @param nodeId Node Identifier of the node with the management session.
83 * @param bridgeName Name / Identifier for a bridge to be created.
84 * @param bridgeConfigs Additional Bridge Configurations.
87 @Path("/bridge/{nodeType}/{nodeId}/{bridgeName}")
89 @StatusCodes( { @ResponseCode(code = 201, condition = "Bridge created successfully"),
90 @ResponseCode(code = 404, condition = "Could not create Bridge"),
91 @ResponseCode(code = 412, condition = "Failed to create Bridge due to an exception"),
92 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
94 public Response createBridge(
95 @PathParam(value = "nodeType") String nodeType,
96 @PathParam(value = "nodeId") String nodeId,
97 @PathParam(value = "bridgeName") String name,
98 Map<String, Object> bridgeConfigs) {
100 IBridgeDomainConfigService configurationService = getConfigurationService();
101 if (configurationService == null) {
102 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
105 Node node = Node.fromString(nodeType, nodeId);
106 Status status = null;
108 Map<ConfigConstants, Object> configs = this.buildConfig(bridgeConfigs);
109 status = configurationService.createBridgeDomain(node, name, configs);
110 if (status.getCode().equals(StatusCode.SUCCESS)) {
111 return Response.status(Response.Status.CREATED).build();
113 } catch (Throwable t) {
114 return Response.status(Response.Status.PRECONDITION_FAILED).build();
116 throw new ResourceNotFoundException(status.getDescription());
120 * Add a Port to a Bridge
126 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/port/STUB/mgmt1/bridge1/port1
129 * @param nodeType Node Type of the node with the management session.
130 * @param nodeId Node Identifier of the node with the management session.
131 * @param bridgeName Name / Identifier of the bridge to which a Port is being added.
132 * @param portName Name / Identifier of a Port that is being added to a bridge.
133 * @param portConfigs Additional Port Configurations.
136 @Path("/port/{nodeType}/{nodeId}/{bridgeName}/{portName}")
138 @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
139 @StatusCodes( { @ResponseCode(code = 201, condition = "Port added successfully"),
140 @ResponseCode(code = 404, condition = "Could not add Port to the Bridge"),
141 @ResponseCode(code = 412, condition = "Failed to add Port due to an exception"),
142 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
144 public Response addPort(
145 @PathParam(value = "nodeType") String nodeType,
146 @PathParam(value = "nodeId") String nodeId,
147 @PathParam(value = "bridgeName") String bridge,
148 @PathParam(value = "portName") String port,
149 Map<String, Object> portConfigs) {
151 IBridgeDomainConfigService configurationService = getConfigurationService();
152 if (configurationService == null) {
153 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
156 Node node = Node.fromString(nodeType, nodeId);
157 Status status = null;
159 Map<ConfigConstants, Object> configs = this.buildConfig(portConfigs);
160 status = configurationService.addPort(node, bridge, port, configs);
161 if (status.getCode().equals(StatusCode.SUCCESS)) {
162 return Response.status(Response.Status.CREATED).build();
164 } catch (Throwable t) {
165 return Response.status(Response.Status.PRECONDITION_FAILED).build();
167 throw new ResourceNotFoundException(status.getDescription());
170 private Map<ConfigConstants, Object> buildConfig(Map<String, Object> rawConfigs) {
171 if (rawConfigs == null) return null;
172 Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
173 for (String key : rawConfigs.keySet()) {
174 ConfigConstants cc = ConfigConstants.valueOf(key.toUpperCase());
175 configs.put(cc, rawConfigs.get(key));
180 * Add a Port,Vlan to a Bridge
185 * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/port/STUB/mgmt1/bridge1/port2/200
188 * @param nodeType Node Type of the node with the management session.
189 * @param nodeId Node Identifier of the node with the management session.
190 * @param bridgeName Name / Identifier of the bridge to which a Port is being added.
191 * @param portName Name / Identifier of a Port that is being added to a bridge.
192 * @param vlan Vlan Id.
195 @Path("/port/{nodeType}/{nodeId}/{bridgeName}/{portName}/{vlan}")
197 @StatusCodes( { @ResponseCode(code = 201, condition = "Created Port with Vlan tag successfully"),
198 @ResponseCode(code = 404, condition = "Could not add Port,Vlan to the Bridge"),
199 @ResponseCode(code = 406, condition = "Invalid Vlan parameter passed."),
200 @ResponseCode(code = 412, condition = "Failed to add Port,Vlan due to an exception"),
201 @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
203 public Response addPort(
204 @PathParam(value = "nodeType") String nodeType,
205 @PathParam(value = "nodeId") String nodeId,
206 @PathParam(value = "bridgeName") String bridge,
207 @PathParam(value = "portName") String port,
208 @PathParam(value = "vlan") String vlan) {
210 IBridgeDomainConfigService configurationService = getConfigurationService();
211 if (configurationService == null) {
212 throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
215 Integer.parseInt(vlan);
216 } catch (Exception e) {
217 throw new NotAcceptableException("Incorrect Vlan :"+vlan);
220 Node node = Node.fromString(nodeType, nodeId);
221 Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
222 configs.put(ConfigConstants.TYPE, ConfigConstants.VLAN.name());
223 configs.put(ConfigConstants.VLAN, vlan);
225 Status status = null;
227 status = configurationService.addPort(node, bridge, port, configs);
228 if (status.getCode().equals(StatusCode.SUCCESS)) {
229 return Response.status(Response.Status.CREATED).build();
231 } catch (Exception e) {
232 return Response.status(Response.Status.PRECONDITION_FAILED).build();
234 throw new ResourceNotFoundException(status.getDescription());