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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.model.schema;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
27 import java.util.Objects;
30 import org.oran.smo.yangtools.parser.model.util.YangIdentity;
33 * Keeps track of all identities in a given schema. For each identity, its base(s)
34 * and derived identities are also stored.
36 * @author Mark Hollmann
38 public class IdentityRegistry {
41 * Map<identity, bases-of-the-identity>
43 private Map<YangIdentity, Set<YangIdentity>> identitiesAndBases = new HashMap<>();
46 * Map<identity, identities-derived-from-the-identity>
48 private Map<YangIdentity, Set<YangIdentity>> identitiesAndDerived = new HashMap<>();
51 identitiesAndBases.clear();
52 identitiesAndDerived.clear();
56 * Adds an identity to the registry.
58 public void addIdentity(final YangIdentity yangIdentity) {
59 identitiesAndBases.put(Objects.requireNonNull(yangIdentity), new HashSet<>());
60 identitiesAndDerived.put(Objects.requireNonNull(yangIdentity), new HashSet<>());
64 * Adds base/derived information to two existing identity (so puts these into a
65 * relation to each other).
67 public void addBaseIdentity(final YangIdentity derivedIdentity, final YangIdentity baseIdentity) {
69 final Set<YangIdentity> bases = identitiesAndBases.get(Objects.requireNonNull(derivedIdentity));
70 final Set<YangIdentity> derivates = identitiesAndDerived.get(Objects.requireNonNull(baseIdentity));
72 if (bases != null && derivates != null) {
73 bases.add(baseIdentity);
74 derivates.add(derivedIdentity);
78 public Set<YangIdentity> getIdentities() {
79 return Collections.unmodifiableSet(identitiesAndBases.keySet());
82 public Set<YangIdentity> getBasesForIdentity(final YangIdentity derivedIdentity) {
83 return identitiesAndBases.get(Objects.requireNonNull(derivedIdentity));
86 public Set<YangIdentity> getDerivatesOfIdentity(final YangIdentity baseIdentity) {
87 return identitiesAndDerived.get(Objects.requireNonNull(baseIdentity));
91 * Returns the identity and all identities deriving from it (at all levels, not
92 * just those directly deriving from the identity).
94 public Set<YangIdentity> getIdentityAndDerivedIdentitiesRecursively(final YangIdentity identity) {
96 if (!identitiesAndDerived.containsKey(Objects.requireNonNull(identity))) {
97 return Collections.<YangIdentity> emptySet();
100 final Set<YangIdentity> result = new HashSet<>();
101 addIdentityAndDerivedIdentities(identity, result);
105 private void addIdentityAndDerivedIdentities(final YangIdentity identity, final Set<YangIdentity> result) {
107 if (result.contains(identity)) { // prevents infinite loops
111 result.add(identity);
112 identitiesAndDerived.get(identity).forEach(derived -> addIdentityAndDerivedIdentities(derived, result));