2 * Copyright (c) 2015, 2017 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;
15 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
20 import org.opendaylight.ovsdb.lib.message.TableUpdates;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocatorSet;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSetBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 public final class HwvtepMcastMacsRemoteUpdateCommand extends AbstractTransactionCommand {
45 private final Map<UUID, McastMacsRemote> updatedMMacsRemoteRows;
46 private final Map<UUID, PhysicalLocatorSet> updatedPLocSetRows;
47 private final Map<UUID, PhysicalLocator> updatedPLocRows;
49 public HwvtepMcastMacsRemoteUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates,
50 DatabaseSchema dbSchema) {
51 super(key, updates, dbSchema);
52 updatedMMacsRemoteRows = TyperUtils.extractRowsUpdated(McastMacsRemote.class, getUpdates(), getDbSchema());
53 updatedPLocSetRows = TyperUtils.extractRowsUpdated(PhysicalLocatorSet.class, getUpdates(), getDbSchema());
54 updatedPLocRows = TyperUtils.extractRowsUpdated(PhysicalLocator.class, getUpdates(), getDbSchema());
58 public void execute(ReadWriteTransaction transaction) {
59 for (Entry<UUID, McastMacsRemote> entry : updatedMMacsRemoteRows.entrySet()) {
60 updateData(transaction, entry.getValue());
64 private void updateData(ReadWriteTransaction transaction, McastMacsRemote macRemote) {
65 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
67 // Ensure the node exists
68 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId,
69 new NodeBuilder().setNodeId(getOvsdbConnectionInstance().getNodeId()).build());
71 // Prepare the update in internal structures
72 final RemoteMcastMacs mac = buildRemoteMcastMacs(macRemote);
73 final InstanceIdentifier<RemoteMcastMacs> macIid = connectionIId.augmentation(HwvtepGlobalAugmentation.class)
74 .child(RemoteMcastMacs.class, mac.key());
75 addToUpdateTx(RemoteMcastMacs.class, macIid, macRemote.getUuid(), macRemote);
77 // Merge update, relying on automatic lifecycle...
78 transaction.merge(LogicalDatastoreType.OPERATIONAL, macIid, mac);
79 if (mac.getLocatorSet() == null) {
80 // ... but delete locator set if it is empty
81 // FIXME: can we use .put() of instead of merge/delete?
82 transaction.delete(LogicalDatastoreType.OPERATIONAL, macIid.child(LocatorSet.class));
86 private RemoteMcastMacs buildRemoteMcastMacs(McastMacsRemote macRemote) {
87 RemoteMcastMacsBuilder macRemoteBuilder = new RemoteMcastMacsBuilder();
88 if (macRemote.getMac().equals(HwvtepSouthboundConstants.UNKNOWN_DST_STRING)) {
89 macRemoteBuilder.setMacEntryKey(HwvtepSouthboundConstants.UNKNOWN_DST_MAC);
91 macRemoteBuilder.setMacEntryKey(new MacAddress(macRemote.getMac()));
93 macRemoteBuilder.setMacEntryUuid(new Uuid(macRemote.getUuid().toString()));
94 setIpAddress(macRemoteBuilder, macRemote);
95 setLocatorSet(macRemoteBuilder, macRemote);
96 setLogicalSwitch(macRemoteBuilder, macRemote);
98 return macRemoteBuilder.build();
101 private void setLogicalSwitch(RemoteMcastMacsBuilder macRemoteBuilder, McastMacsRemote macRemote) {
102 if (macRemote.getLogicalSwitchColumn() != null && macRemote.getLogicalSwitchColumn().getData() != null) {
103 UUID lsUUID = macRemote.getLogicalSwitchColumn().getData();
104 LogicalSwitch logicalSwitch = getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitch(lsUUID);
105 if (logicalSwitch != null) {
106 InstanceIdentifier<LogicalSwitches> switchIid =
107 HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
108 macRemoteBuilder.setLogicalSwitchRef(new HwvtepLogicalSwitchRef(switchIid));
113 private static void setIpAddress(RemoteMcastMacsBuilder macRemoteBuilder, McastMacsRemote macRemote) {
114 if (macRemote.getIpAddr() != null && !macRemote.getIpAddr().isEmpty()) {
115 macRemoteBuilder.setIpaddr(IpAddressBuilder.getDefaultInstance(macRemote.getIpAddr()));
119 private void setLocatorSet(RemoteMcastMacsBuilder macRemoteBuilder, McastMacsRemote macRemote) {
120 if (macRemote.getLocatorSetColumn() != null && macRemote.getLocatorSetColumn().getData() != null) {
121 UUID locSetUUID = macRemote.getLocatorSetColumn().getData();
122 PhysicalLocatorSet plSet = updatedPLocSetRows.get(locSetUUID);
124 if (plSet.getLocatorsColumn() != null && plSet.getLocatorsColumn().getData() != null
125 && !plSet.getLocatorsColumn().getData().isEmpty()) {
126 List<LocatorSet> plsList = new ArrayList<>();
127 for (UUID locUUID : plSet.getLocatorsColumn().getData()) {
128 PhysicalLocator locator = updatedPLocRows.get(locUUID);
129 if (locator == null) {
130 locator = getOvsdbConnectionInstance().getDeviceInfo().getPhysicalLocator(locUUID);
132 InstanceIdentifier<TerminationPoint> tpIid = HwvtepSouthboundMapper.createInstanceIdentifier(
133 getOvsdbConnectionInstance().getInstanceIdentifier(), locator);
134 plsList.add(new LocatorSetBuilder()
135 .setLocatorRef(new HwvtepPhysicalLocatorRef(tpIid)).build());
137 macRemoteBuilder.setLocatorSet(plsList);