Merge "BUG 4718 - null pointer exception in NeutronSubnetChangeListener.java"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / UcastMacsRemoteUpdateCommand.java
1 /*
2  * Copyright (c) 2015 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
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
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.PhysicalLocator;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
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.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
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.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 import com.google.common.base.Optional;
42
43 public class UcastMacsRemoteUpdateCommand extends AbstractTransactionCommand {
44
45     private final Map<UUID, UcastMacsRemote> updatedUMacsRemoteRows;
46     private final Map<UUID, UcastMacsRemote> oldUMacsRemoteRows;
47     private final Map<UUID, PhysicalLocator> updatedPLocRows;
48     private final Map<UUID, LogicalSwitch> updatedLSRows;
49
50     public UcastMacsRemoteUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates,
51             DatabaseSchema dbSchema) {
52         super(key, updates, dbSchema);
53         updatedUMacsRemoteRows = TyperUtils.extractRowsUpdated(UcastMacsRemote.class, getUpdates(), getDbSchema());
54         oldUMacsRemoteRows = TyperUtils.extractRowsOld(UcastMacsRemote.class, getUpdates(), getDbSchema());
55         updatedPLocRows = TyperUtils.extractRowsUpdated(PhysicalLocator.class, getUpdates(), getDbSchema());
56         updatedLSRows = TyperUtils.extractRowsUpdated(LogicalSwitch.class, getUpdates(), getDbSchema());
57     }
58
59     @Override
60     public void execute(ReadWriteTransaction transaction) {
61         for (Entry<UUID, UcastMacsRemote> umrUpdate : updatedUMacsRemoteRows.entrySet()) {
62             updateUcastMacsRemote(transaction, umrUpdate.getValue());
63         }
64     }
65
66     private void updateUcastMacsRemote(ReadWriteTransaction transaction, UcastMacsRemote ucastMacsRemote) {
67         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
68         Optional<Node> connection = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
69         if (connection.isPresent()) {
70             Node connectionNode = buildConnectionNode(ucastMacsRemote);
71             transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
72             //TODO: Handle any deletes
73         }
74     }
75
76     private Node buildConnectionNode(UcastMacsRemote uMacRemote) {
77         NodeBuilder connectionNode = new NodeBuilder();
78         connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
79         InstanceIdentifier<Node> nodeIid = getOvsdbConnectionInstance().getInstanceIdentifier();
80         HwvtepGlobalAugmentationBuilder hgAugmentationBuilder = new HwvtepGlobalAugmentationBuilder();
81         List<RemoteUcastMacs> remoteUMacs = new ArrayList<>();
82         RemoteUcastMacsBuilder rumBuilder = new RemoteUcastMacsBuilder();
83         rumBuilder.setMacEntryKey(new MacAddress(uMacRemote.getMac()));
84         if (uMacRemote.getIpAddr() != null && !uMacRemote.getIpAddr().isEmpty()) {
85             rumBuilder.setIpaddr(new IpAddress(uMacRemote.getIpAddr().toCharArray()));
86         }
87         if (uMacRemote.getLocatorColumn() != null
88                 && uMacRemote.getLocatorColumn().getData() != null) {
89             UUID pLocUUID = uMacRemote.getLocatorColumn().getData();
90             if (updatedPLocRows.get(pLocUUID) != null) {
91                 InstanceIdentifier<TerminationPoint> plIid = HwvtepSouthboundMapper.createInstanceIdentifier(nodeIid, updatedPLocRows.get(pLocUUID));
92                 rumBuilder.setLocatorRef(new HwvtepPhysicalLocatorRef(plIid));
93             }
94         }
95         if (uMacRemote.getLogicalSwitchColumn() != null
96                 && uMacRemote.getLogicalSwitchColumn().getData() != null) {
97             UUID lsUUID = uMacRemote.getLogicalSwitchColumn().getData();
98             if (updatedLSRows.get(lsUUID) != null) {
99                 rumBuilder.setLogicalSwitchRef(new HwvtepLogicalSwitchRef(updatedLSRows.get(lsUUID).getName()));
100             }
101         }
102         remoteUMacs.add(rumBuilder.build());
103         hgAugmentationBuilder.setRemoteUcastMacs(remoteUMacs);
104         connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
105         return connectionNode.build();
106     }
107
108 }