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