c01a225a330688991bdb6c524e4de57f2df8290b
[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 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.util.xml.XmlElement;
21 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.xml.sax.SAXException;
28
29 import javax.annotation.concurrent.ThreadSafe;
30 import javax.management.*;
31 import java.io.Closeable;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.net.InetSocketAddress;
35 import java.util.Collections;
36 import java.util.Set;
37
38 /**
39  * Responsible for listening for notifications from netconf containing latest
40  * committed configuration that should be persisted, and also for loading last
41  * configuration.
42  */
43 @ThreadSafe
44 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
45
46     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
47
48     private final InetSocketAddress address;
49
50     private NetconfClient netconfClient;
51
52     private final Persister persister;
53     private final MBeanServerConnection mbeanServer;
54     private Long currentSessionId;
55
56     private final ObjectName on = DefaultCommitOperationMXBean.objectName;
57
58     public static final long DEFAULT_TIMEOUT = 40000L;
59     private final long timeout;
60
61     public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
62             MBeanServerConnection mbeanServer) {
63         this(persister, address, mbeanServer, DEFAULT_TIMEOUT);
64     }
65
66     public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
67             MBeanServerConnection mbeanServer, long timeout) {
68         this.persister = persister;
69         this.address = address;
70         this.mbeanServer = mbeanServer;
71         this.timeout = timeout;
72     }
73
74     public void init() throws InterruptedException {
75         Optional<Persister.ConfigSnapshotHolder> maybeConfig = loadLastConfig();
76
77         if (maybeConfig.isPresent()) {
78             logger.debug("Last config found {}", persister);
79
80             registerToNetconf(maybeConfig.get().getCapabilities());
81
82             final String configSnapshot = maybeConfig.get().getConfigSnapshot();
83             try {
84                 pushLastConfig(XmlUtil.readXmlToElement(configSnapshot));
85             } catch (SAXException | IOException e) {
86                 throw new IllegalStateException("Unable to load last config", e);
87             }
88
89         } else {
90             // this ensures that netconf is initialized, this is first
91             // connection
92             // this means we can register as listener for commit
93             registerToNetconf(Collections.<String>emptySet());
94
95             logger.info("No last config provided by backend storage {}", persister);
96         }
97         registerAsJMXListener();
98     }
99
100     private synchronized long registerToNetconf(Set<String> expectedCaps) throws InterruptedException {
101
102         Set<String> currentCapabilities = Sets.newHashSet();
103
104         // TODO think about moving capability subset check to netconf client
105         // could be utilized by integration tests
106
107         long pollingStart = System.currentTimeMillis();
108         int delay = 5000;
109
110         int attempt = 0;
111
112         while (true) {
113             attempt++;
114
115             try {
116                 netconfClient = new NetconfClient(this.toString(), address, delay);
117                 // TODO is this correct ex to catch ?
118             } catch (IllegalStateException e) {
119                 logger.debug("Netconf {} was not initialized or is not stable, attempt {}", address, attempt, e);
120                 Thread.sleep(delay);
121                 continue;
122             }
123             currentCapabilities = netconfClient.getCapabilities();
124
125             if (isSubset(currentCapabilities, expectedCaps)) {
126                 logger.debug("Hello from netconf stable with {} capabilities", currentCapabilities);
127                 currentSessionId = netconfClient.getSessionId();
128                 logger.info("Session id received from netconf server: {}", currentSessionId);
129                 return currentSessionId;
130             }
131
132             if (System.currentTimeMillis() > pollingStart + timeout) {
133                 break;
134             }
135
136             logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, currentCapabilities);
137
138             try {
139                 netconfClient.close();
140             } catch (IOException e) {
141                 throw new RuntimeException("Error closing temporary client " + netconfClient);
142             }
143
144             Thread.sleep(delay);
145         }
146
147         throw new RuntimeException("Netconf server did not provide required capabilities " + expectedCaps
148                 + " in time, provided capabilities " + currentCapabilities);
149
150     }
151
152     private boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
153         for (String exCap : expectedCaps) {
154             if (currentCapabilities.contains(exCap) == false)
155                 return false;
156         }
157         return true;
158     }
159
160     private void registerAsJMXListener() {
161         try {
162             mbeanServer.addNotificationListener(on, this, null, null);
163         } catch (InstanceNotFoundException | IOException e) {
164             throw new RuntimeException("Cannot register as JMX listener to netconf", e);
165         }
166     }
167
168     @Override
169     public void handleNotification(Notification notification, Object handback) {
170         if (notification instanceof NetconfJMXNotification == false)
171             return;
172
173         // Socket should not be closed at this point
174         // Activator unregisters this as JMX listener before close is called
175
176         logger.debug("Received notification {}", notification);
177         if (notification instanceof CommitJMXNotification) {
178             try {
179                 handleAfterCommitNotification((CommitJMXNotification) notification);
180             } catch (Exception e) {
181                 // TODO: notificationBroadcast support logs only DEBUG
182                 logger.warn("Exception occured during notification handling: ", e);
183                 throw e;
184             }
185         } else
186             throw new IllegalStateException("Unknown config registry notification type " + notification);
187     }
188
189     private void handleAfterCommitNotification(final CommitJMXNotification notification) {
190         try {
191             final XmlElement configElement = XmlElement.fromDomElement(notification.getConfigSnapshot());
192             persister.persistConfig(new Persister.ConfigSnapshotHolder() {
193                 @Override
194                 public String getConfigSnapshot() {
195                     return XmlUtil.toString(configElement.getDomElement());
196                 }
197
198                 @Override
199                 public Set<String> getCapabilities() {
200                     return notification.getCapabilities();
201                 }
202             });
203             logger.debug("Configuration persisted successfully");
204         } catch (IOException e) {
205             throw new RuntimeException("Unable to persist configuration snapshot", e);
206         }
207     }
208
209     private Optional<Persister.ConfigSnapshotHolder> loadLastConfig() {
210         Optional<Persister.ConfigSnapshotHolder> maybeConfigElement;
211         try {
212             maybeConfigElement = persister.loadLastConfig();
213         } catch (IOException e) {
214             throw new RuntimeException("Unable to load configuration", e);
215         }
216         return maybeConfigElement;
217     }
218
219     private synchronized void pushLastConfig(Element persistedConfig) {
220         StringBuilder response = new StringBuilder("editConfig response = {");
221
222         Element configElement = persistedConfig;
223         NetconfMessage message = createEditConfigMessage(configElement, "/netconfOp/editConfig.xml");
224         NetconfMessage responseMessage = netconfClient.sendMessage(message);
225
226         XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
227         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
228         element = element.getOnlyChildElement();
229
230         checkIsOk(element, responseMessage);
231         response.append(XmlUtil.toString(responseMessage.getDocument()));
232         response.append("}");
233         responseMessage = netconfClient.sendMessage(getNetconfMessageFromResource("/netconfOp/commit.xml"));
234
235         element = XmlElement.fromDomDocument(responseMessage.getDocument());
236         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
237         element = element.getOnlyChildElement();
238
239         checkIsOk(element, responseMessage);
240         response.append("commit response = {");
241         response.append(XmlUtil.toString(responseMessage.getDocument()));
242         response.append("}");
243         logger.debug("Last configuration loaded successfully");
244     }
245
246     private void checkIsOk(XmlElement element, NetconfMessage responseMessage) {
247         if (element.getName().equals(XmlNetconfConstants.OK)) {
248             return;
249         } else {
250             if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
251                 logger.warn("Can not load last configuration, operation failed");
252                 throw new IllegalStateException("Can not load last configuration, operation failed: "
253                         + XmlUtil.toString(responseMessage.getDocument()));
254             }
255             logger.warn("Can not load last configuration. Operation failed.");
256             throw new IllegalStateException("Can not load last configuration. Operation failed: "
257                     + XmlUtil.toString(responseMessage.getDocument()));
258         }
259     }
260
261     private NetconfMessage createEditConfigMessage(Element dataElement, String editConfigResourcename) {
262         try (InputStream stream = getClass().getResourceAsStream(editConfigResourcename)) {
263             Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcename);
264
265             Document doc = XmlUtil.readXmlToDocument(stream);
266
267             doc.getDocumentElement();
268             XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
269             XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
270             editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
271             for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
272                 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), true));
273             }
274             editConfigElement.appendChild(configWrapper.getDomElement());
275             return new NetconfMessage(doc);
276         } catch (IOException | SAXException e) {
277             throw new RuntimeException("Unable to parse message from resources " + editConfigResourcename, e);
278         }
279     }
280
281     private NetconfMessage getNetconfMessageFromResource(String resource) {
282         try (InputStream stream = getClass().getResourceAsStream(resource)) {
283             Preconditions.checkNotNull(stream, "Unable to load resource " + resource);
284             return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
285         } catch (SAXException | IOException e) {
286             throw new RuntimeException("Unable to parse message from resources " + resource, e);
287         }
288     }
289
290     @Override
291     public synchronized void close() {
292         // TODO persister is received from constructor, should not be closed
293         // here
294         try {
295             persister.close();
296         } catch (Exception e) {
297             logger.warn("Unable to close config persister {}", persister, e);
298         }
299
300         if (netconfClient != null) {
301             try {
302                 netconfClient.close();
303             } catch (Exception e) {
304                 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
305             }
306         }
307
308         // unregister from JMX
309         try {
310             if (mbeanServer.isRegistered(on)) {
311                 mbeanServer.removeNotificationListener(on, this);
312             }
313         } catch (Exception e) {
314             logger.warn("Unable to unregister {} as listener for {}", this, on, e);
315         }
316     }
317 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.