+ final SchemaContext baseNetconfCtx = schemaContext.findModuleByNamespace(NETCONF_URI).isEmpty() ? BASE_NETCONF_CTX : schemaContext;
+ if(NetconfMessageTransformUtil.isDataEditOperation(rpcQName)) {
+ writeNormalizedEdit(payload, result, rpc, baseNetconfCtx);
+ } else if(NetconfMessageTransformUtil.isDataRetrievalOperation(rpcQName)) {
+ writeNormalizedGet(payload, result, rpc, baseNetconfCtx);
+ } else {
+ writeNormalizedRpc(payload, result, rpc, schemaContext);
+ }
+ } catch (final XMLStreamException | IOException | IllegalStateException e) {
+ throw new IllegalStateException("Unable to serialize " + rpc, e);
+ }
+
+ final Document node = result.getNode().getOwnerDocument();
+
+ node.getDocumentElement().setAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR, counter.getNewMessageId(MESSAGE_ID_PREFIX));
+ return new NetconfMessage(node);
+ }
+
+ private DOMResult prepareDomResultForRpcRequest(final QName rpcQName) {
+ final Document document = XmlUtil.newDocument();
+ final Element rpcNS = document.createElementNS(NETCONF_RPC_QNAME.getNamespace().toString(), NETCONF_RPC_QNAME.getLocalName());
+ final Element elementNS = document.createElementNS(rpcQName.getNamespace().toString(), rpcQName.getLocalName());
+ rpcNS.appendChild(elementNS);
+ document.appendChild(rpcNS);
+ return new DOMResult(elementNS);
+ }
+
+ static final XMLOutputFactory XML_FACTORY;
+ static {
+ XML_FACTORY = XMLOutputFactory.newFactory();
+ XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
+ }
+
+ // FIXME similar code is in netconf-notifications-impl , DRY
+ private void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result, final SchemaPath schemaPath, final SchemaContext context)
+ throws IOException, XMLStreamException {
+ NormalizedNodeWriter normalizedNodeWriter = null;
+ NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
+ XMLStreamWriter writer = null;
+ try {
+ writer = XML_FACTORY.createXMLStreamWriter(result);
+ normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
+ normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);
+
+ normalizedNodeWriter.write(normalized);
+
+ normalizedNodeWriter.flush();
+ } finally {
+ try {
+ if(normalizedNodeWriter != null) {
+ normalizedNodeWriter.close();
+ }
+ if(normalizedNodeStreamWriter != null) {
+ normalizedNodeStreamWriter.close();
+ }
+ if(writer != null) {
+ writer.close();
+ }
+ } catch (final Exception e) {
+ LOG.warn("Unable to close resource properly", e);
+ }
+ }
+ }
+
+ private void writeNormalizedEdit(final ContainerNode normalized, final DOMResult result, final SchemaPath schemaPath, final SchemaContext baseNetconfCtx) throws IOException, XMLStreamException {
+ final NormalizedNodeWriter normalizedNodeWriter;
+ NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
+ XMLStreamWriter writer = null;
+ try {
+ writer = XML_FACTORY.createXMLStreamWriter(result);
+ normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath);
+ normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);
+
+ Optional<Iterable<Element>> editDataElements = Optional.absent();
+ for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> editElement : normalized.getValue()) {
+ if(editElement.getNodeType().getLocalName().equals(EditContent.QNAME.getLocalName())) {
+ Preconditions.checkState(editElement instanceof ChoiceNode,
+ "Edit content element is expected to be %s, not %s", ChoiceNode.class, editElement);
+ final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> configContentHolder =
+ ((ChoiceNode) editElement).getChild(toId(NETCONF_CONFIG_QNAME));
+ // TODO The config element inside the EditContent should be AnyXml not Container, but AnyXml is based on outdated API
+ Preconditions.checkState(configContentHolder.isPresent() && configContentHolder.get() instanceof ContainerNode,
+ "Edit content/config element is expected to be present as a container node");
+ normalizedNodeStreamWriter.startChoiceNode(toId(editElement.getNodeType()), 1);
+ normalizedNodeStreamWriter.anyxmlNode(toId(NETCONF_CONFIG_QNAME), null);
+ normalizedNodeStreamWriter.endNode();
+
+ editDataElements = Optional.of(serializeAnyXmlAccordingToSchema(((ContainerNode) configContentHolder.get()).getValue()));