Fix findbugs violations in hwvtepsouthbound-impl
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepUcastMacsRemoteUpdateCommand.java
index 131fa1062c99632f52fa65c3e2f4260e744639e9..7b0cbb854bb6122566b579095ca12009d9ad68d8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -9,14 +9,13 @@
 package org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
-
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
-import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
 import org.opendaylight.ovsdb.lib.message.TableUpdates;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
@@ -39,14 +38,13 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-import com.google.common.base.Optional;
-
 public class HwvtepUcastMacsRemoteUpdateCommand extends AbstractTransactionCommand {
 
     private final Map<UUID, UcastMacsRemote> updatedUMacsRemoteRows;
     private final Map<UUID, PhysicalLocator> updatedPLocRows;
 
-    public HwvtepUcastMacsRemoteUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
+    public HwvtepUcastMacsRemoteUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates,
+            DatabaseSchema dbSchema) {
         super(key, updates, dbSchema);
         updatedUMacsRemoteRows = TyperUtils.extractRowsUpdated(UcastMacsRemote.class, getUpdates(), getDbSchema());
         updatedPLocRows = TyperUtils.extractRowsUpdated(PhysicalLocator.class, getUpdates(), getDbSchema());
@@ -54,57 +52,69 @@ public class HwvtepUcastMacsRemoteUpdateCommand extends AbstractTransactionComma
 
     @Override
     public void execute(ReadWriteTransaction transaction) {
-        for (UcastMacsRemote ucastMacsRemote : updatedUMacsRemoteRows.values()) {
-            updateUcastMacsRemote(transaction, ucastMacsRemote);
+        if (updatedUMacsRemoteRows != null && !updatedUMacsRemoteRows.isEmpty()) {
+            updateUcastMacsRemote(transaction, updatedUMacsRemoteRows.values());
         }
     }
 
-    private void updateUcastMacsRemote(ReadWriteTransaction transaction, UcastMacsRemote ucastMacsRemote) {
+    private void updateUcastMacsRemote(ReadWriteTransaction transaction, Collection<UcastMacsRemote> ucastMacsRemote) {
         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
-        Optional<Node> connection = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
-        if (connection.isPresent()) {
-            Node connectionNode = buildConnectionNode(ucastMacsRemote);
-            transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
-            //TODO: Handle any deletes
-        }
+        Node connectionNode = buildConnectionNode(ucastMacsRemote);
+        transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
     }
 
-    private Node buildConnectionNode(UcastMacsRemote uMacRemote) {
+    private Node buildConnectionNode(final Collection<UcastMacsRemote> macRemotes) {
         NodeBuilder connectionNode = new NodeBuilder();
         connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
-        InstanceIdentifier<Node> nodeIid = getOvsdbConnectionInstance().getInstanceIdentifier();
         HwvtepGlobalAugmentationBuilder hgAugmentationBuilder = new HwvtepGlobalAugmentationBuilder();
         List<RemoteUcastMacs> remoteUMacs = new ArrayList<>();
+        macRemotes.forEach(mac -> remoteUMacs.add(buildRemoteUcast(mac)));
+        hgAugmentationBuilder.setRemoteUcastMacs(remoteUMacs);
+        connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
+        return connectionNode.build();
+    }
+
+    private RemoteUcastMacs buildRemoteUcast(final UcastMacsRemote macRemote) {
+        InstanceIdentifier<Node> nodeIid = getOvsdbConnectionInstance().getInstanceIdentifier();
         RemoteUcastMacsBuilder rumBuilder = new RemoteUcastMacsBuilder();
-        rumBuilder.setMacEntryKey(new MacAddress(uMacRemote.getMac()));
-        rumBuilder.setMacEntryUuid(new Uuid(uMacRemote.getUuid().toString()));
-        if (uMacRemote.getIpAddr() != null && !uMacRemote.getIpAddr().isEmpty()) {
-            rumBuilder.setIpaddr(new IpAddress(uMacRemote.getIpAddr().toCharArray()));
+        rumBuilder.setMacEntryKey(new MacAddress(macRemote.getMac()));
+        rumBuilder.setMacEntryUuid(new Uuid(macRemote.getUuid().toString()));
+        if (macRemote.getIpAddr() != null && !macRemote.getIpAddr().isEmpty()) {
+            rumBuilder.setIpaddr(new IpAddress(macRemote.getIpAddr().toCharArray()));
         }
-        if (uMacRemote.getLocatorColumn() != null
-                && uMacRemote.getLocatorColumn().getData() != null) {
-            UUID pLocUUID = uMacRemote.getLocatorColumn().getData();
-            PhysicalLocator physicalLocator = updatedPLocRows.get(pLocUUID);
+        if (macRemote.getLocatorColumn() != null
+                && macRemote.getLocatorColumn().getData() != null) {
+            UUID locUUID = macRemote.getLocatorColumn().getData();
+            PhysicalLocator physicalLocator = updatedPLocRows.get(locUUID);
+            if (physicalLocator == null) {
+                physicalLocator = (PhysicalLocator) getOvsdbConnectionInstance()
+                        .getDeviceInfo().getDeviceOperData(TerminationPoint.class, locUUID);
+            }
             if (physicalLocator != null) {
                 InstanceIdentifier<TerminationPoint> plIid = HwvtepSouthboundMapper.createInstanceIdentifier(nodeIid,
                         physicalLocator);
                 rumBuilder.setLocatorRef(new HwvtepPhysicalLocatorRef(plIid));
             }
         }
-        if (uMacRemote.getLogicalSwitchColumn() != null
-                && uMacRemote.getLogicalSwitchColumn().getData() != null) {
-            UUID lsUUID = uMacRemote.getLogicalSwitchColumn().getData();
+        if (macRemote.getLogicalSwitchColumn() != null
+                && macRemote.getLogicalSwitchColumn().getData() != null) {
+            UUID lsUUID = macRemote.getLogicalSwitchColumn().getData();
             final LogicalSwitch logicalSwitch = getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitch(lsUUID);
             if (logicalSwitch != null) {
-                InstanceIdentifier<LogicalSwitches> lSwitchIid =
+                InstanceIdentifier<LogicalSwitches> switchIid =
                         HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
-                rumBuilder.setLogicalSwitchRef(new HwvtepLogicalSwitchRef(lSwitchIid));
+                rumBuilder.setLogicalSwitchRef(new HwvtepLogicalSwitchRef(switchIid));
             }
         }
-        remoteUMacs.add(rumBuilder.build());
-        hgAugmentationBuilder.setRemoteUcastMacs(remoteUMacs);
-        connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
-        return connectionNode.build();
+        RemoteUcastMacs remoteUcastMacs = rumBuilder.build();
+        InstanceIdentifier<RemoteUcastMacs> macIid = getMacIid(remoteUcastMacs);
+        getOvsdbConnectionInstance().getDeviceInfo().updateDeviceOperData(RemoteUcastMacs.class, macIid,
+                macRemote.getUuid(), macRemote);
+        return remoteUcastMacs;
     }
 
+    private InstanceIdentifier<RemoteUcastMacs> getMacIid(final RemoteUcastMacs remoteUcastMacs) {
+        return getOvsdbConnectionInstance().getInstanceIdentifier()
+                .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class, remoteUcastMacs.getKey());
+    }
 }