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