2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer;
23 import java.util.HashMap;
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;
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;
54 * Representation of mdsal based properties node serializer implementation.
56 public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<SchemaNode, SchemaContext> {
58 private static final Logger log = LoggerFactory.getLogger(
59 MdsalPropertiesNodeSerializer.class);
60 private SchemaNode curSchema;
61 private PropertiesNode node;
64 * Creates the properties node serializer.
66 * @param schemaNode schema node.
67 * @param schemaCtx schema context
68 * @param uri URL of the request
70 public MdsalPropertiesNodeSerializer(SchemaNode schemaNode,
71 SchemaContext schemaCtx, String uri) {
72 super(schemaNode, schemaCtx, uri);
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);
83 paramMap = convertToValidParam(paramMap);
85 updateModNameReq(paramMap, rootUri);
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]))) {
95 createPropertyNode(i, names.length, names[i],
103 private void updateModNameReq(Map<String, String> paramMap,
105 String isReqStr = rootUri + "." + "isNonAppend";
106 String val = paramMap.get(isReqStr);
107 if (val != null && val.equals("true")) {
108 node.nonAppend(true);
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.
116 * @param paramMap list of invalid parameters
117 * @return list of partially valid parameters
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);
124 SchemaPathHolder fixedUrl = getProcessedPath(key, schemaCtx());
125 String fixedUri = fixedUrl.getUri().replaceAll(
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());
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();
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);
154 private void createPropertyNode(int index, int length, String name,
155 String value) throws SvcLogicException {
157 Namespace ns = getNamespace(getListName(name), schemaCtx(),
159 String localName = resolveName(ns, name);
160 SchemaNode schema = getChildSchemaNode(curSchema, localName, ns);
161 if (schema == null) {
165 switch (getNodeType(index, length, name, schema)) {
166 case SINGLE_INSTANCE_NODE:
167 node = node.addChild(localName, ns,
168 SINGLE_INSTANCE_NODE, schema);
172 case MULTI_INSTANCE_NODE:
173 node = node.addChild(getIndex(name), localName, ns,
174 MULTI_INSTANCE_NODE, schema);
178 case SINGLE_INSTANCE_LEAF_NODE:
179 addLeafNode(value, SINGLE_INSTANCE_LEAF_NODE, localName,
183 case MULTI_INSTANCE_LEAF_NODE:
184 addLeafNode(value, MULTI_INSTANCE_LEAF_NODE, localName,
189 node = node.addChild(localName, ns, ANY_XML_NODE,
190 value, null, schema);
191 node = node.endNode();
192 curSchema = ((SchemaNode) node.appInfo());
196 throw new SvcLogicException("Invalid node type");
201 * Adds leaf property node to the current node.
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
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);
220 node = node.addChild(getIndex(name), localName, ns,
221 MULTI_INSTANCE_LEAF_NODE, value,
224 node = node.endNode();
225 curSchema = ((SchemaNode) node.appInfo());