private static void addSubtree(XmlElement filter, XmlElement src, XmlElement dst) throws DocumentedException { for (XmlElement srcChild : src.getChildElements()) { for (XmlElement filterChild : filter.getChildElements()) { addSubtree2(filterChild, srcChild, dst); } } }
public Optional<XmlElement> getOnlyChildElementOptionally(String childName) { List<XmlElement> nameElements = getChildElements(childName); if (nameElements.size() != 1) { return Optional.absent(); } return Optional.of(nameElements.get(0)); }
public Optional<XmlElement> getOnlyChildElementOptionally() { List<XmlElement> children = getChildElements(); if (children.size() != 1) { return Optional.absent(); } return Optional.of(children.get(0)); }
public void checkUnrecognisedElements(List<XmlElement> recognisedElements, XmlElement... additionalRecognisedElements) throws DocumentedException { List<XmlElement> childElements = getChildElements(); childElements.removeAll(recognisedElements); for (XmlElement additionalRecognisedElement : additionalRecognisedElements) { childElements.remove(additionalRecognisedElement); } if (!childElements.isEmpty()){ throw new DocumentedException(String.format("Unrecognised elements %s in %s", childElements, this), DocumentedException.ErrorType.application, DocumentedException.ErrorTag.invalid_value, DocumentedException.ErrorSeverity.error); } }
public XmlElement getOnlyChildElement() throws DocumentedException { List<XmlElement> children = getChildElements(); if (children.size() != 1){ throw new DocumentedException(String.format( "One element expected in %s but was %s", toString(), children.size()), DocumentedException.ErrorType.application, DocumentedException.ErrorTag.invalid_value, DocumentedException.ErrorSeverity.error); } return children.get(0); }
public XmlElement getOnlyChildElement(String childName) throws DocumentedException { List<XmlElement> nameElements = getChildElements(childName); if (nameElements.size() != 1){ throw new DocumentedException("One element " + childName + " expected in " + toString(), DocumentedException.ErrorType.application, DocumentedException.ErrorTag.invalid_value, DocumentedException.ErrorSeverity.error); } return nameElements.get(0); }
/** * * @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); } }
@VisibleForTesting protected YangInstanceIdentifier getInstanceIdentifierFromFilter(XmlElement filterElement) throws DocumentedException { if (filterElement.getChildElements().size() != 1) { throw new DocumentedException("Multiple filter roots not supported yet", ErrorType.application, ErrorTag.operation_not_supported, ErrorSeverity.error); } XmlElement element = filterElement.getOnlyChildElement(); return validator.validate(element); }
public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException { if(xmlElement.getChildElements().size() != 1) { return false; } try { return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK); } catch (DocumentedException e) { throw new NetconfDocumentedException(e); } }
public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException { if(xmlElement.getChildElements().size() != 1) { return false; } try { return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR); } catch (DocumentedException e) { throw new NetconfDocumentedException(e); } }
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; } } }); } }
if (matches != MatchingResult.NO_MATCH && matches != MatchingResult.CONTENT_MISMATCH) { boolean filterHasChildren = filter.getChildElements().isEmpty() == false; for (XmlElement srcChild : src.getChildElements()) { for (XmlElement filterChild : filter.getChildElements()) { MatchingResult childMatch = addSubtree2(filterChild, srcChild, XmlElement.fromDomElement(copied)); if (childMatch == MatchingResult.CONTENT_MISMATCH) { if (numberOfTextMatchingChildren == filter.getChildElements().size()) {
private static Document filteredNotification(XmlElement filter, Document originalNotification) throws DocumentedException { Document result = XmlUtil.newDocument(); XmlElement dataSrc = XmlElement.fromDomDocument(originalNotification); Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false); for (XmlElement filterChild : filter.getChildElements()) { addSubtree2(filterChild, dataSrc.getOnlyChildElement(), XmlElement.fromDomElement(dataDst)); } if(dataDst.getFirstChild() != null) { result.appendChild(dataDst.getFirstChild()); return result; } else { return null; } }
/** * Recursively checks filter elements against the schema. Returns tree of nodes QNames as they appear in filter. * @param element element to check * @param parentNodeSchema parent node schema * @param tree parent node tree * @return tree * @throws ValidationException if filter content is not valid */ private FilterTree validateNode(XmlElement element, DataSchemaNode parentNodeSchema, FilterTree tree) throws ValidationException { final List<XmlElement> childElements = element.getChildElements(); for (XmlElement childElement : childElements) { try { final Deque<DataSchemaNode> path = findSchemaNodeByNameAndNamespace(parentNodeSchema, childElement.getName(), new URI(childElement.getNamespace())); if (path.isEmpty()) { throw new ValidationException(element, childElement); } FilterTree subtree = tree; for (DataSchemaNode dataSchemaNode : path) { subtree = subtree.addChild(dataSchemaNode); } final DataSchemaNode childSchema = path.getLast(); validateNode(childElement, childSchema, subtree); } catch (URISyntaxException | MissingNameSpaceException e) { throw new RuntimeException("Wrong namespace in element + " + childElement.toString()); } } return tree; }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException { final Datastore targetDatastore = extractTargetParameter(operationElement); if (targetDatastore == Datastore.running) { throw new DocumentedException("edit-config on running datastore is not supported", ErrorType.protocol, ErrorTag.operation_not_supported, ErrorSeverity.error); } final ModifyAction defaultAction = getDefaultOperation(operationElement); final XmlElement configElement = getElement(operationElement, CONFIG_KEY); for (XmlElement element : configElement.getChildElements()) { final String ns = element.getNamespace(); final DataSchemaNode schemaNode = getSchemaNodeFromNamespace(ns, element).get(); final DataTreeChangeTracker changeTracker = new DataTreeChangeTracker(defaultAction); final DomToNormalizedNodeParserFactory.BuildingStrategyProvider editOperationStrategyProvider = new EditOperationStrategyProvider(changeTracker); parseIntoNormalizedNode(schemaNode, element, editOperationStrategyProvider); executeOperations(changeTracker); } return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); }