3 * Copyright (c) 2013 Red Hat, 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.connectionmanager.northbound;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.HashMap;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.DefaultValue;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.SecurityContext;
30 import javax.ws.rs.ext.ContextResolver;
32 import org.codehaus.enunciate.jaxrs.ResponseCode;
33 import org.codehaus.enunciate.jaxrs.StatusCodes;
34 import org.codehaus.enunciate.jaxrs.TypeHint;
35 import org.opendaylight.controller.connectionmanager.IConnectionManager;
36 import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException;
37 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
38 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
39 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
40 import org.opendaylight.controller.northbound.commons.query.QueryContext;
41 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
42 import org.opendaylight.controller.sal.authorization.Privilege;
43 import org.opendaylight.controller.sal.connection.ConnectionConstants;
44 import org.opendaylight.controller.sal.core.Node;
45 import org.opendaylight.controller.sal.utils.NetUtils;
46 import org.opendaylight.controller.sal.utils.ServiceHelper;
47 import org.opendaylight.controller.sal.utils.Status;
50 * Connection Manager Northbound APIs
53 public class ConnectionManagerNorthbound {
54 private String username;
55 private QueryContext queryContext;
58 public void setQueryContext(ContextResolver<QueryContext> queryCtxResolver) {
59 if (queryCtxResolver != null) {
60 queryContext = queryCtxResolver.getContext(QueryContext.class);
64 public void setSecurityContext(SecurityContext context) {
65 if (context != null && context.getUserPrincipal() != null) username = context.getUserPrincipal().getName();
67 protected String getUserName() {
71 private IConnectionManager getConnectionManager() {
72 return (IConnectionManager) ServiceHelper
73 .getGlobalInstance(IConnectionManager.class, this);
78 * Retrieve a list of all the nodes connected to a given controller in the cluster.
80 * @param controllerAddress Optional parameter to retrieve the nodes connected to another
81 * controller in the cluster
82 * @return A list of Nodes {@link org.opendaylight.controller.sal.core.Node}
89 * http://localhost:8080/controller/nb/v2/connectionmanager/nodes?controller=1.1.1.1
91 * Response body in XML:
94 * <id>00:00:00:00:00:00:00:52</id>
95 * <type>OF</type>
98 * <id>00:00:00:00:00:00:00:3e</id>
99 * <type>OF</type>
103 * Response body in JSON:
108 * "id": "00:00:00:00:00:00:00:52"
112 * "id": "00:00:00:00:00:00:00:3e"
120 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
121 @TypeHint(Nodes.class)
123 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
124 @ResponseCode(code = 406, condition = "Invalid Controller IP Address passed."),
125 @ResponseCode(code = 503, condition = "Connection Manager Service not available")})
127 public Nodes getNodes(@DefaultValue("") @QueryParam("controller") String controllerAddress,
128 @QueryParam("_q") String queryString) {
129 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
130 throw new UnauthorizedException("User is not authorized to perform this operation on container");
133 IConnectionManager connectionManager = getConnectionManager();
134 if (connectionManager == null) {
135 throw new ServiceUnavailableException("IConnectionManager not available.");
138 if ((controllerAddress != null) && (controllerAddress.trim().length() > 0) &&
139 !NetUtils.isIPv4AddressValid(controllerAddress)) {
140 throw new NotAcceptableException("Invalid ip address "+controllerAddress);
142 Set<Node> nodeSet = null;
144 if (controllerAddress != null) {
146 nodeSet = connectionManager.getNodes(InetAddress.getByName(controllerAddress));
147 } catch (UnknownHostException e) {
148 throw new NotAcceptableException("Invalid ip address "+controllerAddress);
151 nodeSet = connectionManager.getLocalNodes();
153 Nodes nodes = new Nodes(nodeSet);
154 if (queryString != null) {
155 queryContext.createQuery(queryString, Nodes.class)
156 .filter(nodes, Node.class);
162 * If a Network Configuration Service needs a Management Connection and if the
163 * Node Type is unknown, use this REST api to connect to the management session.
169 * PUT http://localhost:8080/controller/nb/v2/connectionmanager/node/mgmt1/address/1.1.1.1/port/6634
175 * <id>mgmt1</id>
176 * <type>STUB</type>
180 * {"id": "mgmt1","type": "STUB"}
183 * @param nodeId User-Defined name of the node to connect with. This can be any alpha numeric value
184 * @param ipAddress IP Address of the Node to connect with.
185 * @param port Layer4 Port of the management session to connect with.
186 * @return Node If the connection is successful, HTTP 404 otherwise.
189 @Path("/node/{nodeId}/address/{ipAddress}/port/{port}/")
191 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
192 @TypeHint(Node.class)
194 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
195 @ResponseCode(code = 404, condition = "Could not connect to the Node with the specified parameters"),
196 @ResponseCode(code = 406, condition = "Invalid IP Address or Port parameter passed."),
197 @ResponseCode(code = 503, condition = "Connection Manager Service not available")} )
199 @PathParam(value = "nodeId") String nodeId,
200 @PathParam(value = "ipAddress") String ipAddress,
201 @PathParam(value = "port") String port) {
203 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
204 throw new UnauthorizedException("User is not authorized to perform this operation on container");
207 IConnectionManager connectionManager = getConnectionManager();
208 if (connectionManager == null) {
209 throw new ServiceUnavailableException("IConnectionManager not available.");
212 if (!NetUtils.isIPv4AddressValid(ipAddress)) {
213 throw new NotAcceptableException("Invalid ip address "+ipAddress);
217 Integer.parseInt(port);
218 } catch (Exception e) {
219 throw new NotAcceptableException("Invalid Layer4 Port "+port);
222 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
223 params.put(ConnectionConstants.ADDRESS, ipAddress);
224 params.put(ConnectionConstants.PORT, port);
228 node = connectionManager.connect(nodeId, params);
230 throw new ResourceNotFoundException("Failed to connect to Node at "+ipAddress+":"+port);
233 } catch (Exception e) {
234 throw new ResourceNotFoundException("Failed to connect to Node with Exception "+e.getMessage());
239 * If a Network Configuration Service needs a Management Connection, and if the
240 * node Type is known, the user can choose to use this REST api to connect to the management session.
246 * PUT http://localhost:8080/controller/nb/v2/connectionmanager/node/STUB/mgmt1/address/1.1.1.1/port/6634
251 * <id>mgmt1</id>
252 * <type>STUB</type>
256 * {"id": "mgmt1","type": "STUB"}
259 * @param nodeType Type of the Node the connection is made for.
260 * @param nodeId User-Defined name of the node to connect with. This can be any alpha numeric value
261 * @param ipAddress IP Address of the Node to connect with.
262 * @param port Layer4 Port of the management session to connect with.
263 * @return Node If the connection is successful, HTTP 404 otherwise.
266 @Path("/node/{nodeType}/{nodeId}/address/{ipAddress}/port/{port}/")
268 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
269 @TypeHint(Node.class)
271 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
272 @ResponseCode(code = 404, condition = "Could not connect to the Node with the specified parameters"),
273 @ResponseCode(code = 406, condition = "Invalid IP Address or Port parameter passed."),
274 @ResponseCode(code = 503, condition = "Connection Manager Service not available")} )
276 @PathParam(value = "nodeType") String nodeType,
277 @PathParam(value = "nodeId") String nodeId,
278 @PathParam(value = "ipAddress") String ipAddress,
279 @PathParam(value = "port") String port) {
281 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
282 throw new UnauthorizedException("User is not authorized to perform this operation on container");
285 IConnectionManager connectionManager = getConnectionManager();
286 if (connectionManager == null) {
287 throw new ServiceUnavailableException("IConnectionManager not available.");
290 if (!NetUtils.isIPv4AddressValid(ipAddress)) {
291 throw new NotAcceptableException("Invalid ip address "+ipAddress);
295 Integer.parseInt(port);
296 } catch (Exception e) {
297 throw new NotAcceptableException("Invalid Layer4 Port "+port);
300 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
301 params.put(ConnectionConstants.ADDRESS, ipAddress);
302 params.put(ConnectionConstants.PORT, port);
306 node = connectionManager.connect(nodeType, nodeId, params);
308 throw new ResourceNotFoundException("Failed to connect to Node at "+ipAddress+":"+port);
311 } catch (Exception e) {
312 throw new ResourceNotFoundException(e.getMessage());
317 * Disconnect an existing Connection.
323 * DELETE http://localhost:8080/controller/nb/v2/connectionmanager/node/STUB/mgmt1
326 * @param nodeType Type of the Node
327 * @param nodeId Connection's NodeId.
330 @Path("/node/{nodeType}/{nodeId}/")
332 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
333 @TypeHint(Response.class)
335 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
336 @ResponseCode(code = 200, condition = "Node disconnected successfully"),
337 @ResponseCode(code = 404, condition = "Could not find a connection with the specified Node identifier"),
338 @ResponseCode(code = 503, condition = "Connection Manager Service not available")} )
339 public Response disconnect(
340 @PathParam(value = "nodeType") String nodeType,
341 @PathParam(value = "nodeId") String nodeId) {
343 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
344 throw new UnauthorizedException("User is not authorized to perform this operation on container");
346 IConnectionManager connectionManager = getConnectionManager();
347 if (connectionManager == null) {
348 throw new ServiceUnavailableException("IConnectionManager not available.");
352 Node node = Node.fromString(nodeType, nodeId);
353 Status status = connectionManager.disconnect(node);
354 if (status.isSuccess()) {
355 return Response.ok().build();
357 return NorthboundUtils.getResponse(status);
358 } catch (Exception e) {
359 throw new ResourceNotFoundException(e.getMessage());