UT and defect fixes for DF serializer
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / pnserializer / MdsalPropertiesNodeSerializer.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.pnserializer;
22
23 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
27 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
28
29 import java.util.Map;
30
31 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getChildSchemaNode;
32 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getIndex;
33 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getListName;
34 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNamespace;
35 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNodeType;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getParsedValue;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision;
38 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace;
39 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName;
40 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE;
41 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE;
42 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE;
43 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_NODE;
44
45 /**
46  * Representation of mdsal based properties node serializer implementation.
47  */
48 public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<SchemaNode, SchemaContext> {
49
50     private SchemaNode curSchema;
51     private PropertiesNode node;
52
53     /**
54      * Creates the properties node serializer.
55      *
56      * @param schemaNode schema node.
57      * @param schemaCtx  schema context
58      * @param uri        URL of the request
59      */
60     public MdsalPropertiesNodeSerializer(SchemaNode schemaNode,
61                                          SchemaContext schemaCtx, String uri) {
62         super(schemaNode, schemaCtx, uri);
63     }
64
65     @Override
66     public PropertiesNode encode(Map<String, String> paramMap) throws SvcLogicException {
67         curSchema = schemaNode();
68         String nodeInUri[] = uri().split("\\/");
69         String lastNodeName = nodeInUri[nodeInUri.length - 1];
70         String rootUri = uri().replaceAll("\\/", "\\.");
71         node = createRootNode(lastNodeName, rootUri);
72
73         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
74             String[] names = entry.getKey().split("\\.");
75             for (int i = 0; i < names.length; i++) {
76                 if (i < nodeInUri.length) {
77                     if (!(nodeInUri[i].equals(names[i]))) {
78                         break;
79                     }
80                 } else {
81                     createPropertyNode(i, names.length, names[i],
82                                        entry.getValue());
83                 }
84             }
85         }
86         return node;
87     }
88
89     @Override
90     public Map<String, String> decode(PropertiesNode propertiesNode)
91             throws SvcLogicException {
92         PropertiesNodeWalker walker = new DefaultPropertiesNodeWalker<>();
93         DefaultPropertiesNodeListener listener = new DefaultPropertiesNodeListener();
94         walker.walk(listener, propertiesNode);
95         return listener.params();
96     }
97
98     private RootNode createRootNode(String lastNodeName, String rootUri) {
99         Module m = SchemaContextUtil.findParentModule(schemaCtx(), curSchema);
100         Namespace ns = new Namespace(m.getName(), m.getNamespace(),
101                                      getRevision(m.getRevision()));
102         return new RootNode(lastNodeName, ns, schemaNode(), rootUri);
103     }
104
105     private void createPropertyNode(int index, int length, String name,
106                                     String value) throws SvcLogicException {
107         String localName = resolveName(name);
108         Namespace ns = getNamespace(getListName(name), schemaCtx(), node);
109         SchemaNode schema = getChildSchemaNode(curSchema, localName, ns);
110         if (schema == null) {
111             return;
112         }
113
114         switch (getNodeType(index, length, name)) {
115             case SINGLE_INSTANCE_NODE:
116                 node = node.addChild(localName, ns,
117                                      SINGLE_INSTANCE_NODE, schema);
118                 curSchema = schema;
119                 break;
120
121             case MULTI_INSTANCE_NODE:
122                 node = node.addChild(getIndex(name), localName, ns,
123                                      MULTI_INSTANCE_NODE, schema);
124                 curSchema = schema;
125                 break;
126
127             case SINGLE_INSTANCE_LEAF_NODE:
128                 Namespace valNs = getValueNamespace(value, schemaCtx());
129                 value = getParsedValue(valNs, value);
130                 node = node.addChild(localName, ns, SINGLE_INSTANCE_LEAF_NODE,
131                                      value, valNs, schema);
132                 node = node.endNode();
133                 curSchema = ((SchemaNode) node.appInfo());
134                 break;
135
136             case MULTI_INSTANCE_LEAF_NODE:
137                 valNs = getValueNamespace(value, schemaCtx());
138                 value = getParsedValue(valNs, value);
139                 node = node.addChild(getIndex(name), localName, ns,
140                                      MULTI_INSTANCE_LEAF_NODE, value,
141                                      valNs, schema);
142                 node = node.endNode();
143                 curSchema = ((SchemaNode) node.appInfo());
144                 break;
145
146             default:
147                 throw new SvcLogicException("Invalid node type");
148         }
149     }
150 }