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