Bump upstreams for 2022.09 Chlorine
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepUcastMacsRemoteUpdateCommand.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 package org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md;
9
10 import java.util.Collection;
11 import java.util.Map;
12 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
16 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactUtils;
17 import org.opendaylight.ovsdb.lib.message.TableUpdates;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
38
39 public final class HwvtepUcastMacsRemoteUpdateCommand extends AbstractTransactionCommand {
40
41     private final Map<UUID, UcastMacsRemote> updatedUMacsRemoteRows;
42     private final Map<UUID, PhysicalLocator> updatedPLocRows;
43
44     public HwvtepUcastMacsRemoteUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates,
45             DatabaseSchema dbSchema) {
46         super(key, updates, dbSchema);
47         updatedUMacsRemoteRows = TyperUtils.extractRowsUpdated(UcastMacsRemote.class, getUpdates(), getDbSchema());
48         updatedPLocRows = TyperUtils.extractRowsUpdated(PhysicalLocator.class, getUpdates(), getDbSchema());
49     }
50
51     @Override
52     public void execute(ReadWriteTransaction transaction) {
53         if (updatedUMacsRemoteRows != null && !updatedUMacsRemoteRows.isEmpty()) {
54             updateUcastMacsRemote(transaction, updatedUMacsRemoteRows.values());
55         }
56     }
57
58     private void updateUcastMacsRemote(ReadWriteTransaction transaction, Collection<UcastMacsRemote> ucastMacsRemote) {
59         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
60         Node connectionNode = buildConnectionNode(ucastMacsRemote);
61         transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
62     }
63
64     private Node buildConnectionNode(final Collection<UcastMacsRemote> macRemotes) {
65         var remoteUMacs = macRemotes.stream().map(this::buildRemoteUcast).collect(BindingMap.toOrderedMap());
66
67         return new NodeBuilder()
68             .setNodeId(getOvsdbConnectionInstance().getNodeId())
69             .addAugmentation(new HwvtepGlobalAugmentationBuilder().setRemoteUcastMacs(remoteUMacs).build())
70             .build();
71     }
72
73     private RemoteUcastMacs buildRemoteUcast(final UcastMacsRemote macRemote) {
74         InstanceIdentifier<Node> nodeIid = getOvsdbConnectionInstance().getInstanceIdentifier();
75         RemoteUcastMacsBuilder rumBuilder = new RemoteUcastMacsBuilder();
76         rumBuilder.setMacEntryKey(new MacAddress(macRemote.getMac()));
77         rumBuilder.setMacEntryUuid(new Uuid(macRemote.getUuid().toString()));
78         if (macRemote.getIpAddr() != null && !macRemote.getIpAddr().isEmpty()) {
79             rumBuilder.setIpaddr(TransactUtils.parseIpAddress(macRemote.getIpAddr()));
80         }
81         if (macRemote.getLocatorColumn() != null
82                 && macRemote.getLocatorColumn().getData() != null) {
83             UUID locUUID = macRemote.getLocatorColumn().getData();
84             PhysicalLocator physicalLocator = updatedPLocRows.get(locUUID);
85             if (physicalLocator == null) {
86                 physicalLocator = getOvsdbConnectionInstance().getDeviceInfo().getPhysicalLocator(locUUID);
87             }
88             if (physicalLocator != null) {
89                 InstanceIdentifier<TerminationPoint> plIid = HwvtepSouthboundMapper.createInstanceIdentifier(nodeIid,
90                         physicalLocator);
91                 rumBuilder.setLocatorRef(new HwvtepPhysicalLocatorRef(plIid));
92             }
93         }
94         if (macRemote.getLogicalSwitchColumn() != null
95                 && macRemote.getLogicalSwitchColumn().getData() != null) {
96             UUID lsUUID = macRemote.getLogicalSwitchColumn().getData();
97             final LogicalSwitch logicalSwitch = getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitch(lsUUID);
98             if (logicalSwitch != null) {
99                 InstanceIdentifier<LogicalSwitches> switchIid =
100                         HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
101                 rumBuilder.setLogicalSwitchRef(new HwvtepLogicalSwitchRef(switchIid));
102             }
103         }
104         RemoteUcastMacs remoteUcastMacs = rumBuilder.build();
105         InstanceIdentifier<RemoteUcastMacs> macIid = getMacIid(remoteUcastMacs);
106         addToUpdateTx(RemoteUcastMacs.class, macIid, macRemote.getUuid(), macRemote);
107         return remoteUcastMacs;
108     }
109
110     private InstanceIdentifier<RemoteUcastMacs> getMacIid(final RemoteUcastMacs remoteUcastMacs) {
111         return getOvsdbConnectionInstance().getInstanceIdentifier()
112                 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class, remoteUcastMacs.key());
113     }
114 }