use of org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode in project yangtools by opendaylight.
the class NormalizedNodeStreamWriterStack method startAnydataNode.
public void startAnydataNode(final NodeIdentifier name) {
final SchemaNode schema = enterDataTree(name);
checkArgument(schema instanceof AnydataSchemaNode, "Node %s is not anydata", schema);
schemaStack.push(schema);
}
use of org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode in project yangtools by opendaylight.
the class YangParserSimpleTest method testParseAnyData.
@Test
public void testParseAnyData() {
final AnydataSchemaNode anydata = (AnydataSchemaNode) MODULE.findDataChildByName(QName.create(MODULE.getQNameModule(), "data2")).orElse(null);
assertNotNull("'anydata data not found'", anydata);
assertEquals("RegularAnydataEffectiveStatement{argument=(urn:opendaylight:simple-nodes?revision=2013-07-30)data2}", anydata.toString());
// test SchemaNode args
assertEquals(QName.create(SN, "data2"), anydata.getQName());
assertEquals(Optional.of("anydata desc"), anydata.getDescription());
assertEquals(Optional.of("data ref"), anydata.getReference());
assertEquals(Status.OBSOLETE, anydata.getStatus());
assertEquals(0, anydata.getUnknownSchemaNodes().size());
// test DataSchemaNode args
assertFalse(anydata.isAugmenting());
assertEquals(Optional.of(Boolean.FALSE), anydata.effectiveConfig());
assertTrue(anydata.isMandatory());
assertTrue(anydata.getWhenCondition().isPresent());
assertEquals("class != 'wheel'", anydata.getWhenCondition().orElseThrow().toString());
final Collection<? extends MustDefinition> mustConstraints = anydata.getMustConstraints();
assertEquals(2, mustConstraints.size());
final String must1 = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)";
final String must2 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
boolean found1 = false;
boolean found2 = false;
for (final MustDefinition must : mustConstraints) {
if (must1.equals(must.getXpath().toString())) {
found1 = true;
assertEquals(Optional.of("An ethernet MTU must be 1500"), must.getErrorMessage());
} else if (must2.equals(must.getXpath().toString())) {
found2 = true;
assertEquals(Optional.of("An atm MTU must be 64 .. 17966"), must.getErrorMessage());
assertEquals(Optional.of("anydata data error-app-tag"), must.getErrorAppTag());
assertEquals(Optional.of("an error occured in data"), must.getDescription());
assertEquals(Optional.of("data must ref"), must.getReference());
}
}
assertTrue(found1);
assertTrue(found2);
}
use of org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode in project yangtools by opendaylight.
the class DeviationResolutionTest method testDeviateAdd.
@Test
public void testDeviateAdd() throws Exception {
final EffectiveModelContext schemaContext = TestUtils.parseYangSource("/deviation-resolution-test/deviation-add/foo.yang", "/deviation-resolution-test/deviation-add/bar.yang");
assertNotNull(schemaContext);
final Module barModule = schemaContext.findModule("bar", Revision.of("2017-01-20")).get();
final LeafListSchemaNode myLeafList = (LeafListSchemaNode) barModule.getDataChildByName(QName.create(barModule.getQNameModule(), "my-leaf-list"));
assertNotNull(myLeafList);
assertEquals(Optional.of(Boolean.FALSE), myLeafList.effectiveConfig());
assertEquals(3, myLeafList.getDefaults().size());
final ElementCountConstraint constraint = myLeafList.getElementCountConstraint().get();
assertEquals((Object) 10, constraint.getMaxElements());
assertEquals((Object) 5, constraint.getMinElements());
assertNotNull(myLeafList.getType().getUnits());
final ListSchemaNode myList = (ListSchemaNode) barModule.getDataChildByName(QName.create(barModule.getQNameModule(), "my-list"));
assertNotNull(myList);
assertEquals(2, myList.getUniqueConstraints().size());
final ChoiceSchemaNode myChoice = (ChoiceSchemaNode) barModule.getDataChildByName(QName.create(barModule.getQNameModule(), "my-choice"));
assertNotNull(myChoice);
assertEquals("c2", myChoice.getDefaultCase().get().getQName().getLocalName());
final RpcDefinition myRpc = barModule.getRpcs().iterator().next();
final InputSchemaNode input = myRpc.getInput();
assertEquals(2, input.getMustConstraints().size());
final OutputSchemaNode output = myRpc.getOutput();
assertEquals(2, output.getMustConstraints().size());
final NotificationDefinition myNotification = barModule.getNotifications().iterator().next();
assertEquals(2, myNotification.getMustConstraints().size());
final AnyxmlSchemaNode myAnyxml = (AnyxmlSchemaNode) barModule.getDataChildByName(QName.create(barModule.getQNameModule(), "my-anyxml"));
assertNotNull(myAnyxml);
assertTrue(myAnyxml.isMandatory());
final AnydataSchemaNode myAnyData = (AnydataSchemaNode) barModule.findDataChildByName(QName.create(barModule.getQNameModule(), "my-anydata")).orElse(null);
assertNotNull(myAnyData);
assertTrue(myAnyData.isMandatory());
}
use of org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode in project yangtools by opendaylight.
the class XmlParserStream method parse.
/**
* This method parses the XML source and emits node events into a NormalizedNodeStreamWriter based on the
* YANG-modeled data contained in the XML source.
*
* @param reader
* StAX reader which is to used to walk through the XML source
* @return
* instance of XmlParserStream
* @throws XMLStreamException
* if a well-formedness error or an unexpected processing condition occurs while parsing the XML
* @throws URISyntaxException
* if the namespace URI of an XML element contains a syntax error
* @throws IOException
* if an error occurs while parsing the value of an anyxml node
* @throws SAXException
* if an error occurs while parsing the value of an anyxml node
*/
public XmlParserStream parse(final XMLStreamReader reader) throws XMLStreamException, URISyntaxException, IOException, SAXException {
if (reader.hasNext()) {
reader.nextTag();
final AbstractNodeDataWithSchema<?> nodeDataWithSchema;
if (parentNode instanceof ContainerLike) {
nodeDataWithSchema = new ContainerNodeDataWithSchema((ContainerLike) parentNode);
} else if (parentNode instanceof ListSchemaNode) {
nodeDataWithSchema = new ListNodeDataWithSchema((ListSchemaNode) parentNode);
} else if (parentNode instanceof AnyxmlSchemaNode) {
nodeDataWithSchema = new AnyXmlNodeDataWithSchema((AnyxmlSchemaNode) parentNode);
} else if (parentNode instanceof LeafSchemaNode) {
nodeDataWithSchema = new LeafNodeDataWithSchema((LeafSchemaNode) parentNode);
} else if (parentNode instanceof LeafListSchemaNode) {
nodeDataWithSchema = new LeafListNodeDataWithSchema((LeafListSchemaNode) parentNode);
} else if (parentNode instanceof AnydataSchemaNode) {
nodeDataWithSchema = new AnydataNodeDataWithSchema((AnydataSchemaNode) parentNode);
} else {
throw new IllegalStateException("Unsupported schema node type " + parentNode.getClass() + ".");
}
read(reader, nodeDataWithSchema, reader.getLocalName());
nodeDataWithSchema.write(writer);
}
return this;
}
use of org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode in project yangtools by opendaylight.
the class XmlParserStream method translateValueByType.
private Object translateValueByType(final Object value, final DataSchemaNode node, final NamespaceContext namespaceCtx) {
if (node instanceof AnyxmlSchemaNode) {
checkArgument(value instanceof Document);
/*
* FIXME: Figure out some YANG extension dispatch, which will reuse JSON parsing or XML parsing -
* anyxml is not well-defined in JSON.
*/
return new DOMSource(((Document) value).getDocumentElement());
}
if (node instanceof AnydataSchemaNode) {
checkArgument(value instanceof Document);
return new DOMSourceAnydata(new DOMSource(((Document) value).getDocumentElement()));
}
checkArgument(node instanceof TypedDataSchemaNode);
checkArgument(value instanceof String);
return codecs.codecFor((TypedDataSchemaNode) node, stack).parseValue(namespaceCtx, (String) value);
}
Aggregations