X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=hwvtepsouthbound%2Fhwvtepsouthbound-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Fhwvtepsouthbound%2Ftransact%2FPhysicalPortUpdateCommand.java;h=25fdfecb40b190c8872fbc939deaa57eb73a4ca2;hb=bf3ddd35cfe8293cc9dfb99b2222c386a0e33ad3;hp=aff805ccae849c2c26325222f586cd4ebd578057;hpb=2679a6aae2124113e18f2fabfac2f3fff0f49653;p=ovsdb.git diff --git a/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalPortUpdateCommand.java b/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalPortUpdateCommand.java index aff805cca..25fdfecb4 100644 --- a/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalPortUpdateCommand.java +++ b/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalPortUpdateCommand.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 China Telecom Beijing Research Institute and others. All rights reserved. + * Copyright © 2015, 2017 China Telecom Beijing Research Institute 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, @@ -12,38 +12,45 @@ import static org.opendaylight.ovsdb.lib.operations.Operations.op; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import com.google.common.collect.Lists; import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo; import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper; +import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil; import org.opendaylight.ovsdb.lib.notation.Mutator; import org.opendaylight.ovsdb.lib.notation.UUID; import org.opendaylight.ovsdb.lib.operations.TransactionBuilder; import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils; import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort; import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch; -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.hwvtep.rev150901.HwvtepNodeName; +import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId; 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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Optional; -import com.google.common.collect.Sets; public class PhysicalPortUpdateCommand extends AbstractTransactCommand { private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortUpdateCommand.class); + private static final VlanBindingsUnMetDependencyGetter DEPENDENCY_GETTER = new VlanBindingsUnMetDependencyGetter(); public PhysicalPortUpdateCommand(HwvtepOperationalState state, Collection> changes) { @@ -59,59 +66,45 @@ public class PhysicalPortUpdateCommand extends AbstractTransactCommand { if (!createds.isEmpty()) { for (Entry, List> created: createds.entrySet()) { - updatePhysicalPort(transaction, created.getKey(), created.getValue(), createdPhysicalSwitches); + updatePhysicalPort(transaction, created.getKey(), created.getValue()); } } Map, List> updateds = - extractUpdated(getChanges(),HwvtepPhysicalPortAugmentation.class); + extractUpdatedPorts(getChanges(), HwvtepPhysicalPortAugmentation.class); if (!updateds.isEmpty()) { for (Entry, List> updated: updateds.entrySet()) { - updatePhysicalPort(transaction, updated.getKey(), updated.getValue(), createdPhysicalSwitches); + updatePhysicalPort(transaction, updated.getKey(), updated.getValue()); } } } - private void updatePhysicalPort(TransactionBuilder transaction, - InstanceIdentifier psNodeiid, - List listPort, - Map, PhysicalSwitchAugmentation> createdPhysicalSwitches ) { + public void updatePhysicalPort(final TransactionBuilder transaction, + final InstanceIdentifier psNodeiid, + final List listPort) { //Get physical switch which the port belong to: in operation DS or new created - PhysicalSwitchAugmentation physicalSwitchBelong = getPhysicalSwitchBelong(psNodeiid, createdPhysicalSwitches); for (HwvtepPhysicalPortAugmentation port : listPort) { LOG.debug("Creating a physical port named: {}", port.getHwvtepNodeName().getValue()); - Optional operationalPhysicalPortOptional = - getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName()); - PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class); - //get managing global node of physicalSwitchBelong - InstanceIdentifier globalNodeIid = physicalSwitchBelong.getManagedBy().getValue(); - setVlanBindings(globalNodeIid, physicalPort, port); - setDescription(physicalPort, port); - if (!operationalPhysicalPortOptional.isPresent()) { - //create a physical port - setName(physicalPort, port, operationalPhysicalPortOptional); - String portUuid = "PhysicalPort_" + HwvtepSouthboundMapper.getRandomUUID(); - transaction.add(op.insert(physicalPort).withId(portUuid)); - //update physical switch table - PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class); - physicalSwitch.setName(physicalSwitchBelong.getHwvtepNodeName().getValue()); - physicalSwitch.setPorts(Sets.newHashSet(new UUID(portUuid))); - LOG.info("execute: physical switch: {}", physicalSwitch); - transaction.add(op.mutate(physicalSwitch) - .addMutation(physicalSwitch.getPortsColumn().getSchema(), Mutator.INSERT, - physicalSwitch.getPortsColumn().getData()) - .where(physicalSwitch.getNameColumn().getSchema().opEqual(physicalSwitch.getNameColumn().getData())) - .build()); + HwvtepDeviceInfo.DeviceData deviceOperdata = getDeviceInfo().getDeviceOperData(TerminationPoint.class, + getTpIid(psNodeiid, port.getHwvtepNodeName().getValue())); + if (deviceOperdata == null) { + //create a physical port always happens from device + LOG.error("Physical port {} not present in oper datastore", port.getHwvtepNodeName().getValue()); } else { - //updated physical port only - HwvtepPhysicalPortAugmentation updatedPhysicalPort = operationalPhysicalPortOptional.get(); - String existingPhysicalPortName = updatedPhysicalPort.getHwvtepNodeName().getValue(); + PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), + PhysicalPort.class); + physicalPort.setName(port.getHwvtepNodeName().getValue()); + setVlanBindings(psNodeiid, physicalPort, port, transaction); + setDescription(physicalPort, port); + String existingPhysicalPortName = port.getHwvtepNodeName().getValue(); PhysicalPort extraPhyscialPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class); extraPhyscialPort.setName(""); + LOG.trace("execute: updating physical port: {}", physicalPort); transaction.add(op.update(physicalPort) .where(extraPhyscialPort.getNameColumn().getSchema().opEqual(existingPhysicalPortName)) .build()); + transaction.add(op.comment("Physical Port: Updating " + existingPhysicalPortName)); } } } @@ -141,39 +134,125 @@ public class PhysicalPortUpdateCommand extends AbstractTransactCommand { private void setDescription(PhysicalPort physicalPort, HwvtepPhysicalPortAugmentation inputPhysicalPort) { if (inputPhysicalPort.getHwvtepNodeDescription() != null) { - physicalPort.setDescription(inputPhysicalPort.getHwvtepNodeDescription().toString()); + physicalPort.setDescription(inputPhysicalPort.getHwvtepNodeDescription()); } } - private void setVlanBindings(InstanceIdentifier globalNodeIid, PhysicalPort physicalPort, - HwvtepPhysicalPortAugmentation inputPhysicalPort) { + private void setVlanBindings(final InstanceIdentifier psNodeiid, + final PhysicalPort physicalPort, + final HwvtepPhysicalPortAugmentation inputPhysicalPort, + final TransactionBuilder transaction) { if (inputPhysicalPort.getVlanBindings() != null) { //get UUID by LogicalSwitchRef - Map bindingMap = new HashMap(); + Map bindingMap = new HashMap<>(); for (VlanBindings vlanBinding: inputPhysicalPort.getVlanBindings()) { - HwvtepNodeName lswitchName = new HwvtepNodeName(vlanBinding.getLogicalSwitchRef().getValue()); - Optional operationalSwitchOptional = - getOperationalState().getLogicalSwitches(globalNodeIid, new LogicalSwitchesKey(lswitchName)); - if (operationalSwitchOptional.isPresent()) { - Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid(); - bindingMap.put(vlanBinding.getVlanIdKey().getValue().longValue(), new UUID(logicalSwitchUuid.getValue())); + InstanceIdentifier vlanIid = getVlanBindingIid(psNodeiid, physicalPort, vlanBinding); + @SuppressWarnings("unchecked") + InstanceIdentifier lswitchIid = + (InstanceIdentifier) vlanBinding.getLogicalSwitchRef().getValue(); + + Map inTransitDependencies = DEPENDENCY_GETTER.getInTransitDependencies( + getOperationalState(), vlanBinding); + Map configDependencies = DEPENDENCY_GETTER.getUnMetConfigDependencies( + getOperationalState(), vlanBinding); + + if (!HwvtepSouthboundUtil.isEmptyMap(configDependencies)) { + createConfigWaitJob(psNodeiid, inputPhysicalPort, + vlanBinding, configDependencies, vlanIid); + continue; + } + if (!HwvtepSouthboundUtil.isEmptyMap(inTransitDependencies)) { + createOperWaitingJob(psNodeiid, inputPhysicalPort, + vlanBinding, inTransitDependencies, vlanIid); + continue; } + + bindingMap.put(vlanBinding.getVlanIdKey().getValue().longValue(), + TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(), lswitchIid)); } physicalPort.setVlanBindings(bindingMap); } } + private void createOperWaitingJob(final InstanceIdentifier psNodeiid, + final HwvtepPhysicalPortAugmentation inputPhysicalPort, + final VlanBindings vlanBinding, + final Map inTransitDependencies, + final InstanceIdentifier vlanIid) { + + DependentJob opWaitingJob = new DependentJob.OpWaitingJob( + vlanIid, vlanBinding, inTransitDependencies) { + @Override + public void onDependencyResolved(final HwvtepOperationalState operationalState, + final TransactionBuilder transactionBuilder) { + PhysicalPortUpdateCommand.this.threadLocalOperationalState.set(operationalState); + PhysicalPortUpdateCommand.this.threadLocalDeviceTransaction.set(transactionBuilder); + updatePhysicalPort(transactionBuilder, psNodeiid, Lists.newArrayList(inputPhysicalPort)); + } + }; + getDeviceInfo().addJobToQueue(opWaitingJob); + } + + private void createConfigWaitJob(final InstanceIdentifier psNodeiid, + final HwvtepPhysicalPortAugmentation inputPhysicalPort, + final VlanBindings vlanBinding, + final Map configDependencies, + final InstanceIdentifier vlanIid) { + + DependentJob configWaitingJob = new DependentJob.ConfigWaitingJob( + vlanIid, vlanBinding, configDependencies) { + @Override + public void onDependencyResolved(final HwvtepOperationalState operationalState, + final TransactionBuilder transactionBuilder) { + PhysicalPortUpdateCommand.this.threadLocalOperationalState.set(operationalState); + PhysicalPortUpdateCommand.this.threadLocalDeviceTransaction.set(transactionBuilder); + updatePhysicalPort(transactionBuilder, psNodeiid, Lists.newArrayList(inputPhysicalPort)); + } + }; + getDeviceInfo().addJobToQueue(configWaitingJob); + } + + private InstanceIdentifier getTpIid(final InstanceIdentifier psNodeiid, + final String portName) { + return psNodeiid.child( + TerminationPoint.class, new TerminationPointKey(new TpId(portName))); + } + + private InstanceIdentifier getVlanBindingIid( + final InstanceIdentifier psNodeiid, + final PhysicalPort physicalPort, + final VlanBindings vlanBinding) { + + return psNodeiid.child( + TerminationPoint.class, new TerminationPointKey(new TpId(physicalPort.getName()))) + .augmentation(HwvtepPhysicalPortAugmentation.class) + .child(VlanBindings.class, new VlanBindingsKey(vlanBinding.getVlanIdKey())); + } + + static class VlanBindingsUnMetDependencyGetter extends UnMetDependencyGetter { + + public List> getLogicalSwitchDependencies(VlanBindings data) { + if (data == null) { + return Collections.emptyList(); + } + return Collections.singletonList(data.getLogicalSwitchRef().getValue()); + } + + public List> getTerminationPointDependencies(VlanBindings data) { + return Collections.emptyList(); + } + } + private Map, List> extractCreated( Collection> changes, Class class1) { - Map, List> result - = new HashMap, List>(); + Map, List> result = new HashMap<>(); if (changes != null && !changes.isEmpty()) { for (DataTreeModification change : changes) { final InstanceIdentifier key = change.getRootPath().getRootIdentifier(); final DataObjectModification mod = change.getRootNode(); Node created = TransactUtils.getCreated(mod); if (created != null) { - List portListUpdated = new ArrayList(); + List portListUpdated = new ArrayList<>(); if (created.getTerminationPoint() != null) { for (TerminationPoint tp : created.getTerminationPoint()) { HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class); @@ -191,8 +270,7 @@ public class PhysicalPortUpdateCommand extends AbstractTransactCommand { private Map, PhysicalSwitchAugmentation> extractCreatedPhyscialSwitch( Collection> changes, Class class1) { - Map, PhysicalSwitchAugmentation> result - = new HashMap, PhysicalSwitchAugmentation>(); + Map, PhysicalSwitchAugmentation> result = new HashMap<>(); if (changes != null && !changes.isEmpty()) { for (DataTreeModification change : changes) { final InstanceIdentifier key = change.getRootPath().getRootIdentifier(); @@ -209,10 +287,9 @@ public class PhysicalPortUpdateCommand extends AbstractTransactCommand { return result; } - private Map, List> extractUpdated( + private Map, List> extractUpdatedPorts( Collection> changes, Class class1) { - Map, List> result - = new HashMap, List>(); + Map, List> result = new HashMap<>(); if (changes != null && !changes.isEmpty()) { for (DataTreeModification change : changes) { final InstanceIdentifier key = change.getRootPath().getRootIdentifier(); @@ -220,8 +297,8 @@ public class PhysicalPortUpdateCommand extends AbstractTransactCommand { Node updated = TransactUtils.getUpdated(mod); Node before = mod.getDataBefore(); if (updated != null && before != null) { - List portListUpdated = new ArrayList(); - List portListBefore = new ArrayList(); + List portListUpdated = new ArrayList<>(); + List portListBefore = new ArrayList<>(); if (updated.getTerminationPoint() != null) { for (TerminationPoint tp : updated.getTerminationPoint()) { HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);