5ce0ded8c331f2408d0ff08de9d241af854895e7
[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.schema;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Set;
29
30 import org.oran.smo.yangtools.parser.model.util.YangIdentity;
31
32 /**
33  * Keeps track of all identities in a given schema. For each identity, its base(s)
34  * and derived identities are also stored.
35  *
36  * @author Mark Hollmann
37  */
38 public class IdentityRegistry {
39
40     /**
41      * Map<identity, bases-of-the-identity>
42      */
43     private Map<YangIdentity, Set<YangIdentity>> identitiesAndBases = new HashMap<>();
44
45     /**
46      * Map<identity, identities-derived-from-the-identity>
47      */
48     private Map<YangIdentity, Set<YangIdentity>> identitiesAndDerived = new HashMap<>();
49
50     public void clear() {
51         identitiesAndBases.clear();
52         identitiesAndDerived.clear();
53     }
54
55     /**
56      * Adds an identity to the registry.
57      */
58     public void addIdentity(final YangIdentity yangIdentity) {
59         identitiesAndBases.put(Objects.requireNonNull(yangIdentity), new HashSet<>());
60         identitiesAndDerived.put(Objects.requireNonNull(yangIdentity), new HashSet<>());
61     }
62
63     /**
64      * Adds base/derived information to two existing identity (so puts these into a
65      * relation to each other).
66      */
67     public void addBaseIdentity(final YangIdentity derivedIdentity, final YangIdentity baseIdentity) {
68
69         final Set<YangIdentity> bases = identitiesAndBases.get(Objects.requireNonNull(derivedIdentity));
70         final Set<YangIdentity> derivates = identitiesAndDerived.get(Objects.requireNonNull(baseIdentity));
71
72         if (bases != null && derivates != null) {
73             bases.add(baseIdentity);
74             derivates.add(derivedIdentity);
75         }
76     }
77
78     public Set<YangIdentity> getIdentities() {
79         return Collections.unmodifiableSet(identitiesAndBases.keySet());
80     }
81
82     public Set<YangIdentity> getBasesForIdentity(final YangIdentity derivedIdentity) {
83         return identitiesAndBases.get(Objects.requireNonNull(derivedIdentity));
84     }
85
86     public Set<YangIdentity> getDerivatesOfIdentity(final YangIdentity baseIdentity) {
87         return identitiesAndDerived.get(Objects.requireNonNull(baseIdentity));
88     }
89
90     /**
91      * Returns the identity and all identities deriving from it (at all levels, not
92      * just those directly deriving from the identity).
93      */
94     public Set<YangIdentity> getIdentityAndDerivedIdentitiesRecursively(final YangIdentity identity) {
95
96         if (!identitiesAndDerived.containsKey(Objects.requireNonNull(identity))) {
97             return Collections.<YangIdentity> emptySet();
98         }
99
100         final Set<YangIdentity> result = new HashSet<>();
101         addIdentityAndDerivedIdentities(identity, result);
102         return result;
103     }
104
105     private void addIdentityAndDerivedIdentities(final YangIdentity identity, final Set<YangIdentity> result) {
106
107         if (result.contains(identity)) {                // prevents infinite loops
108             return;
109         }
110
111         result.add(identity);
112         identitiesAndDerived.get(identity).forEach(derived -> addIdentityAndDerivedIdentities(derived, result));
113     }
114 }