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 org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 
  25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 
  27 import java.util.HashMap;
 
  30 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.addToAugmentations;
 
  31 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.createNode;
 
  32 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getAugmentationNode;
 
  33 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getUri;
 
  34 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.isNamespaceAsParent;
 
  35 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName;
 
  36 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_HOLDER_NODE;
 
  37 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_HOLDER_NODE;
 
  38 import static org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils.findCorrespondingAugment;
 
  41  * Abstraction of node representing properties data tree.
 
  43 public class RootNode<T extends NodeChild> extends PropertiesNode {
 
  45     private Map<String, T> children = new HashMap<String, T>();
 
  47     protected RootNode(String name, Namespace namespace,
 
  48                        Object appInfo, String uri) {
 
  49         super(name, namespace, uri, null, appInfo, null);
 
  57     public Map<String, T> children() {
 
  64      * @param children child nodes
 
  66     public void children(Map<String, T> children) {
 
  67         this.children = children;
 
  71     public PropertiesNode addChild(String name, Namespace namespace,
 
  73                                    Object appInfo) throws SvcLogicException {
 
  74         PropertiesNode node = ((PropertiesNode) children.get(name));
 
  79         // get augment schema, if it is augmented node
 
  80         AugmentationSchemaNode augSchema = null;
 
  81         if (((DataSchemaNode) appInfo).isAugmenting()) {
 
  82             augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
 
  83                                                  ((DataSchemaNode) appInfo));
 
  84             node = getAugmentationNode(augSchema, this, name);
 
  87         // create node based on type, this api will be invoked only for these three types
 
  89             String uri = getUri(this, name, namespace);
 
  90             node = createNode(name, namespace, uri, this, appInfo, type);
 
  93         // If namespace is not same as parent then it is augmented node
 
  94         if (augSchema != null && !isNamespaceAsParent(this, node)) {
 
  95             addToAugmentations(augSchema, this, node);
 
  97             children.put(name, ((T) node));
 
 103     public PropertiesNode addChild(String name, Namespace namespace,
 
 104                                    NodeType type, String value,
 
 106                                    Object appInfo) throws SvcLogicException {
 
 107         LeafNode node = ((LeafNode) children.get(name));
 
 112         AugmentationSchemaNode augSchema = null;
 
 113         if (((DataSchemaNode) appInfo).isAugmenting()) {
 
 114             augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
 
 115                                                  ((DataSchemaNode) appInfo));
 
 118         String uri = getUri(this, name, namespace);
 
 119         node = new LeafNode(name, namespace, uri, this,
 
 120                             appInfo, type, value);
 
 122         if (augSchema != null && !isNamespaceAsParent(this, node)) {
 
 123             addToAugmentations(augSchema, this, node);
 
 125             children.put(name, ((T) node));
 
 131     public PropertiesNode addChild(String index, String name,
 
 132                                    Namespace namespace, NodeType type,
 
 133                                    Object appInfo) throws SvcLogicException {
 
 134         String localname = resolveName(name);
 
 135         PropertiesNode node = ((PropertiesNode) children.get(localname));
 
 137             String uri = getUri(this, name, namespace);
 
 138             AugmentationSchemaNode augSchema = null;
 
 139             if (((DataSchemaNode) appInfo).isAugmenting()) {
 
 140                 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
 
 141                                                      ((DataSchemaNode) appInfo));
 
 142                 node = getAugmentationNode(augSchema, this, localname);
 
 146                 node = new ListHolderNode(localname, namespace, uri,
 
 147                                           this, appInfo, MULTI_INSTANCE_HOLDER_NODE);
 
 150             if (augSchema != null && !isNamespaceAsParent(this, node)) {
 
 151                 addToAugmentations(augSchema, this, node);
 
 153                 children.put(localname, ((T) node));
 
 155             node = node.addChild(index, localname, namespace, type, appInfo);
 
 156         } else if (node instanceof ListHolderNode) {
 
 157             ListHolderChild child = ((ListHolderNode) node).child(index);
 
 158             node = (child != null ? ((MultiInstanceNode) child) :
 
 159                     node.addChild(index, localname, namespace, type, appInfo));
 
 161             throw new SvcLogicException("Duplicate node exist with same node");
 
 167     public PropertiesNode addChild(String index, String name,
 
 168                                    Namespace namespace, NodeType type,
 
 169                                    String value, Namespace valueNs,
 
 170                                    Object appInfo) throws SvcLogicException {
 
 171         String localName = resolveName(name);
 
 172         PropertiesNode node = ((PropertiesNode) children.get(localName));
 
 174             String uri = getUri(this, name, namespace);
 
 175             AugmentationSchemaNode augSchema = null;
 
 176             if (((DataSchemaNode) appInfo).isAugmenting()) {
 
 177                 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
 
 178                                                      ((DataSchemaNode) appInfo));
 
 179                 node = getAugmentationNode(augSchema, this, localName);
 
 183                 node = new LeafListHolderNode(localName, namespace, uri, this,
 
 184                                               appInfo, MULTI_INSTANCE_LEAF_HOLDER_NODE);
 
 187             if (augSchema != null && !isNamespaceAsParent(this, node)) {
 
 188                 addToAugmentations(augSchema, this, node);
 
 190                 children.put(localName, ((T) node));
 
 192             node = node.addChild(index, localName, namespace, type, value, valueNs, appInfo);
 
 193         } else if (node instanceof LeafListHolderNode) {
 
 194             LeafNode child = ((LeafNode) ((HolderNode) node).child(index));
 
 195             node = (child != null ? child : node.addChild(index, localName,
 
 200             throw new SvcLogicException("Duplicate node exist with same node");