X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=southbound%2Fsouthbound-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Fsouthbound%2FOvsdbConnectionInstance.java;h=b3378861d71f673cde913f8ce60e08e4dfc417d0;hb=fcfb5e64040407dbc2c1682ebe081c13d048811c;hp=22abd55e798eed6ae769596a51e77b224d31869b;hpb=a3ce37ff02b2cfdf2c297294dd45d92aecb92938;p=ovsdb.git diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java index 22abd55e7..b3378861d 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2015, 2016 Cisco Systems, 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, @@ -7,12 +7,22 @@ */ package org.opendaylight.ovsdb.southbound; +import static org.opendaylight.ovsdb.lib.operations.Operations.op; + +import com.google.common.collect.Lists; +import com.google.common.util.concurrent.ListenableFuture; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; - +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.clustering.Entity; +import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration; +import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; import org.opendaylight.ovsdb.lib.EchoServiceCallbackFilters; import org.opendaylight.ovsdb.lib.LockAquisitionCallback; import org.opendaylight.ovsdb.lib.LockStolenCallback; @@ -24,7 +34,9 @@ import org.opendaylight.ovsdb.lib.message.MonitorRequest; import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder; import org.opendaylight.ovsdb.lib.message.MonitorSelect; import org.opendaylight.ovsdb.lib.message.TableUpdates; +import org.opendaylight.ovsdb.lib.notation.Mutator; import org.opendaylight.ovsdb.lib.notation.Row; +import org.opendaylight.ovsdb.lib.operations.Mutate; import org.opendaylight.ovsdb.lib.operations.Operation; import org.opendaylight.ovsdb.lib.operations.OperationResult; import org.opendaylight.ovsdb.lib.operations.TransactionBuilder; @@ -32,67 +44,124 @@ 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.lib.schema.typed.TyperUtils; +import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch; +import org.opendaylight.ovsdb.southbound.ovsdb.transact.BridgeOperationalState; import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactCommand; import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvoker; import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvokerImpl; -import org.opendaylight.ovsdb.southbound.transactions.md.OvsdbNodeCreateCommand; +import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactUtils; import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker; +import org.opendaylight.ovsdb.utils.yang.YangUtils; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.Lists; -import com.google.common.util.concurrent.ListenableFuture; - public class OvsdbConnectionInstance implements OvsdbClient { private static final Logger LOG = LoggerFactory.getLogger(OvsdbConnectionInstance.class); private OvsdbClient client; private ConnectionInfo connectionInfo; private TransactionInvoker txInvoker; - private Map transactInvokers = new HashMap(); + private Map transactInvokers; private MonitorCallBack callback; + private InstanceIdentifier instanceIdentifier; + private volatile boolean hasDeviceOwnership = false; + private Entity connectedEntity; + private EntityOwnershipCandidateRegistration deviceOwnershipCandidateRegistration; + private OvsdbNodeAugmentation initialCreateData = null; - OvsdbConnectionInstance(ConnectionInfo key,OvsdbClient client,TransactionInvoker txInvoker) { + OvsdbConnectionInstance(ConnectionInfo key,OvsdbClient client,TransactionInvoker txInvoker, + InstanceIdentifier iid) { this.connectionInfo = key; this.client = client; this.txInvoker = txInvoker; - txInvoker.invoke(new OvsdbNodeCreateCommand(key, null,null)); - registerCallBack(); + // this.key = key; + this.instanceIdentifier = iid; + } + + /** + * Apply the given command to the given events, based on the given bridge state. + * + * @param command The command to run. + * @param state The current bridge state. + * @param events The events to process. + */ + public void transact(TransactCommand command, BridgeOperationalState state, + AsyncDataChangeEvent, DataObject> events) { + for (TransactInvoker transactInvoker : transactInvokers.values()) { + transactInvoker.invoke(command, state, events); + } } - public void transact(TransactCommand command) { - for (TransactInvoker transactInvoker: transactInvokers.values()) { - transactInvoker.invoke(command); + /** + * Apply the given command to the given modifications, based on the given bridge state. + * + * @param command The command to run. + * @param state The current bridge state. + * @param modifications The modifications to process. + */ + public void transact(TransactCommand command, BridgeOperationalState state, + Collection> modifications) { + for (TransactInvoker transactInvoker : transactInvokers.values()) { + transactInvoker.invoke(command, state, modifications); } } - private void registerCallBack() { - this.callback = new OvsdbMonitorCallback(connectionInfo,txInvoker); - try { - List databases = getDatabases().get(); - if (databases != null) { - for (String database : databases) { - DatabaseSchema dbSchema = getSchema(database).get(); - if (dbSchema != null) { - transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema)); - monitorAllTables(database, dbSchema); - } else { - LOG.warn("No schema reported for database {} for key {}",database,connectionInfo); - } + public ListenableFuture> transact( + DatabaseSchema dbSchema, List operations) { + return client.transact(dbSchema, operations); + } + + public void registerCallbacks() { + if ( this.callback == null) { + if (this.initialCreateData != null ) { + this.updateConnectionAttributes(); + } + + try { + String database = SouthboundConstants.OPEN_V_SWITCH; + DatabaseSchema dbSchema = getSchema(database).get(); + if (dbSchema != null) { + LOG.info("Monitoring database: {}", database); + callback = new OvsdbMonitorCallback(this, txInvoker); + monitorAllTables(database, dbSchema); + } else { + LOG.info("No database {} found on {}", database, connectionInfo); } - } else { - LOG.warn("No databases reported from {}",connectionInfo); + } catch (InterruptedException | ExecutionException e) { + LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e); + } + } + } + + public void createTransactInvokers() { + if (transactInvokers == null) { + try { + transactInvokers = new HashMap<>(); + DatabaseSchema dbSchema = getSchema(SouthboundConstants.OPEN_V_SWITCH).get(); + if (dbSchema != null) { + transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema)); + } + } catch (InterruptedException | ExecutionException e) { + LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e); } - } catch (InterruptedException | ExecutionException e) { - LOG.warn("Exception attempting to initialize {}: {}",connectionInfo,e); } } private void monitorAllTables(String database, DatabaseSchema dbSchema) { Set tables = dbSchema.getTables(); if (tables != null) { - List> monitorRequests = Lists.newArrayList(); + List monitorRequests = Lists.newArrayList(); for (String tableName : tables) { + LOG.info("Southbound monitoring table {} in {}", tableName, dbSchema.getName()); GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class); Set columns = tableSchema.getColumns(); MonitorRequestBuilder monitorBuilder = MonitorRequestBuilder.builder(tableSchema); @@ -107,6 +176,78 @@ public class OvsdbConnectionInstance implements OvsdbClient { } } + private void updateConnectionAttributes() { + LOG.debug("Update attributes of ovsdb node ip: {} port: {}", + this.initialCreateData.getConnectionInfo().getRemoteIp(), + this.initialCreateData.getConnectionInfo().getRemotePort()); + for ( Map.Entry entry: transactInvokers.entrySet()) { + + TransactionBuilder transaction = new TransactionBuilder(this, entry.getKey()); + + // OpenVSwitchPart + OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class); + + List externalIds = this.initialCreateData.getOpenvswitchExternalIds(); + + stampInstanceIdentifier(transaction,this.instanceIdentifier.firstIdentifierOf(Node.class)); + + try { + ovs.setExternalIds( + YangUtils.convertYangKeyValueListToMap(externalIds, OpenvswitchExternalIds::getExternalIdKey, + OpenvswitchExternalIds::getExternalIdValue)); + Mutate mutate = op.mutate(ovs) + .addMutation(ovs.getExternalIdsColumn().getSchema(), + Mutator.INSERT, + ovs.getExternalIdsColumn().getData()); + transaction.add(mutate); + } catch (NullPointerException e) { + LOG.warn("Incomplete OVSDB Node external IDs", e); + } + + + + List otherConfigs = this.initialCreateData.getOpenvswitchOtherConfigs(); + if (otherConfigs != null) { + try { + ovs.setOtherConfig(YangUtils.convertYangKeyValueListToMap(otherConfigs, + OpenvswitchOtherConfigs::getOtherConfigKey, + OpenvswitchOtherConfigs::getOtherConfigValue)); + transaction.add(op.mutate(ovs).addMutation(ovs.getOtherConfigColumn().getSchema(), + Mutator.INSERT, + ovs.getOtherConfigColumn().getData())); + } catch (NullPointerException e) { + LOG.warn("Incomplete OVSDB Node other_config", e); + } + } + + invoke(transaction); + } + } + + private void stampInstanceIdentifier(TransactionBuilder transaction,InstanceIdentifier iid) { + OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class); + ovs.setExternalIds(Collections.emptyMap()); + TransactUtils.stampInstanceIdentifier(transaction, + iid, + ovs.getSchema(), + ovs.getExternalIdsColumn().getSchema()); + } + + private void invoke(TransactionBuilder txBuilder) { + ListenableFuture> result = txBuilder.execute(); + LOG.debug("invoke: tb: {}", txBuilder); + if (txBuilder.getOperations().size() > 0) { + try { + List got = result.get(); + LOG.debug("OVSDB transaction result: {}", got); + } catch (Exception e) { + LOG.warn("Transact execution exception: ", e); + } + LOG.trace("invoke exit tb: {}", txBuilder); + } + } + + public ListenableFuture> getDatabases() { return client.getDatabases(); } @@ -119,13 +260,15 @@ public class OvsdbConnectionInstance implements OvsdbClient { return client.transactBuilder(dbSchema); } - public ListenableFuture> transact( - DatabaseSchema dbSchema, List operations) { - return client.transact(dbSchema, operations); + @Override + public > TableUpdates monitor( + DatabaseSchema schema, List monitorRequests, + MonitorHandle monitorHandle, MonitorCallBack callback) { + return null; } public > TableUpdates monitor( - DatabaseSchema schema, List> monitorRequests, + DatabaseSchema schema, List monitorRequests, MonitorCallBack callback) { return client.monitor(schema, monitorRequests, callback); } @@ -192,4 +335,66 @@ public class OvsdbConnectionInstance implements OvsdbClient { public void setMDConnectionInfo(ConnectionInfo key) { this.connectionInfo = key; } + + public InstanceIdentifier getInstanceIdentifier() { + return instanceIdentifier; + } + + public NodeKey getNodeKey() { + return getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class); + } + + public NodeId getNodeId() { + return getNodeKey().getNodeId(); + } + + public void setInstanceIdentifier(InstanceIdentifier iid) { + this.instanceIdentifier = iid; + } + + public Entity getConnectedEntity() { + return this.connectedEntity; + } + + public void setConnectedEntity(Entity entity ) { + this.connectedEntity = entity; + } + + public Boolean hasOvsdbClient(OvsdbClient otherClient) { + return client.equals(otherClient); + } + + public Boolean getHasDeviceOwnership() { + return hasDeviceOwnership; + } + + public void setHasDeviceOwnership(Boolean hasDeviceOwnership) { + if (hasDeviceOwnership != null) { + this.hasDeviceOwnership = hasDeviceOwnership; + } + } + + public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) { + this.deviceOwnershipCandidateRegistration = registration; + } + + public void closeDeviceOwnershipCandidateRegistration() { + if (deviceOwnershipCandidateRegistration != null) { + this.deviceOwnershipCandidateRegistration.close(); + setHasDeviceOwnership(Boolean.FALSE); + } + } + + public OvsdbNodeAugmentation getOvsdbNodeAugmentation() { + return this.initialCreateData; + } + + public void setOvsdbNodeAugmentation(OvsdbNodeAugmentation ovsdbNodeCreateData) { + this.initialCreateData = ovsdbNodeCreateData; + } + + @Override + public void echo() { + client.echo(); + } }