2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.codecs;
10 import static java.util.Objects.requireNonNull;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
15 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 public final class StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
21 private final DataSchemaContextTree dataContextTree;
22 private final SchemaContext context;
23 private final String defaultPrefix;
25 public StringModuleInstanceIdentifierCodec(final SchemaContext context) {
26 this.context = requireNonNull(context);
27 this.dataContextTree = DataSchemaContextTree.from(context);
28 this.defaultPrefix = "";
31 public StringModuleInstanceIdentifierCodec(final SchemaContext context, final @NonNull String defaultPrefix) {
32 this.context = requireNonNull(context);
33 this.dataContextTree = DataSchemaContextTree.from(context);
34 this.defaultPrefix = defaultPrefix;
38 protected Module moduleForPrefix(final String prefix) {
39 final String moduleName = prefix.isEmpty() && !defaultPrefix.isEmpty() ? defaultPrefix : prefix;
40 return context.findModules(moduleName).stream().findFirst().orElse(null);
44 public DataSchemaContextTree getDataContextTree() {
45 return this.dataContextTree;
49 protected String prefixForNamespace(final URI namespace) {
50 return this.context.findModules(namespace).stream().findFirst().map(Module::getName).orElse(null);