38f88bcbd68687aa88861c975fa7c2502cccc4a2
[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.yanglibrary;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26
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;
30
31 /**
32  * Represents an instance of a Yang Library as defined in RFC 8525 (https://datatracker.ietf.org/doc/html/rfc8525).
33  * <p/>
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.
36  *
37  * @author Mark Hollmann
38  */
39 public class YangLibrary {
40
41     private String contentId = "";
42
43     private final List<Datastore> datastores = new ArrayList<>();
44
45     /**
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.
48      */
49     private final YangDataDomNode mountPoint;
50
51     public YangLibrary(final YangDataDomNode mountPoint) {
52         this.mountPoint = mountPoint;
53     }
54
55     public String getContentId() {
56         return contentId;
57     }
58
59     public void setContentId(final String contentId) {
60         this.contentId = contentId;
61     }
62
63     public List<Datastore> getDatastores() {
64         return Collections.unmodifiableList(datastores);
65     }
66
67     public void addDatastore(final Datastore datastore) {
68         datastores.add(datastore);
69     }
70
71     /**
72      * Returns the point under which the library is mounted. May return null,
73      * indicating the top-level schema.
74      */
75     public YangDataDomNode getMountPoint() {
76         return mountPoint;
77     }
78
79     /**
80      * Returns whether this YANG Library describes the top-level schema;
81      * that is, the schema at the root of the schema tree.
82      */
83     public boolean containsTopLevelSchema() {
84         return mountPoint == null || mountPoint instanceof YangDataDomDocumentRoot;
85     }
86
87     /**
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
90      * (but not the root).
91      */
92     public boolean containsMountedSchema() {
93         return !containsTopLevelSchema();
94     }
95
96     /**
97      * Returns the "running" datastore. Returns null if no running datastore was
98      * found (this should not happen - servers must support the running datastore).
99      */
100     public Datastore getRunningDatastore() {
101         return getDatastore(Datastore.RUNNING_DATASTORE_IDENTITY);
102     }
103
104     /**
105      * Returns the specified datastore. Returns null if the datastore was not found.
106      */
107     public Datastore getDatastore(final IdentityRefValue identityRef) {
108         return datastores.stream().filter(ds -> identityRef.equals(ds.getDatastoreName())).findFirst().orElse(null);
109     }
110
111     /**
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.
114      */
115     public static YangLibrary getTopLevelSchema(final List<YangLibrary> yangLibraries) {
116         return yangLibraries.stream().filter(YangLibrary::containsTopLevelSchema).findAny().orElse(null);
117     }
118 }