Merge "Choice and case resolving in JSON output"
[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 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;
32
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;
46 import java.util.Set;
47 import java.util.regex.Pattern;
48
49 /**
50  * Responsible for listening for notifications from netconf containing latest
51  * committed configuration that should be persisted, and also for loading last
52  * configuration.
53  */
54 @ThreadSafe
55 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
56
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;
60
61     private final InetSocketAddress address;
62     private final EventLoopGroup nettyThreadgroup;
63
64     private NetconfClientDispatcher netconfClientDispatcher;
65     private NetconfClient netconfClient;
66
67     private final Persister persister;
68     private final MBeanServerConnection mbeanServer;
69     private Long currentSessionId;
70
71     private final ObjectName on = DefaultCommitOperationMXBean.objectName;
72
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;
76
77     public ConfigPersisterNotificationHandler(Persister persister, InetSocketAddress address,
78             MBeanServerConnection mbeanServer, Pattern ignoredMissingCapabilityRegex) {
79         this(persister, address, mbeanServer, DEFAULT_TIMEOUT, ignoredMissingCapabilityRegex);
80
81     }
82
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;
89
90         this.nettyThreadgroup = new NioEventLoopGroup();
91         this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
92     }
93
94     public void init() throws InterruptedException {
95         Optional<ConfigSnapshotHolder> maybeConfig = loadLastConfig();
96
97         if (maybeConfig.isPresent()) {
98             logger.debug("Last config found {}", persister);
99
100             registerToNetconf(maybeConfig.get().getCapabilities());
101
102             final String configSnapshot = maybeConfig.get().getConfigSnapshot();
103             logger.trace("Pushing following xml to netconf {}", configSnapshot);
104             try {
105                 pushLastConfig(XmlUtil.readXmlToElement(configSnapshot));
106             } catch (SAXException | IOException e) {
107                 throw new IllegalStateException("Unable to load last config", e);
108             }
109
110         } else {
111             // this ensures that netconf is initialized, this is first
112             // connection
113             // this means we can register as listener for commit
114             registerToNetconf(Collections.<String>emptySet());
115
116             logger.info("No last config provided by backend storage {}", persister);
117         }
118         registerAsJMXListener();
119     }
120
121     private synchronized long registerToNetconf(Set<String> expectedCaps) throws InterruptedException {
122
123         Set<String> currentCapabilities = Sets.newHashSet();
124
125         // TODO think about moving capability subset check to netconf client
126         // could be utilized by integration tests
127
128         long pollingStart = System.currentTimeMillis();
129         int delay = 5000;
130
131         int attempt = 0;
132
133         while (true) {
134             attempt++;
135
136             netconfClientDispatcher = new NetconfClientDispatcher(Optional.<SSLContext>absent(), nettyThreadgroup, nettyThreadgroup);
137             try {
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();
142                 Thread.sleep(delay);
143                 continue;
144             }
145             currentCapabilities = netconfClient.getCapabilities();
146
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;
152             }
153
154             if (System.currentTimeMillis() > pollingStart + timeout) {
155                 break;
156             }
157
158             logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, currentCapabilities);
159
160             closeClientAndDispatcher(netconfClient, netconfClientDispatcher);
161
162             Thread.sleep(delay);
163         }
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);
169
170     }
171
172     private static void closeClientAndDispatcher(Closeable client, Closeable dispatcher) {
173         Exception fromClient = null;
174         try {
175             client.close();
176         } catch (Exception e) {
177             fromClient = e;
178         } finally {
179             try {
180                 dispatcher.close();
181             } catch (Exception e) {
182                 if (fromClient != null) {
183                     e.addSuppressed(fromClient);
184                 }
185
186                 throw new RuntimeException("Error closing temporary client ", e);
187             }
188         }
189     }
190
191     private boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
192         for (String exCap : expectedCaps) {
193             if (currentCapabilities.contains(exCap) == false)
194                 return false;
195         }
196         return true;
197     }
198
199     private void registerAsJMXListener() {
200         try {
201             mbeanServer.addNotificationListener(on, this, null, null);
202         } catch (InstanceNotFoundException | IOException e) {
203             throw new RuntimeException("Cannot register as JMX listener to netconf", e);
204         }
205     }
206
207     @Override
208     public void handleNotification(Notification notification, Object handback) {
209         if (notification instanceof NetconfJMXNotification == false)
210             return;
211
212         // Socket should not be closed at this point
213         // Activator unregisters this as JMX listener before close is called
214
215         logger.debug("Received notification {}", notification);
216         if (notification instanceof CommitJMXNotification) {
217             try {
218                 handleAfterCommitNotification((CommitJMXNotification) notification);
219             } catch (Exception e) {
220                 // TODO: notificationBroadcast support logs only DEBUG
221                 logger.warn("Exception occured during notification handling: ", e);
222                 throw e;
223             }
224         } else
225             throw new IllegalStateException("Unknown config registry notification type " + notification);
226     }
227
228     private void handleAfterCommitNotification(final CommitJMXNotification notification) {
229         try {
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);
235         }
236     }
237
238     private Optional<ConfigSnapshotHolder> loadLastConfig() {
239         Optional<ConfigSnapshotHolder> maybeConfigElement;
240         try {
241             maybeConfigElement = persister.loadLastConfig();
242         } catch (IOException e) {
243             throw new RuntimeException("Unable to load configuration", e);
244         }
245         return maybeConfigElement;
246     }
247
248     private synchronized void pushLastConfig(Element xmlToBePersisted) {
249         logger.info("Pushing last configuration to netconf");
250         StringBuilder response = new StringBuilder("editConfig response = {");
251
252
253         NetconfMessage message = createEditConfigMessage(xmlToBePersisted, "/netconfOp/editConfig.xml");
254
255         // sending message to netconf
256         NetconfMessage responseMessage = netconfClient.sendMessage(message, NETCONF_SEND_ATTEMPTS, NETCONF_SEND_ATTEMPT_MS_DELAY);
257
258         XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
259         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
260         element = element.getOnlyChildElement();
261
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);
266
267         element = XmlElement.fromDomDocument(responseMessage.getDocument());
268         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
269         element = element.getOnlyChildElement();
270
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);
277     }
278
279     private void checkIsOk(XmlElement element, NetconfMessage responseMessage) {
280         if (element.getName().equals(XmlNetconfConstants.OK)) {
281             return;
282         } else {
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()));
287             }
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()));
291         }
292     }
293
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);
297
298             Document doc = XmlUtil.readXmlToDocument(stream);
299
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));
306             }
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);
311         }
312     }
313
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);
320         }
321     }
322
323     @Override
324     public synchronized void close() {
325         // TODO persister is received from constructor, should not be closed
326         // here
327         try {
328             persister.close();
329         } catch (Exception e) {
330             logger.warn("Unable to close config persister {}", persister, e);
331         }
332
333         if (netconfClient != null) {
334             try {
335                 netconfClient.close();
336             } catch (Exception e) {
337                 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
338             }
339         }
340
341         if (netconfClientDispatcher != null) {
342             try {
343                 netconfClientDispatcher.close();
344             } catch (Exception e) {
345                 logger.warn("Unable to close connection to netconf {}", netconfClientDispatcher, e);
346             }
347         }
348
349         try {
350             nettyThreadgroup.shutdownGracefully();
351         } catch (Exception e) {
352             logger.warn("Unable to close netconf client thread group {}", netconfClientDispatcher, e);
353         }
354
355         // unregister from JMX
356         try {
357             if (mbeanServer.isRegistered(on)) {
358                 mbeanServer.removeNotificationListener(on, this);
359             }
360         } catch (Exception e) {
361             logger.warn("Unable to unregister {} as listener for {}", this, on, e);
362         }
363     }
364 }