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
9 package org.opendaylight.netconf.callhome.mount;
11 import static com.google.common.base.Preconditions.checkArgument;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
17 import java.io.IOException;
18 import java.net.InetSocketAddress;
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
28 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorizationProvider;
32 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServer;
33 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServerBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.callhome.device.status.rev170112.Device1;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.callhome.device.status.rev170112.Device1Builder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.NetconfCallhomeServer;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.AllowedDevices;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.Device;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.DeviceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.DeviceKey;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataChangeListener {
49 private static final String APPNAME = "CallHomeServer";
50 static final InstanceIdentifier<AllowedDevices> ALL_DEVICES = InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class);
52 private static final Logger LOG = LoggerFactory.getLogger(IetfZeroTouchCallHomeServerProvider.class);
54 private final DataBroker dataBroker;
55 private final CallHomeMountDispatcher mountDispacher;
56 private CallHomeAuthProviderImpl authProvider;
58 protected NetconfCallHomeServer server;
60 private ListenerRegistration<IetfZeroTouchCallHomeServerProvider> listenerReg = null;
62 private static final String CALL_HOME_PORT_KEY = "DefaultCallHomePort";
63 private int port = 0; // 0 = use default in NetconfCallHomeBuilder
64 private CallhomeStatusReporter statusReporter;
66 public IetfZeroTouchCallHomeServerProvider(DataBroker dataBroker, CallHomeMountDispatcher mountDispacher) {
67 this.dataBroker = dataBroker;
68 this.mountDispacher = mountDispacher;
69 this.authProvider = new CallHomeAuthProviderImpl(dataBroker);
70 this.statusReporter = new CallhomeStatusReporter(dataBroker);
74 // Register itself as a listener to changes in Devices subtree
76 LOG.info("Initializing provider for {}", APPNAME);
78 dataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, ALL_DEVICES, this, AsyncDataBroker.DataChangeScope.SUBTREE);
79 LOG.info("Initialization complete for {}", APPNAME);
80 } catch (IOException | Configuration.ConfigurationException e) {
81 LOG.error("Unable to successfully initialize", e);
85 public void setPort(String portStr) {
87 Configuration configuration = new Configuration();
88 configuration.set(CALL_HOME_PORT_KEY, portStr);
89 port = configuration.getAsPort(CALL_HOME_PORT_KEY);
90 LOG.info("Setting port for call home server to {}", portStr);
91 } catch(Configuration.ConfigurationException e) {
92 LOG.error("Problem trying to set port for call home server {}", portStr, e);
96 private CallHomeAuthorizationProvider getCallHomeAuthorization() {
97 return new CallHomeAuthProviderImpl(dataBroker);
100 private void initializeServer() throws IOException {
101 LOG.info("Initializing Call Home server instance");
102 CallHomeAuthorizationProvider provider = this.getCallHomeAuthorization();
103 NetconfCallHomeServerBuilder builder = new NetconfCallHomeServerBuilder(
104 provider, mountDispacher, statusReporter);
106 builder.setBindAddress(new InetSocketAddress(port));
107 server = builder.build();
109 mountDispacher.createTopology();
110 LOG.info("Initialization complete for Call Home server instance");
114 void assertValid(Object obj, String description) {
116 throw new RuntimeException(String.format("Failed to find %s in IetfZeroTouchCallHomeProvider.initialize()", description));
120 public void close() throws Exception {
121 authProvider.close();
122 statusReporter.close();
124 // FIXME unbind the server
125 if (this.listenerReg != null) {
128 if (server != null) {
132 LOG.info("Successfully closed provider for {}", APPNAME);
136 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
138 // In case of any changes to the devices datatree, register the changed values with callhome server
139 // As of now, no way to add a new callhome client key to the CallHomeAuthorization instance since
140 // its created under CallHomeAuthorizationProvider.
141 // Will have to redesign a bit here.
142 // CallHomeAuthorization.
143 ReadOnlyTransaction roConfigTx = dataBroker.newReadOnlyTransaction();
144 CheckedFuture<Optional<AllowedDevices>, ReadFailedException> devicesFuture =
145 roConfigTx.read(LogicalDatastoreType.CONFIGURATION, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
147 if (hasDeletedDevices(change))
148 handleDeletedDevices(change);
151 for (Device confDevice : getReadDevices(devicesFuture)) {
152 readAndUpdateStatus(confDevice);
154 } catch (ReadFailedException e) {
155 LOG.error("Error trying to read the whitelist devices: {}", e);
159 private boolean hasDeletedDevices(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
160 return change.getRemovedPaths() != null;
163 private void handleDeletedDevices(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
164 checkArgument(change.getRemovedPaths() != null);
166 ReadWriteTransaction opTx = dataBroker.newReadWriteTransaction();
168 Set<InstanceIdentifier<?>> removedDevices = change.getRemovedPaths();
169 int numRemoved = removedDevices.size();
171 Iterator<InstanceIdentifier<?>> iterator = removedDevices.iterator();
172 while (iterator.hasNext()) {
173 InstanceIdentifier<?> removedIID = iterator.next();
174 LOG.info("Deleting the entry for callhome device {}", removedIID);
175 opTx.delete(LogicalDatastoreType.OPERATIONAL, removedIID);
182 private List<Device> getReadDevices(CheckedFuture<Optional<AllowedDevices>, ReadFailedException> devicesFuture)
183 throws ReadFailedException {
184 Optional<AllowedDevices> opt = devicesFuture.checkedGet();
185 if (opt.isPresent()) {
186 AllowedDevices confDevices = opt.get();
187 if (confDevices != null) {
188 LOG.debug("Read {} devices", confDevices.getDevice().size());
189 return confDevices.getDevice();
193 LOG.debug("Failed to read devices");
194 return new ArrayList<>();
197 private void readAndUpdateStatus(Device cfgDevice) throws ReadFailedException {
198 InstanceIdentifier<Device> deviceIID = InstanceIdentifier.create(NetconfCallhomeServer.class)
199 .child(AllowedDevices.class)
200 .child(Device.class, new DeviceKey(cfgDevice.getUniqueId()));
202 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
203 CheckedFuture<Optional<Device>, ReadFailedException> deviceFuture = tx.read(LogicalDatastoreType.OPERATIONAL, deviceIID);
205 Optional<Device> opDevGet = deviceFuture.checkedGet();
206 Device1 devStatus = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.DISCONNECTED).build();
207 if (opDevGet.isPresent()) {
208 Device opDevice = opDevGet.get();
209 devStatus = opDevice.getAugmentation(Device1.class);
212 Device newOpDevice = new DeviceBuilder()
213 .addAugmentation(Device1.class, devStatus)
214 .setSshHostKey(cfgDevice.getSshHostKey())
215 .setUniqueId(cfgDevice.getUniqueId()).build();
217 cfgDevice = newOpDevice;
219 tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIID, cfgDevice);