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.BlockingQueue;
27 import java.util.concurrent.LinkedBlockingQueue;
28 import java.util.concurrent.TimeUnit;
29 import javax.annotation.Nonnull;
30 import javax.annotation.concurrent.Immutable;
31 import javax.management.MBeanServerConnection;
32 import org.opendaylight.controller.config.api.ConflictingVersionException;
33 import org.opendaylight.controller.config.persist.api.ConfigPusher;
34 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
35 import org.opendaylight.controller.config.persist.api.Persister;
36 import org.opendaylight.controller.netconf.api.Capability;
37 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
38 import org.opendaylight.controller.netconf.api.NetconfMessage;
39 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
40 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
41 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
42 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
43 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
44 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
45 import org.opendaylight.controller.netconf.util.NetconfUtil;
46 import org.opendaylight.controller.netconf.util.xml.XmlElement;
47 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52 import org.xml.sax.SAXException;
55 public class ConfigPusherImpl implements ConfigPusher {
56 private static final Logger LOG = LoggerFactory.getLogger(ConfigPusherImpl.class);
58 private final long maxWaitForCapabilitiesMillis;
59 private final long conflictingVersionTimeoutMillis;
60 private final NetconfOperationServiceFactory configNetconfConnector;
61 private static final int QUEUE_SIZE = 100;
62 private BlockingQueue<List<? extends ConfigSnapshotHolder>> queue = new LinkedBlockingQueue<List<? extends ConfigSnapshotHolder>>(QUEUE_SIZE);
64 public ConfigPusherImpl(NetconfOperationServiceFactory configNetconfConnector, long maxWaitForCapabilitiesMillis,
65 long conflictingVersionTimeoutMillis) {
66 this.configNetconfConnector = configNetconfConnector;
67 this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
68 this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
71 public void process(List<AutoCloseable> autoCloseables, MBeanServerConnection platformMBeanServer, Persister persisterAggregator) throws InterruptedException {
72 List<? extends ConfigSnapshotHolder> configs;
74 configs = queue.take();
76 internalPushConfigs(configs);
77 ConfigPersisterNotificationHandler jmxNotificationHandler = new ConfigPersisterNotificationHandler(platformMBeanServer, persisterAggregator);
78 synchronized (autoCloseables) {
79 autoCloseables.add(jmxNotificationHandler);
82 LOG.debug("ConfigPusher has pushed configs {}", configs);
83 } catch (NetconfDocumentedException e) {
84 LOG.error("Error pushing configs {}",configs);
85 throw new IllegalStateException(e);
90 public void pushConfigs(List<? extends ConfigSnapshotHolder> configs) throws InterruptedException {
91 LOG.debug("Requested to push configs {}", configs);
92 this.queue.put(configs);
95 private LinkedHashMap<? extends ConfigSnapshotHolder, EditAndCommitResponse> internalPushConfigs(List<? extends ConfigSnapshotHolder> configs) throws NetconfDocumentedException {
96 LOG.debug("Last config snapshots to be pushed to netconf: {}", configs);
97 LinkedHashMap<ConfigSnapshotHolder, EditAndCommitResponse> result = new LinkedHashMap<>();
98 // start pushing snapshots:
99 for (ConfigSnapshotHolder configSnapshotHolder : configs) {
100 if(configSnapshotHolder != null) {
101 EditAndCommitResponse editAndCommitResponseWithRetries = pushConfigWithConflictingVersionRetries(configSnapshotHolder);
102 LOG.debug("Config snapshot pushed successfully: {}, result: {}", configSnapshotHolder, result);
103 result.put(configSnapshotHolder, editAndCommitResponseWithRetries);
106 LOG.debug("All configuration snapshots have been pushed successfully.");
111 * First calls {@link #getOperationServiceWithRetries(java.util.Set, String)} in order to wait until
112 * expected capabilities are present, then tries to push configuration. If {@link ConflictingVersionException}
113 * is caught, whole process is retried - new service instance need to be obtained from the factory. Closes
114 * {@link NetconfOperationService} after each use.
116 private synchronized EditAndCommitResponse pushConfigWithConflictingVersionRetries(ConfigSnapshotHolder configSnapshotHolder) throws NetconfDocumentedException {
117 ConflictingVersionException lastException;
118 Stopwatch stopwatch = Stopwatch.createUnstarted();
120 String idForReporting = configSnapshotHolder.toString();
121 SortedSet<String> expectedCapabilities = checkNotNull(configSnapshotHolder.getCapabilities(),
122 "Expected capabilities must not be null - %s, check %s", idForReporting,
123 configSnapshotHolder.getClass().getName());
124 try (NetconfOperationService operationService = getOperationServiceWithRetries(expectedCapabilities, idForReporting)) {
125 if(!stopwatch.isRunning()) {
128 return pushConfig(configSnapshotHolder, operationService);
129 } catch (ConflictingVersionException e) {
131 LOG.info("Conflicting version detected, will retry after timeout");
134 } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < conflictingVersionTimeoutMillis);
135 throw new IllegalStateException("Max wait for conflicting version stabilization timeout after " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms",
139 private NetconfOperationService getOperationServiceWithRetries(Set<String> expectedCapabilities, String idForReporting) {
140 Stopwatch stopwatch = Stopwatch.createStarted();
141 NotEnoughCapabilitiesException lastException;
144 return getOperationService(expectedCapabilities, idForReporting);
145 } catch (NotEnoughCapabilitiesException e) {
146 LOG.debug("Not enough capabilities: {}", e.toString());
150 } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < maxWaitForCapabilitiesMillis);
151 throw new IllegalStateException("Max wait for capabilities reached." + lastException.getMessage(), lastException);
154 private static class NotEnoughCapabilitiesException extends Exception {
155 private static final long serialVersionUID = 1L;
157 private NotEnoughCapabilitiesException(String message, Throwable cause) {
158 super(message, cause);
161 private NotEnoughCapabilitiesException(String message) {
167 * Get NetconfOperationService iif all required capabilities are present.
169 * @param expectedCapabilities that must be provided by configNetconfConnector
170 * @param idForReporting
171 * @return service if capabilities are present, otherwise absent value
173 private NetconfOperationService getOperationService(Set<String> expectedCapabilities, String idForReporting) throws NotEnoughCapabilitiesException {
174 NetconfOperationService serviceCandidate;
176 serviceCandidate = configNetconfConnector.createService(idForReporting);
177 } catch(RuntimeException e) {
178 throw new NotEnoughCapabilitiesException("Netconf service not stable for " + idForReporting, e);
180 Set<String> notFoundDiff = computeNotFoundCapabilities(expectedCapabilities, configNetconfConnector);
181 if (notFoundDiff.isEmpty()) {
182 return serviceCandidate;
184 serviceCandidate.close();
185 LOG.trace("Netconf server did not provide required capabilities for {} ", idForReporting,
186 "Expected but not found: {}, all expected {}, current {}",
187 notFoundDiff, expectedCapabilities, configNetconfConnector.getCapabilities()
189 throw new NotEnoughCapabilitiesException("Not enough capabilities for " + idForReporting + ". Expected but not found: " + notFoundDiff);
193 private static Set<String> computeNotFoundCapabilities(Set<String> expectedCapabilities, NetconfOperationServiceFactory serviceCandidate) {
194 Collection<String> actual = Collections2.transform(serviceCandidate.getCapabilities(), new Function<Capability, String>() {
196 public String apply(@Nonnull final Capability input) {
197 return input.getCapabilityUri();
200 Set<String> allNotFound = new HashSet<>(expectedCapabilities);
201 allNotFound.removeAll(actual);
207 private void sleep() {
210 } catch (InterruptedException e) {
211 Thread.currentThread().interrupt();
212 throw new IllegalStateException(e);
217 * Sends two RPCs to the netconf server: edit-config and commit.
219 * @param configSnapshotHolder
220 * @throws ConflictingVersionException if commit fails on optimistic lock failure inside of config-manager
221 * @throws java.lang.RuntimeException if edit-config or commit fails otherwise
223 private synchronized EditAndCommitResponse pushConfig(ConfigSnapshotHolder configSnapshotHolder, NetconfOperationService operationService)
224 throws ConflictingVersionException, NetconfDocumentedException {
226 Element xmlToBePersisted;
228 xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
229 } catch (SAXException | IOException e) {
230 throw new IllegalStateException("Cannot parse " + configSnapshotHolder);
232 LOG.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
233 Stopwatch stopwatch = Stopwatch.createStarted();
234 NetconfMessage editConfigMessage = createEditConfigMessage(xmlToBePersisted);
236 Document editResponseMessage = sendRequestGetResponseCheckIsOK(editConfigMessage, operationService,
237 "edit-config", configSnapshotHolder.toString());
239 Document commitResponseMessage = sendRequestGetResponseCheckIsOK(getCommitMessage(), operationService,
240 "commit", configSnapshotHolder.toString());
242 if (LOG.isTraceEnabled()) {
243 StringBuilder response = new StringBuilder("editConfig response = {");
244 response.append(XmlUtil.toString(editResponseMessage));
245 response.append("}");
246 response.append("commit response = {");
247 response.append(XmlUtil.toString(commitResponseMessage));
248 response.append("}");
249 LOG.trace("Last configuration loaded successfully");
250 LOG.trace("Detailed message {}", response);
251 LOG.trace("Total time spent {} ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
253 return new EditAndCommitResponse(editResponseMessage, commitResponseMessage);
256 private NetconfOperation findOperation(NetconfMessage request, NetconfOperationService operationService) throws NetconfDocumentedException {
257 TreeMap<HandlingPriority, NetconfOperation> allOperations = new TreeMap<>();
258 Set<NetconfOperation> netconfOperations = operationService.getNetconfOperations();
259 if (netconfOperations.isEmpty()) {
260 throw new IllegalStateException("Possible code error: no config operations");
262 for (NetconfOperation netconfOperation : netconfOperations) {
263 HandlingPriority handlingPriority = netconfOperation.canHandle(request.getDocument());
264 allOperations.put(handlingPriority, netconfOperation);
266 Entry<HandlingPriority, NetconfOperation> highestEntry = allOperations.lastEntry();
267 if (highestEntry.getKey().isCannotHandle()) {
268 throw new IllegalStateException("Possible code error: operation with highest priority is CANNOT_HANDLE");
270 return highestEntry.getValue();
273 private Document sendRequestGetResponseCheckIsOK(NetconfMessage request, NetconfOperationService operationService,
274 String operationNameForReporting, String configIdForReporting)
275 throws ConflictingVersionException, NetconfDocumentedException {
277 NetconfOperation operation = findOperation(request, operationService);
280 response = operation.handle(request.getDocument(), NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
281 } catch (NetconfDocumentedException | RuntimeException e) {
282 if (e instanceof NetconfDocumentedException && e.getCause() instanceof ConflictingVersionException) {
283 throw (ConflictingVersionException) e.getCause();
285 throw new IllegalStateException("Failed to send " + operationNameForReporting +
286 " for configuration " + configIdForReporting, e);
288 return NetconfUtil.checkIsMessageOk(response);
291 // load editConfig.xml template, populate /rpc/edit-config/config with parameter
292 private static NetconfMessage createEditConfigMessage(Element dataElement) throws NetconfDocumentedException {
293 String editConfigResourcePath = "/netconfOp/editConfig.xml";
294 try (InputStream stream = ConfigPersisterNotificationHandler.class.getResourceAsStream(editConfigResourcePath)) {
295 checkNotNull(stream, "Unable to load resource " + editConfigResourcePath);
297 Document doc = XmlUtil.readXmlToDocument(stream);
299 XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
300 XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
301 editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
302 for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
304 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), deep));
306 editConfigElement.appendChild(configWrapper.getDomElement());
307 return new NetconfMessage(doc);
308 } catch (IOException | SAXException e) {
309 // error reading the xml file bundled into the jar
310 throw new IllegalStateException("Error while opening local resource " + editConfigResourcePath, e);
314 private static NetconfMessage getCommitMessage() {
315 String resource = "/netconfOp/commit.xml";
316 try (InputStream stream = ConfigPusherImpl.class.getResourceAsStream(resource)) {
317 checkNotNull(stream, "Unable to load resource " + resource);
318 return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
319 } catch (SAXException | IOException e) {
320 // error reading the xml file bundled into the jar
321 throw new IllegalStateException("Error while opening local resource " + resource, e);
325 static class EditAndCommitResponse {
326 private final Document editResponse, commitResponse;
328 EditAndCommitResponse(Document editResponse, Document commitResponse) {
329 this.editResponse = editResponse;
330 this.commitResponse = commitResponse;
333 public Document getEditResponse() {
337 public Document getCommitResponse() {
338 return commitResponse;
342 public String toString() {
343 return "EditAndCommitResponse{" +
344 "editResponse=" + editResponse +
345 ", commitResponse=" + commitResponse +