Merge "builder class for path creating"
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceSalProvider.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.netconf.sal.connect.netconf.sal;
9
10 import com.google.common.base.Preconditions;
11
12 import java.util.Collection;
13 import java.util.Collections;
14
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
24 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
28 import org.opendaylight.controller.sal.core.api.Broker;
29 import org.opendaylight.controller.sal.core.api.Provider;
30 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
31 import org.opendaylight.yangtools.concepts.ObjectRegistration;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
37
38     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
39
40     private final RemoteDeviceId id;
41     private MountInstance mountInstance;
42
43     private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
44
45     private DataBroker dataBroker;
46     private BindingTransactionChain txChain;
47
48     private final TransactionChainListener transactionChainListener =  new TransactionChainListener() {
49         @Override
50         public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
51             logger.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
52             chain.close();
53             resetTransactionChainForAdapaters();
54             throw new IllegalStateException(id + "  TransactionChain(" + chain + ") not committed correctly", cause);
55         }
56
57         @Override
58         public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
59             logger.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
60         }
61     };
62
63     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId) {
64         this.id = deviceId;
65     }
66
67     public MountInstance getMountInstance() {
68         Preconditions.checkState(mountInstance != null,
69                 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
70         return mountInstance;
71     }
72
73     public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
74         Preconditions.checkState(topologyDatastoreAdapter != null,
75                 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
76         return topologyDatastoreAdapter;
77     }
78
79     @Override
80     public void onSessionInitiated(final Broker.ProviderSession session) {
81         logger.debug("{}: (BI)Session with sal established {}", id, session);
82
83         final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
84         if (mountService != null) {
85             mountInstance = new MountInstance(mountService, id);
86         }
87     }
88
89     @Override
90     public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
91         return Collections.emptySet();
92     }
93
94     @Override
95     public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
96         logger.debug("{}: Session with sal established {}", id, session);
97
98         this.dataBroker = session.getSALService(DataBroker.class);
99         txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
100
101         topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
102     }
103
104     private void resetTransactionChainForAdapaters() {
105         txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
106
107         topologyDatastoreAdapter.setTxChain(txChain);
108
109         logger.trace("{}: Resetting TransactionChain {}", id, txChain);
110
111     }
112
113     public void close() throws Exception {
114         mountInstance.close();
115         topologyDatastoreAdapter.close();
116         topologyDatastoreAdapter = null;
117         txChain.close();
118     }
119
120     public static final class MountInstance implements AutoCloseable {
121
122         private DOMMountPointService mountService;
123         private final RemoteDeviceId id;
124         private NetconfDeviceNotificationService notificationService;
125
126         private ObjectRegistration<DOMMountPoint> topologyRegistration;
127
128         public MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
129             this.mountService = Preconditions.checkNotNull(mountService);
130             this.id = Preconditions.checkNotNull(id);
131         }
132
133         public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
134                                                     final DOMDataBroker broker, final DOMRpcService rpc,
135                                                     final NetconfDeviceNotificationService notificationService) {
136
137             Preconditions.checkNotNull(mountService, "Closed");
138             Preconditions.checkState(topologyRegistration == null, "Already initialized");
139
140             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
141             mountBuilder.addInitialSchemaContext(initialCtx);
142
143             mountBuilder.addService(DOMDataBroker.class, broker);
144             mountBuilder.addService(DOMRpcService.class, rpc);
145             mountBuilder.addService(DOMNotificationService.class, notificationService);
146             this.notificationService = notificationService;
147
148             topologyRegistration = mountBuilder.register();
149             logger.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
150
151         }
152
153         public synchronized void onTopologyDeviceDisconnected() {
154             if(topologyRegistration == null) {
155                 logger.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
156                 return;
157             }
158
159             try {
160                 topologyRegistration.close();
161             } catch (final Exception e) {
162                 // Only log and ignore
163                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
164             } finally {
165                 logger.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
166                 topologyRegistration = null;
167             }
168         }
169
170         @Override
171         public synchronized void close() throws Exception {
172             onTopologyDeviceDisconnected();
173             mountService = null;
174         }
175
176         public synchronized void publish(final DOMNotification domNotification) {
177             Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
178             notificationService.publishNotification(domNotification);
179         }
180     }
181
182 }