Implementation of Data Format serializer
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / dfserializer / SerializerHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
22
23 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
24 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType;
25 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
26
27 /**
28  * Abstraction of an entity which helps the data format serializers to obtain
29  * schema context details and to build properties from data.
30  *
31  * @param <T> type of schema node
32  * @param <P> type of schema context
33  */
34 public abstract class SerializerHelper<T, P> {
35
36     /**
37      * Schema node of the last element in the URI.
38      */
39     protected T schemaNode;
40
41     /**
42      * Root schema context.
43      */
44     protected P schemaCtx;
45
46     /**
47      * Root URI.
48      */
49     protected String rootUri;
50
51     /**
52      * Creates an instance of the serializer helper with the schema node,
53      * schema context and the URI.
54      *
55      * @param t schema node
56      * @param p schema context
57      * @param u root URI
58      */
59     protected SerializerHelper(T t, P p, String u) {
60         schemaNode = t;
61         schemaCtx = p;
62         rootUri = u;
63     }
64
65     /**
66      * Returns schema node of the last element in the URI.
67      *
68      * @return schema node
69      */
70     protected abstract T getSchemaNode();
71
72     /**
73      * Returns the root schema context.
74      *
75      * @return schema context
76      */
77     protected abstract P getSchemaCtx();
78
79     /**
80      * Returns the current schema context node.
81      *
82      * @return current schema context node
83      */
84     protected abstract T getCurSchema();
85
86     /**
87      * Adds a node to the properties node tree.
88      *
89      * @param name         name of the node
90      * @param nameSpace    name space of the node, it can be either module
91      *                     name or namespace; null indicates parent namespace
92      * @param value        value of the node; applicable for leaf/leaf-list node
93      * @param valNameSpace value namespace for identityref, could be module
94      *                     name or namespace
95      * @param type         type of node if known like in case of JSON
96      * @throws SvcLogicException when adding node fails
97      */
98     protected abstract void addNode(String name, String nameSpace, String value,
99                                     String valNameSpace, NodeType type)
100             throws SvcLogicException;
101
102     /**
103      * Exits the node, in case if it's leaf node then it adds to the properties
104      * map.
105      *
106      * @throws SvcLogicException when properties node tree is improper
107      */
108     protected abstract void exitNode() throws SvcLogicException;
109
110     /**
111      * Returns the built properties corresponding to the data.
112      *
113      * @return properties node.
114      */
115     protected abstract PropertiesNode getPropertiesNode();
116 }