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