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