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