b61ed5816875c6ea70e83541be75fd6e61cd6a84
[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 package org.opendaylight.netconf.sal.rest.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.EffectiveModelContext;
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 StringModuleInstanceIdentifierCodec from restconf-nb-rfc8040
24  */
25 @Deprecated
26 public final class StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
27
28     private final DataSchemaContextTree dataContextTree;
29     private final SchemaContext context;
30     private final String defaultPrefix;
31
32     public StringModuleInstanceIdentifierCodec(final EffectiveModelContext context) {
33         this.context = requireNonNull(context);
34         this.dataContextTree = DataSchemaContextTree.from(context);
35         this.defaultPrefix = "";
36     }
37
38     StringModuleInstanceIdentifierCodec(final EffectiveModelContext context, final @NonNull String defaultPrefix) {
39         this.context = requireNonNull(context);
40         this.dataContextTree = DataSchemaContextTree.from(context);
41         this.defaultPrefix = defaultPrefix;
42     }
43
44     @Override
45     protected Module moduleForPrefix(final String prefix) {
46         if (prefix.isEmpty() && !this.defaultPrefix.isEmpty()) {
47             return this.context.findModules(this.defaultPrefix).stream().findFirst().orElse(null);
48         } else {
49             return this.context.findModules(prefix).stream().findFirst().orElse(null);
50         }
51     }
52
53     @Override
54     protected DataSchemaContextTree getDataContextTree() {
55         return this.dataContextTree;
56     }
57
58     @Override
59     protected String prefixForNamespace(final XMLNamespace namespace) {
60         return this.context.findModules(namespace).stream().findFirst().map(Module::getName).orElse(null);
61     }
62 }