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 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.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 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionCapabilities> {
33
34     private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
35     private static final InstanceIdentifier ROOT_PATH = InstanceIdentifier.builder().toInstance();
36
37     private final RemoteDeviceId id;
38     private final NetconfDeviceSalProvider salProvider;
39
40     private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
41
42     public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext, final ExecutorService executor) {
43         this.id = id;
44         this.salProvider = new NetconfDeviceSalProvider(id, executor);
45         registerToSal(domBroker, bindingBroker, bundleContext);
46     }
47
48     public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
49         domRegistryDependency.registerProvider(salProvider, bundleContext);
50         bindingBroker.registerProvider(salProvider, bundleContext);
51     }
52
53     @Override
54     public synchronized void onNotification(final CompositeNode domNotification) {
55         salProvider.getMountInstance().publish(domNotification);
56     }
57
58     @Override
59     public synchronized void onDeviceConnected(final SchemaContextProvider remoteSchemaContextProvider,
60                                                final NetconfSessionCapabilities netconfSessionPreferences, final RpcImplementation deviceRpc) {
61         salProvider.getMountInstance().setSchemaContext(remoteSchemaContextProvider.getSchemaContext());
62         salProvider.getDatastoreAdapter().updateDeviceState(true, netconfSessionPreferences.getModuleBasedCaps());
63         registerDataHandlersToSal(deviceRpc, netconfSessionPreferences);
64         registerRpcsToSal(deviceRpc);
65     }
66
67     @Override
68     public void onDeviceDisconnected() {
69         salProvider.getDatastoreAdapter().updateDeviceState(false, Collections.<QName>emptySet());
70     }
71
72     private void registerRpcsToSal(final RpcImplementation deviceRpc) {
73         final MountProvisionInstance mountInstance = salProvider.getMountInstance();
74
75         final Map<QName, String> failedRpcs = Maps.newHashMap();
76         for (final RpcDefinition rpcDef : mountInstance.getSchemaContext().getOperations()) {
77             try {
78                 salRegistrations.add(mountInstance.addRpcImplementation(rpcDef.getQName(), deviceRpc));
79                 logger.debug("{}: Rpc {} from netconf registered successfully", id, rpcDef.getQName());
80             } catch (final Exception e) {
81                 // Only debug per rpc, warn for all of them at the end to pollute log a little less (e.g. routed rpcs)
82                 logger.debug("{}: Unable to register rpc {} from netconf device. This rpc will not be available", id,
83                         rpcDef.getQName(), e);
84                 failedRpcs.put(rpcDef.getQName(), e.getClass() + ":" + e.getMessage());
85             }
86         }
87
88         if (failedRpcs.isEmpty() == false) {
89             if (logger.isDebugEnabled()) {
90                 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs);
91             } else {
92                 logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs.keySet());
93             }
94         }
95     }
96
97     private void registerDataHandlersToSal(final RpcImplementation deviceRpc,
98             final NetconfSessionCapabilities netconfSessionPreferences) {
99         final NetconfDeviceDataReader dataReader = new NetconfDeviceDataReader(id, deviceRpc);
100         final NetconfDeviceCommitHandler commitHandler = new NetconfDeviceCommitHandler(id, deviceRpc,
101                 netconfSessionPreferences.isRollbackSupported());
102
103         final MountProvisionInstance mountInstance = salProvider.getMountInstance();
104         salRegistrations.add(mountInstance.registerConfigurationReader(ROOT_PATH, dataReader));
105         salRegistrations.add(mountInstance.registerOperationalReader(ROOT_PATH, dataReader));
106         salRegistrations.add(mountInstance.registerCommitHandler(ROOT_PATH, commitHandler));
107     }
108
109     @Override
110     public void close() {
111         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
112             closeGracefully(reg);
113         }
114         closeGracefully(salProvider);
115     }
116
117     private void closeGracefully(final AutoCloseable resource) {
118         if (resource != null) {
119             try {
120                 resource.close();
121             } catch (final Exception e) {
122                 logger.warn("{}: Ignoring exception while closing {}", id, resource, e);
123             }
124         }
125     }
126 }