2b1476f8220394f953d25f76814e09f858ce48d6
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / util / NetconfMessageTransformUtil.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.util;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.io.IOException;
15 import java.net.URI;
16 import java.time.format.DateTimeParseException;
17 import java.util.AbstractMap;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26 import javax.xml.transform.dom.DOMResult;
27 import javax.xml.transform.dom.DOMSource;
28 import org.opendaylight.controller.config.util.xml.DocumentedException;
29 import org.opendaylight.controller.config.util.xml.XmlElement;
30 import org.opendaylight.controller.config.util.xml.XmlUtil;
31 import org.opendaylight.netconf.api.NetconfDocumentedException;
32 import org.opendaylight.netconf.api.NetconfMessage;
33 import org.opendaylight.netconf.notifications.NetconfNotification;
34 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
35 import org.opendaylight.netconf.util.NetconfUtil;
36 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.edit.config.input.EditContent;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.RpcError;
43 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
49 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
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.schema.Builders;
56 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
57 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
59 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
62 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65 import org.w3c.dom.Document;
66 import org.w3c.dom.Element;
67
68 public class NetconfMessageTransformUtil {
69
70     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageTransformUtil.class);
71
72     public static final String MESSAGE_ID_PREFIX = "m";
73     public static final String MESSAGE_ID_ATTR = "message-id";
74
75     public static final QName CREATE_SUBSCRIPTION_RPC_QNAME = QName.create(CreateSubscriptionInput.QNAME, "create-subscription").intern();
76     private static final String SUBTREE = "subtree";
77
78     // Blank document used for creation of new DOM nodes
79     private static final Document BLANK_DOCUMENT = XmlUtil.newDocument();
80     public static final String EVENT_TIME = "eventTime";
81
82     private NetconfMessageTransformUtil() {}
83
84     public static final QName IETF_NETCONF_MONITORING = QName.create(NetconfState.QNAME, "ietf-netconf-monitoring").intern();
85     public static final QName GET_DATA_QNAME = QName.create(IETF_NETCONF_MONITORING, "data").intern();
86     public static final QName GET_SCHEMA_QNAME = QName.create(IETF_NETCONF_MONITORING, "get-schema").intern();
87     public static final QName IETF_NETCONF_MONITORING_SCHEMA_FORMAT = QName.create(IETF_NETCONF_MONITORING, "format").intern();
88     public static final QName IETF_NETCONF_MONITORING_SCHEMA_LOCATION = QName.create(IETF_NETCONF_MONITORING, "location").intern();
89     public static final QName IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER = QName.create(IETF_NETCONF_MONITORING, "identifier").intern();
90     public static final QName IETF_NETCONF_MONITORING_SCHEMA_VERSION = QName.create(IETF_NETCONF_MONITORING, "version").intern();
91     public static final QName IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE = QName.create(IETF_NETCONF_MONITORING, "namespace").intern();
92
93     public static final QName IETF_NETCONF_NOTIFICATIONS = QName.create(NetconfCapabilityChange.QNAME, "ietf-netconf-notifications").intern();
94
95     public static final QName NETCONF_QNAME = QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "netconf").intern();
96     public static final URI NETCONF_URI = NETCONF_QNAME.getNamespace();
97
98     public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data").intern();
99     public static final QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply").intern();
100     public static final QName NETCONF_OK_QNAME = QName.create(NETCONF_QNAME, "ok").intern();
101     public static final QName NETCONF_ERROR_OPTION_QNAME = QName.create(NETCONF_QNAME, "error-option").intern();
102     public static final QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running").intern();
103     public static final QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source").intern();
104     public static final QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate").intern();
105     public static final QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target").intern();
106     public static final QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config").intern();
107     public static final QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit").intern();
108     public static final QName NETCONF_VALIDATE_QNAME = QName.create(NETCONF_QNAME, "validate").intern();
109     public static final QName NETCONF_COPY_CONFIG_QNAME = QName.create(NETCONF_QNAME, "copy-config").intern();
110     public static final QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation").intern();
111     public static final QName NETCONF_DEFAULT_OPERATION_QNAME = QName.create(NETCONF_OPERATION_QNAME, "default-operation").intern();
112     public static final QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config").intern();
113     public static final QName NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config");
114     public static final QName NETCONF_DISCARD_CHANGES_QNAME = QName.create(NETCONF_QNAME, "discard-changes");
115     public static final QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type").intern();
116     public static final QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter").intern();
117     public static final QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get").intern();
118     public static final QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc").intern();
119
120     public static final URI NETCONF_ROLLBACK_ON_ERROR_URI = URI
121             .create("urn:ietf:params:netconf:capability:rollback-on-error:1.0");
122     public static final String ROLLBACK_ON_ERROR_OPTION = "rollback-on-error";
123
124     public static final URI NETCONF_CANDIDATE_URI = URI
125             .create("urn:ietf:params:netconf:capability:candidate:1.0");
126
127     public static final URI NETCONF_NOTIFICATONS_URI = URI
128             .create("urn:ietf:params:netconf:capability:notification:1.0");
129
130     public static final URI NETCONF_RUNNING_WRITABLE_URI = URI
131             .create("urn:ietf:params:netconf:capability:writable-running:1.0");
132
133     public static final QName NETCONF_LOCK_QNAME = QName.create(NETCONF_QNAME, "lock").intern();
134     public static final QName NETCONF_UNLOCK_QNAME = QName.create(NETCONF_QNAME, "unlock").intern();
135
136     // Discard changes message
137     public static final ContainerNode DISCARD_CHANGES_RPC_CONTENT =
138             Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(NETCONF_DISCARD_CHANGES_QNAME)).build();
139
140     // Commit changes message
141     public static final ContainerNode COMMIT_RPC_CONTENT =
142             Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(NETCONF_COMMIT_QNAME)).build();
143
144     // Get message
145     public static final ContainerNode GET_RPC_CONTENT =
146             Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(NETCONF_GET_QNAME)).build();
147
148     // Create-subscription changes message
149     public static final ContainerNode CREATE_SUBSCRIPTION_RPC_CONTENT =
150             Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(CREATE_SUBSCRIPTION_RPC_QNAME)).build();
151
152     public static final DataContainerChild<?, ?> EMPTY_FILTER;
153
154     static {
155         final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder = Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_FILTER_QNAME));
156         anyXmlBuilder.withAttributes(Collections.singletonMap(NETCONF_TYPE_QNAME, SUBTREE));
157
158         final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_FILTER_QNAME.getLocalName(), Optional.of(NETCONF_FILTER_QNAME.getNamespace().toString()));
159         element.setAttributeNS(NETCONF_FILTER_QNAME.getNamespace().toString(), NETCONF_TYPE_QNAME.getLocalName(), "subtree");
160
161         anyXmlBuilder.withValue(new DOMSource(element));
162
163         EMPTY_FILTER = anyXmlBuilder.build();
164     }
165
166     public static DataContainerChild<?, ?> toFilterStructure(final YangInstanceIdentifier identifier, final SchemaContext ctx) {
167         final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder = Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_FILTER_QNAME));
168         anyXmlBuilder.withAttributes(Collections.singletonMap(NETCONF_TYPE_QNAME, SUBTREE));
169
170         final NormalizedNode<?, ?> filterContent = ImmutableNodes.fromInstanceId(ctx, identifier);
171
172         final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_FILTER_QNAME.getLocalName(), Optional.of(NETCONF_FILTER_QNAME.getNamespace().toString()));
173         element.setAttributeNS(NETCONF_FILTER_QNAME.getNamespace().toString(), NETCONF_TYPE_QNAME.getLocalName(), "subtree");
174
175         try {
176             NetconfUtil.writeNormalizedNode(filterContent, new DOMResult(element), SchemaPath.ROOT, ctx);
177         } catch (IOException | XMLStreamException e) {
178             throw new IllegalStateException("Unable to serialize filter element for path " + identifier, e);
179         }
180         anyXmlBuilder.withValue(new DOMSource(element));
181
182         return anyXmlBuilder.build();
183     }
184
185     public static void checkValidReply(final NetconfMessage input, final NetconfMessage output)
186             throws NetconfDocumentedException {
187         final String inputMsgId = input.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
188         final String outputMsgId = output.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
189
190         if(inputMsgId.equals(outputMsgId) == false) {
191             final Map<String,String> errorInfo = ImmutableMap.<String,String>builder()
192                     .put( "actual-message-id", outputMsgId )
193                     .put( "expected-message-id", inputMsgId )
194                     .build();
195
196             throw new NetconfDocumentedException( "Response message contained unknown \"message-id\"",
197                     null, NetconfDocumentedException.ErrorType.PROTOCOL,
198                     NetconfDocumentedException.ErrorTag.BAD_ATTRIBUTE,
199                     NetconfDocumentedException.ErrorSeverity.ERROR, errorInfo);
200         }
201     }
202
203     public static void checkSuccessReply(final NetconfMessage output) throws NetconfDocumentedException {
204         if(NetconfMessageUtil.isErrorMessage(output)) {
205             throw NetconfDocumentedException.fromXMLDocument(output.getDocument());
206         }
207     }
208
209     public static RpcError toRpcError( final NetconfDocumentedException ex ) {
210         final StringBuilder infoBuilder = new StringBuilder();
211         final Map<String, String> errorInfo = ex.getErrorInfo();
212         if(errorInfo != null) {
213             for( final Entry<String,String> e: errorInfo.entrySet() ) {
214                 infoBuilder.append( '<' ).append( e.getKey() ).append( '>' ).append( e.getValue() )
215                 .append( "</" ).append( e.getKey() ).append( '>' );
216
217             }
218         }
219
220         final ErrorSeverity severity = toRpcErrorSeverity( ex.getErrorSeverity() );
221         return severity == ErrorSeverity.ERROR ?
222                 RpcResultBuilder.newError(
223                         toRpcErrorType( ex.getErrorType() ), ex.getErrorTag().getTagValue(),
224                         ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause() ) :
225                             RpcResultBuilder.newWarning(
226                                     toRpcErrorType( ex.getErrorType() ), ex.getErrorTag().getTagValue(),
227                                     ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause() );
228     }
229
230     private static ErrorSeverity toRpcErrorSeverity( final NetconfDocumentedException.ErrorSeverity severity ) {
231         switch (severity) {
232             case WARNING:
233                 return RpcError.ErrorSeverity.WARNING;
234             default:
235                 return RpcError.ErrorSeverity.ERROR;
236         }
237     }
238
239     private static RpcError.ErrorType toRpcErrorType(final NetconfDocumentedException.ErrorType type) {
240         switch (type) {
241             case PROTOCOL:
242             return RpcError.ErrorType.PROTOCOL;
243             case RPC:
244             return RpcError.ErrorType.RPC;
245             case TRANSPORT:
246             return RpcError.ErrorType.TRANSPORT;
247         default:
248             return RpcError.ErrorType.APPLICATION;
249         }
250     }
251
252     public static NodeIdentifier toId(final PathArgument qname) {
253         return toId(qname.getNodeType());
254     }
255
256     public static NodeIdentifier toId(final QName nodeType) {
257         return new NodeIdentifier(nodeType);
258     }
259
260     public static Element getDataSubtree(final Document doc) {
261         return (Element) doc.getElementsByTagNameNS(NETCONF_URI.toString(), "data").item(0);
262     }
263
264     public static boolean isDataRetrievalOperation(final QName rpc) {
265         return NETCONF_URI.equals(rpc.getNamespace())
266                 && (NETCONF_GET_CONFIG_QNAME.getLocalName().equals(rpc.getLocalName())
267                 || NETCONF_GET_QNAME.getLocalName().equals(rpc.getLocalName()));
268     }
269
270     public static ContainerSchemaNode createSchemaForDataRead(final SchemaContext schemaContext) {
271         return new NodeContainerProxy(NETCONF_DATA_QNAME, schemaContext.getChildNodes());
272     }
273
274     public static ContainerSchemaNode createSchemaForNotification(final NotificationDefinition next) {
275         return new NodeContainerProxy(next.getQName(), next.getChildNodes(), next.getAvailableAugmentations());
276     }
277
278     public static ContainerNode wrap(final QName name, final DataContainerChild<?, ?>... node) {
279         return Builders.containerBuilder().withNodeIdentifier(toId(name)).withValue(ImmutableList.copyOf(node)).build();
280     }
281
282     public static AnyXmlNode createEditConfigAnyxml(final SchemaContext ctx, final YangInstanceIdentifier dataPath,
283                                                                      final Optional<ModifyAction> operation, final Optional<NormalizedNode<?, ?>> lastChildOverride) {
284         final NormalizedNode<?, ?> configContent;
285
286         if (dataPath.isEmpty()) {
287             Preconditions.checkArgument(lastChildOverride.isPresent(), "Data has to be present when creating structure for top level element");
288             Preconditions.checkArgument(lastChildOverride.get() instanceof DataContainerChild<?, ?>,
289                     "Data has to be either container or a list node when creating structure for top level element, but was: %s", lastChildOverride.get());
290             configContent = lastChildOverride.get();
291         } else {
292             final Entry<QName, ModifyAction> modifyOperation =
293                     operation.isPresent() ? new AbstractMap.SimpleEntry<>(NETCONF_OPERATION_QNAME, operation.get()) : null;
294             configContent = ImmutableNodes.fromInstanceId(ctx, dataPath, lastChildOverride, Optional.fromNullable(modifyOperation));
295         }
296
297         final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(), Optional.of(NETCONF_CONFIG_QNAME.getNamespace().toString()));
298         try {
299             NetconfUtil.writeNormalizedNode(configContent, new DOMResult(element), SchemaPath.ROOT, ctx);
300         } catch (IOException | XMLStreamException e) {
301             throw new IllegalStateException("Unable to serialize edit config content element for path " + dataPath, e);
302         }
303         final DOMSource value = new DOMSource(element);
304
305         return Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_CONFIG_QNAME)).withValue(value).build();
306     }
307
308     public static DataContainerChild<?, ?> createEditConfigStructure(final SchemaContext ctx, final YangInstanceIdentifier dataPath,
309                                                                      final Optional<ModifyAction> operation, final Optional<NormalizedNode<?, ?>> lastChildOverride) {
310         return Builders.choiceBuilder().withNodeIdentifier(toId(EditContent.QNAME))
311                 .withChild(createEditConfigAnyxml(ctx, dataPath, operation, lastChildOverride)).build();
312     }
313
314     public static SchemaPath toPath(final QName rpc) {
315         return SchemaPath.create(true, rpc);
316     }
317
318     public static Map.Entry<Date, XmlElement> stripNotification(final NetconfMessage message) {
319         final XmlElement xmlElement = XmlElement.fromDomDocument(message.getDocument());
320         final List<XmlElement> childElements = xmlElement.getChildElements();
321         Preconditions.checkArgument(childElements.size() == 2, "Unable to parse notification %s, unexpected format.\nExpected 2 childElements," +
322                 " actual childElements size is %s", message, childElements.size());
323
324         final XmlElement eventTimeElement;
325         final XmlElement notificationElement;
326
327         if (childElements.get(0).getName().equals(EVENT_TIME)) {
328             eventTimeElement = childElements.get(0);
329             notificationElement = childElements.get(1);
330         }
331         else if(childElements.get(1).getName().equals(EVENT_TIME)) {
332             eventTimeElement = childElements.get(1);
333             notificationElement = childElements.get(0);
334         } else {
335             throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message);
336         }
337
338         try {
339             return new AbstractMap.SimpleEntry<>(
340                     NetconfNotification.RFC3339_DATE_PARSER.apply(eventTimeElement.getTextContent()),
341                     notificationElement);
342         } catch (final DocumentedException e) {
343             throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message);
344         } catch (final DateTimeParseException e) {
345             LOG.warn("Unable to parse event time from {}. Setting time to {}", eventTimeElement, NetconfNotification.UNKNOWN_EVENT_TIME, e);
346             return new AbstractMap.SimpleEntry<>(NetconfNotification.UNKNOWN_EVENT_TIME, notificationElement);
347         }
348     }
349
350     public static DOMResult prepareDomResultForRpcRequest(final QName rpcQName, final MessageCounter counter) {
351         final Document document = XmlUtil.newDocument();
352         final Element rpcNS =
353                 document.createElementNS(NETCONF_RPC_QNAME.getNamespace().toString(), NETCONF_RPC_QNAME.getLocalName());
354         // set msg id
355         rpcNS.setAttribute(MESSAGE_ID_ATTR, counter.getNewMessageId(MESSAGE_ID_PREFIX));
356         final Element elementNS = document.createElementNS(rpcQName.getNamespace().toString(), rpcQName.getLocalName());
357         rpcNS.appendChild(elementNS);
358         document.appendChild(rpcNS);
359         return new DOMResult(elementNS);
360     }
361
362     public static void writeNormalizedRpc(final ContainerNode normalized, final DOMResult result,
363                                           final SchemaPath schemaPath, final SchemaContext baseNetconfCtx) throws IOException, XMLStreamException {
364         final XMLStreamWriter writer = NetconfUtil.XML_FACTORY.createXMLStreamWriter(result);
365         try {
366             try (final NormalizedNodeStreamWriter normalizedNodeStreamWriter =
367                     XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath)) {
368                 try (final SchemaOrderedNormalizedNodeWriter normalizedNodeWriter =
369                         new SchemaOrderedNormalizedNodeWriter(normalizedNodeStreamWriter, baseNetconfCtx, schemaPath)) {
370                     final Collection<DataContainerChild<?, ?>> value = normalized.getValue();
371                     normalizedNodeWriter.write(value);
372                     normalizedNodeWriter.flush();
373                 }
374             }
375         } finally {
376             try {
377                 writer.close();
378             } catch (final Exception e) {
379                LOG.warn("Unable to close resource properly", e);
380             }
381         }
382     }
383 }