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