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 class HwvtepMcastMacsRemoteUpdateCommand extends AbstractTransactionCommand {
46 private final Map<UUID, McastMacsRemote> updatedMMacsRemoteRows;
47 private final Map<UUID, PhysicalLocatorSet> updatedPLocSetRows;
48 private final Map<UUID, PhysicalLocator> updatedPLocRows;
50 public HwvtepMcastMacsRemoteUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates,
51 DatabaseSchema dbSchema) {
52 super(key, updates, dbSchema);
53 updatedMMacsRemoteRows = TyperUtils.extractRowsUpdated(McastMacsRemote.class, getUpdates(), getDbSchema());
54 updatedPLocSetRows = TyperUtils.extractRowsUpdated(PhysicalLocatorSet.class, getUpdates(), getDbSchema());
55 updatedPLocRows = TyperUtils.extractRowsUpdated(PhysicalLocator.class, getUpdates(), getDbSchema());
59 public void execute(ReadWriteTransaction transaction) {
60 for (Entry<UUID, McastMacsRemote> entry : updatedMMacsRemoteRows.entrySet()) {
61 updateData(transaction, entry.getValue());
65 private void updateData(ReadWriteTransaction transaction, McastMacsRemote macRemote) {
66 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
68 // Ensure the node exists
69 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId,
70 new NodeBuilder().setNodeId(getOvsdbConnectionInstance().getNodeId()).build());
72 // Prepare the update in internal structures
73 final RemoteMcastMacs mac = buildRemoteMcastMacs(macRemote);
74 final InstanceIdentifier<RemoteMcastMacs> macIid = connectionIId.augmentation(HwvtepGlobalAugmentation.class)
75 .child(RemoteMcastMacs.class, mac.key());
76 addToUpdateTx(RemoteMcastMacs.class, macIid, macRemote.getUuid(), macRemote);
78 // Merge update, relying on automatic lifecycle...
79 transaction.merge(LogicalDatastoreType.OPERATIONAL, macIid, mac);
80 if (mac.getLocatorSet() == null) {
81 // ... but delete locator set if it is empty
82 // FIXME: can we use .put() of instead of merge/delete?
83 transaction.delete(LogicalDatastoreType.OPERATIONAL, macIid.child(LocatorSet.class));
87 private RemoteMcastMacs buildRemoteMcastMacs(McastMacsRemote macRemote) {
88 RemoteMcastMacsBuilder macRemoteBuilder = new RemoteMcastMacsBuilder();
89 if (macRemote.getMac().equals(HwvtepSouthboundConstants.UNKNOWN_DST_STRING)) {
90 macRemoteBuilder.setMacEntryKey(HwvtepSouthboundConstants.UNKNOWN_DST_MAC);
92 macRemoteBuilder.setMacEntryKey(new MacAddress(macRemote.getMac()));
94 macRemoteBuilder.setMacEntryUuid(new Uuid(macRemote.getUuid().toString()));
95 setIpAddress(macRemoteBuilder, macRemote);
96 setLocatorSet(macRemoteBuilder, macRemote);
97 setLogicalSwitch(macRemoteBuilder, macRemote);
99 return macRemoteBuilder.build();
102 private void setLogicalSwitch(RemoteMcastMacsBuilder macRemoteBuilder, McastMacsRemote macRemote) {
103 if (macRemote.getLogicalSwitchColumn() != null && macRemote.getLogicalSwitchColumn().getData() != null) {
104 UUID lsUUID = macRemote.getLogicalSwitchColumn().getData();
105 LogicalSwitch logicalSwitch = getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitch(lsUUID);
106 if (logicalSwitch != null) {
107 InstanceIdentifier<LogicalSwitches> switchIid =
108 HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
109 macRemoteBuilder.setLogicalSwitchRef(new HwvtepLogicalSwitchRef(switchIid));
114 private static void setIpAddress(RemoteMcastMacsBuilder macRemoteBuilder, McastMacsRemote macRemote) {
115 if (macRemote.getIpAddr() != null && !macRemote.getIpAddr().isEmpty()) {
116 macRemoteBuilder.setIpaddr(IpAddressBuilder.getDefaultInstance(macRemote.getIpAddr()));
120 private void setLocatorSet(RemoteMcastMacsBuilder macRemoteBuilder, McastMacsRemote macRemote) {
121 if (macRemote.getLocatorSetColumn() != null && macRemote.getLocatorSetColumn().getData() != null) {
122 UUID locSetUUID = macRemote.getLocatorSetColumn().getData();
123 PhysicalLocatorSet plSet = updatedPLocSetRows.get(locSetUUID);
125 if (plSet.getLocatorsColumn() != null && plSet.getLocatorsColumn().getData() != null
126 && !plSet.getLocatorsColumn().getData().isEmpty()) {
127 List<LocatorSet> plsList = new ArrayList<>();
128 for (UUID locUUID : plSet.getLocatorsColumn().getData()) {
129 PhysicalLocator locator = updatedPLocRows.get(locUUID);
130 if (locator == null) {
131 locator = getOvsdbConnectionInstance().getDeviceInfo().getPhysicalLocator(locUUID);
133 InstanceIdentifier<TerminationPoint> tpIid = HwvtepSouthboundMapper.createInstanceIdentifier(
134 getOvsdbConnectionInstance().getInstanceIdentifier(), locator);
135 plsList.add(new LocatorSetBuilder()
136 .setLocatorRef(new HwvtepPhysicalLocatorRef(tpIid)).build());
138 macRemoteBuilder.setLocatorSet(plsList);