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