X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fcallhome-provider%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fcallhome%2Fmount%2FCallhomeStatusReporter.java;h=1f819ae0a34ffa3f0cf5a32597ed9c72d1be6c86;hb=f0b0a99508a36b2087b507ad1ab976255599f4af;hp=b315aeef989fc6b1b8cc55219e3eb76101311c2a;hpb=91f932fd6ef4d5e3db81af8c739ace55d7c5c90e;p=netconf.git diff --git a/netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/CallhomeStatusReporter.java b/netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/CallhomeStatusReporter.java index b315aeef98..1f819ae0a3 100644 --- a/netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/CallhomeStatusReporter.java +++ b/netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/CallhomeStatusReporter.java @@ -5,28 +5,28 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.netconf.callhome.mount; -import com.google.common.base.Optional; +import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.PublicKey; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; import java.util.concurrent.ExecutionException; -import javax.annotation.Nonnull; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; -import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener; -import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; -import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; -import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; -import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; -import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.binding.api.DataObjectModification; +import org.opendaylight.mdsal.binding.api.DataTreeChangeListener; +import org.opendaylight.mdsal.binding.api.DataTreeIdentifier; +import org.opendaylight.mdsal.binding.api.DataTreeModification; +import org.opendaylight.mdsal.binding.api.ReadTransaction; +import org.opendaylight.mdsal.binding.api.WriteTransaction; +import org.opendaylight.mdsal.common.api.CommitInfo; +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.netconf.callhome.protocol.AuthorizedKeysDecoder; import org.opendaylight.netconf.callhome.protocol.StatusRecorder; import org.opendaylight.yang.gen.v1.urn.opendaylight.callhome.device.status.rev170112.Device1; @@ -63,12 +63,12 @@ class CallhomeStatusReporter implements DataTreeChangeListener, StatusReco CallhomeStatusReporter(final DataBroker broker) { this.dataBroker = broker; - this.reg = dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, + this.reg = dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, NETCONF_TOPO_IID.child(Node.class)), this); } @Override - public void onDataTreeChanged(@Nonnull final Collection> changes) { + public void onDataTreeChanged(final Collection> changes) { for (DataTreeModification change: changes) { final DataObjectModification rootNode = change.getRootNode(); final InstanceIdentifier identifier = change.getRootPath().getRootIdentifier(); @@ -205,26 +205,25 @@ class CallhomeStatusReporter implements DataTreeChangeListener, StatusReco } private Device readAndGetDevice(final NodeId nodeId) { - return readDevice(nodeId).orNull(); + return readDevice(nodeId).orElse(null); } - @Nonnull private Optional readDevice(final NodeId nodeId) { - ReadOnlyTransaction opTx = dataBroker.newReadOnlyTransaction(); + ReadTransaction opTx = dataBroker.newReadOnlyTransaction(); InstanceIdentifier deviceIID = buildDeviceInstanceIdentifier(nodeId); ListenableFuture> devFuture = opTx.read(LogicalDatastoreType.OPERATIONAL, deviceIID); try { return devFuture.get(); } catch (InterruptedException | ExecutionException e) { - return Optional.absent(); + return Optional.empty(); } } private void writeDevice(final NodeId nodeId, final Device modifiedDevice) { - ReadWriteTransaction opTx = dataBroker.newReadWriteTransaction(); + WriteTransaction opTx = dataBroker.newWriteOnlyTransaction(); opTx.merge(LogicalDatastoreType.OPERATIONAL, buildDeviceInstanceIdentifier(nodeId), modifiedDevice); - opTx.submit(); + commit(opTx, modifiedDevice.key()); } private static InstanceIdentifier buildDeviceInstanceIdentifier(final NodeId nodeId) { @@ -259,23 +258,36 @@ class CallhomeStatusReporter implements DataTreeChangeListener, StatusReco private void setDeviceStatus(final Device device) { WriteTransaction tx = dataBroker.newWriteOnlyTransaction(); - InstanceIdentifier deviceIId = - InstanceIdentifier.create(NetconfCallhomeServer.class) + InstanceIdentifier deviceIId = InstanceIdentifier.create(NetconfCallhomeServer.class) .child(AllowedDevices.class) .child(Device.class, device.key()); tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIId, device); - tx.submit(); + commit(tx, device.key()); + } + + private static void commit(final WriteTransaction tx, final DeviceKey device) { + tx.commit().addCallback(new FutureCallback() { + @Override + public void onSuccess(final CommitInfo result) { + LOG.debug("Device {} committed", device); + } + + @Override + public void onFailure(final Throwable cause) { + LOG.warn("Failed to commit device {}", device, cause); + } + }, MoreExecutors.directExecutor()); } private AllowedDevices getDevices() { - ReadOnlyTransaction rxTransaction = dataBroker.newReadOnlyTransaction(); + ReadTransaction rxTransaction = dataBroker.newReadOnlyTransaction(); ListenableFuture> devicesFuture = rxTransaction.read(LogicalDatastoreType.OPERATIONAL, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES); try { - return devicesFuture.get().orNull(); + return devicesFuture.get().orElse(null); } catch (ExecutionException | InterruptedException e) { - LOG.error("Error trying to read the whitelist devices: {}", e); + LOG.error("Error trying to read the whitelist devices", e); return null; } } @@ -304,7 +316,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener, StatusReco return; } } catch (GeneralSecurityException e) { - LOG.error("Failed decoding a device key with host key: {} {}", keyString, e); + LOG.error("Failed decoding a device key with host key: {}", keyString, e); return; } } @@ -314,7 +326,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener, StatusReco } @Override - public void close() throws Exception { + public void close() { reg.close(); } }