public static String toString(final ConstraintDefinition def) {
return MoreObjects.toStringHelper(def).omitNullValues()
- .add("whenCondition", def.getWhenCondition())
+ .add("whenCondition", def.getWhenCondition().orElse(null))
.add("mustConstraints", def.getMustConstraints())
.add("mandatory", def.isMandatory())
.add("minElements", def.getMinElements())
package org.opendaylight.yangtools.yang.data.util;
import com.google.common.collect.ImmutableSet;
+import java.util.Optional;
import java.util.Set;
import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
import org.opendaylight.yangtools.yang.model.api.MustDefinition;
}
@Override
- public final RevisionAwareXPath getWhenCondition() {
- return null;
+ public final Optional<RevisionAwareXPath> getWhenCondition() {
+ return Optional.empty();
}
@Override
* from a grouping in a "uses" statement.
*/
public interface AugmentationSchemaNode extends DataNodeContainer, NotificationNodeContainer, ActionNodeContainer,
- WithStatus {
- /**
- * Returns when statement.
- *
- * <p>
- * If when condition is present node defined by the parent data definition
- * statement is only valid when the returned XPath
- * expression conceptually evaluates to "true"
- * for a particular instance, then the node defined by the parent data
- * definition statement is valid; otherwise, it is not.
- *
- * @return XPath condition
- */
- RevisionAwareXPath getWhenCondition();
-
+ WhenConditionAware, WithStatus {
/**
* Returns augmentation schema path.
*
* YANG element therefore if the constraint doesn't have sense for some element
* then the method returns <code>null</code> value.
*/
-public interface ConstraintDefinition {
-
- /**
- * Specifies the condition when the data node which contains
- * <code>when</code> YANG substatement has to be present. If XPath
- * expression is evaluated as true then the data node has to be present.
- *
- * @return XPath expression.
- */
- RevisionAwareXPath getWhenCondition();
-
+public interface ConstraintDefinition extends WhenConditionAware {
/**
* Specifies the rules which the node which contains <code>must</code> YANG
* substatement has to match.
package org.opendaylight.yangtools.yang.model.api;
import java.util.Map;
-import java.util.Optional;
import java.util.Set;
import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
/**
- * Contains the methods for getting data and checking properties of the YANG
- * <code>uses</code> substatement.
+ * Contains the methods for getting data and checking properties of the YANG <code>uses</code> substatement.
*/
-public interface UsesNode extends DocumentedNode.WithStatus {
+public interface UsesNode extends WhenConditionAware, WithStatus {
/**
* Returns the schema path to used grouping.
* refined node
*/
@Nonnull Map<SchemaPath, SchemaNode> getRefines();
-
- /**
- * Returns when statement.
- *
- * <p>
- * If when condition is present node defined by the parent data definition
- * statement is only valid when the returned XPath expression conceptually
- * evaluates to "true" for a particular instance, then the node defined by
- * the parent data definition statement is valid; otherwise, it is not.
- *
- * @return Optional of XPath condition
- */
- default @Nonnull Optional<RevisionAwareXPath> getWhenCondition() {
- return Optional.empty();
- }
}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies, 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.model.api;
+
+import com.google.common.annotations.Beta;
+import java.util.Optional;
+
+/**
+ * Mix-in interface for nodes which can be conditional on a when statement.
+ *
+ * @author Robert Varga
+ */
+@Beta
+public interface WhenConditionAware {
+ /**
+ * Returns when statement.
+ *
+ * <p>
+ * If when condition is present node defined by the parent data definition
+ * statement is only valid when the returned XPath
+ * expression conceptually evaluates to "true"
+ * for a particular instance, then the node defined by the parent data
+ * definition statement is valid; otherwise, it is not.
+ *
+ * @return XPath condition
+ */
+ Optional<RevisionAwareXPath> getWhenCondition();
+
+}
}
private void emitConstraints(final ConstraintDefinition constraints) {
- emitWhen(constraints.getWhenCondition());
+ constraints.getWhenCondition().ifPresent(this::emitWhen);
for (final MustDefinition mustCondition : constraints.getMustConstraints()) {
emitMust(mustCondition);
}
private void emitLeaf(final LeafSchemaNode child) {
super.writer.startLeafNode(child.getQName());
- emitWhen(child.getConstraints().getWhenCondition());
+ child.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
// FIXME: BUG-2444: *(ifFeatureNode )
emitTypeNode(child.getPath(), child.getType());
emitUnitsNode(child.getUnits());
private void emitLeafList(final LeafListSchemaNode child) {
super.writer.startLeafListNode(child.getQName());
- emitWhen(child.getConstraints().getWhenCondition());
+ child.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
// FIXME: BUG-2444: *(ifFeatureNode )
emitTypeNode(child.getPath(), child.getType());
emitUnitsNode(child.getType().getUnits());
private void emitList(final ListSchemaNode child) {
super.writer.startListNode(child.getQName());
- emitWhen(child.getConstraints().getWhenCondition());
+ child.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
// FIXME: BUG-2444: *(ifFeatureNode )
emitMustNodes(child.getConstraints().getMustConstraints());
private void emitChoice(final ChoiceSchemaNode choice) {
super.writer.startChoiceNode(choice.getQName());
- emitWhen(choice.getConstraints().getWhenCondition());
+ choice.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
// FIXME: BUG-2444: *(ifFeatureNode )
// FIXME: BUG-2444: defaultNode //Optional
emitConfigNode(choice.isConfiguration());
return;
}
super.writer.startCaseNode(caze.getQName());
- emitWhen(caze.getConstraints().getWhenCondition());
+ caze.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
// FIXME: BUG-2444: *(ifFeatureNode )
emitDocumentedNode(caze);
emitDataNodeContainer(caze);
}
private void emitBodyOfDataSchemaNode(final DataSchemaNode dataSchemaNode) {
- emitWhen(dataSchemaNode.getConstraints().getWhenCondition());
+ dataSchemaNode.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
// FIXME: BUG-2444: *(ifFeatureNode )
emitMustNodes(dataSchemaNode.getConstraints().getMustConstraints());
emitConfigNode(dataSchemaNode.isConfiguration());
}
@Override
- public RevisionAwareXPath getWhenCondition() {
+ public Optional<RevisionAwareXPath> getWhenCondition() {
return delegate.getWhenCondition();
}
}
@Override
- public RevisionAwareXPath getWhenCondition() {
- return whenCondition;
+ public Optional<RevisionAwareXPath> getWhenCondition() {
+ return Optional.ofNullable(whenCondition);
}
@Nonnull
import static java.util.Objects.requireNonNull;
import com.google.common.collect.ImmutableSet;
+import java.util.Optional;
import java.util.Set;
import org.opendaylight.yangtools.yang.data.util.ConstraintDefinitions;
import org.opendaylight.yangtools.yang.data.util.EmptyConstraintDefinition;
}
@Override
- public RevisionAwareXPath getWhenCondition() {
- return whenCondition;
+ public Optional<RevisionAwareXPath> getWhenCondition() {
+ return Optional.ofNullable(whenCondition);
}
@Override
AugmentationSchemaNode augment2 = null;
AugmentationSchemaNode augment3 = null;
for (final AugmentationSchemaNode as : augmentations) {
- if (as.getWhenCondition() == null) {
+ if (!as.getWhenCondition().isPresent()) {
augment3 = as;
- } else if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
+ } else if ("if:ifType='ds0'".equals(as.getWhenCondition().get().toString())) {
augment1 = as;
- } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
+ } else if ("if:ifType='ds2'".equals(as.getWhenCondition().get().toString())) {
augment2 = as;
}
}
package org.opendaylight.yangtools.yang.stmt;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
assertTrue(dataChildByName instanceof ContainerSchemaNode);
ContainerSchemaNode topContainer = (ContainerSchemaNode) dataChildByName;
- RevisionAwareXPath whenConditionTopContainer = topContainer.getConstraints().getWhenCondition();
- assertNull(whenConditionTopContainer);
+ assertFalse(topContainer.getConstraints().getWhenCondition().isPresent());
assertEquals(Status.CURRENT, topContainer.getStatus());
assertNull(topContainer.getDescription());
assertNull(topContainer.getReference());
assertTrue(dataChildByName2 instanceof LeafSchemaNode);
LeafSchemaNode extendedLeaf = (LeafSchemaNode) dataChildByName2;
- RevisionAwareXPath whenConditionExtendedLeaf = extendedLeaf.getConstraints().getWhenCondition();
+ RevisionAwareXPath whenConditionExtendedLeaf = extendedLeaf.getConstraints().getWhenCondition().get();
assertEquals(new RevisionAwareXPathImpl("module1:top = 'extended'", false), whenConditionExtendedLeaf);
assertEquals(Status.DEPRECATED, extendedLeaf.getStatus());
final DataSchemaNode dataNodeBar = schemaContext.getDataChildByName(QName.create("foo", "2016-07-11", "bar"));
assertTrue(dataNodeBar instanceof ContainerSchemaNode);
final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
- final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition();
+ final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition().get();
assertEquals("/foo != \"bar\"", whenCondition.toString());
final Set<TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
final DataSchemaNode dataNodeBar = schemaContext.getDataChildByName(QName.create("foo", "2016-07-11", "bar"));
assertTrue(dataNodeBar instanceof ContainerSchemaNode);
final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
- final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition();
+ final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition().get();
assertEquals("/foo != 'bar'", whenCondition.toString());
final Set<TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
QName.create(testModule.getQNameModule(), "test-container"));
assertNotNull(container);
- assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition().toString());
+ assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition()
+ .get().toString());
}
}
assertFalse(data.isAugmenting());
assertFalse(data.isConfiguration());
final ConstraintDefinition constraints = data.getConstraints();
- assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
+ assertEquals("class != 'wheel'", constraints.getWhenCondition().get().toString());
final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
assertEquals(2, constraints.getMustConstraints().size());
// constraints
final ConstraintDefinition constraints = nodes.getConstraints();
- assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
+ assertEquals("class != 'wheel'", constraints.getWhenCondition().get().toString());
final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
assertEquals(2, constraints.getMustConstraints().size());
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
import org.opendaylight.yangtools.yang.stmt.TestUtils;
assertEquals("main-impl", caseNode.getQName().getLocalName());
assertEquals(13, caseNode.getChildNodes().size());
- final RevisionAwareXPath whenCondition = caseNode.getConstraints().getWhenCondition();
- assertNotNull(whenCondition);
+ assertTrue(caseNode.getConstraints().getWhenCondition().isPresent());
choice = (ChoiceSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "state"));
assertNotNull(choice);