Replacing ":" by "_" for parameters
[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 java.util.HashMap;
24 import java.util.Map;
25
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
31 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
32
33 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.DOT_REGEX;
34 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.SLASH;
35 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getChildSchemaNode;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getIndex;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getListName;
38 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNamespace;
39 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNodeType;
40 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getParsedValue;
41 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getProcessedPath;
42 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision;
43 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace;
44 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName;
45 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE;
46 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE;
47 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE;
48 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_NODE;
49
50 /**
51  * Representation of mdsal based properties node serializer implementation.
52  */
53 public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<SchemaNode, SchemaContext> {
54
55     private SchemaNode curSchema;
56     private PropertiesNode node;
57
58     /**
59      * Creates the properties node serializer.
60      *
61      * @param schemaNode schema node.
62      * @param schemaCtx  schema context
63      * @param uri        URL of the request
64      */
65     public MdsalPropertiesNodeSerializer(SchemaNode schemaNode,
66                                          SchemaContext schemaCtx, String uri) {
67         super(schemaNode, schemaCtx, uri);
68     }
69
70     @Override
71     public PropertiesNode encode(Map<String, String> paramMap) throws SvcLogicException {
72         curSchema = schemaNode();
73         String nodeInUri[] = uri().split("\\/");
74         String lastNodeName = nodeInUri[nodeInUri.length - 1];
75         String rootUri = uri().replaceAll("\\/", "\\.");
76         node = createRootNode(lastNodeName, rootUri);
77
78         paramMap = convertToValidParam(paramMap);
79
80         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
81             String[] names = entry.getKey().split("\\.");
82             for (int i = 0; i < names.length; i++) {
83                 if (i < nodeInUri.length) {
84                     if (!(nodeInUri[i].equals(names[i]))) {
85                         break;
86                     }
87                 } else {
88                     createPropertyNode(i, names.length, names[i],
89                                        entry.getValue());
90                 }
91             }
92         }
93         return node;
94     }
95
96     /**
97      * Converts all the params in the svc logic context into a valid param by
98      * replacing the underscore in module name to colon at necessary places.
99      *
100      * @param paramMap list of invalid parameters
101      * @return list of partially valid parameters
102      */
103     private Map<String, String> convertToValidParam(Map<String, String> paramMap) {
104         Map<String, String> fixedParams = new HashMap<>();
105         for(Map.Entry<String, String> entry : paramMap.entrySet()) {
106             String key = entry.getKey().replaceAll(DOT_REGEX, SLASH);
107             try {
108                 SchemaPathHolder fixedUrl = getProcessedPath(key, schemaCtx());
109                 String fixedUri = fixedUrl.getUri().replaceAll(
110                         SLASH, DOT_REGEX);
111                 fixedParams.put(fixedUri, entry.getValue());
112             } catch (IllegalArgumentException | RestconfDocumentedException
113                     | NullPointerException e) {
114                 fixedParams.put(entry.getKey(), entry.getValue());
115             }
116         }
117         return fixedParams;
118     }
119
120     @Override
121     public Map<String, String> decode(PropertiesNode propertiesNode)
122             throws SvcLogicException {
123         PropertiesNodeWalker walker = new DefaultPropertiesNodeWalker<>();
124         DefaultPropertiesNodeListener listener = new DefaultPropertiesNodeListener();
125         walker.walk(listener, propertiesNode);
126         return listener.params();
127     }
128
129     private RootNode createRootNode(String lastNodeName, String rootUri) {
130         Module m = SchemaContextUtil.findParentModule(schemaCtx(), curSchema);
131         Namespace ns = new Namespace(m.getName(), m.getNamespace(),
132                                      getRevision(m.getRevision()));
133         return new RootNode(lastNodeName, ns, schemaNode(), rootUri);
134     }
135
136     private void createPropertyNode(int index, int length, String name,
137                                     String value) throws SvcLogicException {
138
139         Namespace ns = getNamespace(getListName(name), schemaCtx(),
140                                     node, curSchema);
141         String localName = resolveName(ns, name);
142         SchemaNode schema = getChildSchemaNode(curSchema, localName, ns);
143         if (schema == null) {
144             return;
145         }
146
147         switch (getNodeType(index, length, name)) {
148             case SINGLE_INSTANCE_NODE:
149                 node = node.addChild(localName, ns,
150                                      SINGLE_INSTANCE_NODE, schema);
151                 curSchema = schema;
152                 break;
153
154             case MULTI_INSTANCE_NODE:
155                 node = node.addChild(getIndex(name), localName, ns,
156                                      MULTI_INSTANCE_NODE, schema);
157                 curSchema = schema;
158                 break;
159
160             case SINGLE_INSTANCE_LEAF_NODE:
161                 Namespace valNs = getValueNamespace(value, schemaCtx());
162                 value = getParsedValue(valNs, value);
163                 node = node.addChild(localName, ns, SINGLE_INSTANCE_LEAF_NODE,
164                                      value, valNs, schema);
165                 node = node.endNode();
166                 curSchema = ((SchemaNode) node.appInfo());
167                 break;
168
169             case MULTI_INSTANCE_LEAF_NODE:
170                 valNs = getValueNamespace(value, schemaCtx());
171                 value = getParsedValue(valNs, value);
172                 node = node.addChild(getIndex(name), localName, ns,
173                                      MULTI_INSTANCE_LEAF_NODE, value,
174                                      valNs, schema);
175                 node = node.endNode();
176                 curSchema = ((SchemaNode) node.appInfo());
177                 break;
178
179             default:
180                 throw new SvcLogicException("Invalid node type");
181         }
182     }
183 }