private XmlElement getElement(final XmlElement operationElement, String elementName) throws DocumentedException { final Optional<XmlElement> childNode = operationElement.getOnlyChildElementOptionally(elementName); if (!childNode.isPresent()) { throw new DocumentedException(elementName + " element is missing", ErrorType.protocol, ErrorTag.missing_element, ErrorSeverity.error); } return childNode.get(); }
public Optional<XmlElement> getOnlyChildElementWithSameNamespaceOptionally() { Optional<XmlElement> child = getOnlyChildElementOptionally(); if (child.isPresent() && child.get().getNamespaceOptionally().isPresent() && getNamespaceOptionally().isPresent() && getNamespaceOptionally().get().equals(child.get().getNamespaceOptionally().get())) { return child; } return Optional.absent(); }
/** * * @param operationElement operation element * @return if Filter is present and not empty returns Optional of the InstanceIdentifier to the read location in datastore. * empty filter returns Optional.absent() which should equal an empty <data/> container in the response. * if filter is not present we want to read the entire datastore - return ROOT. * @throws DocumentedException */ protected Optional<YangInstanceIdentifier> getDataRootFromFilter(XmlElement operationElement) throws DocumentedException { Optional<XmlElement> filterElement = operationElement.getOnlyChildElementOptionally(FILTER); if (filterElement.isPresent()) { if (filterElement.get().getChildElements().size() == 0) { return Optional.absent(); } return Optional.of(getInstanceIdentifierFromFilter(filterElement.get())); } else { return Optional.of(ROOT); } }
private static Optional<Datastore> parseSource(final XmlElement xml) throws DocumentedException { final Optional<XmlElement> sourceElement = xml.getOnlyChildElementOptionally(XmlNetconfConstants.SOURCE_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); return sourceElement.isPresent() ? Optional.of(Datastore.valueOf(sourceElement.get().getOnlyChildElement().getName())) : Optional.<Datastore>absent(); }
public static Collection<String> extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException { XmlElement responseElement = XmlElement.fromDomDocument(doc); // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace Optional<XmlElement> capabilitiesElement = responseElement .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES) .or(responseElement .getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES)); List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY); return Collections2.transform(caps, new Function<XmlElement, String>() { @Override public String apply(@Nonnull XmlElement input) { // Trim possible leading/tailing whitespace try { return input.getTextContent().trim(); } catch (DocumentedException e) { LOG.trace("Error fetching input text content",e); return null; } } }); } }
public static Document applyRpcSubtreeFilter(Document requestDocument, Document rpcReply) throws DocumentedException { OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(requestDocument); if (XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(operationNameAndNamespace.getNamespace()) && XmlNetconfConstants.GET.equals(operationNameAndNamespace.getOperationName()) || XmlNetconfConstants.GET_CONFIG.equals(operationNameAndNamespace.getOperationName())) { // process subtree filtering here, in case registered netconf operations do // not implement filtering. Optional<XmlElement> maybeFilter = operationNameAndNamespace.getOperationElement().getOnlyChildElementOptionally( XmlNetconfConstants.FILTER, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); if (!maybeFilter.isPresent()) { return rpcReply; } XmlElement filter = maybeFilter.get(); if (isSupported(filter)) { // do return filtered(maybeFilter.get(), rpcReply); } } return rpcReply; // return identical document }
.getOnlyChildElementOptionally(RpcFacade.CONTEXT_INSTANCE);