public static <E extends Exception> DocumentedException wrap(E exception) throws DocumentedException { final Map<String, String> errorInfo = new HashMap<>(); errorInfo.put(ErrorTag.operation_failed.name(), "Exception thrown"); throw new DocumentedException(exception.getMessage(), exception, ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error, errorInfo); } public static DocumentedException wrap(ValidationException e) throws DocumentedException {
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 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); } }
private ModifyAction getDefaultOperation(final XmlElement operationElement) throws DocumentedException { final NodeList elementsByTagName = operationElement.getDomElement().getElementsByTagName(DEFAULT_OPERATION_KEY); if(elementsByTagName.getLength() == 0) { return ModifyAction.MERGE; } else if(elementsByTagName.getLength() > 1) { throw new DocumentedException("Multiple " + DEFAULT_OPERATION_KEY + " elements", ErrorType.rpc, ErrorTag.unknown_attribute, ErrorSeverity.error); } else { return ModifyAction.fromXmlValue(elementsByTagName.item(0).getTextContent()); } }
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 static DocumentedException wrap(ValidationException e) throws DocumentedException { final Map<String, String> errorInfo = new HashMap<>(); errorInfo.put(ErrorTag.operation_failed.name(), "Validation failed"); throw new DocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error, errorInfo); }
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); }
public static DocumentedException wrap(ConflictingVersionException e) throws DocumentedException { final Map<String, String> errorInfo = new HashMap<>(); errorInfo.put(ErrorTag.operation_failed.name(), "Optimistic lock failed"); throw new DocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error, errorInfo); }
@Override protected Element handle(Document document, XmlElement operationElement, NetconfOperationChainedExecution subsequentOperation) throws DocumentedException { if (!subsequentOperation.isExecutionTermination()){ throw new DocumentedException(String.format("No netconf operation expected to be subsequent to %s, but is %s", this, subsequentOperation), DocumentedException.ErrorType.application, DocumentedException.ErrorTag.malformed_message, DocumentedException.ErrorSeverity.error); } return handleWithNoSubsequentOperations(document, operationElement); }
private Datastore extractTargetParameter(final XmlElement operationElement) throws DocumentedException { final NodeList elementsByTagName = operationElement.getDomElement().getElementsByTagName(TARGET_KEY); // Direct lookup instead of using XmlElement class due to performance if (elementsByTagName.getLength() == 0) { throw new DocumentedException("Missing target element", ErrorType.rpc, ErrorTag.missing_attribute, ErrorSeverity.error); } else if (elementsByTagName.getLength() > 1) { throw new DocumentedException("Multiple target elements", ErrorType.rpc, ErrorTag.unknown_attribute, ErrorSeverity.error); } else { final XmlElement targetChildNode = XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement(); return Datastore.valueOf(targetChildNode.getName()); } }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException { final Datastore targetDatastore = extractTargetParameter(operationElement); if (targetDatastore == Datastore.candidate) { LOG.debug("Locking candidate datastore on session: {}", getNetconfSessionIdForReporting()); return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); } throw new DocumentedException("Unable to lock " + targetDatastore + " datastore", DocumentedException.ErrorType.application, DocumentedException.ErrorTag.operation_not_supported, DocumentedException.ErrorSeverity.error); }
private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws DocumentedException{ if (datastore == Datastore.candidate) { return transactionProvider.getOrCreateTransaction(); } else if (datastore == Datastore.running) { return transactionProvider.createRunningTransaction(); } throw new DocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error); }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException { final Datastore targetDatastore = Lock.extractTargetParameter(operationElement); if (targetDatastore == Datastore.candidate) { LOG.debug("Unlocking candidate datastore on session: {}", getNetconfSessionIdForReporting()); return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); } throw new DocumentedException("Unable to unlock " + targetDatastore + " datastore", DocumentedException.ErrorType.application, DocumentedException.ErrorTag.operation_not_supported, DocumentedException.ErrorSeverity.error); }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException { final Datastore targetDatastore = Lock.extractTargetParameter(operationElement); if(targetDatastore == Datastore.candidate) { // Since candidate datastore instances are allocated per session and not accessible anywhere else, no need to lock LOG.debug("Unlocking {} datastore on session: {}", targetDatastore, getNetconfSessionIdForReporting()); // TODO this should fail if we are not locked return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); } // Not supported running lock throw new DocumentedException("Unable to unlock " + Datastore.running + " datastore", DocumentedException.ErrorType.application, DocumentedException.ErrorTag.operation_not_supported, DocumentedException.ErrorSeverity.error); }
public synchronized boolean commitRunningTransaction(DOMDataReadWriteTransaction tx) throws DocumentedException { allOpenReadWriteTransactions.remove(tx); CheckedFuture<Void, TransactionCommitFailedException> future = tx.submit(); try { future.checkedGet(); } catch (TransactionCommitFailedException e) { LOG.debug("Transaction {} failed on", tx, e); throw new DocumentedException("Transaction commit failed on " + e.getMessage() + " " + netconfSessionIdForReporting, ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error); } return true; }
private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws DocumentedException{ if (datastore == Datastore.candidate) { return transactionProvider.getOrCreateTransaction(); } else if (datastore == Datastore.running) { return transactionProvider.createRunningTransaction(); } throw new DocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error); }
@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); }
@Override protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException { final Datastore targetDatastore = extractTargetParameter(operationElement); if(targetDatastore == Datastore.candidate) { // Since candidate datastore instances are allocated per session and not accessible anywhere else, no need to lock LOG.debug("Locking {} datastore on session: {}", targetDatastore, getNetconfSessionIdForReporting()); // TODO should this fail if we are already locked ? return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); } // Not supported running lock throw new DocumentedException("Unable to lock " + Datastore.running + " datastore", DocumentedException.ErrorType.application, DocumentedException.ErrorTag.operation_not_supported, DocumentedException.ErrorSeverity.error); }
static GetConfigExecution fromXml(final XmlElement xml, final String operationName) throws DocumentedException { try { validateInputRpc(xml, operationName); } catch (final DocumentedException e) { throw new DocumentedException("Incorrect RPC: " + e.getMessage(), e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo()); } final Optional<Datastore> sourceDatastore; try { sourceDatastore = parseSource(xml); } catch (final DocumentedException e) { throw new DocumentedException("Get-config source attribute error: " + e.getMessage(), e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo()); } return new GetConfigExecution(sourceDatastore); }
private void checkXml(XmlElement xml) throws DocumentedException { xml.checkName(VALIDATE); xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); XmlElement sourceChildNode = sourceElement.getOnlyChildElement(); sourceChildNode.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); String datastoreValue = sourceChildNode.getName(); Datastore sourceDatastore = Datastore.valueOf(datastoreValue); if (sourceDatastore != Datastore.candidate){ throw new DocumentedException( "Only " + Datastore.candidate + " is supported as source for " + VALIDATE + " but was " + datastoreValue, ErrorType.application, ErrorTag.data_missing, ErrorSeverity.error); } }