BUG-997 Use shared schema context factory in netconf-connector
[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 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.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;
47
48 @Immutable
49 public class ConfigPusher {
50     private static final Logger logger = LoggerFactory.getLogger(ConfigPusher.class);
51
52     private final long maxWaitForCapabilitiesMillis;
53     private final long conflictingVersionTimeoutMillis;
54     private final NetconfOperationServiceFactory configNetconfConnector;
55
56     public ConfigPusher(NetconfOperationServiceFactory configNetconfConnector, long maxWaitForCapabilitiesMillis,
57                         long conflictingVersionTimeoutMillis) {
58         this.configNetconfConnector = configNetconfConnector;
59         this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
60         this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
61     }
62
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);
72             }
73         }
74         logger.debug("All configuration snapshots have been pushed successfully.");
75         return result;
76     }
77
78     /**
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.
83      */
84     private synchronized EditAndCommitResponse pushConfigWithConflictingVersionRetries(ConfigSnapshotHolder configSnapshotHolder) throws NetconfDocumentedException {
85         ConflictingVersionException lastException;
86         Stopwatch stopwatch = new Stopwatch().start();
87         do {
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) {
95                 lastException = e;
96                 logger.debug("Conflicting version detected, will retry after timeout");
97                 sleep();
98             }
99         } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < conflictingVersionTimeoutMillis);
100         throw new IllegalStateException("Max wait for conflicting version stabilization timeout after " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms",
101                 lastException);
102     }
103
104     private NetconfOperationService getOperationServiceWithRetries(Set<String> expectedCapabilities, String idForReporting) {
105         Stopwatch stopwatch = new Stopwatch().start();
106         NotEnoughCapabilitiesException lastException;
107         do {
108             try {
109                 return getOperationService(expectedCapabilities, idForReporting);
110             } catch (NotEnoughCapabilitiesException e) {
111                 logger.debug("Not enough capabilities: " + e.toString());
112                 lastException = e;
113                 sleep();
114             }
115         } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < maxWaitForCapabilitiesMillis);
116         throw new IllegalStateException("Max wait for capabilities reached." + lastException.getMessage(), lastException);
117     }
118
119     private static class NotEnoughCapabilitiesException extends Exception {
120         private static final long serialVersionUID = 1L;
121
122         private NotEnoughCapabilitiesException(String message, Throwable cause) {
123             super(message, cause);
124         }
125
126         private NotEnoughCapabilitiesException(String message) {
127             super(message);
128         }
129     }
130
131     /**
132      * Get NetconfOperationService iif all required capabilities are present.
133      *
134      * @param expectedCapabilities that must be provided by configNetconfConnector
135      * @param idForReporting
136      * @return service if capabilities are present, otherwise absent value
137      */
138     private NetconfOperationService getOperationService(Set<String> expectedCapabilities, String idForReporting) throws NotEnoughCapabilitiesException {
139         NetconfOperationService serviceCandidate;
140         try {
141             serviceCandidate = configNetconfConnector.createService(idForReporting);
142         } catch(RuntimeException e) {
143             throw new NotEnoughCapabilitiesException("Netconf service not stable for " + idForReporting, e);
144         }
145         Set<String> notFoundDiff = computeNotFoundCapabilities(expectedCapabilities, serviceCandidate);
146         if (notFoundDiff.isEmpty()) {
147             return serviceCandidate;
148         } else {
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()
153             );
154             throw new NotEnoughCapabilitiesException("Not enough capabilities for " + idForReporting + ". Expected but not found: " + notFoundDiff);
155         }
156     }
157
158     private static Set<String> computeNotFoundCapabilities(Set<String> expectedCapabilities, NetconfOperationService serviceCandidate) {
159         Collection<String> actual = Collections2.transform(serviceCandidate.getCapabilities(), new Function<Capability, String>() {
160             @Override
161             public String apply(Capability input) {
162                 return input.getCapabilityUri();
163             }
164         });
165         Set<String> allNotFound = new HashSet<>(expectedCapabilities);
166         allNotFound.removeAll(actual);
167         return allNotFound;
168     }
169
170
171
172     private void sleep() {
173         try {
174             Thread.sleep(100);
175         } catch (InterruptedException e) {
176             Thread.currentThread().interrupt();
177             throw new IllegalStateException(e);
178         }
179     }
180
181     /**
182      * Sends two RPCs to the netconf server: edit-config and commit.
183      *
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
187      */
188     private synchronized EditAndCommitResponse pushConfig(ConfigSnapshotHolder configSnapshotHolder, NetconfOperationService operationService)
189             throws ConflictingVersionException, NetconfDocumentedException {
190
191         Element xmlToBePersisted;
192         try {
193             xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
194         } catch (SAXException | IOException e) {
195             throw new IllegalStateException("Cannot parse " + configSnapshotHolder);
196         }
197         logger.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
198         Stopwatch stopwatch = new Stopwatch().start();
199         NetconfMessage editConfigMessage = createEditConfigMessage(xmlToBePersisted);
200
201         Document editResponseMessage = sendRequestGetResponseCheckIsOK(editConfigMessage, operationService,
202                 "edit-config", configSnapshotHolder.toString());
203
204         Document commitResponseMessage = sendRequestGetResponseCheckIsOK(getCommitMessage(), operationService,
205                 "commit", configSnapshotHolder.toString());
206
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));
217         }
218         return new EditAndCommitResponse(editResponseMessage, commitResponseMessage);
219     }
220
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");
226         }
227         for (NetconfOperation netconfOperation : netconfOperations) {
228             HandlingPriority handlingPriority = netconfOperation.canHandle(request.getDocument());
229             allOperations.put(handlingPriority, netconfOperation);
230         }
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");
234         }
235         return highestEntry.getValue();
236     }
237
238     private Document sendRequestGetResponseCheckIsOK(NetconfMessage request, NetconfOperationService operationService,
239                                                      String operationNameForReporting, String configIdForReporting)
240             throws ConflictingVersionException, NetconfDocumentedException {
241
242         NetconfOperation operation = findOperation(request, operationService);
243         Document response;
244         try {
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();
249             }
250             throw new IllegalStateException("Failed to send " + operationNameForReporting +
251                     " for configuration " + configIdForReporting, e);
252         }
253         return NetconfUtil.checkIsMessageOk(response);
254     }
255
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);
261
262             Document doc = XmlUtil.readXmlToDocument(stream);
263
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()) {
268                 boolean deep = true;
269                 configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), deep));
270             }
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);
276         }
277     }
278
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);
287         }
288     }
289
290     static class EditAndCommitResponse {
291         private final Document editResponse, commitResponse;
292
293         EditAndCommitResponse(Document editResponse, Document commitResponse) {
294             this.editResponse = editResponse;
295             this.commitResponse = commitResponse;
296         }
297
298         public Document getEditResponse() {
299             return editResponse;
300         }
301
302         public Document getCommitResponse() {
303             return commitResponse;
304         }
305
306         @Override
307         public String toString() {
308             return "EditAndCommitResponse{" +
309                     "editResponse=" + editResponse +
310                     ", commitResponse=" + commitResponse +
311                     '}';
312         }
313     }
314 }