2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
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
8 package org.opendaylight.netconf.callhome.mount;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Optional;
23 import java.util.concurrent.ExecutionException;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.DataObjectModification;
26 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
27 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.mdsal.binding.api.DataTreeModification;
29 import org.opendaylight.mdsal.binding.api.ReadTransaction;
30 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.mdsal.binding.api.WriteTransaction;
32 import org.opendaylight.mdsal.common.api.CommitInfo;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorizationProvider;
35 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServer;
36 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServerBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.callhome.device.status.rev170112.Device1;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.callhome.device.status.rev170112.Device1Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.NetconfCallhomeServer;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.AllowedDevices;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.Device;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.DeviceBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.DeviceKey;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataTreeChangeListener<AllowedDevices> {
50 private static final String APPNAME = "CallHomeServer";
51 static final InstanceIdentifier<AllowedDevices> ALL_DEVICES = InstanceIdentifier.create(NetconfCallhomeServer.class)
52 .child(AllowedDevices.class);
54 private static final Logger LOG = LoggerFactory.getLogger(IetfZeroTouchCallHomeServerProvider.class);
56 private final DataBroker dataBroker;
57 private final CallHomeMountDispatcher mountDispacher;
58 private final CallHomeAuthProviderImpl authProvider;
60 protected NetconfCallHomeServer server;
62 private ListenerRegistration<IetfZeroTouchCallHomeServerProvider> listenerReg = null;
64 private static final String CALL_HOME_PORT_KEY = "DefaultCallHomePort";
65 private int port = 0; // 0 = use default in NetconfCallHomeBuilder
66 private final CallhomeStatusReporter statusReporter;
68 public IetfZeroTouchCallHomeServerProvider(final DataBroker dataBroker,
69 final CallHomeMountDispatcher mountDispacher) {
70 this.dataBroker = dataBroker;
71 this.mountDispacher = mountDispacher;
72 this.authProvider = new CallHomeAuthProviderImpl(dataBroker);
73 this.statusReporter = new CallhomeStatusReporter(dataBroker);
77 // Register itself as a listener to changes in Devices subtree
79 LOG.info("Initializing provider for {}", APPNAME);
81 listenerReg = dataBroker.registerDataTreeChangeListener(
82 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, ALL_DEVICES), this);
83 LOG.info("Initialization complete for {}", APPNAME);
84 } catch (IOException | Configuration.ConfigurationException e) {
85 LOG.error("Unable to successfully initialize", e);
89 public void setPort(final String portStr) {
91 Configuration configuration = new Configuration();
92 configuration.set(CALL_HOME_PORT_KEY, portStr);
93 port = configuration.getAsPort(CALL_HOME_PORT_KEY);
94 LOG.info("Setting port for call home server to {}", portStr);
95 } catch (Configuration.ConfigurationException e) {
96 LOG.error("Problem trying to set port for call home server {}", portStr, e);
100 private CallHomeAuthorizationProvider getCallHomeAuthorization() {
101 return new CallHomeAuthProviderImpl(dataBroker);
104 private void initializeServer() throws IOException {
105 LOG.info("Initializing Call Home server instance");
106 CallHomeAuthorizationProvider provider = this.getCallHomeAuthorization();
107 NetconfCallHomeServerBuilder builder = new NetconfCallHomeServerBuilder(provider, mountDispacher,
110 builder.setBindAddress(new InetSocketAddress(port));
112 builder.setNettyGroup(new NioEventLoopGroup());
113 server = builder.build();
115 mountDispacher.createTopology();
116 LOG.info("Initialization complete for Call Home server instance");
120 void assertValid(final Object obj, final String description) {
122 throw new RuntimeException(
123 String.format("Failed to find %s in IetfZeroTouchCallHomeProvider.initialize()", description));
128 public void close() {
129 authProvider.close();
130 statusReporter.close();
132 // FIXME unbind the server
133 if (this.listenerReg != null) {
136 if (server != null) {
140 LOG.info("Successfully closed provider for {}", APPNAME);
144 public void onDataTreeChanged(final Collection<DataTreeModification<AllowedDevices>> changes) {
145 // In case of any changes to the devices datatree, register the changed values with callhome server
146 // As of now, no way to add a new callhome client key to the CallHomeAuthorization instance since
147 // its created under CallHomeAuthorizationProvider.
148 // Will have to redesign a bit here.
149 // CallHomeAuthorization.
150 ReadTransaction roConfigTx = dataBroker.newReadOnlyTransaction();
151 ListenableFuture<Optional<AllowedDevices>> devicesFuture = roConfigTx
152 .read(LogicalDatastoreType.CONFIGURATION, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
154 Set<InstanceIdentifier<?>> deletedDevices = new HashSet<>();
155 for (DataTreeModification<AllowedDevices> change : changes) {
156 DataObjectModification<AllowedDevices> rootNode = change.getRootNode();
157 switch (rootNode.getModificationType()) {
159 deletedDevices.add(change.getRootPath().getRootIdentifier());
166 handleDeletedDevices(deletedDevices);
169 for (Device confDevice : getReadDevices(devicesFuture)) {
170 readAndUpdateStatus(confDevice);
172 } catch (ExecutionException | InterruptedException e) {
173 LOG.error("Error trying to read the whitelist devices: {}", e);
177 private void handleDeletedDevices(final Set<InstanceIdentifier<?>> deletedDevices) {
178 if (deletedDevices.isEmpty()) {
182 WriteTransaction opTx = dataBroker.newWriteOnlyTransaction();
184 for (InstanceIdentifier<?> removedIID : deletedDevices) {
185 LOG.info("Deleting the entry for callhome device {}", removedIID);
186 opTx.delete(LogicalDatastoreType.OPERATIONAL, removedIID);
189 opTx.commit().addCallback(new FutureCallback<CommitInfo>() {
191 public void onSuccess(CommitInfo result) {
192 LOG.debug("Device deletions committed");
196 public void onFailure(Throwable cause) {
197 LOG.warn("Failed to commit device deletions", cause);
199 }, MoreExecutors.directExecutor());
202 private static List<Device> getReadDevices(final ListenableFuture<Optional<AllowedDevices>> devicesFuture)
203 throws InterruptedException, ExecutionException {
204 return devicesFuture.get().map(AllowedDevices::getDevice).orElse(Collections.emptyList());
207 private void readAndUpdateStatus(Device cfgDevice) throws InterruptedException, ExecutionException {
208 InstanceIdentifier<Device> deviceIID = InstanceIdentifier.create(NetconfCallhomeServer.class)
209 .child(AllowedDevices.class).child(Device.class, new DeviceKey(cfgDevice.getUniqueId()));
211 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
212 ListenableFuture<Optional<Device>> deviceFuture = tx.read(LogicalDatastoreType.OPERATIONAL, deviceIID);
214 final Device1 devStatus;
215 Optional<Device> opDevGet = deviceFuture.get();
216 if (opDevGet.isPresent()) {
217 devStatus = opDevGet.get().augmentation(Device1.class);
219 devStatus = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.DISCONNECTED).build();
222 tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIID, new DeviceBuilder()
223 .addAugmentation(Device1.class, devStatus).setSshHostKey(cfgDevice.getSshHostKey())
224 .setUniqueId(cfgDevice.getUniqueId()).build());
225 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
227 public void onSuccess(CommitInfo result) {
228 LOG.debug("Device {} status update committed", cfgDevice.key());
232 public void onFailure(Throwable cause) {
233 LOG.warn("Failed to commit device {} status update", cfgDevice.key(), cause);
235 }, MoreExecutors.directExecutor());