Merge "Initial pass at netvirt model"
[netvirt.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / OvsdbNorthboundV2.java
index 2e2eca8276b6df79001308f55abc9b9b928e98a0..be1d98ea1dfc7930e2dca35a4485f2479e91ade9 100644 (file)
@@ -1,12 +1,11 @@
 /*
- * Copyright (C) 2014 Red Hat, Inc. and others
+ * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
- *
- * Authors : Madhu Venugopal, Brent Salisbury, Dave Tucker
  */
+
 package org.opendaylight.ovsdb.northbound;
 
 import java.io.IOException;
@@ -20,6 +19,7 @@ import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -31,25 +31,21 @@ import org.codehaus.enunciate.jaxrs.StatusCodes;
 import org.codehaus.enunciate.jaxrs.TypeHint;
 import org.opendaylight.controller.northbound.commons.RestMessages;
 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
-import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
 import org.opendaylight.controller.sal.authorization.Privilege;
-import org.opendaylight.controller.sal.core.Node;
-import org.opendaylight.controller.sal.utils.ServiceHelper;
-import org.opendaylight.controller.sal.utils.Status;
+import org.opendaylight.ovsdb.plugin.api.Status;
 import org.opendaylight.ovsdb.lib.OvsdbClient;
 import org.opendaylight.ovsdb.lib.notation.Row;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
-import org.opendaylight.ovsdb.plugin.Connection;
-import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
-import org.opendaylight.ovsdb.plugin.OvsdbConfigService;
-import org.opendaylight.ovsdb.plugin.OvsVswitchdSchemaConstants;
-import org.opendaylight.ovsdb.plugin.StatusWithUuid;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.ovsdb.plugin.api.OvsVswitchdSchemaConstants;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
+import org.opendaylight.ovsdb.plugin.api.StatusWithUuid;
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 
 import com.fasterxml.jackson.databind.JsonNode;
 
@@ -72,7 +68,6 @@ import com.fasterxml.jackson.databind.JsonNode;
 @Path("/v2/")
 @Deprecated
 public class OvsdbNorthboundV2 {
-    protected static final Logger logger = LoggerFactory.getLogger(OvsdbNorthboundV2.class);
 
     @Context
     private UriInfo _uriInfo;
@@ -89,25 +84,13 @@ public class OvsdbNorthboundV2 {
         return username;
     }
 
-    private void handleNameMismatch(String name, String nameinURL) {
-        if (name == null || nameinURL == null) {
-            throw new BadRequestException(RestMessages.INVALIDDATA.toString() + " : Name is null");
-        }
-
-        if (name.equalsIgnoreCase(nameinURL)) {
-            return;
-        }
-        throw new ResourceConflictException(RestMessages.INVALIDDATA.toString()
-                + " : Table Name in URL does not match the row name in request body");
-    }
-
     /**
      * Create a Row for Open_vSwitch schema
      *
      * @param nodeType type of node e.g OVS
      * @param nodeId ID of the node
      * @param tableName name of the OVSDB table
-     * @param row the {@link OvsdbRow} Row that is being inserted
+     * @param rowJson the {@link OvsdbRow} Row that is being inserted
      *
      * @return Response as dictated by the HTTP Response Status code
      *
@@ -325,16 +308,18 @@ public class OvsdbNorthboundV2 {
             throw new UnauthorizedException("User is not authorized to perform this operation");
         }
 
-        OvsdbConfigService ovsdbTable = (OvsdbConfigService)ServiceHelper.getGlobalInstance(OvsdbConfigService.class,
+        OvsdbConfigurationService
+                ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
                                                                                             this);
         if (ovsdbTable == null) {
             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = Node.fromString(nodeType, nodeId);
-        IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
-        Connection connection = connectionService.getConnection(node);
-        OvsdbClient client = connection.getClient();
+        OvsdbConnectionService
+                connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
+        Node node = connectionService.getNode(nodeId);
+
+        OvsdbClient client = connectionService.getConnection(node).getClient();
         OvsdbRow localRow = OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, rowJson);
         String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
 
@@ -342,7 +327,8 @@ public class OvsdbNorthboundV2 {
             return Response.status(Response.Status.BAD_REQUEST).build();
         }
 
-        StatusWithUuid statusWithUuid = ovsdbTable.insertRow(node, bckCompatibleTableName, localRow.getParentUuid(), localRow.getRow());
+        StatusWithUuid
+                statusWithUuid = ovsdbTable.insertRow(node, bckCompatibleTableName, localRow.getParentUuid(), localRow.getRow());
 
         if (statusWithUuid.isSuccess()) {
             UUID uuid = statusWithUuid.getUuid();
@@ -352,7 +338,9 @@ public class OvsdbNorthboundV2 {
                     .entity(uuid.toString())
                     .build();
         }
-        return NorthboundUtils.getResponse(statusWithUuid);
+        return NorthboundUtils.getResponse(
+                new org.opendaylight.controller.sal.utils.Status(
+                        org.opendaylight.controller.sal.utils.StatusCode.SUCCESS));
     }
 
     /**
@@ -406,7 +394,7 @@ public class OvsdbNorthboundV2 {
     @StatusCodes({ @ResponseCode(code = 200, condition = "Row Updated successfully"),
         @ResponseCode(code = 400, condition = "Invalid data passed"),
         @ResponseCode(code = 401, condition = "User not authorized to perform this operation")})
-    @Consumes({ MediaType.APPLICATION_JSON})
+    @Produces({ MediaType.APPLICATION_JSON})
     @TypeHint(Row.class)
     public Row getRow(@PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
                            @PathParam("tableName") String tableName, @PathParam("rowUuid") String rowUuid) {
@@ -415,19 +403,20 @@ public class OvsdbNorthboundV2 {
             throw new UnauthorizedException("User is not authorized to perform this operation");
         }
 
-        OvsdbConfigService ovsdbTable = (OvsdbConfigService)ServiceHelper.getGlobalInstance(OvsdbConfigService.class,
+        OvsdbConfigurationService
+                ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
                                                                                             this);
         if (ovsdbTable == null) {
             throw new ServiceUnavailableException("UserManager " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = Node.fromString(nodeType, nodeId);
-        IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
-        Connection connection = connectionService.getConnection(node);
-        OvsdbClient client = connection.getClient();
+        OvsdbConnectionService
+                connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
+        Node node = connectionService.getNode(nodeId);
+        OvsdbClient client = connectionService.getConnection(node).getClient();
         String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
 
-        Row row = null;
+        Row row;
         try {
             row = ovsdbTable.getRow(node, bckCompatibleTableName, rowUuid);
         } catch (Exception e) {
@@ -489,7 +478,7 @@ public class OvsdbNorthboundV2 {
     @StatusCodes({ @ResponseCode(code = 200, condition = "Row Updated successfully"),
         @ResponseCode(code = 400, condition = "Invalid data passed"),
         @ResponseCode(code = 401, condition = "User not authorized to perform this operation")})
-    @Consumes({ MediaType.APPLICATION_JSON})
+    @Produces({ MediaType.APPLICATION_JSON})
     @TypeHint(OvsdbRows.class)
     public OvsdbRows getAllRows(@PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
                                @PathParam("tableName") String tableName) {
@@ -497,18 +486,19 @@ public class OvsdbNorthboundV2 {
             throw new UnauthorizedException("User is not authorized to perform this operation");
         }
 
-        OvsdbConfigService ovsdbTable = (OvsdbConfigService)ServiceHelper.getGlobalInstance(OvsdbConfigService.class,
+        OvsdbConfigurationService
+                ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
                                                                                             this);
         if (ovsdbTable == null) {
             throw new ServiceUnavailableException("UserManager " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = Node.fromString(nodeType, nodeId);
-        IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
-        Connection connection = connectionService.getConnection(node);
-        OvsdbClient client = connection.getClient();
+        OvsdbConnectionService
+                connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
+        Node node = connectionService.getNode(nodeId);
+        OvsdbClient client = connectionService.getConnection(node).getClient();
         String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
-        Map<String, Row> rows = null;
+        Map<String, Row> rows;
         try {
             rows = ovsdbTable.getRows(node, bckCompatibleTableName);
         } catch (Exception e) {
@@ -571,16 +561,17 @@ public class OvsdbNorthboundV2 {
             throw new UnauthorizedException("User is not authorized to perform this operation");
         }
 
-        OvsdbConfigService ovsdbTable = (OvsdbConfigService)ServiceHelper.getGlobalInstance(OvsdbConfigService.class,
+        OvsdbConfigurationService
+                ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
                                                                                             this);
         if (ovsdbTable == null) {
             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = Node.fromString(nodeType, nodeId);
-        IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
-        Connection connection = connectionService.getConnection(node);
-        OvsdbClient client = connection.getClient();
+        OvsdbConnectionService
+                connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
+        Node node = connectionService.getNode(nodeId);
+        OvsdbClient client = connectionService.getConnection(node).getClient();
         String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
         OvsdbRow localRow = OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, rowJson);
 
@@ -588,8 +579,10 @@ public class OvsdbNorthboundV2 {
             return Response.status(Response.Status.BAD_REQUEST).build();
         }
 
-        Status status = ovsdbTable.updateRow(node, bckCompatibleTableName, localRow.getParentUuid(), rowUuid, localRow.getRow());
-        return NorthboundUtils.getResponse(status);
+        ovsdbTable.updateRow(node, bckCompatibleTableName, localRow.getParentUuid(), rowUuid, localRow.getRow());
+        return NorthboundUtils.getResponse(
+                new org.opendaylight.controller.sal.utils.Status(
+                        org.opendaylight.controller.sal.utils.StatusCode.SUCCESS));
     }
 
     /**
@@ -651,30 +644,37 @@ public class OvsdbNorthboundV2 {
             throw new UnauthorizedException("User is not authorized to perform this operation");
         }
 
-        OvsdbConfigService ovsdbTable = (OvsdbConfigService)ServiceHelper.getGlobalInstance(OvsdbConfigService.class,
+        OvsdbConfigurationService
+                ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
                 this);
         if (ovsdbTable == null) {
             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = Node.fromString(nodeType, nodeId);
-        IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
-        Connection connection = connectionService.getConnection(node);
-        OvsdbClient client = connection.getClient();
+        OvsdbConnectionService
+                connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
+        Node node = connectionService.getNode(nodeId);
+        OvsdbClient client = connectionService.getConnection(node).getClient();
         String bckCompatibleTableName = this.getBackwardCompatibleTableName(client, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName);
 
         Status status = ovsdbTable.deleteRow(node, bckCompatibleTableName, uuid);
         if (status.isSuccess()) {
             return Response.noContent().build();
         }
-        return NorthboundUtils.getResponse(status);
+        return NorthboundUtils.getResponse(
+                new org.opendaylight.controller.sal.utils.Status(
+                        org.opendaylight.controller.sal.utils.StatusCode.SUCCESS));
     }
 
     private String getBackwardCompatibleTableName(OvsdbClient client, String databaseName, String tableName) {
         DatabaseSchema dbSchema = client.getDatabaseSchema(databaseName);
-        if (dbSchema == null || tableName == null) return tableName;
+        if (dbSchema == null || tableName == null) {
+            return tableName;
+        }
         for (String dbTableName : dbSchema.getTables()) {
-            if (dbTableName.equalsIgnoreCase(tableName)) return dbTableName;
+            if (dbTableName.equalsIgnoreCase(tableName)) {
+                return dbTableName;
+            }
         }
         return tableName;
     }