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.md.sal.dom.api.DOMDataBroker;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
19 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCapabilities;
20 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
21 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
22 import org.opendaylight.controller.sal.core.api.Broker;
23 import org.opendaylight.controller.sal.core.api.RpcImplementation;
24 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
25 import org.opendaylight.controller.sal.core.api.notify.NotificationListener;
26 import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService;
27 import org.opendaylight.controller.sal.dom.broker.impl.NotificationRouterImpl;
28 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
29 import org.opendaylight.controller.sal.dom.broker.spi.NotificationRouter;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
36 import org.osgi.framework.BundleContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
42 private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
44 private final RemoteDeviceId id;
45 private final NetconfDeviceSalProvider salProvider;
47 private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
49 public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext, final ExecutorService executor) {
51 this.salProvider = new NetconfDeviceSalProvider(id, executor);
52 registerToSal(domBroker, bindingBroker, bundleContext);
55 public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
56 domRegistryDependency.registerProvider(salProvider, bundleContext);
57 bindingBroker.registerProvider(salProvider, bundleContext);
61 public synchronized void onNotification(final CompositeNode domNotification) {
62 salProvider.getMountInstance().publish(domNotification);
66 public synchronized void onDeviceConnected(final SchemaContext schemaContext,
67 final NetconfSessionPreferences netconfSessionPreferences, final RpcImplementation deviceRpc) {
69 // TODO move SchemaAwareRpcBroker from sal-broker-impl, now we have depend on the whole sal-broker-impl
70 final RpcProvisionRegistry rpcRegistry = new SchemaAwareRpcBroker(id.getPath().toString(), new SchemaContextProvider() {
72 public SchemaContext getSchemaContext() {
76 registerRpcsToSal(schemaContext, rpcRegistry, deviceRpc);
77 final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, deviceRpc, schemaContext, netconfSessionPreferences);
79 // TODO NotificationPublishService and NotificationRouter have the same interface
80 final NotificationPublishService notificationService = new NotificationPublishService() {
82 private final NotificationRouter innerRouter = new NotificationRouterImpl();
85 public void publish(final CompositeNode notification) {
86 innerRouter.publish(notification);
90 public ListenerRegistration<NotificationListener> addNotificationListener(final QName notification, final NotificationListener listener) {
91 return innerRouter.addNotificationListener(notification, listener);
95 salProvider.getMountInstance().onDeviceConnected(schemaContext, domBroker, rpcRegistry, notificationService);
96 salProvider.getDatastoreAdapter().updateDeviceState(true, netconfSessionPreferences.getModuleBasedCaps());
97 salProvider.getMountInstance().onTopologyDeviceConnected(schemaContext, domBroker, rpcRegistry, notificationService);
98 salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
102 public synchronized void onDeviceDisconnected() {
103 salProvider.getDatastoreAdapter().updateDeviceState(false, Collections.<QName>emptySet());
104 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
105 salProvider.getMountInstance().onDeviceDisconnected();
106 salProvider.getMountInstance().onTopologyDeviceDisconnected();
110 public void onDeviceFailed(Throwable throwable) {
111 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
112 salProvider.getMountInstance().onDeviceDisconnected();
113 salProvider.getMountInstance().onTopologyDeviceDisconnected();
116 private void registerRpcsToSal(final SchemaContext schemaContext, final RpcProvisionRegistry rpcRegistry, final RpcImplementation deviceRpc) {
117 final Map<QName, String> failedRpcs = Maps.newHashMap();
118 for (final RpcDefinition rpcDef : schemaContext.getOperations()) {
120 salRegistrations.add(rpcRegistry.addRpcImplementation(rpcDef.getQName(), deviceRpc));
121 logger.debug("{}: Rpc {} from netconf registered successfully", id, rpcDef.getQName());
122 } catch (final Exception e) {
123 // Only debug per rpc, warn for all of them at the end to pollute log a little less (e.g. routed rpcs)
124 logger.debug("{}: Unable to register rpc {} from netconf device. This rpc will not be available", id,
125 rpcDef.getQName(), e);
126 failedRpcs.put(rpcDef.getQName(), e.getClass() + ":" + e.getMessage());
130 if (failedRpcs.isEmpty() == false) {
131 if (logger.isDebugEnabled()) {
132 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs);
134 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs.keySet());
140 public void close() {
141 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
142 closeGracefully(reg);
144 closeGracefully(salProvider);
147 private void closeGracefully(final AutoCloseable resource) {
148 if (resource != null) {
151 } catch (final Exception e) {
152 logger.warn("{}: Ignoring exception while closing {}", id, resource, e);