2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.persist.impl;
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;
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;
51 import java.util.regex.Pattern;
54 * Responsible for listening for notifications from netconf containing latest
55 * committed configuration that should be persisted, and also for loading last
59 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
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;
65 private final InetSocketAddress address;
66 private final EventLoopGroup nettyThreadgroup;
68 private NetconfClientDispatcher netconfClientDispatcher;
69 private NetconfClient netconfClient;
71 private final Persister persister;
72 private final MBeanServerConnection mbeanServer;
75 private final ObjectName on = DefaultCommitOperationMXBean.objectName;
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;
81 public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
82 MBeanServerConnection mbeanServer, Pattern ignoredMissingCapabilityRegex) {
83 this(persister, address, mbeanServer, DEFAULT_TIMEOUT, ignoredMissingCapabilityRegex);
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;
94 this.nettyThreadgroup = new NioEventLoopGroup();
95 this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
98 public void init() throws InterruptedException {
99 Optional<ConfigSnapshotHolder> maybeConfig = loadLastConfig();
101 if (maybeConfig.isPresent()) {
102 logger.debug("Last config found {}", persister);
103 ConflictingVersionException lastException = null;
104 pushLastConfigWithRetries(maybeConfig, lastException);
107 // this ensures that netconf is initialized, this is first
109 // this means we can register as listener for commit
110 registerToNetconf(Collections.<String>emptySet());
112 logger.info("No last config provided by backend storage {}", persister);
114 registerAsJMXListener();
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());
122 final String configSnapshot = maybeConfig.get().getConfigSnapshot();
123 logger.trace("Pushing following xml to netconf {}", configSnapshot);
125 pushLastConfig(XmlUtil.readXmlToElement(configSnapshot));
127 } catch(ConflictingVersionException e) {
128 closeClientAndDispatcher(netconfClient, netconfClientDispatcher);
131 } catch (SAXException | IOException e) {
132 throw new IllegalStateException("Unable to load last config", e);
135 throw new IllegalStateException("Failed to push configuration, maximum attempt count has been reached: "
136 + maxAttempts, lastException);
139 private synchronized long registerToNetconf(Set<String> expectedCaps) throws InterruptedException {
141 Set<String> currentCapabilities = Sets.newHashSet();
143 // TODO think about moving capability subset check to netconf client
144 // could be utilized by integration tests
146 long pollingStart = System.currentTimeMillis();
151 long deadline = pollingStart + timeout;
152 while (System.currentTimeMillis() < deadline) {
154 netconfClientDispatcher = new NetconfClientDispatcher(Optional.<SSLContext>absent(), nettyThreadgroup, nettyThreadgroup);
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();
163 currentCapabilities = netconfClient.getCapabilities();
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;
174 logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, currentCapabilities);
176 closeClientAndDispatcher(netconfClient, netconfClientDispatcher);
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);
188 private static void closeClientAndDispatcher(Closeable client, Closeable dispatcher) {
189 Exception fromClient = null;
192 } catch (Exception e) {
197 } catch (Exception e) {
198 if (fromClient != null) {
199 e.addSuppressed(fromClient);
202 throw new RuntimeException("Error closing temporary client ", e);
207 private boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
208 for (String exCap : expectedCaps) {
209 if (currentCapabilities.contains(exCap) == false)
215 private void registerAsJMXListener() {
216 logger.trace("Called registerAsJMXListener");
218 mbeanServer.addNotificationListener(on, this, null, null);
219 } catch (InstanceNotFoundException | IOException e) {
220 throw new RuntimeException("Cannot register as JMX listener to netconf", e);
225 public void handleNotification(Notification notification, Object handback) {
226 if (notification instanceof NetconfJMXNotification == false)
229 // Socket should not be closed at this point
230 // Activator unregisters this as JMX listener before close is called
232 logger.debug("Received notification {}", notification);
233 if (notification instanceof CommitJMXNotification) {
235 handleAfterCommitNotification((CommitJMXNotification) notification);
236 } catch (Exception e) {
237 // TODO: notificationBroadcast support logs only DEBUG
238 logger.warn("Exception occured during notification handling: ", e);
242 throw new IllegalStateException("Unknown config registry notification type " + notification);
245 private void handleAfterCommitNotification(final CommitJMXNotification notification) {
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);
255 private Optional<ConfigSnapshotHolder> loadLastConfig() {
256 Optional<ConfigSnapshotHolder> maybeConfigElement;
258 maybeConfigElement = persister.loadLastConfig();
259 } catch (IOException e) {
260 throw new RuntimeException("Unable to load configuration", e);
262 return maybeConfigElement;
265 private synchronized void pushLastConfig(Element xmlToBePersisted) throws ConflictingVersionException {
266 logger.info("Pushing last configuration to netconf");
267 StringBuilder response = new StringBuilder("editConfig response = {");
270 NetconfMessage message = createEditConfigMessage(xmlToBePersisted, "/netconfOp/editConfig.xml");
272 // sending message to netconf
273 NetconfMessage responseMessage = netconfClient.sendMessage(message, NETCONF_SEND_ATTEMPTS, NETCONF_SEND_ATTEMPT_MS_DELAY);
275 XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
276 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
277 element = element.getOnlyChildElement();
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);
284 element = XmlElement.fromDomDocument(responseMessage.getDocument());
285 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
286 element = element.getOnlyChildElement();
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);
296 static void checkIsOk(XmlElement element, NetconfMessage responseMessage) throws ConflictingVersionException {
297 if (element.getName().equals(XmlNetconfConstants.OK)) {
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);
309 throw new IllegalStateException("Can not load last configuration, operation failed: "
310 + XmlUtil.toString(responseMessage.getDocument()));
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()));
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);
322 Document doc = XmlUtil.readXmlToDocument(stream);
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));
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);
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);
348 public synchronized void close() {
349 // TODO persister is received from constructor, should not be closed
353 } catch (Exception e) {
354 logger.warn("Unable to close config persister {}", persister, e);
357 if (netconfClient != null) {
359 netconfClient.close();
360 } catch (Exception e) {
361 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
365 if (netconfClientDispatcher != null) {
367 netconfClientDispatcher.close();
368 } catch (Exception e) {
369 logger.warn("Unable to close connection to netconf {}", netconfClientDispatcher, e);
374 nettyThreadgroup.shutdownGracefully();
375 } catch (Exception e) {
376 logger.warn("Unable to close netconf client thread group {}", netconfClientDispatcher, e);
379 // unregister from JMX
381 if (mbeanServer.isRegistered(on)) {
382 mbeanServer.removeNotificationListener(on, this);
384 } catch (Exception e) {
385 logger.warn("Unable to unregister {} as listener for {}", this, on, e);