f39c4d579212984ba0e3d4d254e49d8110e31d42
[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.Maps;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Map;
14
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;
31
32 import com.google.common.collect.Lists;
33
34 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionCapabilities> {
35
36     private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceTwoPhaseCommitTransaction.class);
37     private static final InstanceIdentifier ROOT_PATH = InstanceIdentifier.builder().toInstance();
38
39     private final RemoteDeviceId id;
40     private final NetconfDeviceSalProvider salProvider;
41
42     private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
43
44     public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext, final ExecutorService executor) {
45         this.id = id;
46         this.salProvider = new NetconfDeviceSalProvider(id, executor);
47         registerToSal(domBroker, bindingBroker, bundleContext);
48     }
49
50     public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
51         domRegistryDependency.registerProvider(salProvider, bundleContext);
52         bindingBroker.registerProvider(salProvider, bundleContext);
53     }
54
55     @Override
56     public synchronized void onNotification(final CompositeNode domNotification) {
57         salProvider.getMountInstance().publish(domNotification);
58     }
59
60     @Override
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);
67     }
68
69     @Override
70     public void onDeviceDisconnected() {
71         salProvider.getDatastoreAdapter().updateDeviceState(false, Collections.<QName>emptySet());
72     }
73
74     private void registerRpcsToSal(final RpcImplementation deviceRpc) {
75         final MountProvisionInstance mountInstance = salProvider.getMountInstance();
76
77         final Map<QName, String> failedRpcs = Maps.newHashMap();
78         for (final RpcDefinition rpcDef : mountInstance.getSchemaContext().getOperations()) {
79             try {
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());
87             }
88         }
89
90         if (failedRpcs.isEmpty() == false) {
91             logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs);
92         }
93     }
94
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());
100
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));
105     }
106
107     @Override
108     public void close() {
109         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
110             closeGracefully(reg);
111         }
112         closeGracefully(salProvider);
113     }
114
115     private void closeGracefully(final AutoCloseable resource) {
116         if (resource != null) {
117             try {
118                 resource.close();
119             } catch (final Exception e) {
120                 logger.warn("{}: Ignoring exception while closing {}", id, resource, e);
121             }
122         }
123     }
124 }