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;
31 import org.codehaus.enunciate.jaxrs.ResponseCode;
32 import org.codehaus.enunciate.jaxrs.StatusCodes;
33 import org.codehaus.enunciate.jaxrs.TypeHint;
34 import org.opendaylight.controller.connectionmanager.IConnectionManager;
35 import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException;
36 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
37 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
38 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
39 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
40 import org.opendaylight.controller.sal.authorization.Privilege;
41 import org.opendaylight.controller.sal.connection.ConnectionConstants;
42 import org.opendaylight.controller.sal.core.Node;
43 import org.opendaylight.controller.sal.utils.NetUtils;
44 import org.opendaylight.controller.sal.utils.ServiceHelper;
45 import org.opendaylight.controller.sal.utils.Status;
48 * Connection Manager Northbound APIs
51 public class ConnectionManagerNorthbound {
52 private String username;
55 public void setSecurityContext(SecurityContext context) {
56 if (context != null && context.getUserPrincipal() != null) username = context.getUserPrincipal().getName();
58 protected String getUserName() {
62 private IConnectionManager getConnectionManager() {
63 return (IConnectionManager) ServiceHelper
64 .getGlobalInstance(IConnectionManager.class, this);
69 * Retrieve a list of all the nodes connected to a given controller in the cluster.
71 * @param controllerAddress Optional parameter to retrieve the nodes connected to another
72 * controller in the cluster
73 * @return A list of Nodes {@link org.opendaylight.controller.sal.core.Node}
80 * http://localhost:8080/controller/nb/v2/connectionmanager/nodes?controller=1.1.1.1
82 * Response body in XML:
85 * <id>00:00:00:00:00:00:00:52</id>
86 * <type>OF</type>
89 * <id>00:00:00:00:00:00:00:3e</id>
90 * <type>OF</type>
94 * Response body in JSON:
99 * "id": "00:00:00:00:00:00:00:52"
103 * "id": "00:00:00:00:00:00:00:3e"
111 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
112 @TypeHint(Nodes.class)
114 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
115 @ResponseCode(code = 406, condition = "Invalid Controller IP Address passed."),
116 @ResponseCode(code = 503, condition = "Connection Manager Service not available")})
118 public Nodes getNodes(@DefaultValue("") @QueryParam("controller") String controllerAddress) {
119 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
120 throw new UnauthorizedException("User is not authorized to perform this operation on container");
123 IConnectionManager connectionManager = getConnectionManager();
124 if (connectionManager == null) {
125 throw new ServiceUnavailableException("IConnectionManager not available.");
128 if ((controllerAddress != null) && (controllerAddress.trim().length() > 0) &&
129 !NetUtils.isIPv4AddressValid(controllerAddress)) {
130 throw new NotAcceptableException("Invalid ip address "+controllerAddress);
132 Set<Node> nodeSet = null;
134 if (controllerAddress != null) {
136 nodeSet = connectionManager.getNodes(InetAddress.getByName(controllerAddress));
137 } catch (UnknownHostException e) {
138 throw new NotAcceptableException("Invalid ip address "+controllerAddress);
141 nodeSet = connectionManager.getLocalNodes();
143 return new Nodes(nodeSet);
147 * If a Network Configuration Service needs a Management Connection and if the
148 * Node Type is unknown, use this REST api to connect to the management session.
154 * PUT http://localhost:8080/controller/nb/v2/connectionmanager/node/mgmt1/address/1.1.1.1/port/6634
160 * <id>mgmt1</id>
161 * <type>STUB</type>
165 * {"id": "mgmt1","type": "STUB"}
168 * @param nodeId User-Defined name of the node to connect with. This can be any alpha numeric value
169 * @param ipAddress IP Address of the Node to connect with.
170 * @param port Layer4 Port of the management session to connect with.
171 * @return Node If the connection is successful, HTTP 404 otherwise.
174 @Path("/node/{nodeId}/address/{ipAddress}/port/{port}/")
176 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
177 @TypeHint(Node.class)
179 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
180 @ResponseCode(code = 404, condition = "Could not connect to the Node with the specified parameters"),
181 @ResponseCode(code = 406, condition = "Invalid IP Address or Port parameter passed."),
182 @ResponseCode(code = 503, condition = "Connection Manager Service not available")} )
184 @PathParam(value = "nodeId") String nodeId,
185 @PathParam(value = "ipAddress") String ipAddress,
186 @PathParam(value = "port") String port) {
188 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
189 throw new UnauthorizedException("User is not authorized to perform this operation on container");
192 IConnectionManager connectionManager = getConnectionManager();
193 if (connectionManager == null) {
194 throw new ServiceUnavailableException("IConnectionManager not available.");
197 if (!NetUtils.isIPv4AddressValid(ipAddress)) {
198 throw new NotAcceptableException("Invalid ip address "+ipAddress);
202 Integer.parseInt(port);
203 } catch (Exception e) {
204 throw new NotAcceptableException("Invalid Layer4 Port "+port);
207 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
208 params.put(ConnectionConstants.ADDRESS, ipAddress);
209 params.put(ConnectionConstants.PORT, port);
213 node = connectionManager.connect(nodeId, params);
215 throw new ResourceNotFoundException("Failed to connect to Node at "+ipAddress+":"+port);
218 } catch (Exception e) {
219 throw new ResourceNotFoundException("Failed to connect to Node with Exception "+e.getMessage());
224 * If a Network Configuration Service needs a Management Connection, and if the
225 * node Type is known, the user can choose to use this REST api to connect to the management session.
231 * PUT http://localhost:8080/controller/nb/v2/connectionmanager/node/STUB/mgmt1/address/1.1.1.1/port/6634
236 * <id>mgmt1</id>
237 * <type>STUB</type>
241 * {"id": "mgmt1","type": "STUB"}
244 * @param nodeType Type of the Node the connection is made for.
245 * @param nodeId User-Defined name of the node to connect with. This can be any alpha numeric value
246 * @param ipAddress IP Address of the Node to connect with.
247 * @param port Layer4 Port of the management session to connect with.
248 * @return Node If the connection is successful, HTTP 404 otherwise.
251 @Path("/node/{nodeType}/{nodeId}/address/{ipAddress}/port/{port}/")
253 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
254 @TypeHint(Node.class)
256 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
257 @ResponseCode(code = 404, condition = "Could not connect to the Node with the specified parameters"),
258 @ResponseCode(code = 406, condition = "Invalid IP Address or Port parameter passed."),
259 @ResponseCode(code = 503, condition = "Connection Manager Service not available")} )
261 @PathParam(value = "nodeType") String nodeType,
262 @PathParam(value = "nodeId") String nodeId,
263 @PathParam(value = "ipAddress") String ipAddress,
264 @PathParam(value = "port") String port) {
266 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
267 throw new UnauthorizedException("User is not authorized to perform this operation on container");
270 IConnectionManager connectionManager = getConnectionManager();
271 if (connectionManager == null) {
272 throw new ServiceUnavailableException("IConnectionManager not available.");
275 if (!NetUtils.isIPv4AddressValid(ipAddress)) {
276 throw new NotAcceptableException("Invalid ip address "+ipAddress);
280 Integer.parseInt(port);
281 } catch (Exception e) {
282 throw new NotAcceptableException("Invalid Layer4 Port "+port);
285 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
286 params.put(ConnectionConstants.ADDRESS, ipAddress);
287 params.put(ConnectionConstants.PORT, port);
291 node = connectionManager.connect(nodeType, nodeId, params);
293 throw new ResourceNotFoundException("Failed to connect to Node at "+ipAddress+":"+port);
296 } catch (Exception e) {
297 throw new ResourceNotFoundException(e.getMessage());
302 * Disconnect an existing Connection.
308 * DELETE http://localhost:8080/controller/nb/v2/connectionmanager/node/STUB/mgmt1
311 * @param nodeType Type of the Node
312 * @param nodeId Connection's NodeId.
315 @Path("/node/{nodeType}/{nodeId}/")
317 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
318 @TypeHint(Response.class)
320 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
321 @ResponseCode(code = 200, condition = "Node disconnected successfully"),
322 @ResponseCode(code = 404, condition = "Could not find a connection with the specified Node identifier"),
323 @ResponseCode(code = 503, condition = "Connection Manager Service not available")} )
324 public Response disconnect(
325 @PathParam(value = "nodeType") String nodeType,
326 @PathParam(value = "nodeId") String nodeId) {
328 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
329 throw new UnauthorizedException("User is not authorized to perform this operation on container");
331 IConnectionManager connectionManager = getConnectionManager();
332 if (connectionManager == null) {
333 throw new ServiceUnavailableException("IConnectionManager not available.");
337 Node node = new Node(nodeType, nodeId);
338 Status status = connectionManager.disconnect(node);
339 if (status.isSuccess()) {
340 return Response.ok().build();
342 return NorthboundUtils.getResponse(status);
343 } catch (Exception e) {
344 throw new ResourceNotFoundException(e.getMessage());