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 {
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); }
rpcReply.appendChild( rpcError ); rpcError.appendChild( createTextNode( doc, ERROR_TYPE, getErrorType().getTagValue() ) ); rpcError.appendChild( createTextNode( doc, ERROR_TAG, getErrorTag().getTagValue() ) ); rpcError.appendChild( createTextNode( doc, ERROR_SEVERITY, getErrorSeverity().getTagValue() ) ); rpcError.appendChild( createTextNode( doc, ERROR_MESSAGE, getLocalizedMessage() ) ); Map<String, String> errorInfoMap = getErrorInfo(); if( errorInfoMap != null && !errorInfoMap.isEmpty() ) { errorInfoNode.appendChild( createTextNode( doc, entry.getKey(), entry.getValue() ) );
@Override protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws DocumentedException { checkXml(xml); try { getConfigSubsystemFacade().validateConfiguration(); } catch (ValidationException e) { LOG.warn("Validation failed", e); throw DocumentedException.wrap(e); } catch (IllegalStateException e) { LOG.warn("Validation failed", e); final Map<String, String> errorInfo = new HashMap<>(); errorInfo .put(ErrorTag.operation_failed.name(), "Datastore is not present. Use 'get-config' or 'edit-config' before triggering 'operations' operation"); throw new DocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error, errorInfo); } LOG.trace("Datastore {} validated successfully", Datastore.candidate); return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); } }
errorInfo = parseErrorInfo( rpcErrorChild ); return new DocumentedException( errorMessage, errorType, errorTag, errorSeverity, errorInfo );
public static XmlElement fromString(String s) throws DocumentedException { try { return new XmlElement(XmlUtil.readXmlToElement(s)); } catch (IOException | SAXException e) { throw DocumentedException.wrap(e); } }
@Override public String toString() { return "NetconfDocumentedException{" + "message=" + getMessage() + ", errorType=" + this.errorType + ", errorTag=" + this.errorTag + ", errorSeverity=" + this.errorSeverity + ", errorInfo=" + this.errorInfo + '}'; } }
} catch (DocumentedException e) { LOG.debug("Cannot retrieve netconf operation namespace from message due to ", e); throw new DocumentedException("Cannot retrieve netconf operation namespace from message", ErrorType.protocol, ErrorTag.unknown_namespace, ErrorSeverity.error); throw new DocumentedException("Unable to find module in Schema Context with namespace and name : " + namespaceURI + " " + netconfOperationName + schemaContext.getCurrentContext(), ErrorType.application, ErrorTag.bad_element, ErrorSeverity.error); throw new DocumentedException("Unable to find RpcDefinition with namespace and name : " + namespaceURI + " " + netconfOperationName, ErrorType.application, ErrorTag.bad_element, ErrorSeverity.error); throw DocumentedException.wrap(e);
@Override protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws DocumentedException { checkXml(xml); CommitStatus status; try { status = getConfigSubsystemFacade().commitTransaction(); LOG.trace("Datastore {} committed successfully: {}", Datastore.candidate, status); } catch (ConflictingVersionException | ValidationException e) { throw DocumentedException.wrap(e); } LOG.trace("Datastore {} committed successfully: {}", Datastore.candidate, status); return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent()); }
public static void sendErrorMessage(final Channel channel, final DocumentedException sendErrorException) { LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException); final Document errorDocument = createDocument(sendErrorException); ChannelFuture f = channel.writeAndFlush(new NetconfMessage(errorDocument)); f.addListener(new SendErrorVerifyingListener(sendErrorException)); }
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(); }
GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException { getSchemaElement.checkName(GET_SCHEMA); getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING); XmlElement identifierElement = null; try { identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER); } catch (final DocumentedException e) { LOG.trace("Can't get identifier element as only child element with same namespace due to ",e); throw DocumentedException.wrap(e); } identifier = identifierElement.getTextContent(); final Optional<XmlElement> versionElement = getSchemaElement .getOnlyChildElementWithSameNamespaceOptionally(VERSION); if (versionElement.isPresent()) { version = Optional.of(versionElement.get().getTextContent()); } else { version = Optional.absent(); } } }
public static void sendErrorMessage(final NetconfSession session, final DocumentedException sendErrorException) { LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException); final Document errorDocument = createDocument(sendErrorException); ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument)); f.addListener(new SendErrorVerifyingListener(sendErrorException)); }
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 String getTextContent() throws DocumentedException { NodeList childNodes = element.getChildNodes(); if (childNodes.getLength() == 0) { return DEFAULT_NAMESPACE_PREFIX; } for(int i = 0; i < childNodes.getLength(); i++) { Node textChild = childNodes.item(i); if (textChild instanceof Text) { String content = textChild.getTextContent(); return content.trim(); } } throw new DocumentedException(getName() + " should contain text.", DocumentedException.ErrorType.application, DocumentedException.ErrorTag.invalid_value, DocumentedException.ErrorSeverity.error ); }
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 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); }
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); }