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 static com.google.common.base.Preconditions.checkNotNull;
13 import com.google.common.base.Function;
14 import com.google.common.base.Stopwatch;
15 import com.google.common.collect.Collections2;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
21 import java.util.List;
22 import java.util.Map.Entry;
24 import java.util.SortedSet;
25 import java.util.TreeMap;
26 import java.util.concurrent.TimeUnit;
27 import javax.annotation.concurrent.Immutable;
28 import org.opendaylight.controller.config.api.ConflictingVersionException;
29 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
30 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
31 import org.opendaylight.controller.netconf.api.NetconfMessage;
32 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
33 import org.opendaylight.controller.netconf.mapping.api.Capability;
34 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
35 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
36 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
37 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
38 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
39 import org.opendaylight.controller.netconf.util.NetconfUtil;
40 import org.opendaylight.controller.netconf.util.xml.XmlElement;
41 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.xml.sax.SAXException;
49 public class ConfigPusher {
50 private static final Logger logger = LoggerFactory.getLogger(ConfigPusher.class);
52 private final long maxWaitForCapabilitiesMillis;
53 private final long conflictingVersionTimeoutMillis;
54 private final NetconfOperationServiceFactory configNetconfConnector;
56 public ConfigPusher(NetconfOperationServiceFactory configNetconfConnector, long maxWaitForCapabilitiesMillis,
57 long conflictingVersionTimeoutMillis) {
58 this.configNetconfConnector = configNetconfConnector;
59 this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
60 this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
63 public synchronized LinkedHashMap<ConfigSnapshotHolder, EditAndCommitResponse> pushConfigs(List<ConfigSnapshotHolder> configs) throws NetconfDocumentedException {
64 logger.debug("Last config snapshots to be pushed to netconf: {}", configs);
65 LinkedHashMap<ConfigSnapshotHolder, EditAndCommitResponse> result = new LinkedHashMap<>();
66 // start pushing snapshots:
67 for (ConfigSnapshotHolder configSnapshotHolder : configs) {
68 if(configSnapshotHolder != null) {
69 EditAndCommitResponse editAndCommitResponseWithRetries = pushConfigWithConflictingVersionRetries(configSnapshotHolder);
70 logger.debug("Config snapshot pushed successfully: {}, result: {}", configSnapshotHolder, result);
71 result.put(configSnapshotHolder, editAndCommitResponseWithRetries);
74 logger.debug("All configuration snapshots have been pushed successfully.");
79 * First calls {@link #getOperationServiceWithRetries(java.util.Set, String)} in order to wait until
80 * expected capabilities are present, then tries to push configuration. If {@link ConflictingVersionException}
81 * is caught, whole process is retried - new service instance need to be obtained from the factory. Closes
82 * {@link NetconfOperationService} after each use.
84 private synchronized EditAndCommitResponse pushConfigWithConflictingVersionRetries(ConfigSnapshotHolder configSnapshotHolder) throws NetconfDocumentedException {
85 ConflictingVersionException lastException;
86 Stopwatch stopwatch = new Stopwatch().start();
88 String idForReporting = configSnapshotHolder.toString();
89 SortedSet<String> expectedCapabilities = checkNotNull(configSnapshotHolder.getCapabilities(),
90 "Expected capabilities must not be null - %s, check %s", idForReporting,
91 configSnapshotHolder.getClass().getName());
92 try (NetconfOperationService operationService = getOperationServiceWithRetries(expectedCapabilities, idForReporting)) {
93 return pushConfig(configSnapshotHolder, operationService);
94 } catch (ConflictingVersionException e) {
96 logger.debug("Conflicting version detected, will retry after timeout");
99 } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < conflictingVersionTimeoutMillis);
100 throw new IllegalStateException("Max wait for conflicting version stabilization timeout after " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms",
104 private NetconfOperationService getOperationServiceWithRetries(Set<String> expectedCapabilities, String idForReporting) {
105 Stopwatch stopwatch = new Stopwatch().start();
106 NotEnoughCapabilitiesException lastException;
109 return getOperationService(expectedCapabilities, idForReporting);
110 } catch (NotEnoughCapabilitiesException e) {
111 logger.debug("Not enough capabilities: " + e.toString());
115 } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < maxWaitForCapabilitiesMillis);
116 throw new IllegalStateException("Max wait for capabilities reached." + lastException.getMessage(), lastException);
119 private static class NotEnoughCapabilitiesException extends Exception {
120 private static final long serialVersionUID = 1L;
122 private NotEnoughCapabilitiesException(String message, Throwable cause) {
123 super(message, cause);
126 private NotEnoughCapabilitiesException(String message) {
132 * Get NetconfOperationService iif all required capabilities are present.
134 * @param expectedCapabilities that must be provided by configNetconfConnector
135 * @param idForReporting
136 * @return service if capabilities are present, otherwise absent value
138 private NetconfOperationService getOperationService(Set<String> expectedCapabilities, String idForReporting) throws NotEnoughCapabilitiesException {
139 NetconfOperationService serviceCandidate;
141 serviceCandidate = configNetconfConnector.createService(idForReporting);
142 } catch(RuntimeException e) {
143 throw new NotEnoughCapabilitiesException("Netconf service not stable for " + idForReporting, e);
145 Set<String> notFoundDiff = computeNotFoundCapabilities(expectedCapabilities, serviceCandidate);
146 if (notFoundDiff.isEmpty()) {
147 return serviceCandidate;
149 serviceCandidate.close();
150 logger.trace("Netconf server did not provide required capabilities for {} " +
151 "Expected but not found: {}, all expected {}, current {}",
152 idForReporting, notFoundDiff, expectedCapabilities, serviceCandidate.getCapabilities()
154 throw new NotEnoughCapabilitiesException("Not enough capabilities for " + idForReporting + ". Expected but not found: " + notFoundDiff);
158 private static Set<String> computeNotFoundCapabilities(Set<String> expectedCapabilities, NetconfOperationService serviceCandidate) {
159 Collection<String> actual = Collections2.transform(serviceCandidate.getCapabilities(), new Function<Capability, String>() {
161 public String apply(Capability input) {
162 return input.getCapabilityUri();
165 Set<String> allNotFound = new HashSet<>(expectedCapabilities);
166 allNotFound.removeAll(actual);
172 private void sleep() {
175 } catch (InterruptedException e) {
176 Thread.currentThread().interrupt();
177 throw new IllegalStateException(e);
182 * Sends two RPCs to the netconf server: edit-config and commit.
184 * @param configSnapshotHolder
185 * @throws ConflictingVersionException if commit fails on optimistic lock failure inside of config-manager
186 * @throws java.lang.RuntimeException if edit-config or commit fails otherwise
188 private synchronized EditAndCommitResponse pushConfig(ConfigSnapshotHolder configSnapshotHolder, NetconfOperationService operationService)
189 throws ConflictingVersionException, NetconfDocumentedException {
191 Element xmlToBePersisted;
193 xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
194 } catch (SAXException | IOException e) {
195 throw new IllegalStateException("Cannot parse " + configSnapshotHolder);
197 logger.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
198 Stopwatch stopwatch = new Stopwatch().start();
199 NetconfMessage editConfigMessage = createEditConfigMessage(xmlToBePersisted);
201 Document editResponseMessage = sendRequestGetResponseCheckIsOK(editConfigMessage, operationService,
202 "edit-config", configSnapshotHolder.toString());
204 Document commitResponseMessage = sendRequestGetResponseCheckIsOK(getCommitMessage(), operationService,
205 "commit", configSnapshotHolder.toString());
207 if (logger.isTraceEnabled()) {
208 StringBuilder response = new StringBuilder("editConfig response = {");
209 response.append(XmlUtil.toString(editResponseMessage));
210 response.append("}");
211 response.append("commit response = {");
212 response.append(XmlUtil.toString(commitResponseMessage));
213 response.append("}");
214 logger.trace("Last configuration loaded successfully");
215 logger.trace("Detailed message {}", response);
216 logger.trace("Total time spent {} ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
218 return new EditAndCommitResponse(editResponseMessage, commitResponseMessage);
221 private NetconfOperation findOperation(NetconfMessage request, NetconfOperationService operationService) throws NetconfDocumentedException {
222 TreeMap<HandlingPriority, NetconfOperation> allOperations = new TreeMap<>();
223 Set<NetconfOperation> netconfOperations = operationService.getNetconfOperations();
224 if (netconfOperations.isEmpty()) {
225 throw new IllegalStateException("Possible code error: no config operations");
227 for (NetconfOperation netconfOperation : netconfOperations) {
228 HandlingPriority handlingPriority = netconfOperation.canHandle(request.getDocument());
229 allOperations.put(handlingPriority, netconfOperation);
231 Entry<HandlingPriority, NetconfOperation> highestEntry = allOperations.lastEntry();
232 if (highestEntry.getKey().isCannotHandle()) {
233 throw new IllegalStateException("Possible code error: operation with highest priority is CANNOT_HANDLE");
235 return highestEntry.getValue();
238 private Document sendRequestGetResponseCheckIsOK(NetconfMessage request, NetconfOperationService operationService,
239 String operationNameForReporting, String configIdForReporting)
240 throws ConflictingVersionException, NetconfDocumentedException {
242 NetconfOperation operation = findOperation(request, operationService);
245 response = operation.handle(request.getDocument(), NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
246 } catch (NetconfDocumentedException | RuntimeException e) {
247 if (e instanceof NetconfDocumentedException && e.getCause() instanceof ConflictingVersionException) {
248 throw (ConflictingVersionException) e.getCause();
250 throw new IllegalStateException("Failed to send " + operationNameForReporting +
251 " for configuration " + configIdForReporting, e);
253 return NetconfUtil.checkIsMessageOk(response);
256 // load editConfig.xml template, populate /rpc/edit-config/config with parameter
257 private static NetconfMessage createEditConfigMessage(Element dataElement) throws NetconfDocumentedException {
258 String editConfigResourcePath = "/netconfOp/editConfig.xml";
259 try (InputStream stream = ConfigPersisterNotificationHandler.class.getResourceAsStream(editConfigResourcePath)) {
260 checkNotNull(stream, "Unable to load resource " + editConfigResourcePath);
262 Document doc = XmlUtil.readXmlToDocument(stream);
264 XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
265 XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
266 editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
267 for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
269 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), deep));
271 editConfigElement.appendChild(configWrapper.getDomElement());
272 return new NetconfMessage(doc);
273 } catch (IOException | SAXException e) {
274 // error reading the xml file bundled into the jar
275 throw new IllegalStateException("Error while opening local resource " + editConfigResourcePath, e);
279 private static NetconfMessage getCommitMessage() {
280 String resource = "/netconfOp/commit.xml";
281 try (InputStream stream = ConfigPusher.class.getResourceAsStream(resource)) {
282 checkNotNull(stream, "Unable to load resource " + resource);
283 return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
284 } catch (SAXException | IOException e) {
285 // error reading the xml file bundled into the jar
286 throw new IllegalStateException("Error while opening local resource " + resource, e);
290 static class EditAndCommitResponse {
291 private final Document editResponse, commitResponse;
293 EditAndCommitResponse(Document editResponse, Document commitResponse) {
294 this.editResponse = editResponse;
295 this.commitResponse = commitResponse;
298 public Document getEditResponse() {
302 public Document getCommitResponse() {
303 return commitResponse;
307 public String toString() {
308 return "EditAndCommitResponse{" +
309 "editResponse=" + editResponse +
310 ", commitResponse=" + commitResponse +