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.Maps;
11 import java.util.Collections;
12 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 import com.google.common.collect.Lists;
34 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionCapabilities> {
36 private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceTwoPhaseCommitTransaction.class);
37 private static final InstanceIdentifier ROOT_PATH = InstanceIdentifier.builder().toInstance();
39 private final RemoteDeviceId id;
40 private final NetconfDeviceSalProvider salProvider;
42 private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
44 public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext, final ExecutorService executor) {
46 this.salProvider = new NetconfDeviceSalProvider(id, executor);
47 registerToSal(domBroker, bindingBroker, bundleContext);
50 public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
51 domRegistryDependency.registerProvider(salProvider, bundleContext);
52 bindingBroker.registerProvider(salProvider, bundleContext);
56 public synchronized void onNotification(final CompositeNode domNotification) {
57 salProvider.getMountInstance().publish(domNotification);
61 public synchronized void onDeviceConnected(final SchemaContextProvider remoteSchemaContextProvider,
62 final NetconfSessionCapabilities netconfSessionPreferences, final RpcImplementation deviceRpc) {
63 salProvider.getMountInstance().setSchemaContext(remoteSchemaContextProvider.getSchemaContext());
64 salProvider.getDatastoreAdapter().updateDeviceState(true, netconfSessionPreferences.getModuleBasedCaps());
65 registerDataHandlersToSal(deviceRpc, netconfSessionPreferences);
66 registerRpcsToSal(deviceRpc);
70 public void onDeviceDisconnected() {
71 salProvider.getDatastoreAdapter().updateDeviceState(false, Collections.<QName>emptySet());
74 private void registerRpcsToSal(final RpcImplementation deviceRpc) {
75 final MountProvisionInstance mountInstance = salProvider.getMountInstance();
77 final Map<QName, String> failedRpcs = Maps.newHashMap();
78 for (final RpcDefinition rpcDef : mountInstance.getSchemaContext().getOperations()) {
80 salRegistrations.add(mountInstance.addRpcImplementation(rpcDef.getQName(), deviceRpc));
81 logger.debug("{}: Rpc {} from netconf registered successfully", id, rpcDef.getQName());
82 } catch (final Exception e) {
83 // Only debug per rpc, warn for all of them at the end to pollute log a little less (e.g. routed rpcs)
84 logger.debug("{}: Unable to register rpc {} from netconf device. This rpc will not be available", id,
85 rpcDef.getQName(), e);
86 failedRpcs.put(rpcDef.getQName(), e.getClass() + ":" + e.getMessage());
90 if (failedRpcs.isEmpty() == false) {
91 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs);
95 private void registerDataHandlersToSal(final RpcImplementation deviceRpc,
96 final NetconfSessionCapabilities netconfSessionPreferences) {
97 final NetconfDeviceDataReader dataReader = new NetconfDeviceDataReader(id, deviceRpc);
98 final NetconfDeviceCommitHandler commitHandler = new NetconfDeviceCommitHandler(id, deviceRpc,
99 netconfSessionPreferences.isRollbackSupported());
101 final MountProvisionInstance mountInstance = salProvider.getMountInstance();
102 salRegistrations.add(mountInstance.registerConfigurationReader(ROOT_PATH, dataReader));
103 salRegistrations.add(mountInstance.registerOperationalReader(ROOT_PATH, dataReader));
104 salRegistrations.add(mountInstance.registerCommitHandler(ROOT_PATH, commitHandler));
108 public void close() {
109 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
110 closeGracefully(reg);
112 closeGracefully(salProvider);
115 private void closeGracefully(final AutoCloseable resource) {
116 if (resource != null) {
119 } catch (final Exception e) {
120 logger.warn("{}: Ignoring exception while closing {}", id, resource, e);