import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
import java.io.IOException;
import java.net.InetSocketAddress;
-import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Set;
+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.ReadOnlyTransaction;
import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorizationProvider;
import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServer;
import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServerBuilder;
public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataTreeChangeListener<AllowedDevices> {
private static final String APPNAME = "CallHomeServer";
static final InstanceIdentifier<AllowedDevices> ALL_DEVICES = InstanceIdentifier.create(NetconfCallhomeServer.class)
- .child(AllowedDevices.class);
+ .child(AllowedDevices.class);
private static final Logger LOG = LoggerFactory.getLogger(IetfZeroTouchCallHomeServerProvider.class);
private int port = 0; // 0 = use default in NetconfCallHomeBuilder
private final CallhomeStatusReporter statusReporter;
- public IetfZeroTouchCallHomeServerProvider(DataBroker dataBroker, CallHomeMountDispatcher mountDispacher) {
+ public IetfZeroTouchCallHomeServerProvider(final DataBroker dataBroker,
+ final CallHomeMountDispatcher mountDispacher) {
this.dataBroker = dataBroker;
this.mountDispacher = mountDispacher;
this.authProvider = new CallHomeAuthProviderImpl(dataBroker);
try {
LOG.info("Initializing provider for {}", APPNAME);
initializeServer();
- listenerReg = dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(
- LogicalDatastoreType.CONFIGURATION, ALL_DEVICES), this);
+ listenerReg = dataBroker.registerDataTreeChangeListener(
+ new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, ALL_DEVICES), this);
LOG.info("Initialization complete for {}", APPNAME);
} catch (IOException | Configuration.ConfigurationException e) {
LOG.error("Unable to successfully initialize", e);
}
}
- public void setPort(String portStr) {
+ public void setPort(final String portStr) {
try {
Configuration configuration = new Configuration();
configuration.set(CALL_HOME_PORT_KEY, portStr);
private void initializeServer() throws IOException {
LOG.info("Initializing Call Home server instance");
CallHomeAuthorizationProvider provider = this.getCallHomeAuthorization();
- NetconfCallHomeServerBuilder builder = new NetconfCallHomeServerBuilder(
- provider, mountDispacher, statusReporter);
+ NetconfCallHomeServerBuilder builder = new NetconfCallHomeServerBuilder(provider, mountDispacher,
+ statusReporter);
if (port > 0) {
builder.setBindAddress(new InetSocketAddress(port));
}
}
@VisibleForTesting
- void assertValid(Object obj, String description) {
+ void assertValid(final Object obj, final String description) {
if (obj == null) {
throw new RuntimeException(
- String.format("Failed to find %s in IetfZeroTouchCallHomeProvider.initialize()", description));
+ String.format("Failed to find %s in IetfZeroTouchCallHomeProvider.initialize()", description));
}
}
@Override
- public void close() throws Exception {
+ public void close() {
authProvider.close();
statusReporter.close();
}
@Override
- public void onDataTreeChanged(Collection<DataTreeModification<AllowedDevices>> changes) {
+ public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<AllowedDevices>> changes) {
// In case of any changes to the devices datatree, register the changed values with callhome server
// As of now, no way to add a new callhome client key to the CallHomeAuthorization instance since
// its created under CallHomeAuthorizationProvider.
// Will have to redesign a bit here.
// CallHomeAuthorization.
ReadOnlyTransaction roConfigTx = dataBroker.newReadOnlyTransaction();
- CheckedFuture<Optional<AllowedDevices>, ReadFailedException> devicesFuture =
- roConfigTx.read(LogicalDatastoreType.CONFIGURATION, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
+ ListenableFuture<Optional<AllowedDevices>> devicesFuture = roConfigTx
+ .read(LogicalDatastoreType.CONFIGURATION, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
Set<InstanceIdentifier<?>> deletedDevices = new HashSet<>();
- for (DataTreeModification<AllowedDevices> change: changes) {
+ for (DataTreeModification<AllowedDevices> change : changes) {
DataObjectModification<AllowedDevices> rootNode = change.getRootNode();
switch (rootNode.getModificationType()) {
case DELETE:
for (Device confDevice : getReadDevices(devicesFuture)) {
readAndUpdateStatus(confDevice);
}
- } catch (ReadFailedException e) {
+ } catch (ExecutionException | InterruptedException e) {
LOG.error("Error trying to read the whitelist devices: {}", e);
}
}
- private void handleDeletedDevices(Set<InstanceIdentifier<?>> deletedDevices) {
+ private void handleDeletedDevices(final Set<InstanceIdentifier<?>> deletedDevices) {
if (deletedDevices.isEmpty()) {
return;
}
ReadWriteTransaction opTx = dataBroker.newReadWriteTransaction();
- int numRemoved = deletedDevices.size();
-
- Iterator<InstanceIdentifier<?>> iterator = deletedDevices.iterator();
- while (iterator.hasNext()) {
- InstanceIdentifier<?> removedIID = iterator.next();
+ for (InstanceIdentifier<?> removedIID : deletedDevices) {
LOG.info("Deleting the entry for callhome device {}", removedIID);
opTx.delete(LogicalDatastoreType.OPERATIONAL, removedIID);
}
- if (numRemoved > 0) {
- opTx.submit();
- }
+ opTx.commit();
}
- private List<Device> getReadDevices(CheckedFuture<Optional<AllowedDevices>, ReadFailedException> devicesFuture)
- throws ReadFailedException {
- Optional<AllowedDevices> opt = devicesFuture.checkedGet();
- if (opt.isPresent()) {
- AllowedDevices confDevices = opt.get();
- if (confDevices != null) {
- LOG.debug("Read {} devices", confDevices.getDevice().size());
- return confDevices.getDevice();
- }
- }
-
- LOG.debug("Failed to read devices");
- return new ArrayList<>();
+ private static List<Device> getReadDevices(final ListenableFuture<Optional<AllowedDevices>> devicesFuture)
+ throws InterruptedException, ExecutionException {
+ Optional<AllowedDevices> opt = devicesFuture.get();
+ return opt.isPresent() ? opt.get().getDevice() : Collections.emptyList();
}
- private void readAndUpdateStatus(Device cfgDevice) throws ReadFailedException {
+ private void readAndUpdateStatus(Device cfgDevice) throws InterruptedException, ExecutionException {
InstanceIdentifier<Device> deviceIID = InstanceIdentifier.create(NetconfCallhomeServer.class)
- .child(AllowedDevices.class)
- .child(Device.class, new DeviceKey(cfgDevice.getUniqueId()));
+ .child(AllowedDevices.class).child(Device.class, new DeviceKey(cfgDevice.getUniqueId()));
ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
- CheckedFuture<Optional<Device>, ReadFailedException> deviceFuture = tx.read(
- LogicalDatastoreType.OPERATIONAL, deviceIID);
+ ListenableFuture<Optional<Device>> deviceFuture = tx.read(LogicalDatastoreType.OPERATIONAL, deviceIID);
- Optional<Device> opDevGet = deviceFuture.checkedGet();
+ Optional<Device> opDevGet = deviceFuture.get();
Device1 devStatus = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.DISCONNECTED).build();
if (opDevGet.isPresent()) {
Device opDevice = opDevGet.get();
- devStatus = opDevice.getAugmentation(Device1.class);
+ devStatus = opDevice.augmentation(Device1.class);
}
- Device newOpDevice = new DeviceBuilder()
- .addAugmentation(Device1.class, devStatus)
- .setSshHostKey(cfgDevice.getSshHostKey())
- .setUniqueId(cfgDevice.getUniqueId()).build();
-
- cfgDevice = newOpDevice;
+ cfgDevice = new DeviceBuilder().addAugmentation(Device1.class, devStatus)
+ .setSshHostKey(cfgDevice.getSshHostKey()).setUniqueId(cfgDevice.getUniqueId()).build();
tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIID, cfgDevice);
- tx.submit();
+ tx.commit();
}
}