Split Restconf implementations (draft02 and RFC) - Prepare modules
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / StringModuleInstanceIdentifierCodec.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.sal.rest.impl;
10
11 import com.google.common.base.Preconditions;
12 import java.net.URI;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
16 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19
20 /**
21  * Codec for module instance identifiers.
22  *
23  * @deprecated This class will be replaced by
24  *             {@link org.opendaylight.restconf.jersey.providers.StringModuleInstanceIdentifierCodec}
25  */
26 @Deprecated
27 public final class StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
28
29     private final DataSchemaContextTree dataContextTree;
30     private final SchemaContext context;
31     private final String defaultPrefix;
32
33     public StringModuleInstanceIdentifierCodec(final SchemaContext context) {
34         this.context = Preconditions.checkNotNull(context);
35         this.dataContextTree = DataSchemaContextTree.from(context);
36         this.defaultPrefix = "";
37     }
38
39     StringModuleInstanceIdentifierCodec(final SchemaContext context, @Nonnull final String defaultPrefix) {
40         this.context = Preconditions.checkNotNull(context);
41         this.dataContextTree = DataSchemaContextTree.from(context);
42         this.defaultPrefix = defaultPrefix;
43     }
44
45     @Override
46     protected Module moduleForPrefix(@Nonnull final String prefix) {
47         if (prefix.isEmpty() && !this.defaultPrefix.isEmpty()) {
48             return this.context.findModuleByName(this.defaultPrefix, null);
49         } else {
50             return this.context.findModuleByName(prefix, null);
51         }
52     }
53
54     @Nonnull
55     @Override
56     protected DataSchemaContextTree getDataContextTree() {
57         return this.dataContextTree;
58     }
59
60     @Nullable
61     @Override
62     protected String prefixForNamespace(@Nonnull final URI namespace) {
63         final Module module = this.context.findModuleByNamespaceAndRevision(namespace, null);
64         return module == null ? null : module.getName();
65     }
66 }