Bug 2806 - Immediate and infinite reconnect attempts during negotiation
[netconf.git] / opendaylight / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / schema / mapping / NetconfMessageTransformer.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.netconf.sal.connect.netconf.schema.mapping;
9
10 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.EVENT_TIME;
11 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RPC_QNAME;
12 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_URI;
13 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
14 import com.google.common.base.Function;
15 import com.google.common.base.Preconditions;
16 import com.google.common.collect.Lists;
17 import com.google.common.collect.Maps;
18 import com.google.common.collect.Multimap;
19 import com.google.common.collect.Multimaps;
20 import java.io.IOException;
21 import java.text.ParseException;
22 import java.text.SimpleDateFormat;
23 import java.util.AbstractMap;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.Map;
29 import javax.annotation.Nonnull;
30 import javax.xml.stream.XMLStreamException;
31 import javax.xml.stream.XMLStreamWriter;
32 import javax.xml.transform.dom.DOMResult;
33 import org.opendaylight.controller.config.util.xml.DocumentedException;
34 import org.opendaylight.controller.config.util.xml.MissingNameSpaceException;
35 import org.opendaylight.controller.config.util.xml.XmlElement;
36 import org.opendaylight.controller.config.util.xml.XmlUtil;
37 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
38 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
39 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
40 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
41 import org.opendaylight.netconf.api.NetconfMessage;
42 import org.opendaylight.netconf.notifications.NetconfNotification;
43 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
44 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
45 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
46 import org.opendaylight.netconf.util.OrderedNormalizedNodeWriter;
47 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
52 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
54 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
55 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
56 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
57 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
58 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
60 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
62 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66 import org.w3c.dom.Document;
67 import org.w3c.dom.Element;
68
69 public class NetconfMessageTransformer implements MessageTransformer<NetconfMessage> {
70
71     public static final String MESSAGE_ID_PREFIX = "m";
72
73     private static final Logger LOG= LoggerFactory.getLogger(NetconfMessageTransformer.class);
74
75
76     private static final Function<SchemaNode, QName> QNAME_FUNCTION = new Function<SchemaNode, QName>() {
77         @Override
78         public QName apply(final SchemaNode rpcDefinition) {
79             return rpcDefinition.getQName();
80         }
81     };
82
83     private static final Function<SchemaNode, QName> QNAME_NOREV_FUNCTION = new Function<SchemaNode, QName>() {
84         @Override
85         public QName apply(final SchemaNode notification) {
86             return QNAME_FUNCTION.apply(notification).withoutRevision();
87         }
88     };
89     private static final SchemaContext BASE_NETCONF_CTX;
90
91     static {
92         try {
93             final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
94             moduleInfoBackedContext.addModuleInfos(
95                     Lists.newArrayList(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl.getInstance()));
96             BASE_NETCONF_CTX = moduleInfoBackedContext.tryToCreateSchemaContext().get();
97         } catch (final RuntimeException e) {
98             LOG.error("Unable to prepare schema context for base netconf ops", e);
99             throw new ExceptionInInitializerError(e);
100         }
101     }
102     private static final Map<QName, RpcDefinition> MAPPED_BASE_RPCS = Maps.uniqueIndex(BASE_NETCONF_CTX.getOperations(), QNAME_FUNCTION);
103
104     private final SchemaContext schemaContext;
105     private final MessageCounter counter;
106     private final Map<QName, RpcDefinition> mappedRpcs;
107     private final Multimap<QName, NotificationDefinition> mappedNotifications;
108     private final DomToNormalizedNodeParserFactory parserFactory;
109
110     public NetconfMessageTransformer(final SchemaContext schemaContext, final boolean strictParsing) {
111         this.counter = new MessageCounter();
112         this.schemaContext = schemaContext;
113         parserFactory = DomToNormalizedNodeParserFactory.getInstance(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, schemaContext, strictParsing);
114
115         mappedRpcs = Maps.uniqueIndex(schemaContext.getOperations(), QNAME_FUNCTION);
116         mappedNotifications = Multimaps.index(schemaContext.getNotifications(), QNAME_NOREV_FUNCTION);
117     }
118
119     @Override
120     public synchronized DOMNotification toNotification(final NetconfMessage message) {
121         final Map.Entry<Date, XmlElement> stripped = stripNotification(message);
122         final QName notificationNoRev;
123         try {
124             notificationNoRev = QName.create(stripped.getValue().getNamespace(), stripped.getValue().getName()).withoutRevision();
125         } catch (final MissingNameSpaceException e) {
126             throw new IllegalArgumentException("Unable to parse notification " + message + ", cannot find namespace", e);
127         }
128
129         final Collection<NotificationDefinition> notificationDefinitions = mappedNotifications.get(notificationNoRev);
130         Preconditions.checkArgument(notificationDefinitions.size() > 0,
131                 "Unable to parse notification %s, unknown notification. Available notifications: %s", notificationDefinitions, mappedNotifications.keySet());
132
133         // FIXME if multiple revisions for same notifications are present, we should pick the most recent. Or ?
134         // We should probably just put the most recent notification versions into our map. We can expect that the device sends the data according to the latest available revision of a model.
135         final NotificationDefinition next = notificationDefinitions.iterator().next();
136
137         // We wrap the notification as a container node in order to reuse the parsers and builders for container node
138         final ContainerSchemaNode notificationAsContainerSchemaNode = NetconfMessageTransformUtil.createSchemaForNotification(next);
139
140         final Element element = stripped.getValue().getDomElement();
141         final ContainerNode content;
142         try {
143             content = parserFactory.getContainerNodeParser().parse(Collections.singleton(element),
144                 notificationAsContainerSchemaNode);
145         } catch (IllegalArgumentException e) {
146             throw new IllegalArgumentException(String.format("Failed to parse notification %s", element), e);
147         }
148         return new NetconfDeviceNotification(content, stripped.getKey());
149     }
150
151     private static final ThreadLocal<SimpleDateFormat> EVENT_TIME_FORMAT = new ThreadLocal<SimpleDateFormat>() {
152         @Override
153         protected SimpleDateFormat initialValue() {
154
155             final SimpleDateFormat withMillis = new SimpleDateFormat(
156                 NetconfNotification.RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT);
157
158             return new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT) {
159                 private static final long serialVersionUID = 1L;
160
161                 @Override public Date parse(final String source) throws ParseException {
162                     try {
163                         return super.parse(source);
164                     } catch (ParseException e) {
165                         // In case of failure, try to parse with milliseconds
166                         return withMillis.parse(source);
167                     }
168                 }
169             };
170         }
171
172         @Override
173         public void set(final SimpleDateFormat value) {
174             throw new UnsupportedOperationException();
175         }
176     };
177
178     // FIXME move somewhere to util
179     private static Map.Entry<Date, XmlElement> stripNotification(final NetconfMessage message) {
180         final XmlElement xmlElement = XmlElement.fromDomDocument(message.getDocument());
181         final List<XmlElement> childElements = xmlElement.getChildElements();
182         Preconditions.checkArgument(childElements.size() == 2, "Unable to parse notification %s, unexpected format", message);
183
184         final XmlElement eventTimeElement;
185         final XmlElement notificationElement;
186
187         if (childElements.get(0).getName().equals(EVENT_TIME)) {
188             eventTimeElement = childElements.get(0);
189             notificationElement = childElements.get(1);
190         }
191         else if(childElements.get(1).getName().equals(EVENT_TIME)) {
192             eventTimeElement = childElements.get(1);
193             notificationElement = childElements.get(0);
194         } else {
195             throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message);
196         }
197
198         try {
199             return new AbstractMap.SimpleEntry<>(EVENT_TIME_FORMAT.get().parse(eventTimeElement.getTextContent()), notificationElement);
200         } catch (DocumentedException e) {
201             throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message);
202         } catch (ParseException e) {
203             LOG.warn("Unable to parse event time from {}. Setting time to {}", eventTimeElement, NetconfNotification.UNKNOWN_EVENT_TIME, e);
204             return new AbstractMap.SimpleEntry<>(NetconfNotification.UNKNOWN_EVENT_TIME, notificationElement);
205         }
206     }
207
208     @Override
209     public NetconfMessage toRpcRequest(SchemaPath rpc, final NormalizedNode<?, ?> payload) {
210         // In case no input for rpc is defined, we can simply construct the payload here
211         final QName rpcQName = rpc.getLastComponent();
212         Map<QName, RpcDefinition> currentMappedRpcs = mappedRpcs;
213
214         // Determine whether a base netconf operation is being invoked and also check if the device exposed model for base netconf
215         // If no, use pre built base netconf operations model
216         final boolean needToUseBaseCtx = mappedRpcs.get(rpcQName) == null && isBaseRpc(rpcQName);
217         if(needToUseBaseCtx) {
218             currentMappedRpcs = MAPPED_BASE_RPCS;
219         }
220
221         Preconditions.checkNotNull(currentMappedRpcs.get(rpcQName), "Unknown rpc %s, available rpcs: %s", rpcQName, currentMappedRpcs.keySet());
222         if(currentMappedRpcs.get(rpcQName).getInput() == null) {
223             return new NetconfMessage(prepareDomResultForRpcRequest(rpcQName).getNode().getOwnerDocument());
224         }
225
226         Preconditions.checkNotNull(payload, "Transforming an rpc with input: %s, payload cannot be null", rpcQName);
227         Preconditions.checkArgument(payload instanceof ContainerNode,
228                 "Transforming an rpc with input: %s, payload has to be a container, but was: %s", rpcQName, payload);
229
230         // Set the path to the input of rpc for the node stream writer
231         rpc = rpc.createChild(QName.create(rpcQName, "input").intern());
232         final DOMResult result = prepareDomResultForRpcRequest(rpcQName);
233
234         try {
235             // If the schema context for netconf device does not contain model for base netconf operations, use default pre build context with just the base model
236             // This way operations like lock/unlock are supported even if the source for base model was not provided
237             writeNormalizedRpc(((ContainerNode) payload), result, rpc, needToUseBaseCtx ? BASE_NETCONF_CTX : schemaContext);
238         } catch (final XMLStreamException | IOException | IllegalStateException e) {
239             throw new IllegalStateException("Unable to serialize " + rpc, e);
240         }
241
242         final Document node = result.getNode().getOwnerDocument();
243
244         return new NetconfMessage(node);
245     }
246
247     private static boolean isBaseRpc(final QName rpc) {
248         return rpc.getNamespace().equals(NETCONF_URI);
249     }
250
251     private DOMResult prepareDomResultForRpcRequest(final QName rpcQName) {
252         final Document document = XmlUtil.newDocument();
253         final Element rpcNS = document.createElementNS(NETCONF_RPC_QNAME.getNamespace().toString(), NETCONF_RPC_QNAME.getLocalName());
254         // set msg id
255         rpcNS.setAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR, counter.getNewMessageId(MESSAGE_ID_PREFIX));
256         final Element elementNS = document.createElementNS(rpcQName.getNamespace().toString(), rpcQName.getLocalName());
257         rpcNS.appendChild(elementNS);
258         document.appendChild(rpcNS);
259         return new DOMResult(elementNS);
260     }
261
262     private static void writeNormalizedRpc(final ContainerNode normalized, final DOMResult result,
263             final SchemaPath schemaPath, final SchemaContext baseNetconfCtx) throws IOException, XMLStreamException {
264         final XMLStreamWriter writer = NetconfMessageTransformUtil.XML_FACTORY.createXMLStreamWriter(result);
265         try {
266             try (final NormalizedNodeStreamWriter normalizedNodeStreamWriter =
267                     XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath)) {
268                 try (final OrderedNormalizedNodeWriter normalizedNodeWriter =
269                         new OrderedNormalizedNodeWriter(normalizedNodeStreamWriter, baseNetconfCtx, schemaPath)) {
270                     Collection<DataContainerChild<?, ?>> value = normalized.getValue();
271                     normalizedNodeWriter.write(value);
272                     normalizedNodeWriter.flush();
273                 }
274             }
275         } finally {
276             try {
277                 writer.close();
278             } catch (final Exception e) {
279                 LOG.warn("Unable to close resource properly", e);
280             }
281         }
282     }
283
284     @Override
285     public synchronized DOMRpcResult toRpcResult(final NetconfMessage message, final SchemaPath rpc) {
286         final NormalizedNode<?, ?> normalizedNode;
287         final QName rpcQName = rpc.getLastComponent();
288         if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpcQName)) {
289             final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument());
290             final ContainerSchemaNode schemaForDataRead = NetconfMessageTransformUtil.createSchemaForDataRead(schemaContext);
291             final ContainerNode dataNode;
292
293             try {
294                 dataNode = parserFactory.getContainerNodeParser().parse(Collections.singleton(xmlData), schemaForDataRead);
295             } catch (IllegalArgumentException e) {
296                 throw new IllegalArgumentException(String.format("Failed to parse data response %s", xmlData), e);
297             }
298
299             normalizedNode = Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
300                     .withChild(dataNode).build();
301         } else {
302
303             Map<QName, RpcDefinition> currentMappedRpcs = mappedRpcs;
304
305             // Determine whether a base netconf operation is being invoked and also check if the device exposed model for base netconf
306             // If no, use pre built base netconf operations model
307             final boolean needToUseBaseCtx = mappedRpcs.get(rpcQName) == null && isBaseRpc(rpcQName);
308             if(needToUseBaseCtx) {
309                 currentMappedRpcs = MAPPED_BASE_RPCS;
310             }
311
312             final RpcDefinition rpcDefinition = currentMappedRpcs.get(rpcQName);
313             Preconditions.checkArgument(rpcDefinition != null, "Unable to parse response of %s, the rpc is unknown", rpcQName);
314
315             // In case no input for rpc is defined, we can simply construct the payload here
316             if (rpcDefinition.getOutput() == null) {
317                 Preconditions.checkArgument(XmlElement.fromDomDocument(
318                     message.getDocument()).getOnlyChildElementWithSameNamespaceOptionally("ok").isPresent(),
319                     "Unexpected content in response of rpc: %s, %s", rpcDefinition.getQName(), message);
320                 normalizedNode = null;
321             } else {
322                 final Element element = message.getDocument().getDocumentElement();
323                 try {
324                     normalizedNode = parserFactory.getContainerNodeParser().parse(Collections.singleton(element),
325                         rpcDefinition.getOutput());
326                 } catch (IllegalArgumentException e) {
327                     throw new IllegalArgumentException(String.format("Failed to parse RPC response %s", element), e);
328                 }
329             }
330         }
331         return new DefaultDOMRpcResult(normalizedNode);
332     }
333
334     private static class NetconfDeviceNotification implements DOMNotification, DOMEvent {
335         private final ContainerNode content;
336         private final SchemaPath schemaPath;
337         private final Date eventTime;
338
339         NetconfDeviceNotification(final ContainerNode content, final Date eventTime) {
340             this.content = content;
341             this.eventTime = eventTime;
342             this.schemaPath = toPath(content.getNodeType());
343         }
344
345         @Nonnull
346         @Override
347         public SchemaPath getType() {
348             return schemaPath;
349
350         }
351
352         @Nonnull
353         @Override
354         public ContainerNode getBody() {
355             return content;
356         }
357
358         @Override
359         public Date getEventTime() {
360             return eventTime;
361         }
362     }
363 }