*/
package org.opendaylight.netconf.sal.restconf.impl;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
-import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
private static final String MOUNT_NODE = "mount";
- private static final Charset URI_ENCODING_CHARSET = StandardCharsets.UTF_8;
-
private static final Splitter SLASH_SPLITTER = Splitter.on('/');
private final AtomicReference<Map<QName, RpcDefinition>> qnameToRpc = new AtomicReference<>(Collections.emptyMap());
public Module findModuleByName(final String moduleName) {
checkPreconditions();
- Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
+ checkArgument(moduleName != null && !moduleName.isEmpty());
return this.globalSchema.findModules(moduleName).stream().findFirst().orElse(null);
}
public Module findModuleByName(final DOMMountPoint mountPoint, final String moduleName) {
- Preconditions.checkArgument(moduleName != null && mountPoint != null);
+ checkArgument(moduleName != null && mountPoint != null);
- final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
+ final SchemaContext mountPointSchema = getModelContext(mountPoint);
if (mountPointSchema == null) {
return null;
}
public Module findModuleByNamespace(final URI namespace) {
checkPreconditions();
- Preconditions.checkArgument(namespace != null);
+ checkArgument(namespace != null);
return this.globalSchema.findModules(namespace).stream().findFirst().orElse(null);
}
public Module findModuleByNamespace(final DOMMountPoint mountPoint, final URI namespace) {
- Preconditions.checkArgument(namespace != null && mountPoint != null);
+ checkArgument(namespace != null && mountPoint != null);
- final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
+ final SchemaContext mountPointSchema = getModelContext(mountPoint);
if (mountPointSchema == null) {
return null;
}
public Module findModuleByNameAndRevision(final String name, final Revision revision) {
checkPreconditions();
- Preconditions.checkArgument(name != null && revision != null);
+ checkArgument(name != null && revision != null);
return this.globalSchema.findModule(name, revision).orElse(null);
}
public Module findModuleByNameAndRevision(final DOMMountPoint mountPoint, final String name,
final Revision revision) {
checkPreconditions();
- Preconditions.checkArgument(name != null && revision != null && mountPoint != null);
+ checkArgument(name != null && revision != null && mountPoint != null);
- final SchemaContext schemaContext = mountPoint.getSchemaContext();
+ final SchemaContext schemaContext = getModelContext(mountPoint);
return schemaContext == null ? null : schemaContext.findModule(name, revision).orElse(null);
}
final QName startQName = head.getNodeType();
final SchemaContext schemaContext;
if (mount != null) {
- schemaContext = mount.getSchemaContext();
+ schemaContext = getModelContext(mount);
} else {
schemaContext = this.globalSchema;
}
if (!(element instanceof AugmentationIdentifier)) {
final QName _nodeType = element.getNodeType();
final DataSchemaNode potentialNode = childByQName(node, _nodeType);
- if (!(element instanceof NodeIdentifier && potentialNode instanceof ListSchemaNode)
+ if ((!(element instanceof NodeIdentifier) || !(potentialNode instanceof ListSchemaNode))
&& !(potentialNode instanceof ChoiceSchemaNode)) {
builder.append(convertToRestconfIdentifier(element, potentialNode, mount));
if (potentialNode instanceof DataNodeContainer) {
public Collection<? extends Module> getAllModules(final DOMMountPoint mountPoint) {
checkPreconditions();
- final SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
+ final SchemaContext schemaContext = mountPoint == null ? null : getModelContext(mountPoint);
return schemaContext == null ? null : schemaContext.getModules();
}
public CharSequence toRestconfIdentifier(final QName qname, final DOMMountPoint mount) {
final SchemaContext schema;
if (mount != null) {
- schema = mount.getSchemaContext();
+ schema = getModelContext(mount);
} else {
checkPreconditions();
schema = this.globalSchema;
return null;
}
- return toRestconfIdentifier(mountPoint.getSchemaContext(), qname);
+ return toRestconfIdentifier(getModelContext(mountPoint), qname);
}
public Module getRestconfModule() {
private String toUriString(final Object object, final LeafSchemaNode leafNode, final DOMMountPoint mount)
throws UnsupportedEncodingException {
final IllegalArgumentCodec<Object, Object> codec = RestCodec.from(leafNode.getType(), mount, this);
- // FIXME: UrlEncoder looks up a well-known charset, we need something that will use it directly
- return object == null ? "" : URLEncoder.encode(codec.serialize(object).toString(), URI_ENCODING_CHARSET.name());
+ return object == null ? "" : URLEncoder.encode(codec.serialize(object).toString(), StandardCharsets.UTF_8);
}
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification = "Unrecognised NullableDecl")
private InstanceIdentifierContext<?> collectPathArguments(final InstanceIdentifierBuilder builder,
final List<String> strings, final DataNodeContainer parentNode, final DOMMountPoint mountPoint,
final boolean returnJustMountPoint) {
- Preconditions.checkNotNull(strings);
+ requireNonNull(strings);
if (parentNode == null) {
return null;
}
if (strings.isEmpty()) {
- return createContext(builder.build(), (DataSchemaNode) parentNode,
- mountPoint,mountPoint != null ? mountPoint.getEffectiveModelContext() : this.globalSchema);
+ return createContext(builder.build(), (DataSchemaNode) parentNode, mountPoint,
+ mountPoint != null ? getModelContext(mountPoint) : this.globalSchema);
}
final String head = strings.iterator().next();
final YangInstanceIdentifier partialPath = this.dataNormalizer.toNormalized(builder.build());
final Optional<DOMMountPoint> mountOpt = this.mountService.getMountPoint(partialPath);
- if (!mountOpt.isPresent()) {
+ if (mountOpt.isEmpty()) {
LOG.debug("Instance identifier to missing mount point: {}", partialPath);
throw new RestconfDocumentedException("Mount point does not exist.", ErrorType.PROTOCOL,
ErrorTag.DATA_MISSING);
}
final DOMMountPoint mount = mountOpt.get();
- final EffectiveModelContext mountPointSchema = mount.getEffectiveModelContext();
+ final EffectiveModelContext mountPointSchema = getModelContext(mount);
if (mountPointSchema == null) {
throw new RestconfDocumentedException("Mount point does not contain any schema with modules.",
ErrorType.APPLICATION, ErrorTag.UNKNOWN_ELEMENT);
ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
}
} else {
- final EffectiveModelContext schemaContext = mountPoint.getEffectiveModelContext();
+ final EffectiveModelContext schemaContext = getModelContext(mountPoint);
if (schemaContext != null) {
module = schemaContext.findModules(moduleName).stream().findFirst().orElse(null);
} else {
}
if (rpc != null) {
return new InstanceIdentifierContext<>(builder.build(), rpc, mountPoint,
- mountPoint != null ? mountPoint.getEffectiveModelContext() : this.globalSchema);
+ mountPoint != null ? getModelContext(mountPoint) : this.globalSchema);
}
}
}
return createContext(builder.build(), targetNode, mountPoint,
- mountPoint != null ? mountPoint.getEffectiveModelContext() : this.globalSchema);
+ mountPoint != null ? getModelContext(mountPoint) : this.globalSchema);
}
private static InstanceIdentifierContext<?> createContext(final YangInstanceIdentifier instance,
public static DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container,
final String name, final URI namespace) {
- Preconditions.checkNotNull(namespace);
+ requireNonNull(namespace);
final Iterable<DataSchemaNode> result = Iterables.filter(findInstanceDataChildrenByName(container, name),
node -> namespace.equals(node.getQName().getNamespace()));
public 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 void addKeyValue(final HashMap<QName, Object> map, final DataSchemaNode node, final String uriValue,
final DOMMountPoint mountPoint) {
- Preconditions.checkNotNull(uriValue);
- Preconditions.checkArgument(node instanceof LeafSchemaNode);
+ checkArgument(node instanceof LeafSchemaNode);
- final SchemaContext schemaContext = mountPoint == null ? this.globalSchema : mountPoint.getSchemaContext();
- final String urlDecoded = urlPathArgDecode(uriValue);
+ final SchemaContext schemaContext = mountPoint == null ? this.globalSchema : getModelContext(mountPoint);
+ final String urlDecoded = urlPathArgDecode(requireNonNull(uriValue));
TypeDefinition<?> typedef = ((LeafSchemaNode) node).getType();
final TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(typedef);
if (baseType instanceof LeafrefTypeDefinition) {
}
private static List<String> urlPathArgsDecode(final Iterable<String> strings) {
- try {
- final List<String> decodedPathArgs = new ArrayList<>();
- for (final String pathArg : strings) {
- final String _decode = URLDecoder.decode(pathArg, URI_ENCODING_CHARSET.name());
- decodedPathArgs.add(_decode);
- }
- return decodedPathArgs;
- } catch (final UnsupportedEncodingException e) {
- throw new RestconfDocumentedException("Invalid URL path '" + strings + "': " + e.getMessage(),
- ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
+ final List<String> decodedPathArgs = new ArrayList<>();
+ for (final String pathArg : strings) {
+ final String _decode = URLDecoder.decode(pathArg, StandardCharsets.UTF_8);
+ decodedPathArgs.add(_decode);
}
+ return decodedPathArgs;
}
static String urlPathArgDecode(final String pathArg) {
if (pathArg == null) {
return null;
}
- try {
- return URLDecoder.decode(pathArg, URI_ENCODING_CHARSET.name());
- } catch (final UnsupportedEncodingException e) {
- throw new RestconfDocumentedException("Invalid URL path arg '" + pathArg + "': " + e.getMessage(),
- ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
- }
+ return URLDecoder.decode(pathArg, StandardCharsets.UTF_8);
}
private CharSequence convertToRestconfIdentifier(final PathArgument argument, final DataSchemaNode node,
builder.append('/');
}
- Preconditions.checkState(listChild instanceof LeafSchemaNode,
+ checkState(listChild instanceof LeafSchemaNode,
"List key has to consist of leaves, not %s", listChild);
final Object value = argument.getValue(key);
}
return operation.isMixin();
}
+
+ private static EffectiveModelContext getModelContext(final DOMMountPoint mountPoint) {
+ return mountPoint.getService(DOMSchemaService.class)
+ .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
+ .orElse(null);
+ }
}