use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class NetconfMDSalMappingTest method testEditConfigGetElementByTagName.
@Test
public void testEditConfigGetElementByTagName() throws Exception {
String stringWithoutPrefix = "<rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n" + " <edit-config>\n" + " <target>\n" + " <candidate/>\n" + " </target>\n" + " </edit-config>\n" + "</rpc>";
XmlElement xe = getXmlElement(stringWithoutPrefix);
NodeList nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
assertEquals(1, nodeList.getLength());
String stringWithPrefix = "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n" + " <nc:edit-config>\n" + " <nc:target>\n" + " <nc:candidate/>\n" + " </nc:target>\n" + " </nc:edit-config>\n" + "</nc:rpc>";
xe = getXmlElement(stringWithPrefix);
nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
assertEquals(1, nodeList.getLength());
String stringWithoutTarget = "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n" + " <nc:edit-config>\n" + " <nc:target>\n" + " </nc:target>\n" + " </nc:edit-config>\n" + "</nc:rpc>";
xe = getXmlElement(stringWithoutTarget);
final NodeList targetKey = EditConfig.getElementsByTagName(xe, TARGET_KEY);
assertThrows(DocumentedException.class, () -> XmlElement.fromDomElement((Element) targetKey.item(0)).getOnlyChildElement());
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class NetconfMDSalMappingTest method getXmlElement.
private static XmlElement getXmlElement(final String elementAsString) throws Exception {
Document document = XmlUtil.readXmlToDocument(elementAsString);
Element element = document.getDocumentElement();
return XmlElement.fromDomElement(element);
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class Netconf506Test method testValidateTypes.
@Test
public void testValidateTypes() throws Exception {
final SchemaContext context = YangParserTestUtils.parseYangResources(Bug8084.class, "/yang/filter-validator-test-mod-0.yang", "/yang/mdsal-netconf-mapping-test.yang");
final CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
doReturn(context).when(currentContext).getCurrentContext();
final FilterContentValidator validator = new FilterContentValidator(currentContext);
final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream("/filter/netconf506.xml"));
final XmlElement xmlElement = XmlElement.fromDomDocument(document);
final YangInstanceIdentifier actual = validator.validate(xmlElement);
final Map<QName, Object> inputs = new HashMap<>();
inputs.put(QName.create(BASE, "name"), "foo");
final YangInstanceIdentifier expected = YangInstanceIdentifier.builder().node(BASE).node(QName.create(BASE, "leafref-key-list")).nodeWithKey(QName.create(BASE, "leafref-key-list"), inputs).build();
assertEquals(expected, actual);
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class RuntimeRpc method handle.
@Override
public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
final XmlElement requestElement = getRequestElementWithCheck(requestMessage);
final Document document = XmlUtil.newDocument();
final XmlElement operationElement = requestElement.getOnlyChildElement();
final Map<String, Attr> attributes = requestElement.getAttributes();
final Element response = handle(document, operationElement, subsequentOperation);
final Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
if (XmlElement.fromDomElement(response).hasNamespace()) {
rpcReply.appendChild(response);
} else {
final NodeList list = response.getChildNodes();
if (list.getLength() == 0) {
rpcReply.appendChild(response);
} else {
while (list.getLength() != 0) {
rpcReply.appendChild(list.item(0));
}
}
}
for (final Attr attribute : attributes.values()) {
rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
}
document.appendChild(rpcReply);
return document;
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class FilterContentValidator method getKeyValues.
private Map<QName, Object> getKeyValues(final List<String> path, final XmlElement filterContent, final DataSchemaNode parentSchemaNode, final ListSchemaNode listSchemaNode) {
XmlElement current = filterContent;
// find list element
for (final String pathElement : path) {
final List<XmlElement> childElements = current.getChildElements(pathElement);
// if there are multiple list entries present in the filter, we can't use any keys and must read whole list
if (childElements.size() != 1) {
return Map.of();
}
current = childElements.get(0);
}
final Map<QName, Object> keys = new HashMap<>();
final List<QName> keyDefinition = listSchemaNode.getKeyDefinition();
for (final QName qualifiedName : keyDefinition) {
final Optional<XmlElement> childElements = current.getOnlyChildElementOptionally(qualifiedName.getLocalName());
if (childElements.isEmpty()) {
return Map.of();
}
final Optional<String> keyValue = childElements.get().getOnlyTextContentOptionally();
if (keyValue.isPresent()) {
final LeafSchemaNode listKey = (LeafSchemaNode) listSchemaNode.getDataChildByName(qualifiedName);
if (listKey instanceof IdentityrefTypeDefinition) {
keys.put(qualifiedName, keyValue.get());
} else {
final TypeDefinition<? extends TypeDefinition<?>> keyType = listKey.getType();
if (keyType instanceof IdentityrefTypeDefinition || keyType instanceof LeafrefTypeDefinition) {
final Document document = filterContent.getDomElement().getOwnerDocument();
final NamespaceContext nsContext = new UniversalNamespaceContextImpl(document, false);
final EffectiveModelContext modelContext = schemaContext.getCurrentContext();
final XmlCodecFactory xmlCodecFactory = XmlCodecFactory.create(modelContext);
final SchemaInferenceStack resolver = SchemaInferenceStack.of(modelContext, Absolute.of(parentSchemaNode.getQName(), listSchemaNode.getQName(), listKey.getQName()));
final TypeAwareCodec<?, NamespaceContext, XMLStreamWriter> typeCodec = xmlCodecFactory.codecFor(listKey, resolver);
final Object deserializedKeyValue = typeCodec.parseValue(nsContext, keyValue.get());
keys.put(qualifiedName, deserializedKeyValue);
} else {
final Object deserializedKey = TypeDefinitionAwareCodec.from(keyType).deserialize(keyValue.get());
keys.put(qualifiedName, deserializedKey);
}
}
}
}
return keys;
}
Aggregations