Bump upstreams for 2022.09 Chlorine
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepMcastMacsRemoteUpdateCommand.java
1 /*
2  * Copyright (c) 2015, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
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.hwvtepsouthbound.transact.TransactUtils;
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.McastMacsRemote;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
28 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocatorSet;
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;
43
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;
48
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());
55     }
56
57     @Override
58     public void execute(ReadWriteTransaction transaction) {
59         for (Entry<UUID, McastMacsRemote> entry : updatedMMacsRemoteRows.entrySet()) {
60             updateData(transaction, entry.getValue());
61         }
62     }
63
64     private void updateData(ReadWriteTransaction transaction, McastMacsRemote macRemote) {
65         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
66
67         // Ensure the node exists
68         transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId,
69             new NodeBuilder().setNodeId(getOvsdbConnectionInstance().getNodeId()).build());
70
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);
76
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));
83         }
84     }
85
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);
90         } else {
91             macRemoteBuilder.setMacEntryKey(new MacAddress(macRemote.getMac()));
92         }
93         macRemoteBuilder.setMacEntryUuid(new Uuid(macRemote.getUuid().toString()));
94         setIpAddress(macRemoteBuilder, macRemote);
95         setLocatorSet(macRemoteBuilder, macRemote);
96         setLogicalSwitch(macRemoteBuilder, macRemote);
97
98         return macRemoteBuilder.build();
99     }
100
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));
109             }
110         }
111     }
112
113     private static void setIpAddress(RemoteMcastMacsBuilder macRemoteBuilder, McastMacsRemote macRemote) {
114         if (macRemote.getIpAddr() != null && !macRemote.getIpAddr().isEmpty()) {
115             macRemoteBuilder.setIpaddr(TransactUtils.parseIpAddress(macRemote.getIpAddr()));
116         }
117     }
118
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);
123             if (plSet != null) {
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);
131                         }
132                         InstanceIdentifier<TerminationPoint> tpIid = HwvtepSouthboundMapper.createInstanceIdentifier(
133                                 getOvsdbConnectionInstance().getInstanceIdentifier(), locator);
134                         plsList.add(new LocatorSetBuilder()
135                                 .setLocatorRef(new HwvtepPhysicalLocatorRef(tpIid)).build());
136                     }
137                     macRemoteBuilder.setLocatorSet(plsList);
138                 }
139             }
140         }
141     }
142 }