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