Bug 2669: Use slf4j Logger instead of akka LoggingAdapter
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceSalFacade.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.connect.netconf.sal;
9
10 import com.google.common.collect.Lists;
11 import com.google.common.collect.Maps;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Map;
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.NetconfSessionCapabilities;
20 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
21 import org.opendaylight.controller.sal.core.api.Broker;
22 import org.opendaylight.controller.sal.core.api.RpcImplementation;
23 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
24 import org.opendaylight.controller.sal.core.api.notify.NotificationListener;
25 import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService;
26 import org.opendaylight.controller.sal.dom.broker.impl.NotificationRouterImpl;
27 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
28 import org.opendaylight.controller.sal.dom.broker.spi.NotificationRouter;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
32 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
35 import org.osgi.framework.BundleContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionCapabilities> {
40
41     private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
42
43     private final RemoteDeviceId id;
44     private final NetconfDeviceSalProvider salProvider;
45
46     private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
47
48     public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext, final ExecutorService executor) {
49         this.id = id;
50         this.salProvider = new NetconfDeviceSalProvider(id, executor);
51         registerToSal(domBroker, bindingBroker, bundleContext);
52     }
53
54     public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
55         domRegistryDependency.registerProvider(salProvider, bundleContext);
56         bindingBroker.registerProvider(salProvider, bundleContext);
57     }
58
59     @Override
60     public synchronized void onNotification(final CompositeNode domNotification) {
61         salProvider.getMountInstance().publish(domNotification);
62     }
63
64     @Override
65     public synchronized void onDeviceConnected(final SchemaContext schemaContext,
66                                                final NetconfSessionCapabilities netconfSessionPreferences, final RpcImplementation deviceRpc) {
67
68         // TODO move SchemaAwareRpcBroker from sal-broker-impl, now we have depend on the whole sal-broker-impl
69         final RpcProvisionRegistry rpcRegistry = new SchemaAwareRpcBroker(id.getPath().toString(), new SchemaContextProvider() {
70             @Override
71             public SchemaContext getSchemaContext() {
72                 return schemaContext;
73             }
74         });
75         registerRpcsToSal(schemaContext, rpcRegistry, deviceRpc);
76         final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, deviceRpc, schemaContext, netconfSessionPreferences);
77
78         // TODO NotificationPublishService and NotificationRouter have the same interface
79         final NotificationPublishService notificationService = new NotificationPublishService() {
80
81             private final NotificationRouter innerRouter = new NotificationRouterImpl();
82
83             @Override
84             public void publish(final CompositeNode notification) {
85                 innerRouter.publish(notification);
86             }
87
88             @Override
89             public ListenerRegistration<NotificationListener> addNotificationListener(final QName notification, final NotificationListener listener) {
90                 return innerRouter.addNotificationListener(notification, listener);
91             }
92         };
93
94         salProvider.getMountInstance().onDeviceConnected(schemaContext, domBroker, rpcRegistry, notificationService);
95         salProvider.getDatastoreAdapter().updateDeviceState(true, netconfSessionPreferences.getModuleBasedCaps());
96     }
97
98     @Override
99     public synchronized void onDeviceDisconnected() {
100         salProvider.getDatastoreAdapter().updateDeviceState(false, Collections.<QName>emptySet());
101         salProvider.getMountInstance().onDeviceDisconnected();
102     }
103
104     private void registerRpcsToSal(final SchemaContext schemaContext, final RpcProvisionRegistry rpcRegistry, final RpcImplementation deviceRpc) {
105         final Map<QName, String> failedRpcs = Maps.newHashMap();
106         for (final RpcDefinition rpcDef : schemaContext.getOperations()) {
107             try {
108                 salRegistrations.add(rpcRegistry.addRpcImplementation(rpcDef.getQName(), deviceRpc));
109                 logger.debug("{}: Rpc {} from netconf registered successfully", id, rpcDef.getQName());
110             } catch (final Exception e) {
111                 // Only debug per rpc, warn for all of them at the end to pollute log a little less (e.g. routed rpcs)
112                 logger.debug("{}: Unable to register rpc {} from netconf device. This rpc will not be available", id,
113                         rpcDef.getQName(), e);
114                 failedRpcs.put(rpcDef.getQName(), e.getClass() + ":" + e.getMessage());
115             }
116         }
117
118         if (failedRpcs.isEmpty() == false) {
119             if (logger.isDebugEnabled()) {
120                 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs);
121             } else {
122                 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs.keySet());
123             }
124         }
125     }
126
127     @Override
128     public void close() {
129         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
130             closeGracefully(reg);
131         }
132         closeGracefully(salProvider);
133     }
134
135     private void closeGracefully(final AutoCloseable resource) {
136         if (resource != null) {
137             try {
138                 resource.close();
139             } catch (final Exception e) {
140                 logger.warn("{}: Ignoring exception while closing {}", id, resource, e);
141             }
142         }
143     }
144 }