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