Fixed bug when Binding-Aware Data Change Listeners we're not triggered.
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / ConfigPusher.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 io.netty.channel.EventLoopGroup;
14 import org.opendaylight.controller.config.api.ConflictingVersionException;
15 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.client.NetconfClient;
18 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
19 import org.opendaylight.controller.netconf.util.NetconfUtil;
20 import org.opendaylight.controller.netconf.util.messages.NetconfMessageAdditionalHeader;
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.Immutable;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.InetSocketAddress;
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.Set;
39 import java.util.concurrent.TimeUnit;
40
41 @Immutable
42 public class ConfigPusher {
43     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
44     private static final int NETCONF_SEND_ATTEMPT_MS_DELAY = 1000;
45     private static final int NETCONF_SEND_ATTEMPTS = 20;
46
47     private final InetSocketAddress address;
48     private final EventLoopGroup nettyThreadGroup;
49
50     // Default timeout for netconf becoming stable
51     public static final long DEFAULT_MAX_WAIT_FOR_CAPABILITIES_MILLIS = TimeUnit.MINUTES.toMillis(2);
52     public static final long DEFAULT_CONNECTION_TIMEOUT_MILLIS = 5000;
53     private final int delayMillis = 5000;
54     private final long maxWaitForCapabilitiesMillis;
55     private final long connectionTimeoutMillis;
56
57     public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadGroup) {
58         this(address, nettyThreadGroup, DEFAULT_MAX_WAIT_FOR_CAPABILITIES_MILLIS, DEFAULT_CONNECTION_TIMEOUT_MILLIS);
59     }
60
61     public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadGroup,
62                         long maxWaitForCapabilitiesMillis, long connectionTimeoutMillis) {
63         this.address = address;
64         this.nettyThreadGroup = nettyThreadGroup;
65         this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
66         this.connectionTimeoutMillis = connectionTimeoutMillis;
67     }
68
69     public synchronized LinkedHashMap<ConfigSnapshotHolder, EditAndCommitResponseWithRetries> pushConfigs(
70             List<ConfigSnapshotHolder> configs) throws InterruptedException {
71         logger.debug("Last config snapshots to be pushed to netconf: {}", configs);
72
73         // first just make sure we can connect to netconf, even if nothing is being pushed
74         {
75             NetconfClient netconfClient = makeNetconfConnection(Collections.<String>emptySet());
76             Util.closeClientAndDispatcher(netconfClient);
77         }
78         LinkedHashMap<ConfigSnapshotHolder, EditAndCommitResponseWithRetries> result = new LinkedHashMap<>();
79         // start pushing snapshots:
80         for (ConfigSnapshotHolder configSnapshotHolder : configs) {
81             EditAndCommitResponseWithRetries editAndCommitResponseWithRetries = pushSnapshotWithRetries(configSnapshotHolder);
82             logger.debug("Config snapshot pushed successfully: {}, result: {}", configSnapshotHolder, result);
83             result.put(configSnapshotHolder, editAndCommitResponseWithRetries);
84         }
85         logger.debug("All configuration snapshots have been pushed successfully.");
86         return result;
87     }
88
89     /**
90      * Checks for ConflictingVersionException and retries until optimistic lock succeeds or maximal
91      * number of attempts is reached.
92      */
93     private synchronized EditAndCommitResponseWithRetries pushSnapshotWithRetries(ConfigSnapshotHolder configSnapshotHolder)
94             throws InterruptedException {
95
96         ConflictingVersionException lastException = null;
97         int maxAttempts = 30;
98
99         for (int retryAttempt = 1; retryAttempt <= maxAttempts; retryAttempt++) {
100             NetconfClient netconfClient = makeNetconfConnection(configSnapshotHolder.getCapabilities());
101             logger.trace("Pushing following xml to netconf {}", configSnapshotHolder);
102             try {
103                 EditAndCommitResponse editAndCommitResponse = pushLastConfig(configSnapshotHolder, netconfClient);
104                 return new EditAndCommitResponseWithRetries(editAndCommitResponse, retryAttempt);
105             } catch (ConflictingVersionException e) {
106                 lastException = e;
107                 Thread.sleep(1000);
108             } catch (RuntimeException e) {
109                 throw new IllegalStateException("Unable to load " + configSnapshotHolder, e);
110             } finally {
111                 Util.closeClientAndDispatcher(netconfClient);
112             }
113         }
114         throw new IllegalStateException("Maximum attempt count has been reached for pushing " + configSnapshotHolder,
115                 lastException);
116     }
117
118     /**
119      * @param expectedCaps capabilities that server hello must contain. Will retry until all are found or throws RuntimeException.
120      *                     If empty set is provided, will only make sure netconf client successfuly connected to the server.
121      * @return NetconfClient that has all required capabilities from server.
122      */
123     private synchronized NetconfClient makeNetconfConnection(Set<String> expectedCaps) throws InterruptedException {
124
125         // TODO think about moving capability subset check to netconf client
126         // could be utilized by integration tests
127
128         final long pollingStartNanos = System.nanoTime();
129         final long deadlineNanos = pollingStartNanos + TimeUnit.MILLISECONDS.toNanos(maxWaitForCapabilitiesMillis);
130         int attempt = 0;
131
132         String additionalHeader = NetconfMessageAdditionalHeader.toString("unknown", address.getAddress().getHostAddress(),
133                 Integer.toString(address.getPort()), "tcp", Optional.of("persister"));
134
135         Set<String> latestCapabilities = null;
136         while (System.nanoTime() < deadlineNanos) {
137             attempt++;
138             NetconfClientDispatcher netconfClientDispatcher = new NetconfClientDispatcher(nettyThreadGroup,
139                     nettyThreadGroup, additionalHeader, connectionTimeoutMillis);
140             NetconfClient netconfClient;
141             try {
142                 netconfClient = new NetconfClient(this.toString(), address, delayMillis, netconfClientDispatcher);
143             } catch (IllegalStateException e) {
144                 logger.debug("Netconf {} was not initialized or is not stable, attempt {}", address, attempt, e);
145                 netconfClientDispatcher.close();
146                 Thread.sleep(delayMillis);
147                 continue;
148             }
149             latestCapabilities = netconfClient.getCapabilities();
150             if (Util.isSubset(netconfClient, expectedCaps)) {
151                 logger.debug("Hello from netconf stable with {} capabilities", latestCapabilities);
152                 logger.trace("Session id received from netconf server: {}", netconfClient.getClientSession());
153                 return netconfClient;
154             }
155             logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, latestCapabilities);
156             Util.closeClientAndDispatcher(netconfClient);
157             Thread.sleep(delayMillis);
158         }
159         if (latestCapabilities == null) {
160             logger.error("Could not connect to the server in {} ms", maxWaitForCapabilitiesMillis);
161             throw new RuntimeException("Could not connect to netconf server");
162         }
163         Set<String> allNotFound = new HashSet<>(expectedCaps);
164         allNotFound.removeAll(latestCapabilities);
165         logger.error("Netconf server did not provide required capabilities. Expected but not found: {}, all expected {}, current {}",
166                 allNotFound, expectedCaps, latestCapabilities);
167         throw new RuntimeException("Netconf server did not provide required capabilities. Expected but not found:" + allNotFound);
168     }
169
170
171     /**
172      * Sends two RPCs to the netconf server: edit-config and commit.
173      *
174      * @param configSnapshotHolder
175      * @param netconfClient
176      * @throws ConflictingVersionException if commit fails on optimistic lock failure inside of config-manager
177      * @throws java.lang.RuntimeException  if edit-config or commit fails otherwise
178      */
179     private synchronized EditAndCommitResponse pushLastConfig(ConfigSnapshotHolder configSnapshotHolder, NetconfClient netconfClient)
180             throws ConflictingVersionException {
181
182         Element xmlToBePersisted;
183         try {
184             xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
185         } catch (SAXException | IOException e) {
186             throw new IllegalStateException("Cannot parse " + configSnapshotHolder);
187         }
188         logger.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
189
190         NetconfMessage editConfigMessage = createEditConfigMessage(xmlToBePersisted);
191
192         // sending message to netconf
193         NetconfMessage editResponseMessage;
194         try {
195             editResponseMessage = sendRequestGetResponseCheckIsOK(editConfigMessage, netconfClient);
196         } catch (IOException e) {
197             throw new IllegalStateException("Edit-config failed on " + configSnapshotHolder, e);
198         }
199
200         // commit
201         NetconfMessage commitResponseMessage;
202         try {
203             commitResponseMessage = sendRequestGetResponseCheckIsOK(getCommitMessage(), netconfClient);
204         } catch (IOException e) {
205             throw new IllegalStateException("Edit commit succeeded, but commit failed on " + configSnapshotHolder, e);
206         }
207
208         if (logger.isTraceEnabled()) {
209             StringBuilder response = new StringBuilder("editConfig response = {");
210             response.append(XmlUtil.toString(editResponseMessage.getDocument()));
211             response.append("}");
212             response.append("commit response = {");
213             response.append(XmlUtil.toString(commitResponseMessage.getDocument()));
214             response.append("}");
215             logger.trace("Last configuration loaded successfully");
216             logger.trace("Detailed message {}", response);
217         }
218         return new EditAndCommitResponse(editResponseMessage, commitResponseMessage);
219     }
220
221
222     private static NetconfMessage sendRequestGetResponseCheckIsOK(NetconfMessage request, NetconfClient netconfClient) throws IOException {
223         try {
224             NetconfMessage netconfMessage = netconfClient.sendMessage(request, NETCONF_SEND_ATTEMPTS, NETCONF_SEND_ATTEMPT_MS_DELAY);
225             NetconfUtil.checkIsMessageOk(netconfMessage);
226             return netconfMessage;
227         } catch (RuntimeException e) { // TODO: change NetconfClient#sendMessage to throw checked exceptions
228             logger.debug("Error while executing netconf transaction {} to {}", request, netconfClient, e);
229             throw new IOException("Failed to execute netconf transaction", e);
230         }
231     }
232
233
234     // load editConfig.xml template, populate /rpc/edit-config/config with parameter
235     private static NetconfMessage createEditConfigMessage(Element dataElement) {
236         String editConfigResourcePath = "/netconfOp/editConfig.xml";
237         try (InputStream stream = ConfigPersisterNotificationHandler.class.getResourceAsStream(editConfigResourcePath)) {
238             Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcePath);
239
240             Document doc = XmlUtil.readXmlToDocument(stream);
241
242             XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
243             XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
244             editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
245             for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
246                 boolean deep = true;
247                 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), deep));
248             }
249             editConfigElement.appendChild(configWrapper.getDomElement());
250             return new NetconfMessage(doc);
251         } catch (IOException | SAXException e) {
252             // error reading the xml file bundled into the jar
253             throw new RuntimeException("Error while opening local resource " + editConfigResourcePath, e);
254         }
255     }
256
257     private static NetconfMessage getCommitMessage() {
258         String resource = "/netconfOp/commit.xml";
259         try (InputStream stream = ConfigPusher.class.getResourceAsStream(resource)) {
260             Preconditions.checkNotNull(stream, "Unable to load resource " + resource);
261             return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
262         } catch (SAXException | IOException e) {
263             // error reading the xml file bundled into the jar
264             throw new RuntimeException("Error while opening local resource " + resource, e);
265         }
266     }
267
268     static class EditAndCommitResponse {
269         private final NetconfMessage editResponse, commitResponse;
270
271         EditAndCommitResponse(NetconfMessage editResponse, NetconfMessage commitResponse) {
272             this.editResponse = editResponse;
273             this.commitResponse = commitResponse;
274         }
275
276         public NetconfMessage getEditResponse() {
277             return editResponse;
278         }
279
280         public NetconfMessage getCommitResponse() {
281             return commitResponse;
282         }
283
284         @Override
285         public String toString() {
286             return "EditAndCommitResponse{" +
287                     "editResponse=" + editResponse +
288                     ", commitResponse=" + commitResponse +
289                     '}';
290         }
291     }
292
293
294     static class EditAndCommitResponseWithRetries {
295         private final EditAndCommitResponse editAndCommitResponse;
296         private final int retries;
297
298         EditAndCommitResponseWithRetries(EditAndCommitResponse editAndCommitResponse, int retries) {
299             this.editAndCommitResponse = editAndCommitResponse;
300             this.retries = retries;
301         }
302
303         public int getRetries() {
304             return retries;
305         }
306
307         public EditAndCommitResponse getEditAndCommitResponse() {
308             return editAndCommitResponse;
309         }
310
311         @Override
312         public String toString() {
313             return "EditAndCommitResponseWithRetries{" +
314                     "editAndCommitResponse=" + editAndCommitResponse +
315                     ", retries=" + retries +
316                     '}';
317         }
318     }
319 }