2 * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.sal.connect.netconf.sal;
10 import com.google.common.collect.Lists;
11 import com.google.common.collect.Maps;
12 import java.util.Collections;
13 import java.util.List;
15 import java.util.concurrent.ExecutorService;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
17 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
18 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionCapabilities;
19 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
20 import org.opendaylight.controller.sal.core.api.Broker;
21 import org.opendaylight.controller.sal.core.api.RpcImplementation;
22 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
28 import org.osgi.framework.BundleContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionCapabilities> {
34 private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
35 private static final InstanceIdentifier ROOT_PATH = InstanceIdentifier.builder().toInstance();
37 private final RemoteDeviceId id;
38 private final NetconfDeviceSalProvider salProvider;
40 private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
42 public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext, final ExecutorService executor) {
44 this.salProvider = new NetconfDeviceSalProvider(id, executor);
45 registerToSal(domBroker, bindingBroker, bundleContext);
48 public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
49 domRegistryDependency.registerProvider(salProvider, bundleContext);
50 bindingBroker.registerProvider(salProvider, bundleContext);
54 public synchronized void onNotification(final CompositeNode domNotification) {
55 salProvider.getMountInstance().publish(domNotification);
59 public synchronized void onDeviceConnected(final SchemaContextProvider remoteSchemaContextProvider,
60 final NetconfSessionCapabilities netconfSessionPreferences, final RpcImplementation deviceRpc) {
61 salProvider.getMountInstance().setSchemaContext(remoteSchemaContextProvider.getSchemaContext());
62 salProvider.getDatastoreAdapter().updateDeviceState(true, netconfSessionPreferences.getModuleBasedCaps());
63 registerDataHandlersToSal(deviceRpc, netconfSessionPreferences);
64 registerRpcsToSal(deviceRpc);
68 public void onDeviceDisconnected() {
69 salProvider.getDatastoreAdapter().updateDeviceState(false, Collections.<QName>emptySet());
72 private void registerRpcsToSal(final RpcImplementation deviceRpc) {
73 final MountProvisionInstance mountInstance = salProvider.getMountInstance();
75 final Map<QName, String> failedRpcs = Maps.newHashMap();
76 for (final RpcDefinition rpcDef : mountInstance.getSchemaContext().getOperations()) {
78 salRegistrations.add(mountInstance.addRpcImplementation(rpcDef.getQName(), deviceRpc));
79 logger.debug("{}: Rpc {} from netconf registered successfully", id, rpcDef.getQName());
80 } catch (final Exception e) {
81 // Only debug per rpc, warn for all of them at the end to pollute log a little less (e.g. routed rpcs)
82 logger.debug("{}: Unable to register rpc {} from netconf device. This rpc will not be available", id,
83 rpcDef.getQName(), e);
84 failedRpcs.put(rpcDef.getQName(), e.getClass() + ":" + e.getMessage());
88 if (failedRpcs.isEmpty() == false) {
89 if (logger.isDebugEnabled()) {
90 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs);
92 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs.keySet());
97 private void registerDataHandlersToSal(final RpcImplementation deviceRpc,
98 final NetconfSessionCapabilities netconfSessionPreferences) {
99 final NetconfDeviceDataReader dataReader = new NetconfDeviceDataReader(id, deviceRpc);
100 final NetconfDeviceCommitHandler commitHandler = new NetconfDeviceCommitHandler(id, deviceRpc,
101 netconfSessionPreferences.isRollbackSupported());
103 final MountProvisionInstance mountInstance = salProvider.getMountInstance();
104 salRegistrations.add(mountInstance.registerConfigurationReader(ROOT_PATH, dataReader));
105 salRegistrations.add(mountInstance.registerOperationalReader(ROOT_PATH, dataReader));
106 salRegistrations.add(mountInstance.registerCommitHandler(ROOT_PATH, commitHandler));
110 public void close() {
111 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
112 closeGracefully(reg);
114 closeGracefully(salProvider);
117 private void closeGracefully(final AutoCloseable resource) {
118 if (resource != null) {
121 } catch (final Exception e) {
122 logger.warn("{}: Ignoring exception while closing {}", id, resource, e);