*/
package org.opendaylight.netconf.sal.restconf.impl;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
+import java.util.Map.Entry;
+import java.util.Set;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
import org.opendaylight.netconf.sal.rest.impl.StringModuleInstanceIdentifierCodec;
import org.opendaylight.restconf.common.util.IdentityValuesDTO;
import org.opendaylight.restconf.common.util.IdentityValuesDTO.IdentityValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class RestCodec {
+public final class RestCodec {
private static final Logger LOG = LoggerFactory.getLogger(RestCodec.class);
private RestCodec() {
}
- public static final Codec<Object, Object> from(final TypeDefinition<?> typeDefinition,
- final DOMMountPoint mountPoint) {
- return new ObjectCodec(typeDefinition, mountPoint);
+ public static Codec<Object, Object> from(final TypeDefinition<?> typeDefinition,
+ final DOMMountPoint mountPoint, final ControllerContext controllerContext) {
+ return new ObjectCodec(typeDefinition, mountPoint, controllerContext);
}
@SuppressWarnings("rawtypes")
private static final Logger LOG = LoggerFactory.getLogger(ObjectCodec.class);
public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
+
+ private final ControllerContext controllerContext;
private final Codec instanceIdentifier;
private final Codec identityrefCodec;
private final TypeDefinition<?> type;
- private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint) {
+ private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
+ final ControllerContext controllerContext) {
+ this.controllerContext = controllerContext;
this.type = RestUtil.resolveBaseTypeFrom(typeDefinition);
if (this.type instanceof IdentityrefTypeDefinition) {
- this.identityrefCodec = new IdentityrefCodecImpl(mountPoint);
+ this.identityrefCodec = new IdentityrefCodecImpl(mountPoint, controllerContext);
} else {
this.identityrefCodec = null;
}
if (this.type instanceof InstanceIdentifierTypeDefinition) {
- this.instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint);
+ this.instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint, controllerContext);
} else {
this.instanceIdentifier = null;
}
return this.instanceIdentifier.deserialize(input);
} else {
final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
- ControllerContext.getInstance().getGlobalSchema());
+ controllerContext.getGlobalSchema());
return codec.deserialize((String) input);
}
} else {
}
return typeAwarecodec.deserialize(String.valueOf(input));
} else {
- LOG.debug("Codec for type \"" + this.type.getQName().getLocalName()
- + "\" is not implemented yet.");
+ LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
return null;
}
}
} catch (final ClassCastException e) { // TODO remove this catch when everyone use codecs
- LOG.error(
- "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
- e);
+ LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
return null;
}
}
if (typeAwarecodec != null) {
return typeAwarecodec.serialize(input);
} else {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Codec for type \"" + this.type.getQName().getLocalName()
- + "\" is not implemented yet.");
- }
+ LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
return null;
}
}
} catch (final ClassCastException e) { // TODO remove this catch when everyone use codecs
- LOG.error(
- "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
- e);
+ LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
return input;
}
}
private static final Logger LOG = LoggerFactory.getLogger(IdentityrefCodecImpl.class);
private final DOMMountPoint mountPoint;
+ private final ControllerContext controllerContext;
- public IdentityrefCodecImpl(final DOMMountPoint mountPoint) {
+ public IdentityrefCodecImpl(final DOMMountPoint mountPoint, final ControllerContext controllerContext) {
this.mountPoint = mountPoint;
+ this.controllerContext = controllerContext;
}
@Override
@Override
public QName deserialize(final IdentityValuesDTO data) {
final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
- final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint);
+ final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint,
+ controllerContext);
if (module == null) {
LOG.info("Module was not found for namespace {}", valueWithNamespace.getNamespace());
LOG.info("Idenetityref will be translated as NULL for data - {}", String.valueOf(valueWithNamespace));
public static class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec<IdentityValuesDTO> {
private static final Logger LOG = LoggerFactory.getLogger(InstanceIdentifierCodecImpl.class);
+
private final DOMMountPoint mountPoint;
+ private final ControllerContext controllerContext;
- public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint) {
+ public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint,
+ final ControllerContext controllerContext) {
this.mountPoint = mountPoint;
+ this.controllerContext = controllerContext;
}
@Override
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<>();
- final String value = String.valueOf(((NodeWithValue) pathArgument).getValue());
+ final String value = String.valueOf(((NodeWithValue<?>) pathArgument).getValue());
predicates.add(new Predicate(null, value));
identityValue.setPredicates(predicates);
}
return identityValuesDTO;
}
+ @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
+ justification = "Unrecognised NullableDecl")
@Override
public YangInstanceIdentifier deserialize(final IdentityValuesDTO data) {
final List<PathArgument> result = new ArrayList<>();
final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
- final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint);
+ final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint,
+ controllerContext);
if (module == null) {
LOG.info("Module by namespace '{}' of first node in instance-identifier was not found.",
valueWithNamespace.getNamespace());
final List<IdentityValue> identities = data.getValuesWithNamespaces();
for (int i = 0; i < identities.size(); i++) {
final IdentityValue identityValue = identities.get(i);
- URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), this.mountPoint);
+ URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), this.mountPoint,
+ controllerContext);
final DataSchemaNode node = ControllerContext.findInstanceDataChildByNameAndNamespace(
parentContainer, identityValue.getValue(), validNamespace);
if (node == null) {
final DataNodeContainer listNode = (DataNodeContainer) node;
final Map<QName, Object> predicatesMap = new HashMap<>();
for (final Predicate predicate : identityValue.getPredicates()) {
- validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), this.mountPoint);
+ validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), this.mountPoint,
+ controllerContext);
final DataSchemaNode listKey = ControllerContext
.findInstanceDataChildByNameAndNamespace(listNode, predicate.getName().getValue(),
validNamespace);
predicatesMap.put(listKey.getQName(), predicate.getValue());
}
- pathArgument = new NodeIdentifierWithPredicates(qName, predicatesMap);
+ pathArgument = NodeIdentifierWithPredicates.of(qName, predicatesMap);
} else {
LOG.info("Node {} is not List or Leaf-list.", node);
LOG.info("Instance-identifier will be translated as NULL for data - {}",
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 QName qualifiedName : keyValues.keySet()) {
- final Object value = keyValues.get(qualifiedName);
+ 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)));
}
return result;
}
}
- private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint) {
- final URI validNamespace = resolveValidNamespace(namespace, mountPoint);
+ @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 ControllerContext controllerContext) {
+ final URI validNamespace = resolveValidNamespace(namespace, mountPoint, controllerContext);
Module module = null;
if (mountPoint != null) {
- module = ControllerContext.getInstance().findModuleByNamespace(mountPoint, validNamespace);
+ module = controllerContext.findModuleByNamespace(mountPoint, validNamespace);
} else {
- module = ControllerContext.getInstance().findModuleByNamespace(validNamespace);
+ module = controllerContext.findModuleByNamespace(validNamespace);
}
if (module == null) {
- LOG.info("Module for namespace " + validNamespace + " wasn't found.");
+ LOG.info("Module for namespace {} was not found.", validNamespace);
return null;
}
return module;
}
- private static URI resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint) {
+ private static URI resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint,
+ final ControllerContext controllerContext) {
URI validNamespace;
if (mountPoint != null) {
- validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(mountPoint, namespace);
+ validNamespace = controllerContext.findNamespaceByModuleName(mountPoint, namespace);
} else {
- validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(namespace);
+ validNamespace = controllerContext.findNamespaceByModuleName(namespace);
}
if (validNamespace == null) {
validNamespace = URI.create(namespace);