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