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 org.opendaylight.controller.config.persist.api.Persister;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
17 import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
18 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
19 import org.opendaylight.controller.netconf.client.NetconfClient;
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28 import org.xml.sax.SAXException;
30 import javax.annotation.concurrent.ThreadSafe;
31 import javax.management.InstanceNotFoundException;
32 import javax.management.MBeanServerConnection;
33 import javax.management.Notification;
34 import javax.management.NotificationListener;
35 import javax.management.ObjectName;
36 import javax.net.ssl.SSLContext;
37 import java.io.Closeable;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.net.InetSocketAddress;
41 import java.util.Collections;
45 * Responsible for listening for notifications from netconf containing latest
46 * committed configuration that should be persisted, and also for loading last
50 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
52 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
54 private final InetSocketAddress address;
55 private final NetconfClientDispatcher dispatcher;
57 private NetconfClient netconfClient;
59 private final Persister persister;
60 private final MBeanServerConnection mbeanServer;
61 private Long currentSessionId;
63 private final ObjectName on = DefaultCommitOperationMXBean.objectName;
65 public static final long DEFAULT_TIMEOUT = 40000L;
66 private final long timeout;
68 public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
69 MBeanServerConnection mbeanServer) {
70 this(persister, address, mbeanServer, DEFAULT_TIMEOUT);
73 public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
74 MBeanServerConnection mbeanServer, long timeout) {
75 this.persister = persister;
76 this.address = address;
77 this.mbeanServer = mbeanServer;
78 this.timeout = timeout;
79 this.dispatcher = new NetconfClientDispatcher(Optional.<SSLContext>absent());
82 public void init() throws InterruptedException {
83 Optional<Persister.ConfigSnapshotHolder> maybeConfig = loadLastConfig();
85 if (maybeConfig.isPresent()) {
86 logger.debug("Last config found {}", persister);
88 registerToNetconf(maybeConfig.get().getCapabilities());
90 final String configSnapshot = maybeConfig.get().getConfigSnapshot();
92 pushLastConfig(XmlUtil.readXmlToElement(configSnapshot));
93 } catch (SAXException | IOException e) {
94 throw new IllegalStateException("Unable to load last config", e);
98 // this ensures that netconf is initialized, this is first
100 // this means we can register as listener for commit
101 registerToNetconf(Collections.<String>emptySet());
103 logger.info("No last config provided by backend storage {}", persister);
105 registerAsJMXListener();
108 private synchronized long registerToNetconf(Set<String> expectedCaps) throws InterruptedException {
110 Set<String> currentCapabilities = Sets.newHashSet();
112 // TODO think about moving capability subset check to netconf client
113 // could be utilized by integration tests
115 long pollingStart = System.currentTimeMillis();
124 netconfClient = new NetconfClient(this.toString(), address, delay, dispatcher);
125 // TODO is this correct ex to catch ?
126 } catch (IllegalStateException e) {
127 logger.debug("Netconf {} was not initialized or is not stable, attempt {}", address, attempt, e);
131 currentCapabilities = netconfClient.getCapabilities();
133 if (isSubset(currentCapabilities, expectedCaps)) {
134 logger.debug("Hello from netconf stable with {} capabilities", currentCapabilities);
135 currentSessionId = netconfClient.getSessionId();
136 logger.info("Session id received from netconf server: {}", currentSessionId);
137 return currentSessionId;
140 if (System.currentTimeMillis() > pollingStart + timeout) {
144 logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, currentCapabilities);
147 netconfClient.close();
148 } catch (IOException e) {
149 throw new RuntimeException("Error closing temporary client " + netconfClient);
155 throw new RuntimeException("Netconf server did not provide required capabilities " + expectedCaps
156 + " in time, provided capabilities " + currentCapabilities);
160 private boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
161 for (String exCap : expectedCaps) {
162 if (currentCapabilities.contains(exCap) == false)
168 private void registerAsJMXListener() {
170 mbeanServer.addNotificationListener(on, this, null, null);
171 } catch (InstanceNotFoundException | IOException e) {
172 throw new RuntimeException("Cannot register as JMX listener to netconf", e);
177 public void handleNotification(Notification notification, Object handback) {
178 if (notification instanceof NetconfJMXNotification == false)
181 // Socket should not be closed at this point
182 // Activator unregisters this as JMX listener before close is called
184 logger.debug("Received notification {}", notification);
185 if (notification instanceof CommitJMXNotification) {
187 handleAfterCommitNotification((CommitJMXNotification) notification);
188 } catch (Exception e) {
189 // TODO: notificationBroadcast support logs only DEBUG
190 logger.warn("Exception occured during notification handling: ", e);
194 throw new IllegalStateException("Unknown config registry notification type " + notification);
197 private void handleAfterCommitNotification(final CommitJMXNotification notification) {
199 final XmlElement configElement = XmlElement.fromDomElement(notification.getConfigSnapshot());
200 persister.persistConfig(new Persister.ConfigSnapshotHolder() {
202 public String getConfigSnapshot() {
203 return XmlUtil.toString(configElement.getDomElement());
207 public Set<String> getCapabilities() {
208 return notification.getCapabilities();
211 logger.debug("Configuration persisted successfully");
212 } catch (IOException e) {
213 throw new RuntimeException("Unable to persist configuration snapshot", e);
217 private Optional<Persister.ConfigSnapshotHolder> loadLastConfig() {
218 Optional<Persister.ConfigSnapshotHolder> maybeConfigElement;
220 maybeConfigElement = persister.loadLastConfig();
221 } catch (IOException e) {
222 throw new RuntimeException("Unable to load configuration", e);
224 return maybeConfigElement;
227 private synchronized void pushLastConfig(Element persistedConfig) {
228 StringBuilder response = new StringBuilder("editConfig response = {");
230 Element configElement = persistedConfig;
231 NetconfMessage message = createEditConfigMessage(configElement, "/netconfOp/editConfig.xml");
232 NetconfMessage responseMessage = netconfClient.sendMessage(message);
234 XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
235 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
236 element = element.getOnlyChildElement();
238 checkIsOk(element, responseMessage);
239 response.append(XmlUtil.toString(responseMessage.getDocument()));
240 response.append("}");
241 responseMessage = netconfClient.sendMessage(getNetconfMessageFromResource("/netconfOp/commit.xml"));
243 element = XmlElement.fromDomDocument(responseMessage.getDocument());
244 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
245 element = element.getOnlyChildElement();
247 checkIsOk(element, responseMessage);
248 response.append("commit response = {");
249 response.append(XmlUtil.toString(responseMessage.getDocument()));
250 response.append("}");
251 logger.debug("Last configuration loaded successfully");
254 private void checkIsOk(XmlElement element, NetconfMessage responseMessage) {
255 if (element.getName().equals(XmlNetconfConstants.OK)) {
258 if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
259 logger.warn("Can not load last configuration, operation failed");
260 throw new IllegalStateException("Can not load last configuration, operation failed: "
261 + XmlUtil.toString(responseMessage.getDocument()));
263 logger.warn("Can not load last configuration. Operation failed.");
264 throw new IllegalStateException("Can not load last configuration. Operation failed: "
265 + XmlUtil.toString(responseMessage.getDocument()));
269 private NetconfMessage createEditConfigMessage(Element dataElement, String editConfigResourcename) {
270 try (InputStream stream = getClass().getResourceAsStream(editConfigResourcename)) {
271 Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcename);
273 Document doc = XmlUtil.readXmlToDocument(stream);
275 doc.getDocumentElement();
276 XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
277 XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
278 editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
279 for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
280 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), true));
282 editConfigElement.appendChild(configWrapper.getDomElement());
283 return new NetconfMessage(doc);
284 } catch (IOException | SAXException e) {
285 throw new RuntimeException("Unable to parse message from resources " + editConfigResourcename, e);
289 private NetconfMessage getNetconfMessageFromResource(String resource) {
290 try (InputStream stream = getClass().getResourceAsStream(resource)) {
291 Preconditions.checkNotNull(stream, "Unable to load resource " + resource);
292 return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
293 } catch (SAXException | IOException e) {
294 throw new RuntimeException("Unable to parse message from resources " + resource, e);
299 public synchronized void close() {
300 // TODO persister is received from constructor, should not be closed
304 } catch (Exception e) {
305 logger.warn("Unable to close config persister {}", persister, e);
308 if (netconfClient != null) {
310 netconfClient.close();
311 } catch (Exception e) {
312 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
318 } catch (Exception e) {
319 logger.warn("Unable to close netconf client dispatcher {}", dispatcher, e);
322 // unregister from JMX
324 if (mbeanServer.isRegistered(on)) {
325 mbeanServer.removeNotificationListener(on, this);
327 } catch (Exception e) {
328 logger.warn("Unable to unregister {} as listener for {}", this, on, e);