summaryrefslogtreecommitdiffstats
path: root/plugins/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java
blob: 1fd0d2dee4b51050b4e0ee052dffd6b2e7a4c292 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*-
 * ============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.dfserializer;

import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace;
import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType;
import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.RootNode;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;

import java.util.Deque;

import static java.lang.String.format;
import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.DF_ERR;
import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.NODE_TYPE_ERR;
import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.PROP_NODE_ERR;
import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.getResolvedNamespace;
import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.resolveBaseTypeFrom;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision;
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.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE;
import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_NODE;
import static org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils.findDataChildSchemaByQName;
import static org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils.findSchemaForChild;
import static org.opendaylight.yangtools.yang.data.util.ParserStreamUtils.findSchemaNodeByNameAndNamespace;

/**
 * Representation of MDSAL based serializer helper, which adds properties
 * node to the properties tree based on its types.
 */
public class MdsalSerializerHelper extends SerializerHelper<SchemaNode, SchemaContext> {

    /**
     * Current properties node.
     */
    private PropertiesNode propNode;

    /**
     * Current schema node.
     */
    private SchemaNode curSchemaNode;


    /**
     * Creates MDSAL serializer helper with root schema node, schema context
     * and URI.
     *
     * @param n schema node of the URI's last node
     * @param c schema context
     * @param u URI of the request
     */
    public MdsalSerializerHelper(SchemaNode n, SchemaContext c,
                                 String u) {
        super(n, c, u);
        Namespace ns = new Namespace(n.getQName().getLocalName(),
                                     n.getQName().getNamespace(),
                                     getRevision(n.getQName().getRevision()));
        propNode = new RootNode<>(n.getQName().getLocalName(), ns,
                                  getSchemaNode(), u);
        curSchemaNode = getSchemaNode();
    }

    @Override
    protected SchemaNode getSchemaNode() {
        return schemaNode;
    }

    @Override
    protected SchemaContext getSchemaCtx() {
        return schemaCtx;
    }

    @Override
    protected SchemaNode getCurSchema() {
        return curSchemaNode;
    }

    @Override
    protected void addNode(String name, String nameSpace, String value,
                           String valNameSpace, NodeType type)
            throws SvcLogicException {
        Namespace ns;
        if (type == null) {
            ns = getResolvedNamespace(null, nameSpace,
                                      getSchemaCtx(), propNode);
        } else {
            ns = getResolvedNamespace(nameSpace, null,
                                      getSchemaCtx(), propNode);
        }
        if (isChildPresent(name, ns)) {
            addNodeToProperty(name, ns, value, valNameSpace, type);
        } else {
            throw new SvcLogicException(format(
                    "Unable to add the node %s", name));
        }
    }

    @Override
    protected void exitNode() throws SvcLogicException {
        propNode = propNode.parent();
        if (propNode != null) {
            NodeType type = propNode.nodeType();
            if (type == MULTI_INSTANCE_HOLDER_NODE ||
                    type == MULTI_INSTANCE_LEAF_HOLDER_NODE) {
                propNode = propNode.parent();
            }
        }
        if (propNode == null || propNode.appInfo() == null
                || !(propNode.appInfo() instanceof SchemaNode)) {
            throw new SvcLogicException(PROP_NODE_ERR);
        }
        curSchemaNode = (SchemaNode) propNode.appInfo();
    }

    @Override
    protected PropertiesNode getPropertiesNode() {
        return propNode;
    }

    /**
     * Adds the node to property node based on the type of the schema node,
     * which is decided based on the name and namespace of the input
     * information.
     *
     * @param name         name of the node
     * @param ns           namespace of the node
     * @param value        value of the node if its a leaf/leaf-list
     * @param valNamespace namespace of the value
     * @param type         type of the node
     * @throws SvcLogicException when adding child fails
     */
    private void addNodeToProperty(String name, Namespace ns, String value,
                                   String valNamespace, NodeType type)
            throws SvcLogicException {
        Namespace valueNs;
        if (type != null) {
            validateNodeType(type);
        }
        if (curSchemaNode instanceof LeafSchemaNode) {
            valueNs = getValueNs(curSchemaNode, valNamespace, type);
            propNode = propNode.addChild(name, ns,
                                         SINGLE_INSTANCE_LEAF_NODE,
                                         value, valueNs, curSchemaNode);
        } else if (curSchemaNode instanceof LeafListSchemaNode) {
            valueNs = getValueNs(curSchemaNode, valNamespace, type);
            propNode = propNode.addChild(null, name, ns,
                                         MULTI_INSTANCE_LEAF_NODE, value,
                                         valueNs, curSchemaNode);
        } else if (curSchemaNode instanceof ListSchemaNode) {
            propNode = propNode.addChild(null, name, ns, MULTI_INSTANCE_NODE,
                                         curSchemaNode);
        } else {
            propNode = propNode.addChild(name, ns, SINGLE_INSTANCE_NODE,
                                         curSchemaNode);
        }
    }

