import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
final class MandatoryLeafEnforcer implements Immutable {
private static final Logger LOG = LoggerFactory.getLogger(MandatoryLeafEnforcer.class);
final class MandatoryLeafEnforcer implements Immutable {
private static final Logger LOG = LoggerFactory.getLogger(MandatoryLeafEnforcer.class);
static Optional<MandatoryLeafEnforcer> forContainer(final DataNodeContainer schema,
final DataTreeConfiguration treeConfig) {
if (!treeConfig.isMandatoryNodesValidationEnabled()) {
return Optional.empty();
}
static Optional<MandatoryLeafEnforcer> forContainer(final DataNodeContainer schema,
final DataTreeConfiguration treeConfig) {
if (!treeConfig.isMandatoryNodesValidationEnabled()) {
return Optional.empty();
}
findMandatoryNodes(builder, YangInstanceIdentifier.empty(), schema, treeConfig.getTreeType());
findMandatoryNodes(builder, YangInstanceIdentifier.empty(), schema, treeConfig.getTreeType());
return mandatoryNodes.isEmpty() ? Optional.empty() : Optional.of(new MandatoryLeafEnforcer(mandatoryNodes));
}
void enforceOnData(final NormalizedNode data) {
return mandatoryNodes.isEmpty() ? Optional.empty() : Optional.of(new MandatoryLeafEnforcer(mandatoryNodes));
}
void enforceOnData(final NormalizedNode data) {
- for (final YangInstanceIdentifier id : mandatoryNodes) {
- checkArgument(NormalizedNodes.findNode(data, id).isPresent(),
- "Node %s is missing mandatory descendant %s", data.getIdentifier(), id);
- }
+ mandatoryNodes.forEach(node -> node.enforceOnData(data));
- private static void findMandatoryNodes(final Builder<YangInstanceIdentifier> builder,
- final YangInstanceIdentifier id, final DataNodeContainer schema, final TreeType type) {
+ private static void findMandatoryNodes(final Builder<MandatoryDescendant> builder,
+ final YangInstanceIdentifier id, final DataNodeContainer schema, final TreeType type) {
for (final DataSchemaNode child : schema.getChildNodes()) {
if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
if (child instanceof ContainerSchemaNode) {
final ContainerSchemaNode container = (ContainerSchemaNode) child;
if (!container.isPresenceContainer()) {
for (final DataSchemaNode child : schema.getChildNodes()) {
if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
if (child instanceof ContainerSchemaNode) {
final ContainerSchemaNode container = (ContainerSchemaNode) child;
if (!container.isPresenceContainer()) {
- findMandatoryNodes(builder, id.node(NodeIdentifier.create(child.getQName())), container, type);
+ findMandatoryNodes(builder,
+ id.node(NodeIdentifier.create(container.getQName())), container, type);
}
} else {
boolean needEnforce = child instanceof MandatoryAware && ((MandatoryAware) child).isMandatory();
if (!needEnforce && child instanceof ElementCountConstraintAware) {
}
} else {
boolean needEnforce = child instanceof MandatoryAware && ((MandatoryAware) child).isMandatory();
if (!needEnforce && child instanceof ElementCountConstraintAware) {
- needEnforce = ((ElementCountConstraintAware) child)
- .getElementCountConstraint().map(constraint -> {
- final Integer min = constraint.getMinElements();
- return min != null && min > 0;
- }).orElse(Boolean.FALSE).booleanValue();
+ needEnforce = ((ElementCountConstraintAware) child).getElementCountConstraint()
+ .map(constraint -> {
+ final Integer min = constraint.getMinElements();
+ return min != null && min > 0;
+ })
+ .orElse(Boolean.FALSE);
- final YangInstanceIdentifier childId = id.node(NodeIdentifier.create(child.getQName()));
- LOG.debug("Adding mandatory child {}", childId);
- builder.add(childId.toOptimized());
+ final MandatoryDescendant desc = MandatoryDescendant.create(id, schema, child);
+ LOG.debug("Adding mandatory child {}", desc);
+ builder.add(desc);