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.persist.api.ConfigSnapshotHolder;
17 import org.opendaylight.controller.config.persist.api.Persister;
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
20 import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
21 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
22 import org.opendaylight.controller.netconf.client.NetconfClient;
23 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
24 import org.opendaylight.controller.netconf.util.xml.XmlElement;
25 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
26 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.xml.sax.SAXException;
33 import javax.annotation.concurrent.ThreadSafe;
34 import javax.management.InstanceNotFoundException;
35 import javax.management.MBeanServerConnection;
36 import javax.management.Notification;
37 import javax.management.NotificationListener;
38 import javax.management.ObjectName;
39 import javax.net.ssl.SSLContext;
40 import java.io.Closeable;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.net.InetSocketAddress;
44 import java.util.Collections;
45 import java.util.HashSet;
47 import java.util.regex.Pattern;
50 * Responsible for listening for notifications from netconf containing latest
51 * committed configuration that should be persisted, and also for loading last
55 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
57 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
58 private static final int NETCONF_SEND_ATTEMPT_MS_DELAY = 1000;
59 private static final int NETCONF_SEND_ATTEMPTS = 20;
61 private final InetSocketAddress address;
62 private final EventLoopGroup nettyThreadgroup;
64 private NetconfClientDispatcher netconfClientDispatcher;
65 private NetconfClient netconfClient;
67 private final Persister persister;
68 private final MBeanServerConnection mbeanServer;
69 private Long currentSessionId;
71 private final ObjectName on = DefaultCommitOperationMXBean.objectName;
73 public static final long DEFAULT_TIMEOUT = 120000L;// 120 seconds until netconf must be stable
74 private final long timeout;
75 private final Pattern ignoredMissingCapabilityRegex;
77 public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
78 MBeanServerConnection mbeanServer, Pattern ignoredMissingCapabilityRegex) {
79 this(persister, address, mbeanServer, DEFAULT_TIMEOUT, ignoredMissingCapabilityRegex);
83 public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
84 MBeanServerConnection mbeanServer, long timeout, Pattern ignoredMissingCapabilityRegex) {
85 this.persister = persister;
86 this.address = address;
87 this.mbeanServer = mbeanServer;
88 this.timeout = timeout;
90 this.nettyThreadgroup = new NioEventLoopGroup();
91 this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
94 public void init() throws InterruptedException {
95 Optional<ConfigSnapshotHolder> maybeConfig = loadLastConfig();
97 if (maybeConfig.isPresent()) {
98 logger.debug("Last config found {}", persister);
100 registerToNetconf(maybeConfig.get().getCapabilities());
102 final String configSnapshot = maybeConfig.get().getConfigSnapshot();
103 logger.trace("Pushing following xml to netconf {}", configSnapshot);
105 pushLastConfig(XmlUtil.readXmlToElement(configSnapshot));
106 } catch (SAXException | IOException e) {
107 throw new IllegalStateException("Unable to load last config", e);
111 // this ensures that netconf is initialized, this is first
113 // this means we can register as listener for commit
114 registerToNetconf(Collections.<String>emptySet());
116 logger.info("No last config provided by backend storage {}", persister);
118 registerAsJMXListener();
121 private synchronized long registerToNetconf(Set<String> expectedCaps) throws InterruptedException {
123 Set<String> currentCapabilities = Sets.newHashSet();
125 // TODO think about moving capability subset check to netconf client
126 // could be utilized by integration tests
128 long pollingStart = System.currentTimeMillis();
136 netconfClientDispatcher = new NetconfClientDispatcher(Optional.<SSLContext>absent(), nettyThreadgroup, nettyThreadgroup);
138 netconfClient = new NetconfClient(this.toString(), address, delay, netconfClientDispatcher);
139 } catch (IllegalStateException e) {
140 logger.debug("Netconf {} was not initialized or is not stable, attempt {}", address, attempt, e);
141 netconfClientDispatcher.close();
145 currentCapabilities = netconfClient.getCapabilities();
147 if (isSubset(currentCapabilities, expectedCaps)) {
148 logger.debug("Hello from netconf stable with {} capabilities", currentCapabilities);
149 currentSessionId = netconfClient.getSessionId();
150 logger.info("Session id received from netconf server: {}", currentSessionId);
151 return currentSessionId;
154 if (System.currentTimeMillis() > pollingStart + timeout) {
158 logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, currentCapabilities);
160 closeClientAndDispatcher(netconfClient, netconfClientDispatcher);
164 Set<String> allNotFound = new HashSet<>(expectedCaps);
165 allNotFound.removeAll(currentCapabilities);
166 logger.error("Netconf server did not provide required capabilities. Expected but not found: {}, all expected {}, current {}",
167 allNotFound, expectedCaps ,currentCapabilities);
168 throw new RuntimeException("Netconf server did not provide required capabilities. Expected but not found:" + allNotFound);
172 private static void closeClientAndDispatcher(Closeable client, Closeable dispatcher) {
173 Exception fromClient = null;
176 } catch (Exception e) {
181 } catch (Exception e) {
182 if (fromClient != null) {
183 e.addSuppressed(fromClient);
186 throw new RuntimeException("Error closing temporary client ", e);
191 private boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
192 for (String exCap : expectedCaps) {
193 if (currentCapabilities.contains(exCap) == false)
199 private void registerAsJMXListener() {
201 mbeanServer.addNotificationListener(on, this, null, null);
202 } catch (InstanceNotFoundException | IOException e) {
203 throw new RuntimeException("Cannot register as JMX listener to netconf", e);
208 public void handleNotification(Notification notification, Object handback) {
209 if (notification instanceof NetconfJMXNotification == false)
212 // Socket should not be closed at this point
213 // Activator unregisters this as JMX listener before close is called
215 logger.debug("Received notification {}", notification);
216 if (notification instanceof CommitJMXNotification) {
218 handleAfterCommitNotification((CommitJMXNotification) notification);
219 } catch (Exception e) {
220 // TODO: notificationBroadcast support logs only DEBUG
221 logger.warn("Exception occured during notification handling: ", e);
225 throw new IllegalStateException("Unknown config registry notification type " + notification);
228 private void handleAfterCommitNotification(final CommitJMXNotification notification) {
230 persister.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
231 notification.getCapabilities(), ignoredMissingCapabilityRegex));
232 logger.debug("Configuration persisted successfully");
233 } catch (IOException e) {
234 throw new RuntimeException("Unable to persist configuration snapshot", e);
238 private Optional<ConfigSnapshotHolder> loadLastConfig() {
239 Optional<ConfigSnapshotHolder> maybeConfigElement;
241 maybeConfigElement = persister.loadLastConfig();
242 } catch (IOException e) {
243 throw new RuntimeException("Unable to load configuration", e);
245 return maybeConfigElement;
248 private synchronized void pushLastConfig(Element xmlToBePersisted) {
249 logger.info("Pushing last configuration to netconf");
250 StringBuilder response = new StringBuilder("editConfig response = {");
253 NetconfMessage message = createEditConfigMessage(xmlToBePersisted, "/netconfOp/editConfig.xml");
255 // sending message to netconf
256 NetconfMessage responseMessage = netconfClient.sendMessage(message, NETCONF_SEND_ATTEMPTS, NETCONF_SEND_ATTEMPT_MS_DELAY);
258 XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
259 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
260 element = element.getOnlyChildElement();
262 checkIsOk(element, responseMessage);
263 response.append(XmlUtil.toString(responseMessage.getDocument()));
264 response.append("}");
265 responseMessage = netconfClient.sendMessage(getNetconfMessageFromResource("/netconfOp/commit.xml"), NETCONF_SEND_ATTEMPTS, NETCONF_SEND_ATTEMPT_MS_DELAY);
267 element = XmlElement.fromDomDocument(responseMessage.getDocument());
268 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
269 element = element.getOnlyChildElement();
271 checkIsOk(element, responseMessage);
272 response.append("commit response = {");
273 response.append(XmlUtil.toString(responseMessage.getDocument()));
274 response.append("}");
275 logger.info("Last configuration loaded successfully");
276 logger.trace("Detailed message {}", response);
279 private void checkIsOk(XmlElement element, NetconfMessage responseMessage) {
280 if (element.getName().equals(XmlNetconfConstants.OK)) {
283 if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
284 logger.warn("Can not load last configuration, operation failed");
285 throw new IllegalStateException("Can not load last configuration, operation failed: "
286 + XmlUtil.toString(responseMessage.getDocument()));
288 logger.warn("Can not load last configuration. Operation failed.");
289 throw new IllegalStateException("Can not load last configuration. Operation failed: "
290 + XmlUtil.toString(responseMessage.getDocument()));
294 private static NetconfMessage createEditConfigMessage(Element dataElement, String editConfigResourcename) {
295 try (InputStream stream = ConfigPersisterNotificationHandler.class.getResourceAsStream(editConfigResourcename)) {
296 Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcename);
298 Document doc = XmlUtil.readXmlToDocument(stream);
300 doc.getDocumentElement();
301 XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
302 XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
303 editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
304 for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
305 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), true));
307 editConfigElement.appendChild(configWrapper.getDomElement());
308 return new NetconfMessage(doc);
309 } catch (IOException | SAXException e) {
310 throw new RuntimeException("Unable to parse message from resources " + editConfigResourcename, e);
314 private NetconfMessage getNetconfMessageFromResource(String resource) {
315 try (InputStream stream = getClass().getResourceAsStream(resource)) {
316 Preconditions.checkNotNull(stream, "Unable to load resource " + resource);
317 return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
318 } catch (SAXException | IOException e) {
319 throw new RuntimeException("Unable to parse message from resources " + resource, e);
324 public synchronized void close() {
325 // TODO persister is received from constructor, should not be closed
329 } catch (Exception e) {
330 logger.warn("Unable to close config persister {}", persister, e);
333 if (netconfClient != null) {
335 netconfClient.close();
336 } catch (Exception e) {
337 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
341 if (netconfClientDispatcher != null) {
343 netconfClientDispatcher.close();
344 } catch (Exception e) {
345 logger.warn("Unable to close connection to netconf {}", netconfClientDispatcher, e);
350 nettyThreadgroup.shutdownGracefully();
351 } catch (Exception e) {
352 logger.warn("Unable to close netconf client thread group {}", netconfClientDispatcher, e);
355 // unregister from JMX
357 if (mbeanServer.isRegistered(on)) {
358 mbeanServer.removeNotificationListener(on, this);
360 } catch (Exception e) {
361 logger.warn("Unable to unregister {} as listener for {}", this, on, e);