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;
 
  33 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.DOT_REGEX;
 
  34 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.SLASH;
 
  35 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getChildSchemaNode;
 
  36 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getIndex;
 
  37 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getListName;
 
  38 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNamespace;
 
  39 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNodeType;
 
  40 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getParsedValue;
 
  41 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getProcessedPath;
 
  42 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision;
 
  43 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace;
 
  44 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName;
 
  45 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE;
 
  46 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE;
 
  47 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE;
 
  48 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_NODE;
 
  51  * Representation of mdsal based properties node serializer implementation.
 
  53 public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<SchemaNode, SchemaContext> {
 
  55     private SchemaNode curSchema;
 
  56     private PropertiesNode node;
 
  59      * Creates the properties node serializer.
 
  61      * @param schemaNode schema node.
 
  62      * @param schemaCtx  schema context
 
  63      * @param uri        URL of the request
 
  65     public MdsalPropertiesNodeSerializer(SchemaNode schemaNode,
 
  66                                          SchemaContext schemaCtx, String uri) {
 
  67         super(schemaNode, schemaCtx, uri);
 
  71     public PropertiesNode encode(Map<String, String> paramMap) throws SvcLogicException {
 
  72         curSchema = schemaNode();
 
  73         String nodeInUri[] = uri().split("\\/");
 
  74         String lastNodeName = nodeInUri[nodeInUri.length - 1];
 
  75         String rootUri = uri().replaceAll("\\/", "\\.");
 
  76         node = createRootNode(lastNodeName, rootUri);
 
  78         paramMap = convertToValidParam(paramMap);
 
  80         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
 
  81             String[] names = entry.getKey().split("\\.");
 
  82             for (int i = 0; i < names.length; i++) {
 
  83                 if (i < nodeInUri.length) {
 
  84                     if (!(nodeInUri[i].equals(names[i]))) {
 
  88                     createPropertyNode(i, names.length, names[i],
 
  97      * Converts all the params in the svc logic context into a valid param by
 
  98      * replacing the underscore in module name to colon at necessary places.
 
 100      * @param paramMap list of invalid parameters
 
 101      * @return list of partially valid parameters
 
 103     private Map<String, String> convertToValidParam(Map<String, String> paramMap) {
 
 104         Map<String, String> fixedParams = new HashMap<>();
 
 105         for(Map.Entry<String, String> entry : paramMap.entrySet()) {
 
 106             String key = entry.getKey().replaceAll(DOT_REGEX, SLASH);
 
 108                 SchemaPathHolder fixedUrl = getProcessedPath(key, schemaCtx());
 
 109                 String fixedUri = fixedUrl.getUri().replaceAll(
 
 111                 fixedParams.put(fixedUri, entry.getValue());
 
 112             } catch (IllegalArgumentException | RestconfDocumentedException
 
 113                     | NullPointerException e) {
 
 114                 fixedParams.put(entry.getKey(), entry.getValue());
 
 121     public Map<String, String> decode(PropertiesNode propertiesNode)
 
 122             throws SvcLogicException {
 
 123         PropertiesNodeWalker walker = new DefaultPropertiesNodeWalker<>();
 
 124         DefaultPropertiesNodeListener listener = new DefaultPropertiesNodeListener();
 
 125         walker.walk(listener, propertiesNode);
 
 126         return listener.params();
 
 129     private RootNode createRootNode(String lastNodeName, String rootUri) {
 
 130         Module m = SchemaContextUtil.findParentModule(schemaCtx(), curSchema);
 
 131         Namespace ns = new Namespace(m.getName(), m.getNamespace(),
 
 132                                      getRevision(m.getRevision()));
 
 133         return new RootNode(lastNodeName, ns, schemaNode(), rootUri);
 
 136     private void createPropertyNode(int index, int length, String name,
 
 137                                     String value) throws SvcLogicException {
 
 139         Namespace ns = getNamespace(getListName(name), schemaCtx(),
 
 141         String localName = resolveName(ns, name);
 
 142         SchemaNode schema = getChildSchemaNode(curSchema, localName, ns);
 
 143         if (schema == null) {
 
 147         switch (getNodeType(index, length, name)) {
 
 148             case SINGLE_INSTANCE_NODE:
 
 149                 node = node.addChild(localName, ns,
 
 150                                      SINGLE_INSTANCE_NODE, schema);
 
 154             case MULTI_INSTANCE_NODE:
 
 155                 node = node.addChild(getIndex(name), localName, ns,
 
 156                                      MULTI_INSTANCE_NODE, schema);
 
 160             case SINGLE_INSTANCE_LEAF_NODE:
 
 161                 Namespace valNs = getValueNamespace(value, schemaCtx());
 
 162                 value = getParsedValue(valNs, value);
 
 163                 node = node.addChild(localName, ns, SINGLE_INSTANCE_LEAF_NODE,
 
 164                                      value, valNs, schema);
 
 165                 node = node.endNode();
 
 166                 curSchema = ((SchemaNode) node.appInfo());
 
 169             case MULTI_INSTANCE_LEAF_NODE:
 
 170                 valNs = getValueNamespace(value, schemaCtx());
 
 171                 value = getParsedValue(valNs, value);
 
 172                 node = node.addChild(getIndex(name), localName, ns,
 
 173                                      MULTI_INSTANCE_LEAF_NODE, value,
 
 175                 node = node.endNode();
 
 176                 curSchema = ((SchemaNode) node.appInfo());
 
 180                 throw new SvcLogicException("Invalid node type");