package org.opendaylight.controller.md.sal.common.impl.util.compat;
import com.google.common.base.Optional;
-import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
+import org.eclipse.jdt.annotation.Nullable;
+import org.gaul.modernizer_maven_annotations.SuppressModernizer;
import org.opendaylight.yangtools.concepts.Identifiable;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
public abstract class DataNormalizationOperation<T extends PathArgument> implements Identifiable<T> {
private final T identifier;
- private final Optional<DataSchemaNode> dataSchemaNode;
+ private final DataSchemaNode dataSchemaNode;
@Override
public T getIdentifier() {
protected DataNormalizationOperation(final T identifier, final SchemaNode schema) {
this.identifier = identifier;
- if (schema instanceof DataSchemaNode) {
- this.dataSchemaNode = Optional.of((DataSchemaNode) schema);
- } else {
- this.dataSchemaNode = Optional.absent();
- }
+ dataSchemaNode = schema instanceof DataSchemaNode ? (DataSchemaNode)schema : null;
}
public boolean isMixin() {
public abstract boolean isLeaf();
+ @SuppressModernizer
public Optional<DataSchemaNode> getDataSchemaNode() {
- // FIXME
- return dataSchemaNode;
+ return Optional.fromNullable(dataSchemaNode);
}
private abstract static class SimpleTypeNormalization<T extends PathArgument>
}
return potential;
}
+
+ private static DataNormalizationOperation<?> fromSchemaAndQNameChecked(final DataNodeContainer schema,
+ final QName child) throws DataNormalizationException {
+
+ final DataSchemaNode result = findChildSchemaNode(schema, child);
+ if (result == null) {
+ throw new DataNormalizationException(String.format(
+ "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child,
+ schema,schema.getChildNodes()));
+ }
+
+ // We try to look up if this node was added by augmentation
+ if (schema instanceof DataSchemaNode && result.isAugmenting()) {
+ return fromAugmentation(schema, (AugmentationTarget) schema, result);
+ }
+ return fromDataSchemaNode(result);
+ }
}
private static final class ListItemNormalization extends
super(augmentationIdentifierFrom(augmentation), augmentationProxy(augmentation,schema),null);
}
+ private static DataNodeContainer augmentationProxy(final AugmentationSchemaNode augmentation,
+ final DataNodeContainer schema) {
+ final Set<DataSchemaNode> children = new HashSet<>();
+ for (final DataSchemaNode augNode : augmentation.getChildNodes()) {
+ children.add(schema.getDataChildByName(augNode.getQName()));
+ }
+ return new EffectiveAugmentationSchema(augmentation, children);
+ }
+
@Override
public boolean isMixin() {
return true;
@Override
protected DataNormalizationOperation<?> fromLocalSchemaAndQName(final DataNodeContainer schema,
final QName child) {
- final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
- if (!potential.isPresent()) {
+ final DataSchemaNode result = findChildSchemaNode(schema, child);
+ if (result == null) {
return null;
}
- final DataSchemaNode result = potential.get();
// We try to look up if this node was added by augmentation
if (schema instanceof DataSchemaNode && result.isAugmenting()) {
return fromAugmentation(schema, (AugmentationTarget) schema, result);
public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier()).build();
}
-
}
private static class UnorderedMapMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
}
}
- private static class AnyXmlNormalization extends DataNormalizationOperation<NodeIdentifier> {
- AnyXmlNormalization(final AnyXmlSchemaNode schema) {
+ private static class AnyxmlNormalization extends DataNormalizationOperation<NodeIdentifier> {
+ AnyxmlNormalization(final AnyxmlSchemaNode schema) {
super(new NodeIdentifier(schema.getQName()), schema);
}
}
}
- private static Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent,
- final QName child) {
- DataSchemaNode potential = parent.getDataChildByName(child);
- if (potential == null) {
- final Iterable<ChoiceSchemaNode> choices = FluentIterable.from(parent.getChildNodes())
- .filter(ChoiceSchemaNode.class);
- potential = findChoice(choices, child);
- }
- return Optional.fromNullable(potential);
- }
-
- private static DataNormalizationOperation<?> fromSchemaAndQNameChecked(final DataNodeContainer schema,
- final QName child) throws DataNormalizationException {
-
- final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
- if (!potential.isPresent()) {
- throw new DataNormalizationException(String.format(
- "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child,
- schema,schema.getChildNodes()));
- }
-
- final DataSchemaNode result = potential.get();
- // We try to look up if this node was added by augmentation
- if (schema instanceof DataSchemaNode && result.isAugmenting()) {
- return fromAugmentation(schema, (AugmentationTarget) schema, result);
- }
- return fromDataSchemaNode(result);
+ private static @Nullable DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
+ final DataSchemaNode potential = parent.getDataChildByName(child);
+ return potential != null ? potential : findChoice(parent, child);
}
- private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
- ChoiceSchemaNode foundChoice = null;
- choiceLoop: for (final ChoiceSchemaNode choice : choices) {
+ private static @Nullable ChoiceSchemaNode findChoice(final DataNodeContainer parent, final QName child) {
+ for (final ChoiceSchemaNode choice : Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class)) {
for (final CaseSchemaNode caze : choice.getCases().values()) {
- if (findChildSchemaNode(caze, child).isPresent()) {
- foundChoice = choice;
- break choiceLoop;
+ if (findChildSchemaNode(caze, child) != null) {
+ return choice;
}
}
}
- return foundChoice;
+ return null;
}
public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchemaNode augmentation) {
return new AugmentationIdentifier(potentialChildren.build());
}
- private static DataNodeContainer augmentationProxy(final AugmentationSchemaNode augmentation,
- final DataNodeContainer schema) {
- final Set<DataSchemaNode> children = new HashSet<>();
- for (final DataSchemaNode augNode : augmentation.getChildNodes()) {
- children.add(schema.getDataChildByName(augNode.getQName()));
- }
- return new EffectiveAugmentationSchema(augmentation, children);
- }
-
/**
* Returns a DataNormalizationOperation for provided child node.
*
* otherwise returns a DataNormalizationOperation for child as
* call for {@link #fromDataSchemaNode(DataSchemaNode)}.
*/
+ @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+ justification = "https://github.com/spotbugs/spotbugs/issues/811")
private static DataNormalizationOperation<?> fromAugmentation(final DataNodeContainer parent,
final AugmentationTarget parentAug, final DataSchemaNode child) {
AugmentationSchemaNode augmentation = null;
return new ChoiceNodeNormalization((ChoiceSchemaNode) potential);
} else if (potential instanceof LeafListSchemaNode) {
return fromLeafListSchemaNode((LeafListSchemaNode) potential);
- } else if (potential instanceof AnyXmlSchemaNode) {
- return new AnyXmlNormalization((AnyXmlSchemaNode) potential);
+ } else if (potential instanceof AnyxmlSchemaNode) {
+ return new AnyxmlNormalization((AnyxmlSchemaNode) potential);
}
return null;
}