    /**
     * Returns the namespace of the value namespace in case of identity ref.
     *
     * @param schemaNode schema node
     * @param valNs      value name space
     * @param nodeType   node type
     * @return namespace of value namespace
     * @throws SvcLogicException when namespace resolution fails for identityref
     */
    private Namespace getValueNs(SchemaNode schemaNode, String valNs,
                                 NodeType nodeType) throws SvcLogicException {
        Namespace ns = null;
        if (valNs != null) {
            TypeDefinition type;
            if (schemaNode instanceof LeafSchemaNode) {
                type = ((LeafSchemaNode) schemaNode).getType();
            } else {
                type = ((LeafListSchemaNode) schemaNode).getType();
            }
            TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
            if (baseType instanceof IdentityrefTypeDefinition) {
                if (nodeType == null) {
                    ns = getResolvedNamespace(null, valNs, getSchemaCtx(),
                                              propNode);
                } else {
                    ns = getResolvedNamespace(valNs, null, getSchemaCtx(),
                                              propNode);
                }
            }
        }
        return ns;
    }

    /**
     * Validates that the node type from the data format matches with that of
     * the corresponding schema node.
     *
     * @param type node type from the abstract data format
     * @throws SvcLogicException when the node type is wrong
     */
    private void validateNodeType(NodeType type) throws SvcLogicException {
        boolean verify;
        switch (type) {
            case SINGLE_INSTANCE_LEAF_NODE:
                verify = curSchemaNode instanceof LeafSchemaNode;
                break;

            case MULTI_INSTANCE_LEAF_NODE:
                verify = curSchemaNode instanceof LeafListSchemaNode;
                break;

            case MULTI_INSTANCE_NODE:
                verify = curSchemaNode instanceof ListSchemaNode;
                break;

            case SINGLE_INSTANCE_NODE:
                verify = (!(curSchemaNode instanceof LeafSchemaNode) &&
                        !(curSchemaNode instanceof LeafListSchemaNode) &&
                        !(curSchemaNode instanceof ListSchemaNode));
                break;

            default:
                throw new SvcLogicException(format(NODE_TYPE_ERR,
                                                   type.toString()));
        }
        if (!verify) {
            throw new SvcLogicException(format(DF_ERR, curSchemaNode
                    .getQName().getLocalName(), type.toString()));
        }
    }

    /**
     * Returns true if the child schema is present with the name and
     * namespace inside the current schema node, if present updates the
     * current schema node; false otherwise.
     *
     * @param name      name of the child schema node
     * @param namespace namespace of the child schema node
     * @return returns true if the child schema is available; false otherwise
     */
    private boolean isChildPresent(String name, Namespace namespace) {
        QName qname =  QName.create(namespace.moduleNs(),
                                    Revision.of(namespace.revision()), name);
        SchemaNode childNode = null;
        if (curSchemaNode instanceof DataSchemaNode) {
            Deque<DataSchemaNode> dataSchema = findSchemaNodeByNameAndNamespace(
                    (DataSchemaNode) curSchemaNode, name, namespace.moduleNs());

            if (dataSchema != null && !dataSchema.isEmpty()) {
                childNode = dataSchema.pop();
            }

            if (dataSchema != null && !dataSchema.isEmpty()) {
                childNode = findSchemaForChild(((ChoiceSchemaNode) childNode),
                                               qname);
            }

        } else {
            childNode = findDataChildSchemaByQName(curSchemaNode, qname);
        }

        if (childNode != null) {
            curSchemaNode = childNode;
            return true;
        }
        return false;
    }
}