Release version 1.1.0 of sli/plugins
[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 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.DOT_REGEX;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.SLASH;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getChildSchemaNode;
38 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getIndex;
39 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getListName;
40 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNamespace;
41 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNodeType;
42 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getParsedValue;
43 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getProcessedPath;
44 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision;
45 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace;
46 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName;
47 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.ANY_XML_NODE;
48 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE;
49 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE;
50 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE;
51 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_NODE;
52
53 /**
54  * Representation of mdsal based properties node serializer implementation.
55  */
56 public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<SchemaNode, SchemaContext> {
57
58     private static final Logger log = LoggerFactory.getLogger(
59             MdsalPropertiesNodeSerializer.class);
60     private SchemaNode curSchema;
61     private PropertiesNode node;
62
63     /**
64      * Creates the properties node serializer.
65      *
66      * @param schemaNode schema node.
67      * @param schemaCtx  schema context
68      * @param uri        URL of the request
69      */
70     public MdsalPropertiesNodeSerializer(SchemaNode schemaNode,
71                                          SchemaContext schemaCtx, String uri) {
72         super(schemaNode, schemaCtx, uri);
73     }
74
75     @Override
76     public PropertiesNode encode(Map<String, String> paramMap) throws SvcLogicException {
77         curSchema = schemaNode();
78         String nodeInUri[] = uri().split("\\/");
79         String lastNodeName = nodeInUri[nodeInUri.length - 1];
80         String rootUri = uri().replaceAll("\\/", "\\.");
81         node = createRootNode(lastNodeName, rootUri);
82
83         paramMap = convertToValidParam(paramMap);
84
85         updateModNameReq(paramMap, rootUri);
86
87         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
88             String[] names = entry.getKey().split("\\.");
89             for (int i = 0; i < names.length; i++) {
90                 if (i < nodeInUri.length) {
91                     if (!(nodeInUri[i].equals(names[i]))) {
92                         break;
93                     }
94                 } else {
95                     createPropertyNode(i, names.length, names[i],
96                                        entry.getValue());
97                 }
98             }
99         }
100         return node;
101     }
102
103     private void updateModNameReq(Map<String, String> paramMap,
104                                   String rootUri) {
105         String isReqStr = rootUri + "." + "isNonAppend";
106         String val = paramMap.get(isReqStr);
107         if (val != null && val.equals("true")) {
108             node.nonAppend(true);
109         }
110     }
111
112     /**
113      * Converts all the params in the svc logic context into a valid param by
114      * replacing the underscore in module name to colon at necessary places.
115      *
116      * @param paramMap list of invalid parameters
117      * @return list of partially valid parameters
118      */
119     private Map<String, String> convertToValidParam(Map<String, String> paramMap) {
120         Map<String, String> fixedParams = new HashMap<>();
121         for(Map.Entry<String, String> entry : paramMap.entrySet()) {
122             String key = entry.getKey().replaceAll(DOT_REGEX, SLASH);
123             try {
124                 SchemaPathHolder fixedUrl = getProcessedPath(key, schemaCtx());
125                 String fixedUri = fixedUrl.getUri().replaceAll(
126                         SLASH, DOT_REGEX);
127                 fixedParams.put(fixedUri, entry.getValue());
128             } catch (IllegalArgumentException | RestconfDocumentedException
129                     | NullPointerException e) {
130                 log.info("Exception while processing properties by replacing " +
131                     "underscore with colon. Process the properties as it is." + e);
132                 fixedParams.put(entry.getKey(), entry.getValue());
133             }
134         }
135         return fixedParams;
136     }
137
138     @Override
139     public Map<String, String> decode(PropertiesNode propertiesNode)
140             throws SvcLogicException {
141         PropertiesNodeWalker walker = new DefaultPropertiesNodeWalker<>();
142         DefaultPropertiesNodeListener listener = new DefaultPropertiesNodeListener();
143         walker.walk(listener, propertiesNode);
144         return listener.params();
145     }
146
147     private RootNode createRootNode(String lastNodeName, String rootUri) {
148         Module m = SchemaContextUtil.findParentModule(schemaCtx(), curSchema);
149         Namespace ns = new Namespace(m.getName(), m.getNamespace(),
150                                      getRevision(m.getRevision()));
151         return new RootNode(lastNodeName, ns, schemaNode(), rootUri);
152     }
153
154     private void createPropertyNode(int index, int length, String name,
155                                     String value) throws SvcLogicException {
156
157         Namespace ns = getNamespace(getListName(name), schemaCtx(),
158                                     node, curSchema);
159         String localName = resolveName(ns, name);
160         SchemaNode schema = getChildSchemaNode(curSchema, localName, ns);
161         if (schema == null) {
162             return;
163         }
164
165         switch (getNodeType(index, length, name, schema)) {
166             case SINGLE_INSTANCE_NODE:
167                 node = node.addChild(localName, ns,
168                                      SINGLE_INSTANCE_NODE, schema);
169                 curSchema = schema;
170                 break;
171
172             case MULTI_INSTANCE_NODE:
173                 node = node.addChild(getIndex(name), localName, ns,
174                                      MULTI_INSTANCE_NODE, schema);
175                 curSchema = schema;
176                 break;
177
178             case SINGLE_INSTANCE_LEAF_NODE:
179                 addLeafNode(value, SINGLE_INSTANCE_LEAF_NODE, localName,
180                                ns, schema, name);
181                 break;
182
183             case MULTI_INSTANCE_LEAF_NODE:
184                 addLeafNode(value, MULTI_INSTANCE_LEAF_NODE, localName,
185                                ns, schema, name);
186                 break;
187
188             case ANY_XML_NODE:
189                 node = node.addChild(localName, ns, ANY_XML_NODE,
190                                      value, null, schema);
191                 node = node.endNode();
192                 curSchema = ((SchemaNode) node.appInfo());
193                 break;
194
195             default:
196                 throw new SvcLogicException("Invalid node type");
197         }
198     }
199
200     /**
201      * Adds leaf property node to the current node.
202      *
203      * @param value value of the leaf node
204      * @param type single instance or multi instance leaf node
205      * @param localName name of the leaf node
206      * @param ns namespace of the leaf node
207      * @param schema schema of the leaf node
208      * @param name name of the leaf in properties
209      * @throws SvcLogicException exception while adding leaf node
210      */
211     private void addLeafNode(String value, NodeType type,
212                                 String localName, Namespace ns,
213                                 SchemaNode schema, String name) throws SvcLogicException {
214         Namespace valNs = getValueNamespace(value, schemaCtx());
215         value = getParsedValue(valNs, value);
216         if (SINGLE_INSTANCE_LEAF_NODE == type) {
217             node = node.addChild(localName, ns, SINGLE_INSTANCE_LEAF_NODE,
218                                  value, valNs, schema);
219         } else {
220             node = node.addChild(getIndex(name), localName, ns,
221                                  MULTI_INSTANCE_LEAF_NODE, value,
222                                  valNs, schema);
223         }
224         node = node.endNode();
225         curSchema = ((SchemaNode) node.appInfo());
226     }
227 }