use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class CopyConfig method copyToUrl.
private void copyToUrl(final XmlElement urlElement, final XmlElement operationElement) throws DocumentedException {
final String url = urlElement.getTextContent();
if (!url.startsWith("file:")) {
throw new DocumentedException("Unsupported <url> protocol: " + url, ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
}
// Read data from datastore:
final XmlElement source = getSourceElement(operationElement).getOnlyChildElement();
final ContainerNode data = readData(source);
// Transform NN to XML:
final Document document = operationElement.getDomElement().getOwnerDocument();
final Node node = transformNormalizedNode(document, data);
// Save XML to file:
final String xml = XmlUtil.toString((Element) node);
try {
final Path file = Paths.get(new URI(url));
Files.write(file, xml.getBytes(StandardCharsets.UTF_8));
} catch (URISyntaxException | IllegalArgumentException e) {
throw new DocumentedException("Invalid URI: " + url, e, ErrorType.RPC, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
} catch (IOException e) {
throw new DocumentedException("Failed to write : " + url, e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
}
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class CopyConfig method copyToCandidate.
private void copyToCandidate(final XmlElement operationElement) throws DocumentedException {
final XmlElement source = getSourceElement(operationElement);
final List<XmlElement> configElements = getConfigElement(source).getChildElements();
// <copy-config>, unlike <edit-config>, always replaces entire configuration,
// so remove old configuration first:
final DOMDataTreeReadWriteTransaction rwTx = transactionProvider.getOrCreateTransaction();
rwTx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty(), EMPTY_ROOT_NODE);
// Then create nodes present in the <config> element:
for (final XmlElement element : configElements) {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
parseIntoNormalizedNode(getSchemaNodeFromNamespace(element.getNamespace(), element), element, ImmutableNormalizedNodeStreamWriter.from(resultHolder));
final NormalizedNode data = resultHolder.getResult();
final YangInstanceIdentifier path = YangInstanceIdentifier.create(data.getIdentifier());
// Doing merge instead of put to support top-level list:
rwTx.merge(LogicalDatastoreType.CONFIGURATION, path, data);
}
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class EditConfig method handleWithNoSubsequentOperations.
@Override
protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
final XmlElement targetElement = extractTargetElement(operationElement, OPERATION_NAME);
final Datastore targetDatastore = Datastore.valueOf(targetElement.getName());
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 = getConfigElement(operationElement);
for (final XmlElement element : configElement.getChildElements()) {
final SplittingNormalizedNodeMetadataStreamWriter writer = new SplittingNormalizedNodeMetadataStreamWriter(defaultAction);
parseIntoNormalizedNode(getSchemaNodeFromNamespace(element.getNamespace(), element), element, writer);
executeOperations(writer.getDataTreeChanges());
}
return document.createElement(XmlNetconfConstants.OK);
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class FilterContentValidator method validateNode.
/**
* 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(final XmlElement element, final DataSchemaNode parentNodeSchema, final FilterTree tree) throws ValidationException {
final List<XmlElement> childElements = element.getChildElements();
for (final XmlElement childElement : childElements) {
try {
final Deque<DataSchemaNode> path = findSchemaNodeByNameAndNamespace(parentNodeSchema, childElement.getName(), XMLNamespace.of(childElement.getNamespace()));
if (path.isEmpty()) {
throw new ValidationException(element, childElement);
}
FilterTree subtree = tree;
for (final DataSchemaNode dataSchemaNode : path) {
subtree = subtree.addChild(dataSchemaNode);
}
final DataSchemaNode childSchema = path.getLast();
validateNode(childElement, childSchema, subtree);
} catch (IllegalArgumentException | MissingNameSpaceException e) {
throw new RuntimeException("Wrong namespace in element + " + childElement.toString(), e);
}
}
return tree;
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class AbstractConfigOperation method getElementsByTagName.
protected static NodeList getElementsByTagName(final XmlElement parent, final String key) throws DocumentedException {
final Element domParent = parent.getDomElement();
final NodeList elementsByTagName;
if (Strings.isNullOrEmpty(domParent.getPrefix())) {
elementsByTagName = domParent.getElementsByTagName(key);
} else {
elementsByTagName = domParent.getElementsByTagNameNS(parent.getNamespace(), key);
}
return elementsByTagName;
}
Aggregations