Merge "Cleanup root pom "name"."
[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.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;
39
40 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
41
42     private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
43
44     private final RemoteDeviceId id;
45     private final NetconfDeviceSalProvider salProvider;
46
47     private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
48
49     public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext, final ExecutorService executor) {
50         this.id = id;
51         this.salProvider = new NetconfDeviceSalProvider(id, executor);
52         registerToSal(domBroker, bindingBroker, bundleContext);
53     }
54
55     public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
56         domRegistryDependency.registerProvider(salProvider, bundleContext);
57         bindingBroker.registerProvider(salProvider, bundleContext);
58     }
59
60     @Override
61     public synchronized void onNotification(final CompositeNode domNotification) {
62         salProvider.getMountInstance().publish(domNotification);
63     }
64
65     @Override
66     public synchronized void onDeviceConnected(final SchemaContext schemaContext,
67                                                final NetconfSessionPreferences netconfSessionPreferences, final RpcImplementation deviceRpc) {
68
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() {
71             @Override
72             public SchemaContext getSchemaContext() {
73                 return schemaContext;
74             }
75         });
76         registerRpcsToSal(schemaContext, rpcRegistry, deviceRpc);
77         final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, deviceRpc, schemaContext, netconfSessionPreferences);
78
79         // TODO NotificationPublishService and NotificationRouter have the same interface
80         final NotificationPublishService notificationService = new NotificationPublishService() {
81
82             private final NotificationRouter innerRouter = new NotificationRouterImpl();
83
84             @Override
85             public void publish(final CompositeNode notification) {
86                 innerRouter.publish(notification);
87             }
88
89             @Override
90             public ListenerRegistration<NotificationListener> addNotificationListener(final QName notification, final NotificationListener listener) {
91                 return innerRouter.addNotificationListener(notification, listener);
92             }
93         };
94
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());
99     }
100
101     @Override
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();
107     }
108
109     @Override
110     public void onDeviceFailed(Throwable throwable) {
111         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
112         salProvider.getMountInstance().onDeviceDisconnected();
113         salProvider.getMountInstance().onTopologyDeviceDisconnected();
114     }
115
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()) {
119             try {
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());
127             }
128         }
129
130         if (failedRpcs.isEmpty() == false) {
131             if (logger.isDebugEnabled()) {
132                 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs);
133             } else {
134                 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs.keySet());
135             }
136         }
137     }
138
139     @Override
140     public void close() {
141         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
142             closeGracefully(reg);
143         }
144         closeGracefully(salProvider);
145     }
146
147     private void closeGracefully(final AutoCloseable resource) {
148         if (resource != null) {
149             try {
150                 resource.close();
151             } catch (final Exception e) {
152                 logger.warn("{}: Ignoring exception while closing {}", id, resource, e);
153             }
154         }
155     }
156 }