Merge "Bug 2697: Improvement wrong response handling, missing message"
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / ConfigPusherImpl.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 static com.google.common.base.Preconditions.checkNotNull;
12
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;
23 import java.util.Set;
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;
53
54 @Immutable
55 public class ConfigPusherImpl implements ConfigPusher {
56     private static final Logger LOG = LoggerFactory.getLogger(ConfigPusherImpl.class);
57
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);
63
64     public ConfigPusherImpl(NetconfOperationServiceFactory configNetconfConnector, long maxWaitForCapabilitiesMillis,
65                         long conflictingVersionTimeoutMillis) {
66         this.configNetconfConnector = configNetconfConnector;
67         this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
68         this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
69     }
70
71     public void process(List<AutoCloseable> autoCloseables, MBeanServerConnection platformMBeanServer, Persister persisterAggregator) throws InterruptedException {
72         List<? extends ConfigSnapshotHolder> configs;
73         while(true) {
74             configs = queue.take();
75             try {
76                 internalPushConfigs(configs);
77                 ConfigPersisterNotificationHandler jmxNotificationHandler = new ConfigPersisterNotificationHandler(platformMBeanServer, persisterAggregator);
78                 synchronized (autoCloseables) {
79                     autoCloseables.add(jmxNotificationHandler);
80                 }
81
82                 LOG.debug("ConfigPusher has pushed configs {}", configs);
83             } catch (NetconfDocumentedException e) {
84                 LOG.error("Error pushing configs {}",configs);
85                 throw new IllegalStateException(e);
86             }
87         }
88     }
89
90     public void pushConfigs(List<? extends ConfigSnapshotHolder> configs) throws InterruptedException {
91         LOG.debug("Requested to push configs {}", configs);
92         this.queue.put(configs);
93     }
94
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);
104             }
105         }
106         LOG.debug("All configuration snapshots have been pushed successfully.");
107         return result;
108     }
109
110     /**
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.
115      */
116     private synchronized EditAndCommitResponse pushConfigWithConflictingVersionRetries(ConfigSnapshotHolder configSnapshotHolder) throws NetconfDocumentedException {
117         ConflictingVersionException lastException;
118         Stopwatch stopwatch = Stopwatch.createUnstarted();
119         do {
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()) {
126                     stopwatch.start();
127                 }
128                 return pushConfig(configSnapshotHolder, operationService);
129             } catch (ConflictingVersionException e) {
130                 lastException = e;
131                 LOG.info("Conflicting version detected, will retry after timeout");
132                 sleep();
133             }
134         } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < conflictingVersionTimeoutMillis);
135         throw new IllegalStateException("Max wait for conflicting version stabilization timeout after " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms",
136                 lastException);
137     }
138
139     private NetconfOperationService getOperationServiceWithRetries(Set<String> expectedCapabilities, String idForReporting) {
140         Stopwatch stopwatch = Stopwatch.createStarted();
141         NotEnoughCapabilitiesException lastException;
142         do {
143             try {
144                 return getOperationService(expectedCapabilities, idForReporting);
145             } catch (NotEnoughCapabilitiesException e) {
146                 LOG.debug("Not enough capabilities: {}", e.toString());
147                 lastException = e;
148                 sleep();
149             }
150         } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < maxWaitForCapabilitiesMillis);
151         throw new IllegalStateException("Max wait for capabilities reached." + lastException.getMessage(), lastException);
152     }
153
154     private static class NotEnoughCapabilitiesException extends Exception {
155         private static final long serialVersionUID = 1L;
156
157         private NotEnoughCapabilitiesException(String message, Throwable cause) {
158             super(message, cause);
159         }
160
161         private NotEnoughCapabilitiesException(String message) {
162             super(message);
163         }
164     }
165
166     /**
167      * Get NetconfOperationService iif all required capabilities are present.
168      *
169      * @param expectedCapabilities that must be provided by configNetconfConnector
170      * @param idForReporting
171      * @return service if capabilities are present, otherwise absent value
172      */
173     private NetconfOperationService getOperationService(Set<String> expectedCapabilities, String idForReporting) throws NotEnoughCapabilitiesException {
174         NetconfOperationService serviceCandidate;
175         try {
176             serviceCandidate = configNetconfConnector.createService(idForReporting);
177         } catch(RuntimeException e) {
178             throw new NotEnoughCapabilitiesException("Netconf service not stable for " + idForReporting, e);
179         }
180         Set<String> notFoundDiff = computeNotFoundCapabilities(expectedCapabilities, configNetconfConnector);
181         if (notFoundDiff.isEmpty()) {
182             return serviceCandidate;
183         } else {
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()
188             );
189             throw new NotEnoughCapabilitiesException("Not enough capabilities for " + idForReporting + ". Expected but not found: " + notFoundDiff);
190         }
191     }
192
193     private static Set<String> computeNotFoundCapabilities(Set<String> expectedCapabilities, NetconfOperationServiceFactory serviceCandidate) {
194         Collection<String> actual = Collections2.transform(serviceCandidate.getCapabilities(), new Function<Capability, String>() {
195             @Override
196             public String apply(@Nonnull final Capability input) {
197                 return input.getCapabilityUri();
198             }
199         });
200         Set<String> allNotFound = new HashSet<>(expectedCapabilities);
201         allNotFound.removeAll(actual);
202         return allNotFound;
203     }
204
205
206
207     private void sleep() {
208         try {
209             Thread.sleep(100);
210         } catch (InterruptedException e) {
211             Thread.currentThread().interrupt();
212             throw new IllegalStateException(e);
213         }
214     }
215
216     /**
217      * Sends two RPCs to the netconf server: edit-config and commit.
218      *
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
222      */
223     private synchronized EditAndCommitResponse pushConfig(ConfigSnapshotHolder configSnapshotHolder, NetconfOperationService operationService)
224             throws ConflictingVersionException, NetconfDocumentedException {
225
226         Element xmlToBePersisted;
227         try {
228             xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
229         } catch (SAXException | IOException e) {
230             throw new IllegalStateException("Cannot parse " + configSnapshotHolder);
231         }
232         LOG.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
233         Stopwatch stopwatch = Stopwatch.createStarted();
234         NetconfMessage editConfigMessage = createEditConfigMessage(xmlToBePersisted);
235
236         Document editResponseMessage = sendRequestGetResponseCheckIsOK(editConfigMessage, operationService,
237                 "edit-config", configSnapshotHolder.toString());
238
239         Document commitResponseMessage = sendRequestGetResponseCheckIsOK(getCommitMessage(), operationService,
240                 "commit", configSnapshotHolder.toString());
241
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));
252         }
253         return new EditAndCommitResponse(editResponseMessage, commitResponseMessage);
254     }
255
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");
261         }
262         for (NetconfOperation netconfOperation : netconfOperations) {
263             HandlingPriority handlingPriority = netconfOperation.canHandle(request.getDocument());
264             allOperations.put(handlingPriority, netconfOperation);
265         }
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");
269         }
270         return highestEntry.getValue();
271     }
272
273     private Document sendRequestGetResponseCheckIsOK(NetconfMessage request, NetconfOperationService operationService,
274                                                      String operationNameForReporting, String configIdForReporting)
275             throws ConflictingVersionException, NetconfDocumentedException {
276
277         NetconfOperation operation = findOperation(request, operationService);
278         Document response;
279         try {
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();
284             }
285             throw new IllegalStateException("Failed to send " + operationNameForReporting +
286                     " for configuration " + configIdForReporting, e);
287         }
288         return NetconfUtil.checkIsMessageOk(response);
289     }
290
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);
296
297             Document doc = XmlUtil.readXmlToDocument(stream);
298
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()) {
303                 boolean deep = true;
304                 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), deep));
305             }
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);
311         }
312     }
313
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);
322         }
323     }
324
325     static class EditAndCommitResponse {
326         private final Document editResponse, commitResponse;
327
328         EditAndCommitResponse(Document editResponse, Document commitResponse) {
329             this.editResponse = editResponse;
330             this.commitResponse = commitResponse;
331         }
332
333         public Document getEditResponse() {
334             return editResponse;
335         }
336
337         public Document getCommitResponse() {
338             return commitResponse;
339         }
340
341         @Override
342         public String toString() {
343             return "EditAndCommitResponse{" +
344                     "editResponse=" + editResponse +
345                     ", commitResponse=" + commitResponse +
346                     '}';
347         }
348     }
349 }