public static Element createTextElement(final Document document, final String qName, final String content, final Optional<String> namespaceURI) { Element typeElement = createElement(document, qName, namespaceURI); typeElement.appendChild(document.createTextNode(content)); return typeElement; }
public static Document createDocumentCopy(final Document original) { final Document copiedDocument = newDocument(); final Node copiedRoot = copiedDocument.importNode(original.getDocumentElement(), true); copiedDocument.appendChild(copiedRoot); return copiedDocument; } }
public static String toString(final Element xml) { return toString(xml, false); }
public static Element createTextElementWithNamespacedContent(final Document document, final String qName, final String prefix, final String namespace, final String contentWithoutPrefix, final Optional<String> namespaceURI) { String content = createPrefixedValue(XmlMappingConstants.PREFIX, contentWithoutPrefix); Element element = createTextElement(document, qName, content, namespaceURI); String prefixedNamespaceAttr = createPrefixedValue(XMLNS_ATTRIBUTE_KEY, prefix); element.setAttributeNS(XMLNS_URI, prefixedNamespaceAttr, namespace); return element; }
@Override public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException { XmlElement requestElement = getRequestElementWithCheck(requestMessage); Document document = XmlUtil.newDocument(); XmlElement operationElement = requestElement.getOnlyChildElement(); Map<String, Attr> attributes = requestElement.getAttributes(); Element response = handle(document, operationElement, subsequentOperation); Element rpcReply = XmlUtil.createElement(document, XmlMappingConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)); if(XmlElement.fromDomElement(response).hasNamespace()) { rpcReply.appendChild(response); } else { Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)); NodeList list = response.getChildNodes(); while(list.getLength()!=0) { responseNS.appendChild(list.item(0)); } rpcReply.appendChild(responseNS); } for (Attr attribute : attributes.values()) { rpcReply.setAttributeNode((Attr) document.importNode(attribute, true)); } document.appendChild(rpcReply); return document; }
ValidationException, ConflictingVersionException { Document document = XmlUtil.newDocument(); Element dataElement = XmlUtil.createElement(document, XmlMappingConstants.DATA_KEY, Optional.<String>absent()); Element modulesElement = XmlUtil.createElement(document, XmlMappingConstants.MODULES_KEY, Optional.of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG)); dataElement.appendChild(modulesElement); LOG.debug("Pushing config xml: {}", XmlUtil.toString(dataElement));
public static Element readXmlToElement(final InputStream xmlContent) throws SAXException, IOException { Document doc = readXmlToDocument(xmlContent); return doc.getDocumentElement(); }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml) throws DocumentedException { final GetSchemaEntry entry; entry = new GetSchemaEntry(xml); final String schema; try { schema = cap.getSchemaForCapability(entry.identifier, entry.version); } catch (final IllegalStateException e) { final Map<String, String> errorInfo = Maps.newHashMap(); errorInfo.put(entry.identifier, e.getMessage()); LOG.warn("Rpc error: {}", DocumentedException.ErrorTag.operation_failed, e); throw new DocumentedException(e.getMessage(), DocumentedException.ErrorType.application, DocumentedException.ErrorTag.operation_failed, DocumentedException.ErrorSeverity.error, errorInfo); } final Element getSchemaResult; getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING)); LOG.trace("{} operation successful", GET_SCHEMA); return getSchemaResult; }
@Override public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException { final XmlElement requestElement = getRequestElementWithCheck(requestMessage); final Document document = XmlUtil.newDocument(); final XmlElement operationElement = requestElement.getOnlyChildElement(); final Map<String, Attr> attributes = requestElement.getAttributes(); final Element response = handle(document, operationElement, subsequentOperation); final Element rpcReply = XmlUtil.createElement(document, XmlMappingConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)); if(XmlElement.fromDomElement(response).hasNamespace()) { rpcReply.appendChild(response); } else { final NodeList list = response.getChildNodes(); if (list.getLength() == 0) { rpcReply.appendChild(response); } else { while (list.getLength() != 0) { rpcReply.appendChild(list.item(0)); } } } for (Attr attribute : attributes.values()) { rpcReply.setAttributeNode((Attr) document.importNode(attribute, true)); } document.appendChild(rpcReply); return document; }
public static Element readXmlToElement(final String xmlContent) throws SAXException, IOException { Document doc = readXmlToDocument(xmlContent); return doc.getDocumentElement(); }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException { boolean commitStatus = transactionProvider.commitTransaction(); LOG.trace("Commit completed successfully {}", commitStatus); return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); }
private AnyXmlNode encapsulate(final DOMNotification body) { // FIXME: Introduce something like YangModeledAnyXmlNode in Yangtools final Document doc = XmlUtil.newDocument(); final Optional<String> namespace = Optional.of(PAYLOAD_ARG.getNodeType().getNamespace().toString()); final Element element = XmlUtil.createElement(doc, "payload", namespace); final DOMResult result = new DOMResult(element); final SchemaContext context = mount.getSchemaContext(); final SchemaPath schemaPath = body.getType(); try { NetconfUtil.writeNormalizedNode(body.getBody(), result, schemaPath, context); return Builders.anyXmlBuilder().withNodeIdentifier(PAYLOAD_ARG).withValue(new DOMSource(element)).build(); } catch (IOException | XMLStreamException e) { LOG.error("Unable to encapsulate notification.", e); throw Throwables.propagate(e); } }
private static Document filtered(XmlElement filter, Document originalReplyDocument) throws DocumentedException { Document result = XmlUtil.newDocument(); // even if filter is empty, copy /rpc/data Element rpcReply = originalReplyDocument.getDocumentElement(); Node rpcReplyDst = result.importNode(rpcReply, false); result.appendChild(rpcReplyDst); XmlElement dataSrc = XmlElement.fromDomElement(rpcReply).getOnlyChildElement("data", XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false); rpcReplyDst.appendChild(dataDst); addSubtree(filter, dataSrc, XmlElement.fromDomElement(dataDst)); return result; }
public static String toString(final Document document) { return toString(document.getDocumentElement()); }
public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException { return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8))); }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException { try { transactionProvider.abortTransaction(); } catch (IllegalStateException e) { LOG.warn("Abort failed ", e); final Map<String, String> errorInfo = new HashMap<>(); errorInfo .put(ErrorTag.operation_failed.name(), "Operation failed. Use 'get-config' or 'edit-config' before triggering 'discard-changes' operation"); throw new DocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error, errorInfo); } return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); }
public static NetconfStartExiMessage create(final EXIOptions exiOptions, final String messageId) { final Document doc = XmlUtil.newDocument(); final Element rpcElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.RPC_KEY); rpcElement.setAttributeNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID, messageId); // TODO draft http://tools.ietf.org/html/draft-varga-netconf-exi-capability-02#section-3.5.1 has no namespace for start-exi element in xml final Element startExiElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_EXI_1_0, START_EXI); addAlignment(exiOptions, doc, startExiElement); addFidelity(exiOptions, doc, startExiElement); rpcElement.appendChild(startExiElement); doc.appendChild(rpcElement); return new NetconfStartExiMessage(doc); }
public static String toString(final Document doc, final boolean addXmlDeclaration) { return toString(doc.getDocumentElement(), addXmlDeclaration); }
public static Element readXmlToElement(final File xmlFile) throws SAXException, IOException { return readXmlToDocument(new FileInputStream(xmlFile)).getDocumentElement(); }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException { final Datastore targetDatastore = extractTargetParameter(operationElement); if (targetDatastore == Datastore.candidate) { LOG.debug("Locking candidate datastore on session: {}", getNetconfSessionIdForReporting()); return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); } throw new DocumentedException("Unable to lock " + targetDatastore + " datastore", DocumentedException.ErrorType.application, DocumentedException.ErrorTag.operation_not_supported, DocumentedException.ErrorSeverity.error); }