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 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;
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;
52 * Representation of mdsal based properties node serializer implementation.
54 public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<SchemaNode, EffectiveModelContext> {
56 private static final Logger log = LoggerFactory.getLogger(
57 MdsalPropertiesNodeSerializer.class);
58 private SchemaNode curSchema;
59 private PropertiesNode node;
62 * Creates the properties node serializer.
64 * @param schemaNode schema node.
65 * @param schemaCtx schema context
66 * @param uri URL of the request
68 public MdsalPropertiesNodeSerializer(SchemaNode schemaNode,
69 EffectiveModelContext schemaCtx, String uri) {
70 super(schemaNode, schemaCtx, uri);
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);
81 paramMap = convertToValidParam(paramMap);
83 updateModNameReq(paramMap, rootUri);
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]))) {
93 createPropertyNode(i, names.length, names[i],
101 private void updateModNameReq(Map<String, String> paramMap,
103 String isReqStr = rootUri + "." + "isNonAppend";
104 String val = paramMap.get(isReqStr);
105 if (val != null && val.equals("true")) {
106 node.nonAppend(true);
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.
114 * @param paramMap list of invalid parameters
115 * @return list of partially valid parameters
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);
122 SchemaPathHolder fixedUrl = getProcessedPath(key, schemaCtx());
123 String fixedUri = fixedUrl.getUri().replaceAll(
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());
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();
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);
152 private void createPropertyNode(int index, int length, String name,
153 String value) throws SvcLogicException {
155 Namespace ns = getNamespace(getListName(name), schemaCtx(),
157 String localName = resolveName(ns, name);
158 SchemaNode schema = getChildSchemaNode(curSchema, localName, ns);
159 if (schema == null) {
163 switch (getNodeType(index, length, name, schema)) {
164 case SINGLE_INSTANCE_NODE:
165 node = node.addChild(localName, ns,
166 SINGLE_INSTANCE_NODE, schema);
170 case MULTI_INSTANCE_NODE:
171 node = node.addChild(getIndex(name), localName, ns,
172 MULTI_INSTANCE_NODE, schema);
176 case SINGLE_INSTANCE_LEAF_NODE:
177 addLeafNode(value, SINGLE_INSTANCE_LEAF_NODE, localName,
181 case MULTI_INSTANCE_LEAF_NODE:
182 addLeafNode(value, MULTI_INSTANCE_LEAF_NODE, localName,
187 node = node.addChild(localName, ns, ANY_XML_NODE,
188 value, null, schema);
189 node = node.endNode();
190 curSchema = ((SchemaNode) node.appInfo());
194 throw new SvcLogicException("Invalid node type");
199 * Adds leaf property node to the current node.
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
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);
218 node = node.addChild(getIndex(name), localName, ns,
219 MULTI_INSTANCE_LEAF_NODE, value,
222 node = node.endNode();
223 curSchema = ((SchemaNode) node.appInfo());