X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Fplugin%2Fimpl%2FConfigurationServiceImpl.java;h=d284eb6854d399df51378095d17966b21ca17ae0;hb=55d039163fe5525b4f4dc5f28ffe298ab39ffa6f;hp=90e02948f7f502108e445f7df67d600d46870821;hpb=afefc0105ddc1f10f2b5bd7a6b81423fa7fa6270;p=netvirt.git diff --git a/plugin/src/main/java/org/opendaylight/ovsdb/plugin/impl/ConfigurationServiceImpl.java b/plugin/src/main/java/org/opendaylight/ovsdb/plugin/impl/ConfigurationServiceImpl.java index 90e02948f7..d284eb6854 100644 --- a/plugin/src/main/java/org/opendaylight/ovsdb/plugin/impl/ConfigurationServiceImpl.java +++ b/plugin/src/main/java/org/opendaylight/ovsdb/plugin/impl/ConfigurationServiceImpl.java @@ -16,22 +16,13 @@ import java.net.UnknownHostException; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; -import org.eclipse.osgi.framework.console.CommandInterpreter; -import org.eclipse.osgi.framework.console.CommandProvider; -import org.opendaylight.controller.clustering.services.IClusterGlobalServices; -import org.opendaylight.controller.sal.connection.ConnectionConstants; import org.opendaylight.controller.sal.core.Node; -import org.opendaylight.controller.sal.core.NodeConnector; -import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants; -import org.opendaylight.controller.sal.networkconfig.bridgedomain.IPluginInBridgeDomainConfigService; import org.opendaylight.controller.sal.utils.Status; import org.opendaylight.controller.sal.utils.StatusCode; import org.opendaylight.ovsdb.lib.OvsdbClient; @@ -46,12 +37,12 @@ import org.opendaylight.ovsdb.lib.operations.Insert; import org.opendaylight.ovsdb.lib.operations.Operation; import org.opendaylight.ovsdb.lib.operations.OperationResult; import org.opendaylight.ovsdb.lib.operations.TransactionBuilder; +import org.opendaylight.ovsdb.lib.schema.BaseType.UuidBaseType; import org.opendaylight.ovsdb.lib.schema.ColumnSchema; import org.opendaylight.ovsdb.lib.schema.DatabaseSchema; import org.opendaylight.ovsdb.lib.schema.GenericTableSchema; import org.opendaylight.ovsdb.lib.schema.TableSchema; import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable; -import org.opendaylight.ovsdb.plugin.OvsdbConfigService; import org.opendaylight.ovsdb.plugin.api.Connection; import org.opendaylight.ovsdb.plugin.api.OvsVswitchdSchemaConstants; import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService; @@ -61,32 +52,25 @@ import org.opendaylight.ovsdb.plugin.api.StatusWithUuid; import org.opendaylight.ovsdb.plugin.error.OvsdbPluginException; import org.opendaylight.ovsdb.schema.openvswitch.Bridge; import org.opendaylight.ovsdb.schema.openvswitch.Controller; -import org.opendaylight.ovsdb.schema.openvswitch.Interface; -import org.opendaylight.ovsdb.schema.openvswitch.Manager; import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch; import org.opendaylight.ovsdb.schema.openvswitch.Port; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; +import org.opendaylight.ovsdb.utils.config.ConfigProperties; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.node.ObjectNode; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.google.common.util.concurrent.ListenableFuture; -public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigService, - OvsdbConfigurationService, - OvsdbConfigService, - CommandProvider +public class ConfigurationServiceImpl implements OvsdbConfigurationService { private static final Logger logger = LoggerFactory .getLogger(ConfigurationServiceImpl.class); OvsdbConnectionService connectionService; OvsdbInventoryService ovsdbInventoryService; - boolean forceConnect = false; - protected static final String OPENFLOW_10 = "1.0"; protected static final String OPENFLOW_13 = "1.3"; void init() { @@ -107,14 +91,6 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ * */ void start() { - registerWithOSGIConsole(); - } - - private void registerWithOSGIConsole() { - BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()) - .getBundleContext(); - bundleContext.registerService(CommandProvider.class.getName(), this, - null); } /** @@ -146,18 +122,6 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ } } - private IClusterGlobalServices clusterServices; - - public void setClusterServices(IClusterGlobalServices i) { - this.clusterServices = i; - } - - public void unsetClusterServices(IClusterGlobalServices i) { - if (this.clusterServices == i) { - this.clusterServices = null; - } - } - private Connection getConnection (Node node) { Connection connection = connectionService.getConnection(node); if (connection == null || !connection.getClient().isActive()) { @@ -285,35 +249,8 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ @Deprecated public Status updateRow (Node node, String tableName, String parentUUID, String rowUUID, Row row) { String databaseName = OvsVswitchdSchemaConstants.DATABASE_NAME; - Connection connection = connectionService.getConnection(node); - OvsdbClient client = connection.getClient(); - - logger.debug("updateRow : Connection : {} databaseName : {} tableName : {} rowUUID : {} row : {}", - client.getConnectionInfo(), databaseName, tableName, rowUUID, row.toString()); - try{ - DatabaseSchema dbSchema = client.getDatabaseSchema(databaseName); - TransactionBuilder transactionBuilder = client.transactBuilder(dbSchema); - TableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class); - ColumnSchema _uuid = tableSchema.column("_uuid", UUID.class); - transactionBuilder.add(op.update(tableSchema, row) - .where(_uuid.opEqual(new UUID(rowUUID))) - .build()); - - ListenableFuture> results = transactionBuilder.execute(); - List operationResults = results.get(); - if (operationResults.isEmpty() || (transactionBuilder.getOperations().size() != operationResults.size())) { - return new StatusWithUuid(StatusCode.INTERNALERROR); - } - for (OperationResult result : operationResults) { - if (result.getError() != null) { - return new StatusWithUuid(StatusCode.BADREQUEST, result.getError()); - } - } - return new StatusWithUuid(StatusCode.SUCCESS); - } catch(Exception e){ - logger.error("Error in updateRow(): ",e); - } - return new Status(StatusCode.INTERNALERROR); + Row updatedRow = this.updateRow(node, databaseName, tableName, new UUID(rowUUID), row, true); + return new StatusWithUuid(StatusCode.SUCCESS); } private void processDeleteTransaction(OvsdbClient client, String databaseName, String childTable, @@ -370,8 +307,7 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ } } } catch (InterruptedException | ExecutionException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error("Error in deleteRow() {} {}", node, tableName, e); } return new Status(StatusCode.SUCCESS); @@ -395,52 +331,32 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ @Override @Deprecated public List getTables(Node node) { - ConcurrentMap> cache = ovsdbInventoryService.getCache(node, OvsVswitchdSchemaConstants.DATABASE_NAME); - if (cache == null) return null; - return new ArrayList(cache.keySet()); + return this.getTables(node, OvsVswitchdSchemaConstants.DATABASE_NAME); } - private List getControllerIPAddresses(Connection connection) { - List controllers = null; + private InetAddress getControllerIPAddress(Connection connection) { InetAddress controllerIP = null; - controllers = new ArrayList(); - String addressString = System.getProperty("ovsdb.controller.address"); + String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address"); if (addressString != null) { try { controllerIP = InetAddress.getByName(addressString); if (controllerIP != null) { - controllers.add(controllerIP); - return controllers; + return controllerIP; } } catch (UnknownHostException e) { logger.error("Host {} is invalid", addressString); } } - if (clusterServices != null) { - controllers = clusterServices.getClusteredControllers(); - if (controllers != null && controllers.size() > 0) { - if (controllers.size() == 1) { - InetAddress controller = controllers.get(0); - if (!controller.equals(InetAddress.getLoopbackAddress())) { - return controllers; - } - } else { - return controllers; - } - } - } - - addressString = System.getProperty("of.address"); + addressString = ConfigProperties.getProperty(this.getClass(), "of.address"); if (addressString != null) { try { controllerIP = InetAddress.getByName(addressString); if (controllerIP != null) { - controllers.add(controllerIP); - return controllers; + return controllerIP; } } catch (UnknownHostException e) { logger.error("Host {} is invalid", addressString); @@ -449,18 +365,17 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ try { controllerIP = connection.getClient().getConnectionInfo().getLocalAddress(); - controllers.add(controllerIP); - return controllers; + return controllerIP; } catch (Exception e) { logger.debug("Invalid connection provided to getControllerIPAddresses", e); } - return controllers; + return controllerIP; } private short getControllerOFPort() { Short defaultOpenFlowPort = 6633; Short openFlowPort = defaultOpenFlowPort; - String portString = System.getProperty("of.listenPort"); + String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort"); if (portString != null) { try { openFlowPort = Short.decode(portString).shortValue(); @@ -472,6 +387,22 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ return openFlowPort; } + private UUID getCurrentControllerUuid(Node node, final String controllerTableName, final String target) { + ConcurrentMap rows = this.getRows(node, controllerTableName); + + if (rows != null) { + for (Map.Entry entry : rows.entrySet()) { + Controller currController = this.getTypedRow(node, Controller.class, entry.getValue()); + Column column = currController.getTargetColumn(); + String currTarget = column.getData(); + if (currTarget != null && currTarget.equalsIgnoreCase(target)) { + return currController.getUuid(); + } + } + } + return null; + } + @Override public Boolean setOFController(Node node, String bridgeUUID) throws InterruptedException, ExecutionException { Connection connection = this.getConnection(node); @@ -485,15 +416,12 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ try { OvsdbSet protocols = new OvsdbSet(); - String ofVersion = System.getProperty("ovsdb.of.version", OPENFLOW_10); + String ofVersion = System.getProperty("ovsdb.of.version", OPENFLOW_13); switch (ofVersion) { case OPENFLOW_13: - protocols.add("OpenFlow13"); - break; - case OPENFLOW_10: //fall through default: - protocols.add("OpenFlow10"); + protocols.add("OpenFlow13"); break; } bridge.setProtocols(protocols); @@ -512,14 +440,22 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ } Status status = null; - List ofControllerAddrs = this.getControllerIPAddresses(connection); + UUID currControllerUuid = null; + InetAddress ofControllerAddr = this.getControllerIPAddress(connection); short ofControllerPort = getControllerOFPort(); - for (InetAddress ofControllerAddress : ofControllerAddrs) { - String newController = "tcp:"+ofControllerAddress.getHostAddress()+":"+ofControllerPort; - Controller controllerRow = connection.getClient().createTypedRowWrapper(Controller.class); - controllerRow.setTarget(newController); - //ToDo: Status gets overwritten on each iteration. If any operation other than the last fails it's ignored. - status = this.insertRow(node, controllerRow.getSchema().getName(), bridgeUUID, controllerRow.getRow()); + String newControllerTarget = "tcp:"+ofControllerAddr.getHostAddress()+":"+ofControllerPort; + Controller newController = connection.getClient().createTypedRowWrapper(Controller.class); + newController.setTarget(newControllerTarget); + final String controllerTableName = newController.getSchema().getName(); + + currControllerUuid = getCurrentControllerUuid(node, controllerTableName, newControllerTarget); + + if (currControllerUuid != null) { + bridge = connection.getClient().createTypedRowWrapper(Bridge.class); + bridge.setController(Sets.newHashSet(currControllerUuid)); + status = this.updateRow(node, bridge.getSchema().getName(), null, bridgeUUID, bridge.getRow()); + } else { + status = this.insertRow(node, controllerTableName, bridgeUUID, newController.getRow()); } if (status != null) { @@ -579,602 +515,41 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ return client.createTypedRowWrapper(typedClass); } - public void _ovsconnect (CommandInterpreter ci) { - String bridgeName = ci.nextArgument(); - if (bridgeName == null) { - ci.println("Please enter Bridge Name"); - return; - } - - String ovsdbserver = ci.nextArgument(); - if (ovsdbserver == null) { - ci.println("Please enter valid IP-Address"); - return; - } - try { - InetAddress.getByName(ovsdbserver); - } catch (UnknownHostException e) { - logger.error("Unable to resolve " + ovsdbserver, e); - ci.println("Please enter valid IP-Address"); - return; - } - String port = ci.nextArgument(); - if (port == null) { - port = "6634"; - } - - ci.println("connecting to ovsdb server : "+ovsdbserver+":"+port+" ... "); - Map params = new HashMap(); - params.put(ConnectionConstants.ADDRESS, ovsdbserver); - params.put(ConnectionConstants.PORT, port); - Node node = connectionService.connect(bridgeName, params); - if (node != null) ci.println("Node Name: "+node.toString()); - else ci.println("Could not connect to Node"); - } - - public void _addBridge (CommandInterpreter ci) { - String nodeName = ci.nextArgument(); - if (nodeName == null) { - ci.println("Please enter Node Name"); - return; - } - String bridgeName = ci.nextArgument(); - if (bridgeName == null) { - ci.println("Please enter Bridge Name"); - return; - } - Status status; - - Node node = Node.fromString(nodeName); - if (node == null) { - ci.println("Invalid Node"); - return; - } - status = this.createBridgeDomain(node, bridgeName, null); - ci.println("Bridge creation status : "+status.toString()); - } - - public void _getBridgeDomains (CommandInterpreter ci) { - String nodeName = ci.nextArgument(); - if (nodeName == null) { - ci.println("Please enter Node Name"); - return; - } - - List brlist = new ArrayList(); - Node node = Node.fromString(nodeName); - brlist = this.getBridgeDomains(node); - if (node == null) { - ci.println("Invalid Node"); - return; - } - ci.println("Existing Bridges: "+brlist.toString()); - } - - public void _deleteBridgeDomain (CommandInterpreter ci) { - String nodeName = ci.nextArgument(); - if (nodeName == null) { - ci.println("Please enter Node Name"); - return; - } - String bridgeName = ci.nextArgument(); - if (bridgeName == null) { - ci.println("Please enter Bridge Name"); - return; - } - Status status; - Node node = Node.fromString(nodeName); - if (node == null) { - ci.println("Invalid Node"); - return; - } - status = this.deleteBridgeDomain(node, bridgeName); - ci.println("Bridge deletion status : "+status.toString()); - } - - public void _addPort (CommandInterpreter ci) { - String nodeName = ci.nextArgument(); - if (nodeName == null) { - ci.println("Please enter Node Name"); - return; - } - - String bridgeName = ci.nextArgument(); - if (bridgeName == null) { - ci.println("Please enter Bridge Name"); - return; - } - - String portName = ci.nextArgument(); - if (portName == null) { - ci.println("Please enter Port Name"); - return; - } - - String type = ci.nextArgument(); - - Map configs = new HashMap(); - while(true) { - String configKey = ci.nextArgument(); - if (configKey == null) break; - String configValue = ci.nextArgument(); - if (configValue == null) break; - configs.put(configKey, configValue); - } - - Map customConfigs = null; - if (type != null) { - customConfigs = new HashMap(); - customConfigs.put(ConfigConstants.TYPE, type); - } - - if (configs.size() > 0) { - if (customConfigs == null) customConfigs = new HashMap(); - customConfigs.put(ConfigConstants.CUSTOM, configs); - ci.println(customConfigs.toString()); - } - Status status; - Node node = Node.fromString(nodeName); - if (node == null) { - ci.println("Invalid Node"); - return; - } - status = this.addPort(node, bridgeName, portName, customConfigs); - ci.println("Port creation status : "+status.toString()); - } - - public void _deletePort (CommandInterpreter ci) { - String nodeName = ci.nextArgument(); - if (nodeName == null) { - ci.println("Please enter Node Name"); - return; - } - - String bridgeName = ci.nextArgument(); - if (bridgeName == null) { - ci.println("Please enter Bridge Name"); - return; - } - - String portName = ci.nextArgument(); - if (portName == null) { - ci.println("Please enter Port Name"); - return; - } - - Status status; - Node node = Node.fromString(nodeName); - if (node == null) { - ci.println("Invalid Node"); - return; - } - status = this.deletePort(node, bridgeName, portName); - ci.println("Port deletion status : "+status.toString()); - } - - public void _addPortVlan (CommandInterpreter ci) { - String nodeName = ci.nextArgument(); - if (nodeName == null) { - ci.println("Please enter Node Name"); - return; - } - - String bridgeName = ci.nextArgument(); - if (bridgeName == null) { - ci.println("Please enter Bridge Name"); - return; - } - - String portName = ci.nextArgument(); - if (portName == null) { - ci.println("Please enter Port Name"); - return; - } + // SCHEMA-INDEPENDENT Configuration Service APIs - String vlan = ci.nextArgument(); - if (vlan == null) { - ci.println("Please enter Valid Vlan"); - return; - } else { - try { - Integer.parseInt(vlan); - } catch (NumberFormatException e) { - ci.println("Please enter Valid Vlan"); - return; + private String getTableNameForRowUuid(Node node, String databaseName, UUID rowUuid) { + ConcurrentMap> cache = ovsdbInventoryService.getCache(node, databaseName); + if (cache == null) return null; + for (String tableName : cache.keySet()) { + ConcurrentMap rows = cache.get(tableName); + if (rows.get(rowUuid.toString()) != null) { + return tableName; } } - - Map configs = new HashMap(); - configs.put(ConfigConstants.TYPE, "VLAN"); - configs.put(ConfigConstants.VLAN, vlan); - - Status status; - Node node = Node.fromString(nodeName); - if (node == null) { - ci.println("Invalid Node"); - return; - } - status = this.addPort(node, bridgeName, portName, configs); - ci.println("Port creation status : "+status.toString()); - } - - public void _addTunnel (CommandInterpreter ci) { - String nodeName = ci.nextArgument(); - if (nodeName == null) { - ci.println("Please enter Node Name"); - return; - } - - String bridgeName = ci.nextArgument(); - if (bridgeName == null) { - ci.println("Please enter Bridge Name"); - return; - } - - String portName = ci.nextArgument(); - if (portName == null) { - ci.println("Please enter Port Name"); - return; - } - - String tunnelType = ci.nextArgument(); - if (tunnelType == null) { - ci.println("Please enter Tunnel Type"); - return; - } - - String remoteIp = ci.nextArgument(); - if (remoteIp == null) { - ci.println("Please enter valid Remote IP Address"); - return; - } - - try { - InetAddress.getByName(remoteIp); - } catch (Exception e) { - logger.error("Unable to resolve " + remoteIp, e); - ci.println("Please enter valid Remote IP Address"); - return; - } - - Map configs = new HashMap(); - configs.put(ConfigConstants.TYPE, "TUNNEL"); - configs.put(ConfigConstants.TUNNEL_TYPE, tunnelType); - configs.put(ConfigConstants.DEST_IP, remoteIp); - - Status status; - Node node = Node.fromString(nodeName); - if (node == null) { - ci.println("Invalid Node"); - return; - } - status = this.addPort(node, bridgeName, portName, configs); - ci.println("Port creation status : "+status.toString()); - } - - public void _printCache (CommandInterpreter ci) { - String nodeName = ci.nextArgument(); - if (nodeName == null) { - ci.println("Please enter Node Name"); - return; - } - Node node = Node.fromString(nodeName); - if (node == null) { - ci.println("Invalid Node"); - return; - } - ovsdbInventoryService.printCache(node); - } - - public void _forceConnect (CommandInterpreter ci) { - String force = ci.nextArgument(); - if (force.equalsIgnoreCase("YES")) { - forceConnect = true; - } - else if (force.equalsIgnoreCase("NO")) { - forceConnect = false; - } - else { - ci.println("Please enter YES or NO."); - } - ci.println("Current ForceConnect State : "+forceConnect); - } - - @Override - public String getHelp() { - StringBuilder help = new StringBuilder(); - help.append("---OVSDB CLI---\n"); - help.append("\t ovsconnect - Connect to OVSDB\n"); - help.append("\t addBridge - Add Bridge\n"); - help.append("\t getBridgeDomains - Get Bridges\n"); - help.append("\t deleteBridgeDomain - Delete a Bridge\n"); - help.append("\t addPort - Add Port\n"); - help.append("\t deletePort - Delete Port\n"); - help.append("\t addPortVlan - Add Port, Vlan\n"); - help.append("\t addTunnel - Add Tunnel\n"); - help.append("\t printCache - Prints Table Cache"); - return help.toString(); - } - - - /** - * Add a new bridge - * @param node Node serving this configuration service - * @param bridgeIdentifier String representation of a Bridge Connector - * @return Bridge Connector configurations - */ - @Override - @Deprecated - public Status createBridgeDomain(Node node, String bridgeIdentifier, Map configs) { - Connection connection = connectionService.getConnection(node); - OvsdbClient client = connection.getClient(); - Bridge bridge = client.createTypedRowWrapper(Bridge.class); - bridge.setName(bridgeIdentifier); - - String ovsTableUuid = this.getSpecialCaseParentUUID(node, OvsVswitchdSchemaConstants.DATABASE_NAME, bridge.getSchema().getName()); - return this.insertRow(node, bridge.getSchema().getName(), ovsTableUuid, bridge.getRow()); + return null; } - /** - * Create a Port Attached to a Bridge - * Ex. ovs-vsctl add-port br0 vif0 - * @param node Node serving this configuration service - * @param bridgeIdentifier String representation of a Bridge Domain - * @param portIdentifier String representation of a user defined Port Name - */ - @Override - @Deprecated - public Status addPort(Node node, String bridgeIdentifier, String portIdentifier, - Map configs) { - Connection connection = connectionService.getConnection(node); - OvsdbClient client = connection.getClient(); - - Bridge bridge = client.getTypedRowWrapper(Bridge.class, null); - ConcurrentMap rows = this.getRows(node, bridge.getSchema().getName()); - if (rows == null || rows.size() == 0) { - return new Status(StatusCode.NOTFOUND); - } - for (String bridgeUuid : rows.keySet()) { - Row bridgeRow = rows.get(bridgeUuid); - bridge = client.getTypedRowWrapper(Bridge.class, bridgeRow); - if (bridge.getName().equals(bridgeIdentifier)) break; - } - if (bridge.getName() == null || !bridge.getName().equals(bridgeIdentifier)) { - return new Status(StatusCode.NOTFOUND); - } - - Map options = null; - String type = null; - Set tags = null; - if (configs != null) { - type = (String) configs.get(ConfigConstants.TYPE); - Map customConfigs = (Map) configs.get(ConfigConstants.CUSTOM); - if (customConfigs != null) { - options = new HashMap(); - for (String customConfig : customConfigs.keySet()) { - options.put(customConfig, customConfigs.get(customConfig)); + private String getReferencingColumn (TableSchema parentTableSchema, String childTableName) throws OvsdbPluginException { + Map columnSchemas = parentTableSchema.getColumnSchemas(); + String refColumn = null; + for (String columnName : columnSchemas.keySet()) { + ColumnSchema columnSchema = columnSchemas.get(columnName); + if (columnSchema.getType().getBaseType().getClass().equals(UuidBaseType.class)) { + UuidBaseType refType = (UuidBaseType)columnSchema.getType().getBaseType(); + if (refType.getRefTable() != null && refType.getRefTable().equalsIgnoreCase(childTableName)) { + if (refColumn == null) { + refColumn = columnName; + } else { + throw new OvsdbPluginException("Multiple Referencing Columns for "+ childTableName +" on "+ parentTableSchema.getName()); + } } } } - - if (type != null) { - logger.debug("Port type : " + type); - if (type.equalsIgnoreCase(OvsVswitchdSchemaConstants.PortType.VLAN.name())) { - tags = new HashSet(); - tags.add(Long.parseLong((String)configs.get(ConfigConstants.VLAN))); - } - } - - Port port = client.createTypedRowWrapper(Port.class); - port.setName(portIdentifier); - if (tags != null) port.setTag(tags); - StatusWithUuid portStatus = this.insertRow(node, port.getSchema().getName(), bridge.getUuid().toString(), port.getRow()); - - if (!portStatus.isSuccess()) return portStatus; - // Ugly hack by adding a sleep for the Monitor Update to catch up. - // TODO : Remove this once the Select operation is in place. - // We are currently relying on the local Cache for any GET operation and that might fail if we try to - // fetch the last installed entry. Hence we need the Select operation to work. - - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - Interface interfaceRow = client.createTypedRowWrapper(Interface.class); - ConcurrentMap intfRows = this.getRows(node, interfaceRow.getSchema().getName()); - if (intfRows == null || intfRows.size() == 0) { - return new Status(StatusCode.NOTFOUND); - } - for (String intfUuid : intfRows.keySet()) { - Row intfRow = rows.get(intfUuid); - interfaceRow = client.getTypedRowWrapper(Interface.class, intfRow); - if (interfaceRow == null || interfaceRow.getName() == null) continue; - if (interfaceRow.getName().equals(portIdentifier)) break; - } - if (interfaceRow.getName() == null || !interfaceRow.getName().equals(portIdentifier)) { - return new Status(StatusCode.NOTFOUND); - } - - if (type != null) { - logger.debug("Interface type : " + type); - if (type.equalsIgnoreCase(OvsVswitchdSchemaConstants.PortType.TUNNEL.name())) { - interfaceRow.setType((String)configs.get(ConfigConstants.TUNNEL_TYPE)); - if (options == null) options = new HashMap(); - options.put("remote_ip", (String)configs.get(ConfigConstants.DEST_IP)); - } else if (type.equalsIgnoreCase(OvsVswitchdSchemaConstants.PortType.PATCH.name()) || - type.equalsIgnoreCase(OvsVswitchdSchemaConstants.PortType.INTERNAL.name())) { - interfaceRow.setType(type.toLowerCase()); - } - } - if (options != null) { - interfaceRow.setOptions(options); - } - - Status intfStatus = null; - intfStatus = this.updateRow(node, interfaceRow.getSchema().getName(), portStatus.getUuid().toString(), - interfaceRow.getUuid().toString(), interfaceRow.getRow()); - - if (intfStatus.isSuccess()) return portStatus; - return intfStatus; - } - - /** - * Implements the OVS Connection for Managers - * - * @param node Node serving this configuration service - * @param managerip String Representing IP and connection types - */ - @SuppressWarnings("unchecked") - @Deprecated - public boolean setManager(Node node, String managerip) { - Connection connection = connectionService.getConnection(node); - OvsdbClient client = connection.getClient(); - Manager manager = client.createTypedRowWrapper(Manager.class); - manager.setTarget(ImmutableSet.of(managerip)); - - OpenVSwitch openVSwitch = connection.getClient().getTypedRowWrapper(OpenVSwitch.class, null); - ConcurrentMap row = this.getRows(node, openVSwitch.getSchema().getName()); - if (row == null || row.size() == 0) { - return false; - } - String ovsTableUuid = (String)row.keySet().toArray()[0]; - - Status status = this.insertRow(node, manager.getSchema().getName(), ovsTableUuid, manager.getRow()); - return status.isSuccess(); - } - - @Override - @Deprecated - public Status addBridgeDomainConfig(Node node, String bridgeIdentfier, - Map configs) { - String mgmt = (String)configs.get(ConfigConstants.MGMT); - if (mgmt != null) { - if (setManager(node, mgmt)) return new Status(StatusCode.SUCCESS); - } - return new Status(StatusCode.BADREQUEST); - } - - @Override - @Deprecated - public Status deletePort(Node node, String bridgeIdentifier, String portIdentifier) { - Connection connection = connectionService.getConnection(node); - OvsdbClient client = connection.getClient(); - - Port port = client.getTypedRowWrapper(Port.class, null); - ConcurrentMap rows = this.getRows(node, port.getSchema().getName()); - if (rows == null || rows.size() == 0) { - return new Status(StatusCode.NOTFOUND); - } - for (String portUuid : rows.keySet()) { - Row bridgeRow = rows.get(portUuid); - port = client.getTypedRowWrapper(Port.class, bridgeRow); - if (port.getName().equals(portIdentifier)) break; - } - if (port.getName() == null || !port.getName().equals(portIdentifier)) { - return new Status(StatusCode.NOTFOUND); - } - return this.deleteRow(node, port.getSchema().getName(), port.getUuid().toString()); - } - - @Override - @Deprecated - public Status deleteBridgeDomain(Node node, String bridgeIdentifier) { - Connection connection = connectionService.getConnection(node); - OvsdbClient client = connection.getClient(); - - Bridge bridge = client.getTypedRowWrapper(Bridge.class, null); - ConcurrentMap rows = this.getRows(node, bridge.getSchema().getName()); - if (rows == null || rows.size() == 0) { - return new Status(StatusCode.NOTFOUND); - } - for (String bridgeUuid : rows.keySet()) { - Row bridgeRow = rows.get(bridgeUuid); - bridge = client.getTypedRowWrapper(Bridge.class, bridgeRow); - if (bridge.getName().equals(bridgeIdentifier)) break; + if (refColumn != null) { + return refColumn; } - if (bridge.getName() == null || !bridge.getName().equals(bridgeIdentifier)) { - return new Status(StatusCode.NOTFOUND); - } - return this.deleteRow(node, bridge.getSchema().getName(), bridge.getUuid().toString()); + throw new OvsdbPluginException("No Referencing Column for "+childTableName+" on "+parentTableSchema.getName()); } - - @Override - public List getBridgeDomains(Node node) { - if (connectionService == null) { - logger.error("Couldn't refer to the ConnectionService"); - return null; - } - - Connection connection = connectionService.getConnection(node); - Bridge bridge = connection.getClient().getTypedRowWrapper(Bridge.class, null); - List brlist = new ArrayList(); - Map brTableCache = ovsdbInventoryService.getTableCache(node, OvsVswitchdSchemaConstants.DATABASE_NAME, bridge.getSchema().getName()); - if(brTableCache != null){ - for (String uuid : brTableCache.keySet()) { - bridge = connection.getClient().getTypedRowWrapper(Bridge.class, brTableCache.get(uuid)); - brlist.add(bridge.getName()); - } - } - return brlist; - } - - @Override - public NodeConnector getNodeConnector(Node arg0, String arg1, String arg2) { - return null; - } - - @Override - @Deprecated - public Status addPortConfig(Node node, String bridgeIdentifier, String portIdentifier, - Map configs) { - // TODO Auto-generated method stub - return null; - } - - @Override - @Deprecated - public Node getBridgeDomainNode(Node node, String bridgeIdentifier) { - // TODO Auto-generated method stub - return null; - } - - @Override - @Deprecated - public Map getPortConfigs(Node node, String bridgeIdentifier, - String portIdentifier) { - // TODO Auto-generated method stub - return null; - } - - @Override - @Deprecated - public Status removeBridgeDomainConfig(Node node, String bridgeIdentifier, - Map configs) { - // TODO Auto-generated method stub - return null; - } - - @Override - @Deprecated - public Status removePortConfig(Node node, String bridgeIdentifier, String portIdentifier, - Map configs) { - // TODO Auto-generated method stub - return null; - } - - @Override - @Deprecated - public Map getBridgeDomainConfigs(Node node, String bridgeIdentifier) { - // TODO Auto-generated method stub - return null; - } - - - // SCHEMA-INDEPENDENT Configuration Service APIs - /* * A common Insert Transaction convenience method that populates the TransactionBuilder with insert operation * for a Child Row and also mutates the parent row with the UUID of the inserted Child. @@ -1282,6 +657,17 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ if (databaseName == null || tableName == null) { throw new OvsdbPluginException("databaseName, tableName and parentUuid are Mandatory Parameters"); } + + if (parentTable == null && parentUuid != null) { + parentTable = this.getTableNameForRowUuid(node, databaseName, parentUuid); + } + + if (parentColumn == null && parentTable != null) { + DatabaseSchema dbSchema = client.getDatabaseSchema(databaseName); + TableSchema parentTableSchema = dbSchema.table(parentTable, GenericTableSchema.class); + parentColumn = this.getReferencingColumn(parentTableSchema, tableName); + } + logger.debug("insertTree Connection : {} Table : {} ParentTable : {} Parent Column: {} Parent UUID : {} Row : {}", client.getConnectionInfo(), tableName, parentTable, parentColumn, parentUuid, row); @@ -1457,43 +843,118 @@ public class ConfigurationServiceImpl implements IPluginInBridgeDomainConfigServ public Row updateRow(Node node, String databaseName, String tableName, UUID rowUuid, Row row, boolean overwrite) throws OvsdbPluginException { - throw new OvsdbPluginException("Not implemented Yet"); + Connection connection = connectionService.getConnection(node); + OvsdbClient client = connection.getClient(); + + logger.debug("updateRow : Connection : {} databaseName : {} tableName : {} rowUUID : {} row : {}", + client.getConnectionInfo(), databaseName, tableName, rowUuid, row.toString()); + try{ + DatabaseSchema dbSchema = client.getDatabaseSchema(databaseName); + TransactionBuilder transactionBuilder = client.transactBuilder(dbSchema); + TableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class); + ColumnSchema _uuid = tableSchema.column("_uuid", UUID.class); + transactionBuilder.add(op.update(tableSchema, row) + .where(_uuid.opEqual(rowUuid)) + .build()); + + ListenableFuture> results = transactionBuilder.execute(); + List operationResults = results.get(); + for (OperationResult result : operationResults) { + if (result.getError() != null) { + throw new OvsdbPluginException("Error updating row : " + result.getError() + + " Details: " + result.getDetails()); + } + } + if (operationResults.isEmpty() || (transactionBuilder.getOperations().size() != operationResults.size())) { + throw new OvsdbPluginException("Failed to update row. Please check OVS logs for more info."); + } + + return this.getRow(node, databaseName, tableName, rowUuid); + } catch(Exception e){ + throw new OvsdbPluginException("Error updating row due to an exception "+ e.getMessage()); + } } @Override public void deleteRow(Node node, String databaseName, String tableName, String parentTable, UUID parentRowUuid, String parentColumn, UUID rowUuid) throws OvsdbPluginException { - throw new OvsdbPluginException("Not implemented Yet"); + Connection connection = connectionService.getConnection(node); + OvsdbClient client = connection.getClient(); + + if (parentTable == null && parentRowUuid != null) { + parentTable = this.getTableNameForRowUuid(node, databaseName, parentRowUuid); + } + + if (parentColumn == null && parentTable != null) { + DatabaseSchema dbSchema = client.getDatabaseSchema(databaseName); + TableSchema parentTableSchema = dbSchema.table(parentTable, GenericTableSchema.class); + parentColumn = this.getReferencingColumn(parentTableSchema, tableName); + } + + logger.debug("deleteRow : Connection : {} databaseName : {} tableName : {} Uuid : {} ParentTable : {} ParentColumn : {}", + client.getConnectionInfo(), databaseName, tableName, rowUuid, parentTable, parentColumn); + + DatabaseSchema dbSchema = client.getDatabaseSchema(databaseName); + TransactionBuilder transactionBuilder = client.transactBuilder(dbSchema); + this.processDeleteTransaction(client, databaseName, tableName, + parentTable, parentColumn, rowUuid.toString(), transactionBuilder); + + ListenableFuture> results = transactionBuilder.execute(); + List operationResults; + try { + operationResults = results.get(); + if (operationResults.isEmpty() || (transactionBuilder.getOperations().size() != operationResults.size())) { + throw new OvsdbPluginException("Delete Operation Failed"); + } + for (OperationResult result : operationResults) { + if (result.getError() != null) { + throw new OvsdbPluginException("Delete Operation Failed with Error : "+result.getError().toString()); + } + } + } catch (InterruptedException | ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } @Override - public void deleteRow(Node node, String databaseName, String tableName, - UUID rowUuid) throws OvsdbPluginException { - throw new OvsdbPluginException("Not implemented Yet"); + public void deleteRow(Node node, String databaseName, String tableName, UUID rowUuid) throws OvsdbPluginException { + this.deleteRow(node, databaseName, tableName, null, null, null, rowUuid); } @Override public Row getRow(Node node, String databaseName, String tableName, UUID uuid) throws OvsdbPluginException { - throw new OvsdbPluginException("Not implemented Yet"); + ConcurrentMap> rows = this.getRows(node, databaseName, tableName); + if (rows != null) { + return rows.get(uuid); + } + return null; } @Override public ConcurrentMap> getRows(Node node, String databaseName, String tableName) throws OvsdbPluginException { - throw new OvsdbPluginException("Not implemented Yet"); + ConcurrentMap ovsTable = ovsdbInventoryService.getTableCache(node, databaseName, tableName); + if (ovsTable == null) return null; + ConcurrentMap> tableDB = Maps.newConcurrentMap(); + for (String uuidStr : ovsTable.keySet()) { + tableDB.put(new UUID(uuidStr), ovsTable.get(uuidStr)); + } + return tableDB; } @Override public ConcurrentMap> getRows(Node node, String databaseName, String tableName, String fiqlQuery) throws OvsdbPluginException { - throw new OvsdbPluginException("Not implemented Yet"); + return this.getRows(node, databaseName, tableName); } @Override public List getTables(Node node, String databaseName) throws OvsdbPluginException { - throw new OvsdbPluginException("Not implemented Yet"); + ConcurrentMap> cache = ovsdbInventoryService.getCache(node, databaseName); + if (cache == null) return null; + return new ArrayList(cache.keySet()); } } -