use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class CopyConfigTest method testConfigFromInvalidUrl.
@Test
public void testConfigFromInvalidUrl() {
final DocumentedException e = assertThrows(DocumentedException.class, () -> copyConfig("messages/mapping/copyConfigs/copyConfig_invalid_url.xml"));
assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
assertEquals(e.getErrorTag(), ErrorTag.INVALID_VALUE);
assertEquals(e.getErrorType(), ErrorType.APPLICATION);
assertTrue(e.getCause() instanceof MalformedURLException);
}
use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class AbstractEdit method getSchemaNodeFromNamespace.
final SchemaTreeInference getSchemaNodeFromNamespace(final String namespace, final XmlElement element) throws DocumentedException {
final XMLNamespace ns;
try {
ns = XMLNamespace.of(namespace);
} catch (final IllegalArgumentException e) {
throw new NetconfDocumentedException("Unable to create URI for namespace : " + namespace, e, ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
}
// Returns module with newest revision since findModuleByNamespace returns a set of modules and we only
// need the newest one
final EffectiveModelContext ctx = schemaContext.getCurrentContext();
final Iterator<? extends @NonNull Module> it = ctx.findModules(ns).iterator();
if (!it.hasNext()) {
// No module is present with this namespace
throw new NetconfDocumentedException("Unable to find module by namespace: " + namespace, ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
}
final Module module = it.next();
final SchemaInferenceStack stack = SchemaInferenceStack.of(ctx);
final String elementName = element.getName();
try {
// FIXME: This is a bit suspect. The element is formed using XML encoding, hence it corresponds to
// enterDataTree(). But then we use the result of this method to create a NormalizedNode tree,
// which contains ChoiceNode. This needs to be tested with something like to following:
//
// module mod {
// choice foo {
// case bar {
// leaf baz {
// type string;
// }
// }
// }
// }
stack.enterSchemaTree(QName.create(module.getQNameModule(), elementName));
} catch (IllegalArgumentException e) {
throw new DocumentedException("Unable to find node " + elementName + " with namespace: " + namespace + " in module: " + module, e, ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
}
return stack.toSchemaTreeInference();
}
use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class NetconfMessageUtil method extractCapabilitiesFromHello.
public static Collection<String> extractCapabilitiesFromHello(final Document doc) {
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);
if (capabilitiesElement.isEmpty()) {
capabilitiesElement = responseElement.getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES);
}
List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
return Collections2.transform(caps, input -> {
// Trim possible leading/tailing whitespace
try {
return input.getTextContent().trim();
} catch (DocumentedException e) {
LOG.trace("Error fetching input text content", e);
return null;
}
});
}
use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class FutureCallbackTx method addCallback.
/**
* Add callback to the future object and close transaction chain.
*
* @param listenableFuture
* future object
* @param txType
* type of operation (READ, POST, PUT, DELETE)
* @param dataFactory
* factory setting result
* @param path unique identifier of a particular node instance in the data tree
* @throws RestconfDocumentedException
* if the Future throws an exception
*/
// FIXME: this is a *synchronous operation* and has to die
static <T> void addCallback(final ListenableFuture<T> listenableFuture, final String txType, final FutureDataFactory<? super T> dataFactory, final YangInstanceIdentifier path) throws RestconfDocumentedException {
try {
final T result = listenableFuture.get();
dataFactory.setResult(result);
LOG.trace("Transaction({}) SUCCESSFUL", txType);
} catch (InterruptedException e) {
dataFactory.setFailureStatus();
LOG.warn("Transaction({}) FAILED!", txType, e);
throw new RestconfDocumentedException("Transaction failed", e);
} catch (ExecutionException e) {
dataFactory.setFailureStatus();
LOG.warn("Transaction({}) FAILED!", txType, e);
final Throwable cause = e.getCause();
if (cause instanceof TransactionCommitFailedException) {
/* If device send some error message we want this message to get to client
and not just to throw it away or override it with new generic message.
We search for NetconfDocumentedException that was send from netconfSB
and we create RestconfDocumentedException accordingly.
*/
final List<Throwable> causalChain = Throwables.getCausalChain(cause);
for (Throwable error : causalChain) {
if (error instanceof DocumentedException) {
final ErrorTag errorTag = ((DocumentedException) error).getErrorTag();
if (errorTag.equals(ErrorTag.DATA_EXISTS)) {
LOG.trace("Operation via Restconf was not executed because data at {} already exists", path);
throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, "Data already exists", path));
} else if (errorTag.equals(ErrorTag.DATA_MISSING)) {
LOG.trace("Operation via Restconf was not executed because data at {} does not exist", path);
throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "Data does not exist", path));
}
}
if (error instanceof NetconfDocumentedException) {
throw new RestconfDocumentedException(error.getMessage(), ((NetconfDocumentedException) error).getErrorType(), ((NetconfDocumentedException) error).getErrorTag(), e);
}
}
throw new RestconfDocumentedException("Transaction(" + txType + ") not committed correctly", e);
} else {
throw new RestconfDocumentedException("Transaction failed", e);
}
}
}
Aggregations