2 * Copyright (C) 2014 Red Hat, Inc. and others
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Madhu Venugopal, Brent Salisbury, Dave Tucker
10 package org.opendaylight.ovsdb.northbound;
12 import java.io.IOException;
14 import java.util.concurrent.ExecutionException;
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.core.Context;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.SecurityContext;
28 import javax.ws.rs.core.UriInfo;
30 import org.codehaus.enunciate.jaxrs.ResponseCode;
31 import org.codehaus.enunciate.jaxrs.StatusCodes;
32 import org.codehaus.enunciate.jaxrs.TypeHint;
33 import org.opendaylight.controller.northbound.commons.RestMessages;
34 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
35 import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
36 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
37 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
38 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
39 import org.opendaylight.controller.sal.authorization.Privilege;
40 import org.opendaylight.controller.sal.core.Node;
41 import org.opendaylight.controller.sal.utils.ServiceHelper;
42 import org.opendaylight.controller.sal.utils.Status;
43 import org.opendaylight.ovsdb.lib.OvsdbClient;
44 import org.opendaylight.ovsdb.lib.notation.Row;
45 import org.opendaylight.ovsdb.lib.notation.UUID;
46 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
47 import org.opendaylight.ovsdb.plugin.api.OvsVswitchdSchemaConstants;
48 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
49 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
50 import org.opendaylight.ovsdb.plugin.api.StatusWithUuid;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.fasterxml.jackson.databind.JsonNode;
57 * OVSDB Northbound REST API.<br>
58 * This class provides REST APIs to Create, Read, Update and Delete OVSDB Row in any of the ovsdb table
59 * database one at a time. The JSON used to create rows is in the same format as the OVSDB JSON-RPC messages.
60 * This format is documented in the <a href="http://openvswitch.org/ovs-vswitchd.conf.db.5.pdf">OVSDB Schema</a>
61 * and in <a href="http://tools.ietf.org/rfc/rfc7047.txt">RFC 7047</a>.
65 * Authentication scheme : <b>HTTP Basic</b><br>
66 * Authentication realm : <b>opendaylight</b><br>
67 * Transport : <b>HTTP and HTTPS</b><br>
69 * HTTPS Authentication is disabled by default.
74 public class OvsdbNorthboundV2 {
75 protected static final Logger logger = LoggerFactory.getLogger(OvsdbNorthboundV2.class);
78 private UriInfo _uriInfo;
79 private String username;
82 public void setSecurityContext(SecurityContext context) {
83 if (context != null && context.getUserPrincipal() != null) {
84 username = context.getUserPrincipal().getName();
88 protected String getUserName() {
92 private void handleNameMismatch(String name, String nameinURL) {
93 if (name == null || nameinURL == null) {
94 throw new BadRequestException(RestMessages.INVALIDDATA.toString() + " : Name is null");
97 if (name.equalsIgnoreCase(nameinURL)) {
100 throw new ResourceConflictException(RestMessages.INVALIDDATA.toString()
101 + " : Table Name in URL does not match the row name in request body");
105 * Create a Row for Open_vSwitch schema
107 * @param nodeType type of node e.g OVS
108 * @param nodeId ID of the node
109 * @param tableName name of the OVSDB table
110 * @param row the {@link OvsdbRow} Row that is being inserted
112 * @return Response as dictated by the HTTP Response Status code
117 * Create a Bridge Row:
121 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/bridge/rows
128 * "datapath_type": "OPENFLOW"
139 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/port/rows
143 * "parent_uuid": "b01cd26b-9c63-4216-8cf2-55f7087adab1",
149 * "00:00:00:00:00:01"
161 * Create an Interface Row:
165 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/interface/rows
169 * "parent_uuid": "c7b54c9b-9b25-4801-a81d-d7bc489d4840",
175 * "00:00:bb:bb:00:01"
177 * "admin_state": "up"
188 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/SSL/rows
195 * "ca_cert": "ca_cert",
196 * "bootstrap_ca_cert": true,
197 * "certificate": "pieceofpaper",
198 * "private_key": "private"
205 * Create an sFlow Row:
209 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/sflow/rows
213 * "parent_uuid": "6b3072ba-a120-4db9-82f8-a8ce4eae6942",
234 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/qos/rows
238 * "parent_uuid": "b109dbcf-47bb-4121-b244-e623b3421d6e",
241 * "type": "linux-htb"
248 * Create a Queue Row:
252 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/queue/rows
255 * "parent_uuid": "b16eae7d-7e97-46d2-95d1-333d1de4a3d7",
268 * Create a Netflow Row:
272 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/netflow/rows
276 * "parent_uuid": "b01cd26b-9c63-4216-8cf2-55f7087adab1",
282 * "192.168.1.102:9998"
285 * "active_timeout": "0"
292 * Create a Manager Row:
296 * POST http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/manager/rows
300 * "parent_uuid": "8d3fb89b-5fac-4631-a990-f5a4e7f5383a",
303 * "target": "a_string",
304 * "is_connected": true,
310 * @throws IOException
311 * @throws ExecutionException
312 * @throws InterruptedException
315 @Path("/node/{nodeType}/{nodeId}/tables/{tableName}/rows")
317 @StatusCodes({ @ResponseCode(code = 201, condition = "Row Inserted successfully"),
318 @ResponseCode(code = 400, condition = "Invalid data passed"),
319 @ResponseCode(code = 401, condition = "User not authorized to perform this operation")})
320 @Consumes({ MediaType.APPLICATION_JSON})
321 public Response addRow(@PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
322 @PathParam("tableName") String tableName, JsonNode rowJson) throws IOException, InterruptedException, ExecutionException {
324 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
325 throw new UnauthorizedException("User is not authorized to perform this operation");
328 OvsdbConfigurationService
329 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
331 if (ovsdbTable == null) {
332 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
335 Node node = Node.fromString(nodeType, nodeId);
336 OvsdbConnectionService
337 connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
338 OvsdbClient client = connectionService.getConnection(node).getClient();
339 OvsdbRow localRow = OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, rowJson);
340 String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
342 if (localRow == null) {
343 return Response.status(Response.Status.BAD_REQUEST).build();
347 statusWithUuid = ovsdbTable.insertRow(node, bckCompatibleTableName, localRow.getParentUuid(), localRow.getRow());
349 if (statusWithUuid.isSuccess()) {
350 UUID uuid = statusWithUuid.getUuid();
351 return Response.status(Response.Status.CREATED)
352 .header("Location", String.format("%s/%s", _uriInfo.getAbsolutePath().toString(),
354 .entity(uuid.toString())
357 return NorthboundUtils.getResponse(statusWithUuid);
363 * @param nodeType type of node e.g OVS
364 * @param nodeId ID of the node
365 * @param tableName name of the ovsdb table
366 * @param rowUuid UUID of the row being read
368 * @return Row corresponding to the UUID.
374 * Get a specific Bridge Row:
375 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/bridge/rows/6f4c602c-026f-4390-beea-d50d6d448100
377 * Get a specific Port Row:
378 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/port/rows/6f4c602c-026f-4390-beea-d50d6d448100
380 * Get a specific Interface Row:
381 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/interface/rows/6f4c602c-026f-4390-beea-d50d6d448100
383 * Get a specific Controller Row:
384 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/controller/rows/6f4c602c-026f-4390-beea-d50d6d448100
386 * Get a specific SSL Row:
387 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/SSL/rows/6f4c602c-026f-4390-beea-d50d6d448100
389 * Get a specific sFlow Row:
390 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/sflow/rows/6f4c602c-026f-4390-beea-d50d6d448100
392 * Get a specific QoS Row:
393 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/qos/rows/6f4c602c-026f-4390-beea-d50d6d448100
395 * Get a specific Queue Row:
396 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/queue/rows/6f4c602c-026f-4390-beea-d50d6d448100
398 * Get a specific Netflow Row:
399 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/netflow/rows/6f4c602c-026f-4390-beea-d50d6d448100
401 * Get a specific Manager Row:
402 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/manager/rows/6f4c602c-026f-4390-beea-d50d6d448100
406 @Path("/node/{nodeType}/{nodeId}/tables/{tableName}/rows/{rowUuid}")
408 @StatusCodes({ @ResponseCode(code = 200, condition = "Row Updated successfully"),
409 @ResponseCode(code = 400, condition = "Invalid data passed"),
410 @ResponseCode(code = 401, condition = "User not authorized to perform this operation")})
411 @Produces({ MediaType.APPLICATION_JSON})
413 public Row getRow(@PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
414 @PathParam("tableName") String tableName, @PathParam("rowUuid") String rowUuid) {
416 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
417 throw new UnauthorizedException("User is not authorized to perform this operation");
420 OvsdbConfigurationService
421 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
423 if (ovsdbTable == null) {
424 throw new ServiceUnavailableException("UserManager " + RestMessages.SERVICEUNAVAILABLE.toString());
427 Node node = Node.fromString(nodeType, nodeId);
428 OvsdbConnectionService
429 connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
430 OvsdbClient client = connectionService.getConnection(node).getClient();
431 String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
435 row = ovsdbTable.getRow(node, bckCompatibleTableName, rowUuid);
436 } catch (Exception e) {
437 throw new BadRequestException(e.getMessage());
443 * Read all Rows of a table
445 * @param nodeType type of node e.g OVS
446 * @param nodeId ID of the node
447 * @param tableName name of the ovsdb table
449 * @return All the Rows of a table
455 * Get all Bridge Rows:
456 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/bridge/rows
459 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/port/rows
461 * Get all Interface Rows:
462 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/interface/rows
464 * Get all Controller Rows:
465 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/controller/rows
468 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/SSL/rows
470 * Get all sFlow Rows:
471 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/sflow/rows
474 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/qos/rows
476 * Get all Queue Rows:
477 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/queue/rows
479 * Get all Netflow Rows:
480 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/netflow/rows
482 * Get all Manager Rows:
483 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/manager/rows
485 * Get all Open vSwitch Rows:
486 * GET http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/open_vswitch/rows
490 @Path("/node/{nodeType}/{nodeId}/tables/{tableName}/rows")
492 @StatusCodes({ @ResponseCode(code = 200, condition = "Row Updated successfully"),
493 @ResponseCode(code = 400, condition = "Invalid data passed"),
494 @ResponseCode(code = 401, condition = "User not authorized to perform this operation")})
495 @Produces({ MediaType.APPLICATION_JSON})
496 @TypeHint(OvsdbRows.class)
497 public OvsdbRows getAllRows(@PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
498 @PathParam("tableName") String tableName) {
499 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
500 throw new UnauthorizedException("User is not authorized to perform this operation");
503 OvsdbConfigurationService
504 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
506 if (ovsdbTable == null) {
507 throw new ServiceUnavailableException("UserManager " + RestMessages.SERVICEUNAVAILABLE.toString());
510 Node node = Node.fromString(nodeType, nodeId);
511 OvsdbConnectionService
512 connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
513 OvsdbClient client = connectionService.getConnection(node).getClient();
514 String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
515 Map<String, Row> rows = null;
517 rows = ovsdbTable.getRows(node, bckCompatibleTableName);
518 } catch (Exception e) {
519 throw new BadRequestException(e.getMessage());
521 return new OvsdbRows(rows);
528 * @param nodeType type of node e.g OVS
529 * @param nodeId ID of the node
530 * @param tableName name of the ovsdb table
531 * @param rowUuid UUID of the row being updated
532 * @param row the {@link OVSDBRow} Row that is being updated
534 * @return Response as dictated by the HTTP Response Status code
539 * Update the Bridge row to add a controller
543 * PUT http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/bridge/rows/b01cd26b-9c63-4216-8cf2-55f7087adab1
554 * "a566e8b4-fc38-499b-8623-6087d5b36b72"
564 @Path("/node/{nodeType}/{nodeId}/tables/{tableName}/rows/{rowUuid}")
566 @StatusCodes({ @ResponseCode(code = 200, condition = "Row Updated successfully"),
567 @ResponseCode(code = 400, condition = "Invalid data passed"),
568 @ResponseCode(code = 401, condition = "User not authorized to perform this operation")})
569 @Consumes({ MediaType.APPLICATION_JSON})
570 public Response updateRow(@PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
571 @PathParam("tableName") String tableName, @PathParam("rowUuid") String rowUuid,
574 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
575 throw new UnauthorizedException("User is not authorized to perform this operation");
578 OvsdbConfigurationService
579 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
581 if (ovsdbTable == null) {
582 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
585 Node node = Node.fromString(nodeType, nodeId);
586 OvsdbConnectionService
587 connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
588 OvsdbClient client = connectionService.getConnection(node).getClient();
589 String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
590 OvsdbRow localRow = OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, rowJson);
592 if (localRow == null) {
593 return Response.status(Response.Status.BAD_REQUEST).build();
596 Status status = ovsdbTable.updateRow(node, bckCompatibleTableName, localRow.getParentUuid(), rowUuid, localRow.getRow());
597 return NorthboundUtils.getResponse(status);
603 * @param nodeType type of node e.g OVS
604 * @param nodeId ID of the node
605 * @param tableName name of the ovsdb table
606 * @param uuid UUID of the Row to be removed
608 * @return Response as dictated by the HTTP Response Status code
614 * Delete a specific Bridge Row:
615 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/bridge/rows/6f4c602c-026f-4390-beea-d50d6d448100
617 * Delete a specific Port Row:
618 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/port/rows/6f4c602c-026f-4390-beea-d50d6d448100
620 * Delete a specific Interface Row:
621 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/interface/rows/6f4c602c-026f-4390-beea-d50d6d448100
623 * Delete a specific Controller Row:
624 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/controller/rows/6f4c602c-026f-4390-beea-d50d6d448100
626 * Delete a specific SSL Row:
627 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/SSL/rows/6f4c602c-026f-4390-beea-d50d6d448100
629 * Delete a specific sFlow Row:
630 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/sflow/rows/6f4c602c-026f-4390-beea-d50d6d448100
632 * Delete a specific QoS Row:
633 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/qos/rows/6f4c602c-026f-4390-beea-d50d6d448100
635 * Delete a specific Queue Row:
636 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/queue/rows/6f4c602c-026f-4390-beea-d50d6d448100
638 * Delete a specific Netflow Row:
639 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/netflow/rows/6f4c602c-026f-4390-beea-d50d6d448100
641 * Delete a specific Manager Row:
642 * DELETE http://localhost:8080/ovsdb/nb/v2/node/OVS/HOST1/tables/manager/rows/6f4c602c-026f-4390-beea-d50d6d448100
646 @Path("/node/{nodeType}/{nodeId}/tables/{tableName}/rows/{uuid}")
648 @StatusCodes({ @ResponseCode(code = 204, condition = "User Deleted Successfully"),
649 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
650 @ResponseCode(code = 404, condition = "The userName passed was not found"),
651 @ResponseCode(code = 500, condition = "Internal Server Error : Removal of user failed"),
652 @ResponseCode(code = 503, condition = "Service unavailable") })
653 public Response removeRow(@PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
654 @PathParam("tableName") String tableName, @PathParam("uuid") String uuid) {
655 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
656 throw new UnauthorizedException("User is not authorized to perform this operation");
659 OvsdbConfigurationService
660 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
662 if (ovsdbTable == null) {
663 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
666 Node node = Node.fromString(nodeType, nodeId);
667 OvsdbConnectionService
668 connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
669 OvsdbClient client = connectionService.getConnection(node).getClient();
670 String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
672 Status status = ovsdbTable.deleteRow(node, bckCompatibleTableName, uuid);
673 if (status.isSuccess()) {
674 return Response.noContent().build();
676 return NorthboundUtils.getResponse(status);
679 private String getBackwardCompatibleTableName(OvsdbClient client, String databaseName, String tableName) {
680 DatabaseSchema dbSchema = client.getDatabaseSchema(databaseName);
681 if (dbSchema == null || tableName == null) return tableName;
682 for (String dbTableName : dbSchema.getTables()) {
683 if (dbTableName.equalsIgnoreCase(tableName)) return dbTableName;