*/
package org.opendaylight.restconf.nb.rfc8040.codecs;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.collect.Iterables;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Set;
import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.restconf.common.util.IdentityValuesDTO;
import org.opendaylight.restconf.common.util.IdentityValuesDTO.IdentityValue;
import org.opendaylight.restconf.common.util.IdentityValuesDTO.Predicate;
import org.opendaylight.restconf.common.util.RestUtil;
-import org.opendaylight.yangtools.concepts.Codec;
+import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.codec.InstanceIdentifierCodec;
import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec;
import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
-import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
private RestCodec() {
}
- public static Codec<Object, Object> from(final TypeDefinition<?> typeDefinition,
- final DOMMountPoint mountPoint, final SchemaContext schemaContext) {
+ // FIXME: IllegalArgumentCodec is not quite accurate
+ public static IllegalArgumentCodec<Object, Object> from(final TypeDefinition<?> typeDefinition,
+ final DOMMountPoint mountPoint, final EffectiveModelContext schemaContext) {
return new ObjectCodec(typeDefinition, mountPoint, schemaContext);
}
@SuppressWarnings("rawtypes")
- public static final class ObjectCodec implements Codec<Object, Object> {
+ public static final class ObjectCodec implements IllegalArgumentCodec<Object, Object> {
private static final Logger LOG = LoggerFactory.getLogger(ObjectCodec.class);
- public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
- private final Codec instanceIdentifier;
- private final Codec identityrefCodec;
+ public static final IllegalArgumentCodec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
+ private final IllegalArgumentCodec instanceIdentifier;
+ private final IllegalArgumentCodec identityrefCodec;
private final TypeDefinition<?> type;
- private final SchemaContext schemaContext;
+ private final EffectiveModelContext schemaContext;
private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
- final SchemaContext schemaContext) {
+ final EffectiveModelContext schemaContext) {
this.schemaContext = schemaContext;
this.type = RestUtil.resolveBaseTypeFrom(typeDefinition);
if (this.type instanceof IdentityrefTypeDefinition) {
final IdentityValue identityValue = qNameToIdentityValue(pathArgument.getNodeType());
if (pathArgument instanceof NodeIdentifierWithPredicates && identityValue != null) {
final List<Predicate> predicates =
- keyValuesToPredicateList(((NodeIdentifierWithPredicates) pathArgument).getKeyValues());
+ keyValuesToPredicateList(((NodeIdentifierWithPredicates) pathArgument).entrySet());
identityValue.setPredicates(predicates);
} else if (pathArgument instanceof NodeWithValue && identityValue != null) {
final List<Predicate> predicates = new ArrayList<>();
return identityValuesDTO;
}
- @SuppressFBWarnings(value = {"NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE",
- "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"}, justification = "Unrecognised NullableDecl")
+ @SuppressFBWarnings(value = {
+ "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE",
+ "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"
+ }, justification = "Unrecognised NullableDecl")
@Override
public YangInstanceIdentifier deserialize(final IdentityValuesDTO data) {
final List<PathArgument> result = new ArrayList<>();
final List<IdentityValue> identities = data.getValuesWithNamespaces();
for (int i = 0; i < identities.size(); i++) {
final IdentityValue identityValue = identities.get(i);
- URI validNamespace =
+ XMLNamespace validNamespace =
resolveValidNamespace(identityValue.getNamespace(), this.mountPoint, schemaContext);
final DataSchemaNode node = findInstanceDataChildByNameAndNamespace(
parentContainer, identityValue.getValue(), validNamespace);
return result.isEmpty() ? null : YangInstanceIdentifier.create(result);
}
- private static List<Predicate> keyValuesToPredicateList(final Map<QName, Object> keyValues) {
+ private static List<Predicate> keyValuesToPredicateList(final Set<Entry<QName, Object>> keyValues) {
final List<Predicate> result = new ArrayList<>();
- for (final Entry<QName, Object> entry : keyValues.entrySet()) {
+ for (final Entry<QName, Object> entry : keyValues) {
final QName qualifiedName = entry.getKey();
final Object value = entry.getValue();
result.add(new Predicate(qNameToIdentityValue(qualifiedName), String.valueOf(value)));
}
}
+ @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+ justification = "https://github.com/spotbugs/spotbugs/issues/811")
private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint,
final SchemaContext schemaContext) {
- final URI validNamespace = resolveValidNamespace(namespace, mountPoint, schemaContext);
+ final XMLNamespace validNamespace = resolveValidNamespace(namespace, mountPoint, schemaContext);
Module module = null;
if (mountPoint != null) {
- module = mountPoint.getSchemaContext().findModules(validNamespace).iterator().next();
+ module = modelContext(mountPoint).findModules(validNamespace).iterator().next();
} else {
module = schemaContext.findModules(validNamespace).iterator().next();
}
return module;
}
- private static URI resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint,
+ private static XMLNamespace resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint,
final SchemaContext schemaContext) {
- URI validNamespace;
+ XMLNamespace validNamespace;
if (mountPoint != null) {
- validNamespace = findFirstModuleByName(mountPoint.getSchemaContext(), namespace);
+ validNamespace = findFirstModuleByName(modelContext(mountPoint), namespace);
} else {
validNamespace = findFirstModuleByName(schemaContext, namespace);
}
if (validNamespace == null) {
- validNamespace = URI.create(namespace);
+ validNamespace = XMLNamespace.of(namespace);
}
return validNamespace;
}
- private static URI findFirstModuleByName(final SchemaContext schemaContext, final String name) {
+ private static XMLNamespace findFirstModuleByName(final SchemaContext schemaContext, final String name) {
for (final Module module : schemaContext.getModules()) {
if (module.getName().equals(name)) {
return module.getNamespace();
return null;
}
+ @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+ justification = "https://github.com/spotbugs/spotbugs/issues/811")
private static DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container,
- final String name, final URI namespace) {
- Preconditions.checkNotNull(namespace);
+ final String name, final XMLNamespace namespace) {
+ requireNonNull(namespace);
final Iterable<DataSchemaNode> result = Iterables.filter(findInstanceDataChildrenByName(container, name),
node -> namespace.equals(node.getQName().getNamespace()));
private static List<DataSchemaNode> findInstanceDataChildrenByName(final DataNodeContainer container,
final String name) {
- Preconditions.checkNotNull(container);
- Preconditions.checkNotNull(name);
-
final List<DataSchemaNode> instantiatedDataNodeContainers = new ArrayList<>();
- collectInstanceDataNodeContainers(instantiatedDataNodeContainers, container, name);
+ collectInstanceDataNodeContainers(instantiatedDataNodeContainers, requireNonNull(container),
+ requireNonNull(name));
return instantiatedDataNodeContainers;
}
private static void collectInstanceDataNodeContainers(final List<DataSchemaNode> potentialSchemaNodes,
final DataNodeContainer container, final String name) {
- final Iterable<DataSchemaNode> nodes =
+ final Iterable<? extends DataSchemaNode> nodes =
Iterables.filter(container.getChildNodes(), node -> name.equals(node.getQName().getLocalName()));
// Can't combine this loop with the filter above because the filter is
final Iterable<ChoiceSchemaNode> choiceNodes =
Iterables.filter(container.getChildNodes(), ChoiceSchemaNode.class);
- final Iterable<Collection<CaseSchemaNode>> map = Iterables.transform(choiceNodes,
- choice -> choice.getCases().values());
+ final Iterable<Collection<? extends CaseSchemaNode>> map = Iterables.transform(choiceNodes,
+ ChoiceSchemaNode::getCases);
for (final CaseSchemaNode caze : Iterables.concat(map)) {
collectInstanceDataNodeContainers(potentialSchemaNodes, caze, name);
}
private static boolean isInstantiatedDataSchema(final DataSchemaNode node) {
return node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode
|| node instanceof ContainerSchemaNode || node instanceof ListSchemaNode
- || node instanceof AnyXmlSchemaNode;
+ || node instanceof AnyxmlSchemaNode;
+ }
+
+ private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
+ return mountPoint.getService(DOMSchemaService.class)
+ .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
+ .orElse(null);
}
}