fa5f05fd94fe9b24f2f478dff8678ea8671a020a
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.model;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Objects;
26
27 import org.oran.smo.yangtools.parser.PrefixResolver;
28 import org.oran.smo.yangtools.parser.model.schema.ModuleAndNamespaceResolver;
29
30 /**
31  * A resolver that can handle prefixes, module names, and namespaces, and map
32  * back and forth between these. Namespaces are of importance when it comes to
33  * data; specifically, YANG data over NETCONF is XML encoded, requiring namespaces.
34  * <p/>
35  * However, this class should not be used for handling of data, as it does not
36  * support setting the default namespace.
37  *
38  * @author Mark Hollmann
39  */
40 public class ModulePrefixResolver extends PrefixResolver {
41
42     private final ModuleAndNamespaceResolver globalNamespaceResolver;
43
44     private final Map<String, ModuleIdentity> prefixToModulename = new HashMap<>();
45
46     public ModulePrefixResolver(final ModuleAndNamespaceResolver globalNamespaceResolver) {
47         this.globalNamespaceResolver = globalNamespaceResolver;
48     }
49
50     /*
51      * ================ Here is all the module stuff =====================
52      */
53
54     /**
55      * Records a mapping between a prefix and a module name. This mapping has typically
56      * been extracted from the header of a YAM.
57      */
58     public void addModuleMapping(final String prefix, final ModuleIdentity moduleIdentity) {
59         prefixToModulename.put(Objects.requireNonNull(prefix), Objects.requireNonNull(moduleIdentity));
60     }
61
62     /**
63      * Records the default mapping for this prefix resolver, i.e. the module that is mapped
64      * to "no prefix".
65      */
66     public void setDefaultModuleMapping(final ModuleIdentity moduleIdentity) {
67         prefixToModulename.put(PrefixResolver.NO_PREFIX, Objects.requireNonNull(moduleIdentity));
68     }
69
70     /**
71      * Returns the module representing the prefix. May return null if the prefix is unknown.
72      */
73     public ModuleIdentity getModuleForPrefix(final String prefix) {
74         if (prefix == null) {
75             return getDefaultModuleIdentity();
76         }
77         return prefixToModulename.get(prefix);
78     }
79
80     /**
81      * Returns the module representing the default prefix. May be null if unknown.
82      */
83     public ModuleIdentity getDefaultModuleIdentity() {
84         return prefixToModulename.get(PrefixResolver.NO_PREFIX);
85     }
86
87     /*
88      * ================ Here is all the namespace stuff =====================
89      */
90
91     @Override
92     public void setDefaultNamespaceUri(final String namespaceUri) {
93         throw new UnsupportedOperationException();
94     }
95
96     @Override
97     public void addMapping(final String prefix, final String namespaceUri) {
98         throw new UnsupportedOperationException();
99     }
100
101     /**
102      * Returns the default namespace URI for this module. May return null.
103      */
104     @Override
105     public String getDefaultNamespaceUri() {
106         final ModuleIdentity moduleIdentityForPrefix = getDefaultModuleIdentity();
107         return moduleIdentityForPrefix == null ?
108                 null :
109                 globalNamespaceResolver.getNamespaceForModule(moduleIdentityForPrefix.getModuleName());
110     }
111
112     /**
113      * Returns the namespace URI for the given prefix or null if no mapping exists for the prefix.
114      */
115     @Override
116     public String resolveNamespaceUri(final String prefix) {
117         final ModuleIdentity moduleIdentityForPrefix = getModuleForPrefix(prefix);
118         return moduleIdentityForPrefix == null ?
119                 null :
120                 globalNamespaceResolver.getNamespaceForModule(moduleIdentityForPrefix.getModuleName());
121     }
122
123     /**
124      * Returns the name of the module for the given namespace.
125      */
126     public String resolveModuleName(final String namespace) {
127         return globalNamespaceResolver.getModuleForNamespace(namespace);
128     }
129
130     @Override
131     public String toString() {
132         return "Mappings: " + prefixToModulename.toString();
133     }
134 }