aboutsummaryrefslogtreecommitdiffstats
path: root/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java
blob: 0711a7cff93a53ab4af838dfbdcdc05491d24d6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*-
 * ============LICENSE_START=======================================================
 * ONAP - CCSDK
 * ================================================================================
 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer;

import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;

import java.util.HashMap;
import java.util.Map;

import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.addToAugmentations;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.createNode;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getAugmentationNode;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getUri;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.isNamespaceAsParent;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_HOLDER_NODE;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_HOLDER_NODE;
import static org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils.findCorrespondingAugment;

/**
 * Abstraction of an entity that represents an inner node to properties data
 * tree.
 *
 * @param <T> type of child
 */
public abstract class InnerNode<T extends NodeChild> extends PropertiesNode {

    private Map<String, T> children = new HashMap<String, T>();

    protected InnerNode(String name, Namespace namespace, String uri,
                        PropertiesNode parent, Object appInfo, NodeType nodeType) {
        super(name, namespace, uri, parent, appInfo, nodeType);
    }

    /**
     * Returns children.
     *
     * @return children
     */
    public Map<String, T> children() {
        return children;
    }

    /**
     * Sets children.
     *
     * @param children child nodes
     */
    public void children(Map<String, T> children) {
        this.children = children;
    }

    @Override
    public PropertiesNode addChild(String name, Namespace namespace,
                                   NodeType type,
                                   Object appInfo) throws SvcLogicException {
        PropertiesNode node = ((PropertiesNode) children.get(name));
        if (node != null) {
            return node;
        }

        // get augment schema, if it is augmented node
        AugmentationSchemaNode augSchema = null;
        if (((DataSchemaNode) appInfo).isAugmenting()) {
            augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()), ((DataSchemaNode) appInfo));
            node = getAugmentationNode(augSchema, this, name);
        }

        // create node based on type
        if (node == null) {
            String uri = getUri(this, name, namespace);
            node = createNode(name, namespace, uri, this, appInfo, type);
        }

        // If namespace is not same as parent then it is augmented node
        if (augSchema != null && !isNamespaceAsParent(this, node)) {
            addToAugmentations(augSchema, this, node);
        } else {
            children.put(name, ((T) node));
        }
        return node;
    }

    @Override
    public PropertiesNode addChild(String name, Namespace namespace,
                                   NodeType type, String value,
                                   Namespace valueNs,
                                   Object appInfo) throws SvcLogicException {
        LeafNode node = ((LeafNode) children.get(name));
        if (node != null) {
            return  node;
        }

        AugmentationSchemaNode augSchema = null;
        if (((DataSchemaNode) appInfo).isAugmenting()) {
            augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
                                                 ((DataSchemaNode) appInfo));
        }

        String uri = getUri(this, name, namespace);
        node = new LeafNode(name, namespace, uri, this,
                            appInfo, type, value);
        node.valueNs(valueNs);

        if (augSchema != null && !isNamespaceAsParent(this, node)) {
            addToAugmentations(augSchema, this, node);
        } else {
            children.put(name, ((T) node));
        }
        return node;
    }

    @Override
    public PropertiesNode addChild(String index, String name,
                                   Namespace namespace, NodeType type,
                                   Object appInfo) throws SvcLogicException {
        String localname = resolveName(name);
        PropertiesNode node = ((PropertiesNode) children.get(localname));

        if (node == null) {
            AugmentationSchemaNode augSchema = null;
            if (((DataSchemaNode) appInfo).isAugmenting()) {
                augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
                                                     ((DataSchemaNode) appInfo));
                node = getAugmentationNode(augSchema, this, localname);
            }

            if (node == null) {
                String uri = getUri(this, name, namespace);
                node = new ListHolderNode(localname, namespace, uri,
                                          this, appInfo, MULTI_INSTANCE_HOLDER_NODE);
            }

            if (augSchema != null && !isNamespaceAsParent(this, node)) {
                addToAugmentations(augSchema, this, node);
            } else {
                children.put(localname, ((T) node));
            }

            node = node.addChild(index, localname, namespace, type, appInfo);
        } else if (node instanceof ListHolderNode) {
            ListHolderChild child = ((ListHolderNode) node).child(index);
            node = (child != null ? ((MultiInstanceNode) child) :
                    node.addChild(index, localname, namespace, type, appInfo));
        } else {
            throw new SvcLogicException("Duplicate node exist with same node");
        }
        return node;
    }

    @Override
    public PropertiesNode addChild(String index, String name,
                                   Namespace namespace, NodeType type,
                                   String value, Namespace valueNs,
                                   Object appInfo) throws SvcLogicException {
        String localName = resolveName(name);
        PropertiesNode node = ((PropertiesNode) children.get(localName));

        if (node == null) {

            AugmentationSchemaNode augSchema = null;
            if (((DataSchemaNode) appInfo).isAugmenting()) {
                augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
                                                     ((DataSchemaNode) appInfo));
                node = getAugmentationNode(augSchema, this, localName);
            }

            if (node == null) {
                String uri = getUri(this, name, namespace);
                node = new LeafListHolderNode(localName, namespace, uri, this,
                                              appInfo, MULTI_INSTANCE_LEAF_HOLDER_NODE);
            }

            if (augSchema != null && !isNamespaceAsParent(this, node)) {
                addToAugmentations(augSchema, this, node);
            } else {
                children.put(localName, ((T) node));
            }

            node = node.addChild(index, localName, namespace, type, value, valueNs, appInfo);
        } else if (node instanceof LeafListHolderNode) {
            LeafNode child = ((LeafNode) ((HolderNode) node).child(index));
            node = (child != null ? child : node.addChild(index, localName,
                                                          namespace, type,
                                                          value, valueNs,
                                                          appInfo));
        } else {
            throw new SvcLogicException("Duplicate node exist with same node");
        }
        return node;
    }
}