2 * Copyright (c) 2015, 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md;
11 import java.util.ArrayList;
12 import java.util.List;
14 import java.util.Map.Entry;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
21 import org.opendaylight.ovsdb.lib.message.TableUpdates;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.google.common.base.Optional;
53 public class PhysicalPortUpdateCommand extends AbstractTransactionCommand {
55 private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortUpdateCommand.class);
56 private Map<UUID, PhysicalPort> updatedPPRows;
57 private Map<UUID, PhysicalPort> oldPPRows;
58 private Map<UUID, PhysicalSwitch> switchUpdatedRows;
60 public PhysicalPortUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
61 super(key, updates, dbSchema);
62 updatedPPRows = TyperUtils.extractRowsUpdated(PhysicalPort.class, getUpdates(), getDbSchema());
63 oldPPRows = TyperUtils.extractRowsOld(PhysicalPort.class, getUpdates(), getDbSchema());
64 switchUpdatedRows = TyperUtils.extractRowsUpdated(PhysicalSwitch.class, getUpdates(), getDbSchema());
68 public void execute(ReadWriteTransaction transaction) {
69 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
70 if (updatedPPRows.isEmpty()) {
73 LOG.trace("PhysicalPortTable updated: {}", updatedPPRows);
74 Optional<Node> node = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
75 if (node.isPresent()) {
76 updateTerminationPoints(transaction, node.get());
77 // TODO: Handle Deletion of VLAN Bindings
81 private void updateTerminationPoints(ReadWriteTransaction transaction, Node node) {
82 for (Entry<UUID, PhysicalPort> pPortUpdateEntry : updatedPPRows.entrySet()) {
83 PhysicalPort pPortUpdate = pPortUpdateEntry.getValue();
84 String portName = pPortUpdate.getNameColumn().getData();
85 Optional<InstanceIdentifier<Node>> switchIid = getTerminationPointSwitch(pPortUpdateEntry.getKey());
86 if (!switchIid.isPresent()) {
87 switchIid = getTerminationPointSwitch(transaction, node, portName);
89 if (switchIid.isPresent()) {
90 TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
91 TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
92 tpBuilder.setKey(tpKey);
93 tpBuilder.setTpId(tpKey.getTpId());
94 InstanceIdentifier<TerminationPoint> tpPath = getInstanceIdentifier(switchIid.get(), pPortUpdate);
95 HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder =
96 new HwvtepPhysicalPortAugmentationBuilder();
97 buildTerminationPoint(tpAugmentationBuilder, pPortUpdate);
98 tpBuilder.addAugmentation(HwvtepPhysicalPortAugmentation.class, tpAugmentationBuilder.build());
99 if (oldPPRows.containsKey(pPortUpdateEntry.getKey())) {
100 transaction.merge(LogicalDatastoreType.OPERATIONAL, tpPath, tpBuilder.build());
102 transaction.put(LogicalDatastoreType.OPERATIONAL, tpPath, tpBuilder.build());
104 // Update with Deleted VlanBindings
105 if (oldPPRows.get(pPortUpdateEntry.getKey()) != null
106 && oldPPRows.get(pPortUpdateEntry.getKey()).getVlanBindingsColumn() != null) {
107 List<InstanceIdentifier<VlanBindings>> vBIiList = new ArrayList<>();
108 Map<Long, UUID> oldVb = oldPPRows.get(pPortUpdateEntry.getKey()).getVlanBindingsColumn().getData();
109 Map<Long, UUID> updatedVb = pPortUpdateEntry.getValue().getVlanBindingsColumn().getData();
110 for (Map.Entry<Long, UUID> oldVbEntry : oldVb.entrySet()) {
111 Long key = oldVbEntry.getKey();
112 if (!updatedVb.containsKey(key)) {
113 VlanBindings vBindings = createVlanBinding(key, oldVbEntry.getValue());
114 InstanceIdentifier<VlanBindings> vBid = getInstanceIdentifier(tpPath, vBindings);
117 deleteEntries(transaction, vBIiList);
124 private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
125 List<InstanceIdentifier<T>> entryIids) {
126 for (InstanceIdentifier<T> entryIid : entryIids) {
127 transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
131 private InstanceIdentifier<VlanBindings> getInstanceIdentifier(InstanceIdentifier<TerminationPoint> tpPath,
132 VlanBindings vBindings) {
133 return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), tpPath, vBindings);
136 private void buildTerminationPoint(HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder,
137 PhysicalPort portUpdate) {
138 updatePhysicalPortId(portUpdate, tpAugmentationBuilder);
139 updatePort(portUpdate, tpAugmentationBuilder);
142 private void updatePort(PhysicalPort portUpdate, HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
143 updateVlanBindings(portUpdate, tpAugmentationBuilder);
144 tpAugmentationBuilder.setPhysicalPortUuid(new Uuid(portUpdate.getUuid().toString()));
147 private void updatePhysicalPortId(PhysicalPort portUpdate,
148 HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
149 tpAugmentationBuilder.setHwvtepNodeName(new HwvtepNodeName(portUpdate.getName()));
150 if (portUpdate.getDescription() != null) {
151 tpAugmentationBuilder.setHwvtepNodeDescription(portUpdate.getDescription());
155 private void updateVlanBindings(PhysicalPort portUpdate,
156 HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
157 Map<Long, UUID> vlanBindings = portUpdate.getVlanBindingsColumn().getData();
158 if (vlanBindings != null && !vlanBindings.isEmpty()) {
159 List<VlanBindings> vlanBindingsList = new ArrayList<>();
160 for (Map.Entry<Long, UUID> vlanBindingEntry : vlanBindings.entrySet()) {
161 Long vlanBindingKey = vlanBindingEntry.getKey();
162 UUID vlanBindingValue = vlanBindingEntry.getValue();
163 if (vlanBindingValue != null && vlanBindingKey != null) {
164 vlanBindingsList.add(createVlanBinding(vlanBindingKey, vlanBindingValue));
167 tpAugmentationBuilder.setVlanBindings(vlanBindingsList);
171 private VlanBindings createVlanBinding(Long key, UUID value) {
172 VlanBindingsBuilder vbBuilder = new VlanBindingsBuilder();
173 VlanBindingsKey vbKey = new VlanBindingsKey(new VlanId(key.intValue()));
174 vbBuilder.setKey(vbKey);
175 vbBuilder.setVlanIdKey(vbKey.getVlanIdKey());
176 HwvtepLogicalSwitchRef lSwitchRef = this.getLogicalSwitchRef(value);
177 vbBuilder.setLogicalSwitchRef(lSwitchRef);
178 return vbBuilder.build();
181 private HwvtepLogicalSwitchRef getLogicalSwitchRef(UUID switchUUID) {
182 LogicalSwitch logicalSwitch = getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitch(switchUUID);
183 if (logicalSwitch != null) {
184 InstanceIdentifier<LogicalSwitches> lSwitchIid =
185 HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
186 return new HwvtepLogicalSwitchRef(lSwitchIid);
188 LOG.debug("Failed to get LogicalSwitch {}", switchUUID);
189 LOG.trace("Available LogicalSwitches: {}",
190 getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitches().values());
194 private Optional<InstanceIdentifier<Node>> getTerminationPointSwitch(UUID portUUID) {
195 for (PhysicalSwitch updatedPhysicalSwitch : switchUpdatedRows.values()) {
196 if (updatedPhysicalSwitch.getPortsColumn().getData().contains(portUUID)) {
197 return Optional.of(HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
198 updatedPhysicalSwitch));
201 return Optional.absent();
204 private Optional<InstanceIdentifier<Node>> getTerminationPointSwitch(final ReadWriteTransaction transaction,
205 Node node, String tpName) {
206 HwvtepGlobalAugmentation hwvtepNode = node.getAugmentation(HwvtepGlobalAugmentation.class);
207 List<Switches> switchNodes = hwvtepNode.getSwitches();
208 for (Switches managedNodeEntry : switchNodes) {
209 @SuppressWarnings("unchecked")
210 Node switchNode = HwvtepSouthboundUtil
211 .readNode(transaction, (InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue()).get();
212 TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
213 TerminationPointKey tpKey = new TerminationPointKey(new TpId(tpName));
214 tpBuilder.setKey(tpKey);
215 if (switchNode.getTerminationPoint() != null
216 && switchNode.getTerminationPoint().contains(tpBuilder.build())) {
217 return Optional.of((InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue());
220 return Optional.absent();
223 private InstanceIdentifier<TerminationPoint> getInstanceIdentifier(InstanceIdentifier<Node> switchIid,
224 PhysicalPort pPort) {
225 return switchIid.child(TerminationPoint.class, new TerminationPointKey(new TpId(pPort.getName())));