Dispose HashedWheelTimer instances properly in netconf client and server
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / ConfigPersisterNotificationHandler.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.persist.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Sets;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import org.opendaylight.controller.config.persist.api.Persister;
17 import org.opendaylight.controller.netconf.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
19 import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
20 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
21 import org.opendaylight.controller.netconf.client.NetconfClient;
22 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
23 import org.opendaylight.controller.netconf.util.xml.XmlElement;
24 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
25 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 import org.xml.sax.SAXException;
31
32 import javax.annotation.concurrent.ThreadSafe;
33 import javax.management.InstanceNotFoundException;
34 import javax.management.MBeanServerConnection;
35 import javax.management.Notification;
36 import javax.management.NotificationListener;
37 import javax.management.ObjectName;
38 import javax.net.ssl.SSLContext;
39 import java.io.Closeable;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.net.InetSocketAddress;
43 import java.util.Collections;
44 import java.util.Set;
45
46 /**
47  * Responsible for listening for notifications from netconf containing latest
48  * committed configuration that should be persisted, and also for loading last
49  * configuration.
50  */
51 @ThreadSafe
52 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
53
54     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
55
56     private final InetSocketAddress address;
57     private final EventLoopGroup nettyThreadgroup;
58
59     private NetconfClientDispatcher netconfClientDispatcher;
60     private NetconfClient netconfClient;
61
62     private final Persister persister;
63     private final MBeanServerConnection mbeanServer;
64     private Long currentSessionId;
65
66     private final ObjectName on = DefaultCommitOperationMXBean.objectName;
67
68     public static final long DEFAULT_TIMEOUT = 40000L;
69     private final long timeout;
70
71     public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
72             MBeanServerConnection mbeanServer) {
73         this(persister, address, mbeanServer, DEFAULT_TIMEOUT);
74     }
75
76     public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
77             MBeanServerConnection mbeanServer, long timeout) {
78         this.persister = persister;
79         this.address = address;
80         this.mbeanServer = mbeanServer;
81         this.timeout = timeout;
82
83         this.nettyThreadgroup = new NioEventLoopGroup();
84     }
85
86     public void init() throws InterruptedException {
87         Optional<Persister.ConfigSnapshotHolder> maybeConfig = loadLastConfig();
88
89         if (maybeConfig.isPresent()) {
90             logger.debug("Last config found {}", persister);
91
92             registerToNetconf(maybeConfig.get().getCapabilities());
93
94             final String configSnapshot = maybeConfig.get().getConfigSnapshot();
95             try {
96                 pushLastConfig(XmlUtil.readXmlToElement(configSnapshot));
97             } catch (SAXException | IOException e) {
98                 throw new IllegalStateException("Unable to load last config", e);
99             }
100
101         } else {
102             // this ensures that netconf is initialized, this is first
103             // connection
104             // this means we can register as listener for commit
105             registerToNetconf(Collections.<String>emptySet());
106
107             logger.info("No last config provided by backend storage {}", persister);
108         }
109         registerAsJMXListener();
110     }
111
112     private synchronized long registerToNetconf(Set<String> expectedCaps) throws InterruptedException {
113
114         Set<String> currentCapabilities = Sets.newHashSet();
115
116         // TODO think about moving capability subset check to netconf client
117         // could be utilized by integration tests
118
119         long pollingStart = System.currentTimeMillis();
120         int delay = 5000;
121
122         int attempt = 0;
123
124         while (true) {
125             attempt++;
126
127             netconfClientDispatcher = new NetconfClientDispatcher(Optional.<SSLContext>absent(), nettyThreadgroup, nettyThreadgroup);
128             try {
129                 netconfClient = new NetconfClient(this.toString(), address, delay, netconfClientDispatcher);
130             } catch (IllegalStateException e) {
131                 logger.debug("Netconf {} was not initialized or is not stable, attempt {}", address, attempt, e);
132                 netconfClientDispatcher.close();
133                 Thread.sleep(delay);
134                 continue;
135             }
136             currentCapabilities = netconfClient.getCapabilities();
137
138             if (isSubset(currentCapabilities, expectedCaps)) {
139                 logger.debug("Hello from netconf stable with {} capabilities", currentCapabilities);
140                 currentSessionId = netconfClient.getSessionId();
141                 logger.info("Session id received from netconf server: {}", currentSessionId);
142                 return currentSessionId;
143             }
144
145             if (System.currentTimeMillis() > pollingStart + timeout) {
146                 break;
147             }
148
149             logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, currentCapabilities);
150
151             closeClientAndDispatcher(netconfClient, netconfClientDispatcher);
152
153             Thread.sleep(delay);
154         }
155
156         throw new RuntimeException("Netconf server did not provide required capabilities " + expectedCaps
157                 + " in time, provided capabilities " + currentCapabilities);
158
159     }
160
161     private static void closeClientAndDispatcher(Closeable client, Closeable dispatcher) {
162         Exception fromClient = null;
163         try {
164             client.close();
165         } catch (Exception e) {
166             fromClient = e;
167         } finally {
168             try {
169                 dispatcher.close();
170             } catch (Exception e) {
171                 if (fromClient != null) {
172                     e.addSuppressed(fromClient);
173                 }
174
175                 throw new RuntimeException("Error closing temporary client ", e);
176             }
177         }
178     }
179
180     private boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
181         for (String exCap : expectedCaps) {
182             if (currentCapabilities.contains(exCap) == false)
183                 return false;
184         }
185         return true;
186     }
187
188     private void registerAsJMXListener() {
189         try {
190             mbeanServer.addNotificationListener(on, this, null, null);
191         } catch (InstanceNotFoundException | IOException e) {
192             throw new RuntimeException("Cannot register as JMX listener to netconf", e);
193         }
194     }
195
196     @Override
197     public void handleNotification(Notification notification, Object handback) {
198         if (notification instanceof NetconfJMXNotification == false)
199             return;
200
201         // Socket should not be closed at this point
202         // Activator unregisters this as JMX listener before close is called
203
204         logger.debug("Received notification {}", notification);
205         if (notification instanceof CommitJMXNotification) {
206             try {
207                 handleAfterCommitNotification((CommitJMXNotification) notification);
208             } catch (Exception e) {
209                 // TODO: notificationBroadcast support logs only DEBUG
210                 logger.warn("Exception occured during notification handling: ", e);
211                 throw e;
212             }
213         } else
214             throw new IllegalStateException("Unknown config registry notification type " + notification);
215     }
216
217     private void handleAfterCommitNotification(final CommitJMXNotification notification) {
218         try {
219             final XmlElement configElement = XmlElement.fromDomElement(notification.getConfigSnapshot());
220             persister.persistConfig(new Persister.ConfigSnapshotHolder() {
221                 @Override
222                 public String getConfigSnapshot() {
223                     return XmlUtil.toString(configElement.getDomElement());
224                 }
225
226                 @Override
227                 public Set<String> getCapabilities() {
228                     return notification.getCapabilities();
229                 }
230             });
231             logger.debug("Configuration persisted successfully");
232         } catch (IOException e) {
233             throw new RuntimeException("Unable to persist configuration snapshot", e);
234         }
235     }
236
237     private Optional<Persister.ConfigSnapshotHolder> loadLastConfig() {
238         Optional<Persister.ConfigSnapshotHolder> maybeConfigElement;
239         try {
240             maybeConfigElement = persister.loadLastConfig();
241         } catch (IOException e) {
242             throw new RuntimeException("Unable to load configuration", e);
243         }
244         return maybeConfigElement;
245     }
246
247     private synchronized void pushLastConfig(Element persistedConfig) {
248         StringBuilder response = new StringBuilder("editConfig response = {");
249
250         Element configElement = persistedConfig;
251         NetconfMessage message = createEditConfigMessage(configElement, "/netconfOp/editConfig.xml");
252         NetconfMessage responseMessage = netconfClient.sendMessage(message);
253
254         XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
255         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
256         element = element.getOnlyChildElement();
257
258         checkIsOk(element, responseMessage);
259         response.append(XmlUtil.toString(responseMessage.getDocument()));
260         response.append("}");
261         responseMessage = netconfClient.sendMessage(getNetconfMessageFromResource("/netconfOp/commit.xml"));
262
263         element = XmlElement.fromDomDocument(responseMessage.getDocument());
264         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
265         element = element.getOnlyChildElement();
266
267         checkIsOk(element, responseMessage);
268         response.append("commit response = {");
269         response.append(XmlUtil.toString(responseMessage.getDocument()));
270         response.append("}");
271         logger.debug("Last configuration loaded successfully");
272     }
273
274     private void checkIsOk(XmlElement element, NetconfMessage responseMessage) {
275         if (element.getName().equals(XmlNetconfConstants.OK)) {
276             return;
277         } else {
278             if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
279                 logger.warn("Can not load last configuration, operation failed");
280                 throw new IllegalStateException("Can not load last configuration, operation failed: "
281                         + XmlUtil.toString(responseMessage.getDocument()));
282             }
283             logger.warn("Can not load last configuration. Operation failed.");
284             throw new IllegalStateException("Can not load last configuration. Operation failed: "
285                     + XmlUtil.toString(responseMessage.getDocument()));
286         }
287     }
288
289     private NetconfMessage createEditConfigMessage(Element dataElement, String editConfigResourcename) {
290         try (InputStream stream = getClass().getResourceAsStream(editConfigResourcename)) {
291             Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcename);
292
293             Document doc = XmlUtil.readXmlToDocument(stream);
294
295             doc.getDocumentElement();
296             XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
297             XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
298             editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
299             for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
300                 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), true));
301             }
302             editConfigElement.appendChild(configWrapper.getDomElement());
303             return new NetconfMessage(doc);
304         } catch (IOException | SAXException e) {
305             throw new RuntimeException("Unable to parse message from resources " + editConfigResourcename, e);
306         }
307     }
308
309     private NetconfMessage getNetconfMessageFromResource(String resource) {
310         try (InputStream stream = getClass().getResourceAsStream(resource)) {
311             Preconditions.checkNotNull(stream, "Unable to load resource " + resource);
312             return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
313         } catch (SAXException | IOException e) {
314             throw new RuntimeException("Unable to parse message from resources " + resource, e);
315         }
316     }
317
318     @Override
319     public synchronized void close() {
320         // TODO persister is received from constructor, should not be closed
321         // here
322         try {
323             persister.close();
324         } catch (Exception e) {
325             logger.warn("Unable to close config persister {}", persister, e);
326         }
327
328         if (netconfClient != null) {
329             try {
330                 netconfClient.close();
331             } catch (Exception e) {
332                 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
333             }
334         }
335
336         if (netconfClientDispatcher != null) {
337             try {
338                 netconfClientDispatcher.close();
339             } catch (Exception e) {
340                 logger.warn("Unable to close connection to netconf {}", netconfClientDispatcher, e);
341             }
342         }
343
344         try {
345             nettyThreadgroup.shutdownGracefully();
346         } catch (Exception e) {
347             logger.warn("Unable to close netconf client thread group {}", netconfClientDispatcher, e);
348         }
349
350         // unregister from JMX
351         try {
352             if (mbeanServer.isRegistered(on)) {
353                 mbeanServer.removeNotificationListener(on, this);
354             }
355         } catch (Exception e) {
356             logger.warn("Unable to unregister {} as listener for {}", this, on, e);
357         }
358     }
359 }