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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.