2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.persist.impl;
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;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
23 import javax.annotation.concurrent.Immutable;
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;
41 import com.google.common.base.Preconditions;
42 import io.netty.channel.EventLoopGroup;
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;
50 private final InetSocketAddress address;
51 private final EventLoopGroup nettyThreadGroup;
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;
60 public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadGroup) {
61 this(address, nettyThreadGroup, DEFAULT_MAX_WAIT_FOR_CAPABILITIES_MILLIS, DEFAULT_CONNECTION_TIMEOUT_MILLIS);
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;
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);
76 // first just make sure we can connect to netconf, even if nothing is being pushed
78 NetconfClient netconfClient = makeNetconfConnection(Collections.<String>emptySet());
79 Util.closeClientAndDispatcher(netconfClient);
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);
88 logger.debug("All configuration snapshots have been pushed successfully.");
93 * Checks for ConflictingVersionException and retries until optimistic lock succeeds or maximal
94 * number of attempts is reached.
96 private synchronized EditAndCommitResponseWithRetries pushSnapshotWithRetries(ConfigSnapshotHolder configSnapshotHolder)
97 throws InterruptedException {
99 ConflictingVersionException lastException = null;
100 int maxAttempts = 30;
102 for (int retryAttempt = 1; retryAttempt <= maxAttempts; retryAttempt++) {
103 NetconfClient netconfClient = makeNetconfConnection(configSnapshotHolder.getCapabilities());
104 logger.trace("Pushing following xml to netconf {}", configSnapshotHolder);
106 EditAndCommitResponse editAndCommitResponse = pushLastConfig(configSnapshotHolder, netconfClient);
107 return new EditAndCommitResponseWithRetries(editAndCommitResponse, retryAttempt);
108 } catch (ConflictingVersionException e) {
111 } catch (RuntimeException e) {
112 throw new IllegalStateException("Unable to load " + configSnapshotHolder, e);
114 Util.closeClientAndDispatcher(netconfClient);
117 throw new IllegalStateException("Maximum attempt count has been reached for pushing " + configSnapshotHolder,
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.
126 private synchronized NetconfClient makeNetconfConnection(Set<String> expectedCaps) throws InterruptedException {
128 // TODO think about moving capability subset check to netconf client
129 // could be utilized by integration tests
131 final long pollingStartNanos = System.nanoTime();
132 final long deadlineNanos = pollingStartNanos + TimeUnit.MILLISECONDS.toNanos(maxWaitForCapabilitiesMillis);
135 NetconfHelloMessageAdditionalHeader additionalHeader = new NetconfHelloMessageAdditionalHeader("unknown", address.getAddress().getHostAddress(),
136 Integer.toString(address.getPort()), "tcp", "persister");
138 Set<String> latestCapabilities = null;
139 while (System.nanoTime() < deadlineNanos) {
141 NetconfClientDispatcher netconfClientDispatcher = new NetconfClientDispatcher(nettyThreadGroup,
142 nettyThreadGroup, additionalHeader, connectionTimeoutMillis);
143 NetconfClient netconfClient;
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);
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;
158 logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, latestCapabilities);
159 Util.closeClientAndDispatcher(netconfClient);
160 Thread.sleep(delayMillis);
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");
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);
175 * Sends two RPCs to the netconf server: edit-config and commit.
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
182 private synchronized EditAndCommitResponse pushLastConfig(ConfigSnapshotHolder configSnapshotHolder, NetconfClient netconfClient)
183 throws ConflictingVersionException {
185 Element xmlToBePersisted;
187 xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
188 } catch (SAXException | IOException e) {
189 throw new IllegalStateException("Cannot parse " + configSnapshotHolder);
191 logger.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
193 NetconfMessage editConfigMessage = createEditConfigMessage(xmlToBePersisted);
195 // sending message to netconf
196 NetconfMessage editResponseMessage;
198 editResponseMessage = sendRequestGetResponseCheckIsOK(editConfigMessage, netconfClient);
199 } catch (IOException e) {
200 throw new IllegalStateException("Edit-config failed on " + configSnapshotHolder, e);
204 NetconfMessage commitResponseMessage;
206 commitResponseMessage = sendRequestGetResponseCheckIsOK(getCommitMessage(), netconfClient);
207 } catch (IOException e) {
208 throw new IllegalStateException("Edit commit succeeded, but commit failed on " + configSnapshotHolder, e);
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);
221 return new EditAndCommitResponse(editResponseMessage, commitResponseMessage);
225 private static NetconfMessage sendRequestGetResponseCheckIsOK(NetconfMessage request, NetconfClient netconfClient) throws IOException {
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);
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);
242 Document doc = XmlUtil.readXmlToDocument(stream);
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()) {
249 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), deep));
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);
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);
270 static class EditAndCommitResponse {
271 private final NetconfMessage editResponse, commitResponse;
273 EditAndCommitResponse(NetconfMessage editResponse, NetconfMessage commitResponse) {
274 this.editResponse = editResponse;
275 this.commitResponse = commitResponse;
278 public NetconfMessage getEditResponse() {
282 public NetconfMessage getCommitResponse() {
283 return commitResponse;
287 public String toString() {
288 return "EditAndCommitResponse{" +
289 "editResponse=" + editResponse +
290 ", commitResponse=" + commitResponse +
296 static class EditAndCommitResponseWithRetries {
297 private final EditAndCommitResponse editAndCommitResponse;
298 private final int retries;
300 EditAndCommitResponseWithRetries(EditAndCommitResponse editAndCommitResponse, int retries) {
301 this.editAndCommitResponse = editAndCommitResponse;
302 this.retries = retries;
305 public int getRetries() {
309 public EditAndCommitResponse getEditAndCommitResponse() {
310 return editAndCommitResponse;
314 public String toString() {
315 return "EditAndCommitResponseWithRetries{" +
316 "editAndCommitResponse=" + editAndCommitResponse +
317 ", retries=" + retries +