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