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