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.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;
53 * Representation of mdsal based properties node serializer implementation.
55 public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<SchemaNode, SchemaContext> {
57 private static final Logger log = LoggerFactory.getLogger(
58 MdsalPropertiesNodeSerializer.class);
59 private SchemaNode curSchema;
60 private PropertiesNode node;
63 * Creates the properties node serializer.
65 * @param schemaNode schema node.
66 * @param schemaCtx schema context
67 * @param uri URL of the request
69 public MdsalPropertiesNodeSerializer(SchemaNode schemaNode,
70 SchemaContext schemaCtx, String uri) {
71 super(schemaNode, schemaCtx, uri);
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);
82 paramMap = convertToValidParam(paramMap);
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]))) {
92 createPropertyNode(i, names.length, names[i],
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.
104 * @param paramMap list of invalid parameters
105 * @return list of partially valid parameters
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);
112 SchemaPathHolder fixedUrl = getProcessedPath(key, schemaCtx());
113 String fixedUri = fixedUrl.getUri().replaceAll(
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());
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();
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);
142 private void createPropertyNode(int index, int length, String name,
143 String value) throws SvcLogicException {
145 Namespace ns = getNamespace(getListName(name), schemaCtx(),
147 String localName = resolveName(ns, name);
148 SchemaNode schema = getChildSchemaNode(curSchema, localName, ns);
149 if (schema == null) {
153 switch (getNodeType(index, length, name)) {
154 case SINGLE_INSTANCE_NODE:
155 node = node.addChild(localName, ns,
156 SINGLE_INSTANCE_NODE, schema);
160 case MULTI_INSTANCE_NODE:
161 node = node.addChild(getIndex(name), localName, ns,
162 MULTI_INSTANCE_NODE, schema);
166 case SINGLE_INSTANCE_LEAF_NODE:
167 addLeafNode(value, SINGLE_INSTANCE_LEAF_NODE, localName,
171 case MULTI_INSTANCE_LEAF_NODE:
172 addLeafNode(value, MULTI_INSTANCE_LEAF_NODE, localName,
177 throw new SvcLogicException("Invalid node type");
182 * Adds leaf property node to the current node.
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
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);
201 node = node.addChild(getIndex(name), localName, ns,
202 MULTI_INSTANCE_LEAF_NODE, value,
205 node = node.endNode();
206 curSchema = ((SchemaNode) node.appInfo());