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