X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=southbound%2Fsouthbound-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Fsouthbound%2Fovsdb%2Ftransact%2FTerminationPointCreateCommand.java;h=4e2ddb8e55b8f5a772673b5aa37c55fe218e9bd9;hb=50e7f939993fa9da3c80a1c8d166629a4868af9c;hp=1ffb31be3ca92b25f76e4e3fa0ed5fe28868923d;hpb=e4e303ad1ffd5fe271810362006a376f066fbb77;p=ovsdb.git diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java index 1ffb31be3..968eeaee0 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved. + * Copyright (c) 2015, 2016 Brocade Communications 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, @@ -8,97 +8,371 @@ package org.opendaylight.ovsdb.southbound.ovsdb.transact; import static org.opendaylight.ovsdb.lib.operations.Operations.op; +import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog; +import com.google.common.base.Optional; +import com.google.common.collect.Sets; +import com.google.common.util.concurrent.CheckedFuture; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Set; - +import java.util.concurrent.ExecutionException; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; +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.notation.Mutator; import org.opendaylight.ovsdb.lib.notation.UUID; +import org.opendaylight.ovsdb.lib.operations.Mutate; import org.opendaylight.ovsdb.lib.operations.TransactionBuilder; import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils; import org.opendaylight.ovsdb.schema.openvswitch.Bridge; import org.opendaylight.ovsdb.schema.openvswitch.Interface; import org.opendaylight.ovsdb.schema.openvswitch.Port; +import org.opendaylight.ovsdb.southbound.SouthboundConstants; import org.opendaylight.ovsdb.southbound.SouthboundMapper; +import org.opendaylight.ovsdb.southbound.SouthboundProvider; +import org.opendaylight.ovsdb.utils.yang.YangUtils; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfd; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks; 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.node.TerminationPoint; +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.ImmutableMap; -import com.google.common.collect.Sets; - public class TerminationPointCreateCommand implements TransactCommand { - private AsyncDataChangeEvent, OvsdbTerminationPointAugmentation> changes; + private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class); - public TerminationPointCreateCommand(AsyncDataChangeEvent, - OvsdbTerminationPointAugmentation> changes) { - this.changes = changes; + @Override + public void execute(TransactionBuilder transaction, BridgeOperationalState state, + AsyncDataChangeEvent, DataObject> events) { + execute(transaction, state, TransactUtils.extractCreated(events, OvsdbTerminationPointAugmentation.class), + TransactUtils.extractCreatedOrUpdated(events, Node.class)); } @Override - public void execute(TransactionBuilder transaction) { - for (OvsdbTerminationPointAugmentation terminationPoint: changes.getCreatedData().values()) { - LOG.debug("Received request to create termination point {} at managed node {}", - terminationPoint.getName(), - terminationPoint.getAttachedTo().getValue().firstIdentifierOf(Node.class)); - - - // Configure interface - String interfaceUuid = "Interface_" + terminationPoint.getName(); - Interface ovsInterface = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class); - ovsInterface.setName(terminationPoint.getName()); - ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType())); - Integer ofPort = terminationPoint.getOfport(); - if (ofPort != null) { - ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort)); + public void execute(TransactionBuilder transaction, BridgeOperationalState state, + Collection> modifications) { + execute(transaction, state, + TransactUtils.extractCreated(modifications, OvsdbTerminationPointAugmentation.class), + TransactUtils.extractCreatedOrUpdated(modifications, Node.class)); + } + + private void execute(TransactionBuilder transaction, BridgeOperationalState state, + Map, + OvsdbTerminationPointAugmentation> + createdTerminationPoints, Map, Node> nodes) { + for (Entry, OvsdbTerminationPointAugmentation> entry : + createdTerminationPoints.entrySet()) { + OvsdbTerminationPointAugmentation terminationPoint = entry.getValue(); + LOG.debug("Received request to create termination point {}", + terminationPoint.getName()); + InstanceIdentifier terminationPointIid = entry.getKey(); + Optional terminationPointOptional = + state.getBridgeTerminationPoint(terminationPointIid); + if (!terminationPointOptional.isPresent()) { + // Configure interface + String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUuid(); + Interface ovsInterface = + TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class); + createInterface(terminationPoint, ovsInterface); + transaction.add(op.insert(ovsInterface).withId(interfaceUuid)); + + stampInstanceIdentifier(transaction, entry.getKey(), ovsInterface.getName()); + + // Configure port with the above interface details + String portUuid = "Port_" + SouthboundMapper.getRandomUuid(); + Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class); + createPort(terminationPoint, port, interfaceUuid); + transaction.add(op.insert(port).withId(portUuid)); + + //Configure bridge with the above port details + Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class); + if (getBridge(entry.getKey(), nodes) != null) { + bridge.setName(getBridge(entry.getKey(), nodes).getBridgeName().getValue()); + bridge.setPorts(Sets.newHashSet(new UUID(portUuid))); + + transaction.add(op.mutate(bridge) + .addMutation(bridge.getPortsColumn().getSchema(), + Mutator.INSERT, bridge.getPortsColumn().getData()) + .where(bridge.getNameColumn().getSchema() + .opEqual(bridge.getNameColumn().getData())).build()); + } } - Integer ofPortRequest = terminationPoint.getOfportRequest(); - if (ofPortRequest != null) { - ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest)); + } + + } + + private void createInterface( + final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { + ovsInterface.setName(terminationPoint.getName()); + + createInterfaceType(terminationPoint, ovsInterface); + createOfPort(terminationPoint, ovsInterface); + createOfPortRequest(terminationPoint, ovsInterface); + createInterfaceOptions(terminationPoint, ovsInterface); + createInterfaceOtherConfig(terminationPoint, ovsInterface); + createInterfaceExternalIds(terminationPoint, ovsInterface); + createInterfaceLldp(terminationPoint, ovsInterface); + createInterfaceBfd(terminationPoint, ovsInterface); + } + + private void createInterfaceType(final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { + + Class mdsaltype = terminationPoint.getInterfaceType(); + if (mdsaltype != null) { + ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(mdsaltype)); + } + } + + private void createPort( + final OvsdbTerminationPointAugmentation terminationPoint, + final Port port, final String interfaceUuid) { + + port.setName(terminationPoint.getName()); + port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid))); + createPortOtherConfig(terminationPoint, port); + createPortVlanTag(terminationPoint, port); + createPortVlanTrunk(terminationPoint, port); + createPortVlanMode(terminationPoint, port); + createPortExternalIds(terminationPoint, port); + } + + private void createOfPort( + final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { + + Long ofPort = terminationPoint.getOfport(); + if (ofPort != null) { + ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort)); + } + } + + private void createOfPortRequest( + final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { + + Integer ofPortRequest = terminationPoint.getOfportRequest(); + if (ofPortRequest != null) { + ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue())); + } + } + + private void createInterfaceOptions( + final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { + + //Configure optional input + if (terminationPoint.getOptions() != null) { + try { + ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(), + Options::getOption, Options::getValue)); + } catch (NullPointerException e) { + LOG.warn("Incomplete OVSDB interface options", e); } + } + } + + private void createInterfaceExternalIds( + final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { - //Configure optional input - if (terminationPoint.getOptions() != null) { - HashMap optionsMap = new HashMap(); - for (Options option : terminationPoint.getOptions()) { - optionsMap.put(option.getOption(), option.getValue()); + List interfaceExternalIds = + terminationPoint.getInterfaceExternalIds(); + if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) { + try { + ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds, + InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue)); + } catch (NullPointerException e) { + LOG.warn("Incomplete OVSDB interface external_ids", e); + } + } + } + + private void createInterfaceOtherConfig( + final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { + + List interfaceOtherConfigs = + terminationPoint.getInterfaceOtherConfigs(); + if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) { + Map otherConfigsMap = new HashMap<>(); + for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) { + otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(), + interfaceOtherConfig.getOtherConfigValue()); + } + try { + ovsInterface.setOtherConfig(otherConfigsMap); + } catch (NullPointerException e) { + LOG.warn("Incomplete OVSDB interface other_config", e); + } + } + } + + private void createInterfaceLldp( + final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { + + try { + List interfaceLldpList = + terminationPoint.getInterfaceLldp(); + if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) { + try { + ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList, + InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue)); + } catch (NullPointerException e) { + LOG.warn("Incomplete OVSDB interface lldp", e); } + } + } catch (SchemaVersionMismatchException e) { + schemaMismatchLog("lldp", "Interface", e); + } + } + + private void createInterfaceBfd(final OvsdbTerminationPointAugmentation terminationPoint, + final Interface ovsInterface) { + + try { + List interfaceBfdList = terminationPoint.getInterfaceBfd(); + if (interfaceBfdList != null && !interfaceBfdList.isEmpty()) { try { - ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap)); + ovsInterface.setBfd(YangUtils.convertYangKeyValueListToMap(interfaceBfdList, + InterfaceBfd::getBfdKey, InterfaceBfd::getBfdValue)); } catch (NullPointerException e) { - LOG.warn("Incomplete OVSDB interface options"); + LOG.warn("Incomplete OVSDB interface bfd", e); } } - transaction.add(op.insert(ovsInterface).withId(interfaceUuid)); - - // Configure port with the above interface details - String portUuid = "Port_" + terminationPoint.getName(); - Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class); - port.setName(terminationPoint.getName()); - port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid))); - if (terminationPoint.getVlanTag() != null) { - Set vlanTag = new HashSet(); - vlanTag.add(terminationPoint.getVlanTag().getValue().longValue()); - port.setTag(vlanTag); + } catch (SchemaVersionMismatchException e) { + schemaMismatchLog("bfd", "Interface", e); + } + } + + private void createPortExternalIds( + final OvsdbTerminationPointAugmentation terminationPoint, + final Port port) { + + List portExternalIds = terminationPoint.getPortExternalIds(); + if (portExternalIds != null && !portExternalIds.isEmpty()) { + try { + port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds, + PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue)); + } catch (NullPointerException e) { + LOG.warn("Incomplete OVSDB port external_ids", e); } - transaction.add(op.insert(port).withId(portUuid)); + } + } - //Configure bridge with the above port details - Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class); - bridge.setName(terminationPoint.getBridgeName()); - bridge.setPorts(Sets.newHashSet(new UUID(portUuid))); + private void createPortVlanTag( + final OvsdbTerminationPointAugmentation terminationPoint, + final Port port) { - transaction.add(op.mutate(bridge) - .addMutation(bridge.getPortsColumn().getSchema(), Mutator.INSERT,bridge.getPortsColumn().getData()) - .where(bridge.getNameColumn().getSchema().opEqual(bridge.getNameColumn().getData())).build()); + if (terminationPoint.getVlanTag() != null) { + Set vlanTag = new HashSet<>(); + vlanTag.add(terminationPoint.getVlanTag().getValue().longValue()); + port.setTag(vlanTag); } + } + + private void createPortVlanTrunk( + final OvsdbTerminationPointAugmentation terminationPoint, + final Port port) { + + if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) { + Set portTrunks = new HashSet<>(); + List modelTrunks = terminationPoint.getTrunks(); + for (Trunks trunk: modelTrunks) { + if (trunk.getTrunk() != null) { + portTrunks.add(trunk.getTrunk().getValue().longValue()); + } + } + port.setTrunks(portTrunks); + } + } + + private void createPortVlanMode( + final OvsdbTerminationPointAugmentation terminationPoint, + final Port port) { + if (terminationPoint.getVlanMode() != null) { + Set portVlanMode = new HashSet<>(); + VlanMode modelVlanMode = terminationPoint.getVlanMode(); + portVlanMode.add(SouthboundConstants.VlanModes.values()[modelVlanMode.getIntValue() - 1].getMode()); + port.setVlanMode(portVlanMode); + } + } + + private void createPortOtherConfig( + final OvsdbTerminationPointAugmentation terminationPoint, + final Port ovsPort) { + List portOtherConfigs = + terminationPoint.getPortOtherConfigs(); + if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) { + try { + ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs, + PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue)); + } catch (NullPointerException e) { + LOG.warn("Incomplete OVSDB port other_config", e); + } + } + } + + private OvsdbBridgeAugmentation getBridge(InstanceIdentifier key, Map, Node> nodes) { + OvsdbBridgeAugmentation bridge = null; + InstanceIdentifier nodeIid = key.firstIdentifierOf(Node.class); + if (nodes != null && nodes.get(nodeIid) != null) { + Node node = nodes.get(nodeIid); + bridge = node.getAugmentation(OvsdbBridgeAugmentation.class); + if (bridge == null) { + ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction(); + CheckedFuture, ReadFailedException> future = + transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid); + try { + Optional nodeOptional = future.get(); + if (nodeOptional.isPresent()) { + bridge = nodeOptional.get().getAugmentation(OvsdbBridgeAugmentation.class); + } + } catch (InterruptedException | ExecutionException e) { + LOG.warn("Error reading from datastore",e); + } + transaction.close(); + } + } + return bridge; + } + public static void stampInstanceIdentifier(TransactionBuilder transaction, InstanceIdentifier + iid, String interfaceName) { + Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class); + port.setName(interfaceName); + port.setExternalIds(Collections.emptyMap()); + Mutate mutate = TransactUtils.stampInstanceIdentifierMutation(transaction, + iid, + port.getSchema(), + port.getExternalIdsColumn().getSchema()); + transaction.add(mutate + .where(port.getNameColumn().getSchema().opEqual(interfaceName)) + .build()); } }