We are sharing essentially the same code in both JSON and XML
parsers, where we really to make this common code exposed from
appropriate NodeDataWithSchema.
Centralizing the interface contract and implementations allows us
to ditch external users of CompositeNodeDataWithSchema.addChild(),
which is now deprecated.
Change-Id: I5aca3d3eecba3f9e6ffee4b9ee15e70afa47aed6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit
94336a79d974543e50cd5e9047d6bd148e4ddbda)
import org.opendaylight.yangtools.yang.data.util.AbstractNodeDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema;
-import org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema;
-import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.MultipleEntryDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.OperationAsContainer;
import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
}
private static AbstractNodeDataWithSchema<?> newArrayEntry(final AbstractNodeDataWithSchema<?> parent) {
- AbstractNodeDataWithSchema<?> newChild;
- if (parent instanceof ListNodeDataWithSchema) {
- newChild = ListEntryNodeDataWithSchema.forSchema(((ListNodeDataWithSchema) parent).getSchema());
- } else if (parent instanceof LeafListNodeDataWithSchema) {
- newChild = new LeafListEntryNodeDataWithSchema(((LeafListNodeDataWithSchema) parent).getSchema());
- } else {
+ if (!(parent instanceof MultipleEntryDataWithSchema)) {
throw new IllegalStateException("Found an unexpected array nested under " + parent.getSchema().getQName());
}
- ((CompositeNodeDataWithSchema<?>) parent).addChild(newChild);
- return newChild;
+ return ((MultipleEntryDataWithSchema<?>) parent).newChildEntry();
}
private void setValue(final AbstractNodeDataWithSchema<?> parent, final String value) {
import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.MountPointData;
+import org.opendaylight.yangtools.yang.data.util.MultipleEntryDataWithSchema;
import org.opendaylight.yangtools.yang.data.util.OperationAsContainer;
import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
}
private static AbstractNodeDataWithSchema<?> newEntryNode(final AbstractNodeDataWithSchema<?> parent) {
- final AbstractNodeDataWithSchema<?> newChild;
- if (parent instanceof ListNodeDataWithSchema) {
- newChild = ListEntryNodeDataWithSchema.forSchema(((ListNodeDataWithSchema) parent).getSchema());
- } else {
- verify(parent instanceof LeafListNodeDataWithSchema, "Unexpected parent %s", parent);
- newChild = new LeafListEntryNodeDataWithSchema(((LeafListNodeDataWithSchema) parent).getSchema());
- }
- ((CompositeNodeDataWithSchema<?>) parent).addChild(newChild);
- return newChild;
+ verify(parent instanceof MultipleEntryDataWithSchema, "Unexpected parent %s", parent);
+ return ((MultipleEntryDataWithSchema<?>) parent).newChildEntry();
}
@Override
return newChild == null ? addCompositeChild(schema) : newChild;
}
+ @Deprecated
public void addChild(final AbstractNodeDataWithSchema<?> newChild) {
children.add(newChild);
}
* <p>
* Represents a YANG leaf-list node.
*/
-public class LeafListNodeDataWithSchema extends CompositeNodeDataWithSchema<LeafListSchemaNode> {
+public class LeafListNodeDataWithSchema extends CompositeNodeDataWithSchema<LeafListSchemaNode>
+ implements MultipleEntryDataWithSchema<LeafListEntryNodeDataWithSchema> {
public LeafListNodeDataWithSchema(final LeafListSchemaNode schema) {
super(schema);
}
super.write(writer, metaWriter);
writer.endNode();
}
+
+ @Override
+ public final LeafListEntryNodeDataWithSchema newChildEntry() {
+ final LeafListEntryNodeDataWithSchema child = new LeafListEntryNodeDataWithSchema(getSchema());
+ addChild(child);
+ return child;
+ }
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension;
import org.opendaylight.yangtools.util.ImmutableMapTemplate;
import org.opendaylight.yangtools.yang.common.QName;
super(schema);
}
- public static ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
+ public static @NonNull ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
final List<QName> keyDef = schema.getKeyDefinition();
return keyDef.isEmpty() ? new Unkeyed(schema) : new Keyed(schema, keyDef);
}
* <p>
* Represents a YANG list node.
*/
-public class ListNodeDataWithSchema extends CompositeNodeDataWithSchema<ListSchemaNode> {
-
+public class ListNodeDataWithSchema extends CompositeNodeDataWithSchema<ListSchemaNode>
+ implements MultipleEntryDataWithSchema<ListEntryNodeDataWithSchema> {
public ListNodeDataWithSchema(final ListSchemaNode schema) {
super(schema);
}
writer.endNode();
}
+ @Override
+ public final ListEntryNodeDataWithSchema newChildEntry() {
+ final ListEntryNodeDataWithSchema child = ListEntryNodeDataWithSchema.forSchema(getSchema());
+ addChild(child);
+ return child;
+ }
}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.util;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * Marker interface for {@link AbstractNodeDataWithSchema} specializations which are composed of multiple entries.
+ *
+ * @param <T> Entry AbstractNodeDataWithSchema type
+ */
+@Beta
+public interface MultipleEntryDataWithSchema<T extends AbstractNodeDataWithSchema<?>> {
+ /**
+ * Create a new child entry.
+ *
+ * @return A new child entry.
+ */
+ @NonNull T newChildEntry();
+}