X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=southbound%2Fsouthbound-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Fsouthbound%2Ftransactions%2Fmd%2FOvsdbBridgeUpdateCommand.java;h=a86638ffed20e1e8843932631957a854e4b5029d;hb=55ce82cbd9fdcb0edf9e629bd2d626b193c6ab26;hp=417cb0ba8f88313a4dcc634b723ef8b807316d79;hpb=59228a542e8f0b9140b2bead1ae8fab08061d23e;p=ovsdb.git diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommand.java index 417cb0ba8..a86638ffe 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommand.java @@ -1,21 +1,40 @@ +/* + * Copyright (c) 2015 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + package org.opendaylight.ovsdb.southbound.transactions.md; +import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog; + +import java.net.InetAddress; +import java.net.NetworkInterface; import java.util.ArrayList; +import java.util.Enumeration; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import org.apache.commons.lang3.math.NumberUtils; import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException; import org.opendaylight.ovsdb.lib.message.TableUpdates; import org.opendaylight.ovsdb.lib.notation.UUID; import org.opendaylight.ovsdb.lib.schema.DatabaseSchema; import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils; import org.opendaylight.ovsdb.schema.openvswitch.Bridge; +import org.opendaylight.ovsdb.schema.openvswitch.Controller; +import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance; import org.opendaylight.ovsdb.southbound.SouthboundConstants; import org.opendaylight.ovsdb.southbound.SouthboundMapper; +import org.opendaylight.ovsdb.southbound.SouthboundUtil; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation; @@ -32,14 +51,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey; -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.ManagedNodeEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder; 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.NodeBuilder; +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; @@ -47,13 +67,14 @@ import org.slf4j.LoggerFactory; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.net.InetAddresses; public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class); private Map updatedBridgeRows; private Map oldBridgeRows; - public OvsdbBridgeUpdateCommand(ConnectionInfo key, TableUpdates updates, + public OvsdbBridgeUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) { super(key,updates,dbSchema); updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema()); @@ -69,8 +90,8 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { private void updateBridge(ReadWriteTransaction transaction, Bridge bridge) { - final InstanceIdentifier connectionIId = SouthboundMapper.createInstanceIdentifier(getConnectionInfo()); - Optional connection = readNode(transaction, connectionIId); + final InstanceIdentifier connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier(); + Optional connection = SouthboundUtil.readNode(transaction, connectionIId); if (connection.isPresent()) { LOG.debug("Connection {} is present",connection); @@ -79,7 +100,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode); // Update the bridge node with whatever data we are getting - InstanceIdentifier bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge); + InstanceIdentifier bridgeIid = getInstanceIdentifier(bridge); Node bridgeNode = buildBridgeNode(bridge); transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode); deleteEntries(transaction, protocolEntriesToRemove(bridgeIid,bridge)); @@ -99,8 +120,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { InstanceIdentifier bridgeIid, Bridge bridge) { Preconditions.checkNotNull(bridgeIid); Preconditions.checkNotNull(bridge); - List> result = - new ArrayList>(); + List> result = new ArrayList<>(); Bridge oldBridge = oldBridgeRows.get(bridge.getUuid()); @@ -124,8 +144,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { InstanceIdentifier bridgeIid, Bridge bridge) { Preconditions.checkNotNull(bridgeIid); Preconditions.checkNotNull(bridge); - List> result = - new ArrayList>(); + List> result = new ArrayList<>(); Bridge oldBridge = oldBridgeRows.get(bridge.getUuid()); @@ -149,48 +168,40 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { InstanceIdentifier bridgeIid, Bridge bridge) { Preconditions.checkNotNull(bridgeIid); Preconditions.checkNotNull(bridge); - List> result = - new ArrayList>(); + List> result = new ArrayList<>(); Bridge oldBridge = oldBridgeRows.get(bridge.getUuid()); - if (oldBridge != null && oldBridge.getProtocolsColumn() != null) { - for (String protocol: oldBridge.getProtocolsColumn().getData()) { - if (bridge.getProtocolsColumn() == null - || !bridge.getProtocolsColumn().getData().contains(protocol)) { - Class proto = - SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse().get(protocol); - InstanceIdentifier iid = bridgeIid - .augmentation(OvsdbBridgeAugmentation.class) - .child(ProtocolEntry.class, - new ProtocolEntryKey(proto)); - result.add(iid); + try { + if (oldBridge != null && oldBridge.getProtocolsColumn() != null) { + for (String protocol : oldBridge.getProtocolsColumn().getData()) { + if (bridge.getProtocolsColumn() == null || !bridge.getProtocolsColumn().getData() + .contains(protocol)) { + Class proto = SouthboundConstants.OVSDB_PROTOCOL_MAP + .inverse().get(protocol); + InstanceIdentifier iid = bridgeIid + .augmentation(OvsdbBridgeAugmentation.class) + .child(ProtocolEntry.class, + new ProtocolEntryKey(proto)); + result.add(iid); + } } } + } catch (SchemaVersionMismatchException e) { + schemaMismatchLog("protocols", "Bridge", e); } return result; } - private Optional readNode(ReadWriteTransaction transaction, - final InstanceIdentifier connectionIid) { - Optional node = Optional.absent(); - try { - node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet(); - } catch (final ReadFailedException e) { - LOG.debug("Read Operational/DS for Node fail! {}", connectionIid, e); - } - return node; - } - private Node buildConnectionNode( Bridge bridge) { //Update node with managed node reference NodeBuilder connectionNode = new NodeBuilder(); - connectionNode.setNodeId(SouthboundMapper.createNodeId(getConnectionInfo().getRemoteIp(), - getConnectionInfo().getRemotePort())); + connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId()); OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder(); - List managedBridges = new ArrayList(); - InstanceIdentifier bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge); + List managedBridges = new ArrayList<>(); + InstanceIdentifier bridgeIid = SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), + bridge); ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef( new OvsdbBridgeRef(bridgeIid)).build(); managedBridges.add(managedBridge); @@ -198,14 +209,14 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { connectionNode.addAugmentation(OvsdbNodeAugmentation.class, ovsdbConnectionAugmentationBuilder.build()); - LOG.debug("Update node with bridge node ref {}",ovsdbConnectionAugmentationBuilder.toString()); + LOG.debug("Update node with bridge node ref {}", + ovsdbConnectionAugmentationBuilder.getManagedNodeEntry().iterator().next()); return connectionNode.build(); } private Node buildBridgeNode(Bridge bridge) { NodeBuilder bridgeNodeBuilder = new NodeBuilder(); - InstanceIdentifier bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge); - NodeId bridgeNodeId = SouthboundMapper.createManagedNodeId(bridgeIid); + NodeId bridgeNodeId = getNodeId(bridge); bridgeNodeBuilder.setNodeId(bridgeNodeId); OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder(); ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridge.getName())); @@ -216,16 +227,34 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { setExternalIds(ovsdbBridgeAugmentationBuilder, bridge); setOtherConfig(ovsdbBridgeAugmentationBuilder, bridge); setFailMode(ovsdbBridgeAugmentationBuilder, bridge); + setOpenFlowNodeRef(ovsdbBridgeAugmentationBuilder, bridge); setManagedBy(ovsdbBridgeAugmentationBuilder); + setAutoAttach(ovsdbBridgeAugmentationBuilder, bridge); bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build()); LOG.debug("Built with the intent to store bridge data {}", - ovsdbBridgeAugmentationBuilder.toString()); + ovsdbBridgeAugmentationBuilder.build()); return bridgeNodeBuilder.build(); } + private void setAutoAttach(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, + Bridge bridge) { + try { + if (bridge.getAutoAttachColumn() != null + && bridge.getAutoAttachColumn().getData() != null + && !bridge.getAutoAttachColumn().getData().isEmpty()) { + Set uuids = bridge.getAutoAttachColumn().getData(); + for (UUID uuid : uuids) { + ovsdbBridgeAugmentationBuilder.setAutoAttach(new Uuid(uuid.toString())); + } + } + } catch (SchemaVersionMismatchException e) { + schemaMismatchLog("auto_attach", "Bridge", e); + } + } + private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) { - InstanceIdentifier connectionNodePath = SouthboundMapper.createInstanceIdentifier(getConnectionInfo()); + InstanceIdentifier connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier(); ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath)); } @@ -253,7 +282,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { .getOtherConfigColumn().getData(); if (otherConfigs != null && !otherConfigs.isEmpty()) { Set otherConfigKeys = otherConfigs.keySet(); - List otherConfigList = new ArrayList(); + List otherConfigList = new ArrayList<>(); String otherConfigValue; for (String otherConfigKey : otherConfigKeys) { otherConfigValue = otherConfigs.get(otherConfigKey); @@ -274,7 +303,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { .getData(); if (externalIds != null && !externalIds.isEmpty()) { Set externalIdKeys = externalIds.keySet(); - List externalIdsList = new ArrayList(); + List externalIdsList = new ArrayList<>(); String externalIdValue; for (String externalIdKey : externalIdKeys) { externalIdValue = externalIds.get(externalIdKey); @@ -304,4 +333,58 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand { ovsdbBridgeAugmentationBuilder.setDatapathId(dpid); } } + + private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, + Bridge bridge) { + Map updatedControllerRows = + TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema()); + LOG.debug("setOpenFlowNodeRef: updatedControllerRows: {}", updatedControllerRows); + for (ControllerEntry controllerEntry: SouthboundMapper.createControllerEntries(bridge, updatedControllerRows)) { + if (controllerEntry != null + && controllerEntry.isIsConnected() != null && controllerEntry.isIsConnected()) { + String [] controllerTarget = controllerEntry.getTarget().getValue().split(":"); + IpAddress bridgeControllerIpAddress = null; + PortNumber bridgeControllerPortNumber = null; + for (String targetElement : controllerTarget) { + if (InetAddresses.isInetAddress(targetElement)) { + bridgeControllerIpAddress = new IpAddress(targetElement.toCharArray()); + continue; + } + if (NumberUtils.isNumber(targetElement)) { + bridgeControllerPortNumber = new PortNumber( + Integer.valueOf(String.valueOf(targetElement))); + continue; + } + } + try { + Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); + networkInterfacesLoop: + while (networkInterfaces.hasMoreElements()) { + NetworkInterface networkInterface = networkInterfaces.nextElement(); + Enumeration networkInterfaceAddresses = networkInterface.getInetAddresses(); + while (networkInterfaceAddresses.hasMoreElements()) { + InetAddress networkInterfaceAddress = networkInterfaceAddresses.nextElement(); + if (bridgeControllerIpAddress.getIpv4Address().getValue() + .equals(networkInterfaceAddress.getHostAddress())) { + ovsdbBridgeAugmentationBuilder.setBridgeOpenflowNodeRef( + getOvsdbConnectionInstance().getInstanceIdentifier()); + break networkInterfacesLoop; + } + } + } + } catch (Exception e) { + LOG.warn("Error getting local ip address", e); + } + } + } + } + private InstanceIdentifier getInstanceIdentifier(Bridge bridge) { + return SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), + bridge); + } + + private NodeId getNodeId(Bridge bridge) { + NodeKey nodeKey = getInstanceIdentifier(bridge).firstKeyOf(Node.class, NodeKey.class); + return nodeKey.getNodeId(); + } }