d50d0ef4e5476fff37a26abd06693f859d15ef21
[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.instance;
22
23 import java.util.Objects;
24
25 import org.oran.smo.yangtools.parser.data.dom.YangDataDomNode;
26 import org.oran.smo.yangtools.parser.model.statements.AbstractStatement;
27
28 /**
29  * Represents a piece of data in the data tree (e.g. a container, a leaf)
30  *
31  * @author Mark Hollmann
32  */
33 public abstract class AbstractDataInstance {
34
35     /**
36      * The node in the schema that this data relates to. Will be null for the root instance.
37      */
38     private final AbstractStatement schemaNode;
39
40     /**
41      * The data DOM node that backs this data instance. Will be null for the root instance or
42      * default values.
43      */
44     private final YangDataDomNode dataDomNode;
45
46     /**
47      * The parent structure instance (i.e., the parent container or list). Will be null for
48      * the root instance.
49      */
50     private AbstractStructureInstance parent;
51
52     /**
53      * Constructor for an instance that has been specified in data.
54      */
55     public AbstractDataInstance(final AbstractStatement schemaNode, final YangDataDomNode dataDomNode,
56             final AbstractStructureInstance parent) {
57         this.schemaNode = schemaNode;
58         this.dataDomNode = Objects.requireNonNull(dataDomNode);
59         this.parent = parent;
60     }
61
62     /**
63      * Constructor for an instance that has been specified as default value.
64      */
65     public AbstractDataInstance(final AbstractStatement schemaNode, final AbstractStructureInstance parent) {
66         this.schemaNode = schemaNode;
67         this.dataDomNode = null;
68         this.parent = parent;
69     }
70
71     public void reparent(final AbstractStructureInstance newParent) {
72         parent = newParent;
73     }
74
75     /**
76      * Returns the schema node (really, a data node) in the schema to which this data instance relates.
77      */
78     public AbstractStatement getSchemaNode() {
79         return schemaNode;
80     }
81
82     /**
83      * Returns the data DOM node. May be null if NP-container or default value.
84      */
85     public YangDataDomNode getDataDomNode() {
86         return dataDomNode;
87     }
88
89     public String getName() {
90         if (dataDomNode == null) {
91             return schemaNode.getStatementIdentifier();
92         }
93         return dataDomNode.getName();
94     }
95
96     /**
97      * Returns the namespace. Where a data DOM node exists, will return that - otherwise, will return the effective
98      * namespace of the schema node. May return null.
99      */
100     public String getNamespace() {
101         if (dataDomNode == null) {
102             return schemaNode.getEffectiveNamespace();
103         }
104         return dataDomNode.getNamespace();
105     }
106
107     public AbstractStructureInstance getParent() {
108         return parent;
109     }
110 }