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 an entity that represents an inner node to properties data
 
  44  * @param <T> type of child
 
  46 public abstract class InnerNode<T extends NodeChild> extends PropertiesNode {
 
  48     private Map<String, T> children = new HashMap<String, T>();
 
  50     protected InnerNode(String name, Namespace namespace, String uri,
 
  51                         PropertiesNode parent, Object appInfo, NodeType nodeType) {
 
  52         super(name, namespace, uri, parent, appInfo, nodeType);
 
  60     public Map<String, T> children() {
 
  67      * @param children child nodes
 
  69     public void children(Map<String, T> children) {
 
  70         this.children = children;
 
  74     public PropertiesNode addChild(String name, Namespace namespace,
 
  76                                    Object appInfo) throws SvcLogicException {
 
  77         PropertiesNode node = ((PropertiesNode) children.get(name));
 
  82         // get augment schema, if it is augmented node
 
  83         AugmentationSchemaNode augSchema = null;
 
  84         if (((DataSchemaNode) appInfo).isAugmenting()) {
 
  85             augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()), ((DataSchemaNode) appInfo));
 
  86             node = getAugmentationNode(augSchema, this, name);
 
  89         // create node based on type
 
  91             String uri = getUri(this, name, namespace);
 
  92             node = createNode(name, namespace, uri, this, appInfo, type);
 
  95         // If namespace is not same as parent then it is augmented node
 
  96         if (augSchema != null && !isNamespaceAsParent(this, node)) {
 
  97             addToAugmentations(augSchema, this, node);
 
  99             children.put(name, ((T) node));
 
 105     public PropertiesNode addChild(String name, Namespace namespace,
 
 106                                    NodeType type, String value,
 
 108                                    Object appInfo) throws SvcLogicException {
 
 109         LeafNode node = ((LeafNode) children.get(name));
 
 114         AugmentationSchemaNode augSchema = null;
 
 115         if (((DataSchemaNode) appInfo).isAugmenting()) {
 
 116             augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
 
 117                                                  ((DataSchemaNode) appInfo));
 
 120         String uri = getUri(this, name, namespace);
 
 121         node = new LeafNode(name, namespace, uri, this,
 
 122                             appInfo, type, value);
 
 124         if (augSchema != null && !isNamespaceAsParent(this, node)) {
 
 125             addToAugmentations(augSchema, this, node);
 
 127             children.put(name, ((T) node));
 
 133     public PropertiesNode addChild(String index, String name,
 
 134                                    Namespace namespace, NodeType type,
 
 135                                    Object appInfo) throws SvcLogicException {
 
 136         String localname = resolveName(name);
 
 137         PropertiesNode node = ((PropertiesNode) children.get(localname));
 
 140             AugmentationSchemaNode augSchema = null;
 
 141             if (((DataSchemaNode) appInfo).isAugmenting()) {
 
 142                 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
 
 143                                                      ((DataSchemaNode) appInfo));
 
 144                 node = getAugmentationNode(augSchema, this, localname);
 
 148                 String uri = getUri(this, name, namespace);
 
 149                 node = new ListHolderNode(localname, namespace, uri,
 
 150                                           this, appInfo, MULTI_INSTANCE_HOLDER_NODE);
 
 153             if (augSchema != null && !isNamespaceAsParent(this, node)) {
 
 154                 addToAugmentations(augSchema, this, node);
 
 156                 children.put(localname, ((T) node));
 
 159             node = node.addChild(index, localname, namespace, type, appInfo);
 
 160         } else if (node instanceof ListHolderNode) {
 
 161             ListHolderChild child = ((ListHolderNode) node).child(index);
 
 162             node = (child != null ? ((MultiInstanceNode) child) :
 
 163                     node.addChild(index, localname, namespace, type, appInfo));
 
 165             throw new SvcLogicException("Duplicate node exist with same node");
 
 171     public PropertiesNode addChild(String index, String name,
 
 172                                    Namespace namespace, NodeType type,
 
 173                                    String value, Namespace valueNs,
 
 174                                    Object appInfo) throws SvcLogicException {
 
 175         String localName = resolveName(name);
 
 176         PropertiesNode node = ((PropertiesNode) children.get(localName));
 
 180             AugmentationSchemaNode augSchema = null;
 
 181             if (((DataSchemaNode) appInfo).isAugmenting()) {
 
 182                 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
 
 183                                                      ((DataSchemaNode) appInfo));
 
 184                 node = getAugmentationNode(augSchema, this, localName);
 
 188                 String uri = getUri(this, name, namespace);
 
 189                 node = new LeafListHolderNode(localName, namespace, uri, this,
 
 190                                               appInfo, MULTI_INSTANCE_LEAF_HOLDER_NODE);
 
 193             if (augSchema != null && !isNamespaceAsParent(this, node)) {
 
 194                 addToAugmentations(augSchema, this, node);
 
 196                 children.put(localName, ((T) node));
 
 199             node = node.addChild(index, localName, namespace, type, value, null, appInfo);
 
 200         } else if (node instanceof LeafListHolderNode) {
 
 201             LeafNode child = ((LeafNode) ((HolderNode) node).child(index));
 
 202             node = (child != null ? child : node.addChild(index, localName,
 
 207             throw new SvcLogicException("Duplicate node exist with same node");