use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class FilterContentValidator method validate.
/**
* Validates filter content against this validator schema context. If the filter is valid,
* method returns {@link YangInstanceIdentifier} of node which can be used as root for data selection.
*
* @param filterContent filter content
* @return YangInstanceIdentifier
* @throws DocumentedException if filter content validation failed
*/
public YangInstanceIdentifier validate(final XmlElement filterContent) throws DocumentedException {
final XMLNamespace namespace;
try {
namespace = XMLNamespace.of(filterContent.getNamespace());
} catch (final IllegalArgumentException e) {
throw new RuntimeException("Wrong namespace in element + " + filterContent.toString(), e);
}
try {
final Module module = schemaContext.getCurrentContext().findModules(namespace).iterator().next();
final DataSchemaNode schema = getRootDataSchemaNode(module, namespace, filterContent.getName());
final FilterTree filterTree = validateNode(filterContent, schema, new FilterTree(schema.getQName(), Type.OTHER, schema));
return getFilterDataRoot(filterTree, filterContent, YangInstanceIdentifier.builder());
} catch (final ValidationException e) {
LOG.debug("Filter content isn't valid", e);
throw new DocumentedException("Validation failed. Cause: " + e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
}
}
use of org.opendaylight.netconf.api.DocumentedException 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.DocumentedException 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.DocumentedException in project netconf by opendaylight.
the class EditConfig method executeChange.
private void executeChange(final DOMDataTreeReadWriteTransaction rwtx, final DataTreeChange change) throws DocumentedException {
final YangInstanceIdentifier path = change.getPath();
final NormalizedNode changeData = change.getChangeRoot();
switch(change.getAction()) {
case NONE:
return;
case MERGE:
mergeParentMixin(rwtx, path, changeData);
rwtx.merge(LogicalDatastoreType.CONFIGURATION, path, changeData);
break;
case CREATE:
try {
if (rwtx.exists(LogicalDatastoreType.CONFIGURATION, path).get()) {
throw new DocumentedException("Data already exists, cannot execute CREATE operation", ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, ErrorSeverity.ERROR);
}
mergeParentMixin(rwtx, path, changeData);
rwtx.put(LogicalDatastoreType.CONFIGURATION, path, changeData);
} catch (final InterruptedException | ExecutionException e) {
LOG.warn("Read from datastore failed when trying to read data for create operation {}", change, e);
}
break;
case REPLACE:
mergeParentMixin(rwtx, path, changeData);
rwtx.put(LogicalDatastoreType.CONFIGURATION, path, changeData);
break;
case DELETE:
try {
if (!rwtx.exists(LogicalDatastoreType.CONFIGURATION, path).get()) {
throw new DocumentedException("Data is missing, cannot execute DELETE operation", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, ErrorSeverity.ERROR);
}
rwtx.delete(LogicalDatastoreType.CONFIGURATION, path);
} catch (final InterruptedException | ExecutionException e) {
LOG.warn("Read from datastore failed when trying to read data for delete operation {}", change, e);
}
break;
case REMOVE:
rwtx.delete(LogicalDatastoreType.CONFIGURATION, path);
break;
default:
LOG.warn("Unknown/not implemented operation, not executing");
}
}
use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class CopyConfigTest method testTargetMissing.
@Test
public void testTargetMissing() {
final DocumentedException e = assertThrows(DocumentedException.class, () -> copyConfig("messages/mapping/copyConfigs/copyConfig_no_target.xml"));
assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
assertEquals(e.getErrorTag(), ErrorTag.MISSING_ATTRIBUTE);
assertEquals(e.getErrorType(), ErrorType.PROTOCOL);
}
Aggregations