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 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;
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;
52 * Responsible for listening for notifications from netconf containing latest
53 * committed configuration that should be persisted, and also for loading last
57 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
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;
63 private final InetSocketAddress address;
64 private final EventLoopGroup nettyThreadgroup;
66 private NetconfClientDispatcher netconfClientDispatcher;
67 private NetconfClient netconfClient;
69 private final Persister persister;
70 private final MBeanServerConnection mbeanServer;
73 private final ObjectName on = DefaultCommitOperationMXBean.objectName;
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;
79 public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
80 MBeanServerConnection mbeanServer, Pattern ignoredMissingCapabilityRegex) {
81 this(persister, address, mbeanServer, DEFAULT_TIMEOUT, ignoredMissingCapabilityRegex);
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;
92 this.nettyThreadgroup = new NioEventLoopGroup();
93 this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
96 public void init() throws InterruptedException {
97 Optional<ConfigSnapshotHolder> maybeConfig = loadLastConfig();
99 if (maybeConfig.isPresent()) {
100 logger.debug("Last config found {}", persister);
101 ConflictingVersionException lastException = null;
102 pushLastConfigWithRetries(maybeConfig, lastException);
105 // this ensures that netconf is initialized, this is first
107 // this means we can register as listener for commit
108 registerToNetconf(Collections.<String>emptySet());
110 logger.info("No last config provided by backend storage {}", persister);
112 registerAsJMXListener();
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());
120 final String configSnapshot = maybeConfig.get().getConfigSnapshot();
121 logger.trace("Pushing following xml to netconf {}", configSnapshot);
123 pushLastConfig(XmlUtil.readXmlToElement(configSnapshot));
125 } catch(ConflictingVersionException e) {
126 closeClientAndDispatcher(netconfClient, netconfClientDispatcher);
129 } catch (SAXException | IOException e) {
130 throw new IllegalStateException("Unable to load last config", e);
133 throw new IllegalStateException("Failed to push configuration, maximum attempt count has been reached: "
134 + maxAttempts, lastException);
137 private synchronized long registerToNetconf(Set<String> expectedCaps) throws InterruptedException {
139 Set<String> currentCapabilities = Sets.newHashSet();
141 // TODO think about moving capability subset check to netconf client
142 // could be utilized by integration tests
144 long pollingStart = System.currentTimeMillis();
149 long deadline = pollingStart + timeout;
150 while (System.currentTimeMillis() < deadline) {
152 netconfClientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup);
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();
161 currentCapabilities = netconfClient.getCapabilities();
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;
172 logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, currentCapabilities);
174 closeClientAndDispatcher(netconfClient, netconfClientDispatcher);
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);
186 private static void closeClientAndDispatcher(Closeable client, Closeable dispatcher) {
187 Exception fromClient = null;
190 } catch (Exception e) {
195 } catch (Exception e) {
196 if (fromClient != null) {
197 e.addSuppressed(fromClient);
200 throw new RuntimeException("Error closing temporary client ", e);
205 private boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
206 for (String exCap : expectedCaps) {
207 if (currentCapabilities.contains(exCap) == false)
213 private void registerAsJMXListener() {
214 logger.trace("Called registerAsJMXListener");
216 mbeanServer.addNotificationListener(on, this, null, null);
217 } catch (InstanceNotFoundException | IOException e) {
218 throw new RuntimeException("Cannot register as JMX listener to netconf", e);
223 public void handleNotification(Notification notification, Object handback) {
224 if (notification instanceof NetconfJMXNotification == false)
227 // Socket should not be closed at this point
228 // Activator unregisters this as JMX listener before close is called
230 logger.info("Received notification {}", notification);
231 if (notification instanceof CommitJMXNotification) {
233 handleAfterCommitNotification((CommitJMXNotification) notification);
234 } catch (Exception e) {
235 // TODO: notificationBroadcast support logs only DEBUG
236 logger.warn("Exception occured during notification handling: ", e);
240 throw new IllegalStateException("Unknown config registry notification type " + notification);
243 private void handleAfterCommitNotification(final CommitJMXNotification notification) {
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);
253 private Optional<ConfigSnapshotHolder> loadLastConfig() {
254 Optional<ConfigSnapshotHolder> maybeConfigElement;
256 maybeConfigElement = persister.loadLastConfig();
257 } catch (IOException e) {
258 throw new RuntimeException("Unable to load configuration", e);
260 return maybeConfigElement;
263 private synchronized void pushLastConfig(Element xmlToBePersisted) throws ConflictingVersionException {
264 logger.info("Pushing last configuration to netconf");
265 StringBuilder response = new StringBuilder("editConfig response = {");
268 NetconfMessage message = createEditConfigMessage(xmlToBePersisted, "/netconfOp/editConfig.xml");
270 // sending message to netconf
271 NetconfMessage responseMessage = netconfClient.sendMessage(message, NETCONF_SEND_ATTEMPTS, NETCONF_SEND_ATTEMPT_MS_DELAY);
273 XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
274 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
275 element = element.getOnlyChildElement();
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);
282 element = XmlElement.fromDomDocument(responseMessage.getDocument());
283 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
284 element = element.getOnlyChildElement();
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);
294 static void checkIsOk(XmlElement element, NetconfMessage responseMessage) throws ConflictingVersionException {
295 if (element.getName().equals(XmlNetconfConstants.OK)) {
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);
307 throw new IllegalStateException("Can not load last configuration, operation failed: "
308 + XmlUtil.toString(responseMessage.getDocument()));
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()));
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);
320 Document doc = XmlUtil.readXmlToDocument(stream);
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));
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);
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);
346 public synchronized void close() {
347 // TODO persister is received from constructor, should not be closed
351 } catch (Exception e) {
352 logger.warn("Unable to close config persister {}", persister, e);
355 if (netconfClient != null) {
357 netconfClient.close();
358 } catch (Exception e) {
359 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
363 if (netconfClientDispatcher != null) {
365 netconfClientDispatcher.close();
366 } catch (Exception e) {
367 logger.warn("Unable to close connection to netconf {}", netconfClientDispatcher, e);
372 nettyThreadgroup.shutdownGracefully();
373 } catch (Exception e) {
374 logger.warn("Unable to close netconf client thread group {}", netconfClientDispatcher, e);
377 // unregister from JMX
379 if (mbeanServer.isRegistered(on)) {
380 mbeanServer.removeNotificationListener(on, this);
382 } catch (Exception e) {
383 logger.warn("Unable to unregister {} as listener for {}", this, on, e);