13a3dee04253ca5c9d235277e249fefbc15e405c
[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.data.util;
22
23 import java.util.Objects;
24
25 import org.oran.smo.yangtools.parser.PrefixResolver;
26 import org.oran.smo.yangtools.parser.util.NamespaceModuleIdentifier;
27 import org.oran.smo.yangtools.parser.util.QNameHelper;
28
29 /**
30  * Holds the value of a data node instance of type "identityref"
31  *
32  * @author Mark Hollmann
33  */
34 public class IdentityRefValue {
35
36     private final NamespaceModuleIdentifier value;
37
38     public IdentityRefValue(final String namespace, final String moduleName, final String identityName) {
39         this.value = new NamespaceModuleIdentifier(namespace, moduleName, Objects.requireNonNull(identityName));
40     }
41
42     /**
43      * Constructor for data encoded in XML, where prefixes are used and a prefix resolver is available for the namespace
44      * resolution.
45      */
46     public IdentityRefValue(final String val, final PrefixResolver prefixResolver, final String defaultNamespace) {
47
48         final boolean hasPrefix = QNameHelper.hasPrefix(val);
49         final String namespace = hasPrefix ?
50                 prefixResolver.resolveNamespaceUri(QNameHelper.extractPrefix(val)) :
51                 defaultNamespace;
52         final String name = hasPrefix ? QNameHelper.extractName(val) : val;
53
54         value = new NamespaceModuleIdentifier(namespace, null, name);
55     }
56
57     /**
58      * Constructor for data encoded in JSON, where module names are used.
59      */
60     public IdentityRefValue(final String val, final String defaultModuleName) {
61
62         final boolean hasPrefix = QNameHelper.hasPrefix(val);
63         final String moduleName = hasPrefix ? QNameHelper.extractPrefix(val) : defaultModuleName;
64         final String name = hasPrefix ? QNameHelper.extractName(val) : val;
65
66         value = new NamespaceModuleIdentifier(null, moduleName, name);
67     }
68
69     /**
70      * The name of the identity
71      */
72     public String getIdentityName() {
73         return value.getIdentifier();
74     }
75
76     /**
77      * The name of the module in which the identity has been declared. May return null if the value was encoded in XML.
78      */
79     public String getIdentityModuleName() {
80         return value.getModuleName();
81     }
82
83     /**
84      * The namespace of the module in which the identity has been declared. May return null if the value was encoded in
85      * JSON.
86      */
87     public String getIdentityNamespace() {
88         return value.getNamespace();
89     }
90
91     @Override
92     public String toString() {
93         return "IdentityRef value " + getIdentityNamespace() + "/" + getIdentityModuleName() + "/" + getIdentityName();
94     }
95
96     @Override
97     public boolean equals(Object obj) {
98
99         if (obj == null || !obj.getClass().getName().equals(IdentityRefValue.class.getName())) {
100             return false;
101         }
102
103         final IdentityRefValue other = (IdentityRefValue) obj;
104
105         if (!this.getIdentityName().equals(other.getIdentityName())) {
106             return false;
107         }
108
109         if (this.getIdentityModuleName() != null && other.getIdentityModuleName() != null && this
110                 .getIdentityNamespace() != null && other.getIdentityNamespace() != null) {
111             return this.getIdentityModuleName().equals(other.getIdentityModuleName()) && this.getIdentityNamespace().equals(
112                     other.getIdentityNamespace());
113         }
114
115         /*
116          * The comparison is a little different to how this would usually be done. Depending on the encoding
117          * of the input, either the namespace or the module name may be null. However, a client having constructed
118          * such an object, will typically have knowledge of both (as they will know the model). So we will try both.
119          */
120         if (this.getIdentityModuleName() != null && other.getIdentityModuleName() != null) {
121             return this.getIdentityModuleName().equals(other.getIdentityModuleName());
122         }
123
124         if (this.getIdentityNamespace() != null && other.getIdentityNamespace() != null) {
125             return this.getIdentityNamespace().equals(other.getIdentityNamespace());
126         }
127
128         return false;
129     }
130 }