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