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.yanglibrary;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
27 import org.oran.smo.yangtools.parser.data.dom.YangDataDomDocumentRoot;
28 import org.oran.smo.yangtools.parser.data.dom.YangDataDomNode;
29 import org.oran.smo.yangtools.parser.data.util.IdentityRefValue;
32 * Represents an instance of a Yang Library as defined in RFC 8525 (https://datatracker.ietf.org/doc/html/rfc8525).
34 * Note that most servers are not using NMDA and will typically make use of the (deprecated) "modules-state"
35 * container. Also see {@link ModulesState} class.
37 * @author Mark Hollmann
39 public class YangLibrary {
41 private String contentId = "";
43 private final List<Datastore> datastores = new ArrayList<>();
46 * Denotes the point in the schema tree in which this YANG Library is mounted.
47 * This value may be null, denoting the top-level Yang Library.
49 private final YangDataDomNode mountPoint;
51 public YangLibrary(final YangDataDomNode mountPoint) {
52 this.mountPoint = mountPoint;
55 public String getContentId() {
59 public void setContentId(final String contentId) {
60 this.contentId = contentId;
63 public List<Datastore> getDatastores() {
64 return Collections.unmodifiableList(datastores);
67 public void addDatastore(final Datastore datastore) {
68 datastores.add(datastore);
72 * Returns the point under which the library is mounted. May return null,
73 * indicating the top-level schema.
75 public YangDataDomNode getMountPoint() {
80 * Returns whether this YANG Library describes the top-level schema;
81 * that is, the schema at the root of the schema tree.
83 public boolean containsTopLevelSchema() {
84 return mountPoint == null || mountPoint instanceof YangDataDomDocumentRoot;
88 * Returns whether this YANG Library describes a mounted schema; that
89 * is, the schema has been mounted at some point in the schema tree
92 public boolean containsMountedSchema() {
93 return !containsTopLevelSchema();
97 * Returns the "running" datastore. Returns null if no running datastore was
98 * found (this should not happen - servers must support the running datastore).
100 public Datastore getRunningDatastore() {
101 return getDatastore(Datastore.RUNNING_DATASTORE_IDENTITY);
105 * Returns the specified datastore. Returns null if the datastore was not found.
107 public Datastore getDatastore(final IdentityRefValue identityRef) {
108 return datastores.stream().filter(ds -> identityRef.equals(ds.getDatastoreName())).findFirst().orElse(null);
112 * Returns the top-level schema from among all the YANG Library instances supplied.
113 * Returns null if the top-level schema could not be found.
115 public static YangLibrary getTopLevelSchema(final List<YangLibrary> yangLibraries) {
116 return yangLibraries.stream().filter(YangLibrary::containsTopLevelSchema).findAny().orElse(null);