a265ea95a212b40c6dc4bcd5698fb2de57e5c372
[netconf.git] / restconf / sal-rest-connector / 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  * @deprecated This class will be replaced by
22  * {@link org.opendaylight.restconf.utils.patch.Draft11StringModuleInstanceIdentifierCodec}
23  */
24 @Deprecated
25 final class StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
26
27     private final DataSchemaContextTree dataContextTree;
28     private final SchemaContext context;
29     private final String defaultPrefix;
30
31     StringModuleInstanceIdentifierCodec(SchemaContext context) {
32         this.context = Preconditions.checkNotNull(context);
33         this.dataContextTree = DataSchemaContextTree.from(context);
34         this.defaultPrefix = "";
35     }
36
37     StringModuleInstanceIdentifierCodec(SchemaContext context, @Nonnull String defaultPrefix) {
38         this.context = Preconditions.checkNotNull(context);
39         this.dataContextTree = DataSchemaContextTree.from(context);
40         this.defaultPrefix = defaultPrefix;
41     }
42
43     @Override
44     protected Module moduleForPrefix(@Nonnull String prefix) {
45         if (prefix.isEmpty() && !this.defaultPrefix.isEmpty()) {
46             return this.context.findModuleByName(this.defaultPrefix, null);
47         } else {
48             return this.context.findModuleByName(prefix, null);
49         }
50     }
51
52     @Nonnull
53     @Override
54     protected DataSchemaContextTree getDataContextTree() {
55         return this.dataContextTree;
56     }
57
58     @Nullable
59     @Override
60     protected String prefixForNamespace(@Nonnull URI namespace) {
61         final Module module = this.context.findModuleByNamespaceAndRevision(namespace, null);
62         return module == null ? null : module.getName();
63     }
64 }