Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.linker.impl; |
| 18 | |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.onosproject.yangutils.datamodel.YangAugment; |
| 22 | import org.onosproject.yangutils.datamodel.YangAugmentableNode; |
| 23 | import org.onosproject.yangutils.datamodel.YangAugmentedInfo; |
| 24 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 25 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 27 | import org.onosproject.yangutils.datamodel.YangNode; |
| 28 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
| 29 | |
| 30 | /** |
| 31 | * Represent utilities for YANG linker. |
| 32 | */ |
| 33 | public final class YangLinkerUtils { |
| 34 | |
| 35 | private YangLinkerUtils() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Detects collision between target nodes leaf/leaf-list or child node with |
| 40 | * augmented leaf/leaf-list or child node. |
| 41 | * |
| 42 | * @param targetNode target node |
| 43 | * @param augment augment node |
| 44 | */ |
| 45 | private static void detectCollision(YangNode targetNode, YangAugment augment) { |
| 46 | YangNode targetNodesChild = targetNode.getChild(); |
| 47 | YangNode augmentsChild = augment.getChild(); |
| 48 | YangLeavesHolder augmentsLeavesHolder = augment; |
| 49 | YangLeavesHolder targetNodesLeavesHolder = (YangLeavesHolder) targetNode; |
| 50 | |
| 51 | YangNode parent = targetNode; |
| 52 | if (targetNode instanceof YangAugment) { |
| 53 | parent = targetNode.getParent(); |
| 54 | } else { |
| 55 | while (parent.getParent() != null) { |
| 56 | parent = parent.getParent(); |
| 57 | } |
| 58 | } |
| 59 | if (augmentsLeavesHolder.getListOfLeaf() != null && augmentsLeavesHolder.getListOfLeaf().size() != 0 |
| 60 | && targetNodesLeavesHolder.getListOfLeaf() != null) { |
| 61 | for (YangLeaf leaf : augmentsLeavesHolder.getListOfLeaf()) { |
| 62 | for (YangLeaf targetLeaf : targetNodesLeavesHolder.getListOfLeaf()) { |
| 63 | if (targetLeaf.getName().equals(leaf.getName())) { |
| 64 | throw new LinkerException("target node " + targetNode.getName() |
| 65 | + " contains augmented leaf " + leaf.getName() + " in module " |
| 66 | + parent.getName()); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } else if (augmentsLeavesHolder.getListOfLeafList() != null |
| 71 | && augmentsLeavesHolder.getListOfLeafList().size() != 0 |
| 72 | && targetNodesLeavesHolder.getListOfLeafList() != null) { |
| 73 | for (YangLeafList leafList : augmentsLeavesHolder.getListOfLeafList()) { |
| 74 | for (YangLeafList targetLeafList : targetNodesLeavesHolder.getListOfLeafList()) { |
| 75 | if (targetLeafList.getName().equals(leafList.getName())) { |
| 76 | throw new LinkerException("target node " + targetNode.getName() |
| 77 | + " contains augmented leaf-list" + leafList.getName() + " in module " |
| 78 | + parent.getName()); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } else { |
| 83 | while (augmentsChild != null) { |
| 84 | while (targetNodesChild != null) { |
| 85 | if (targetNodesChild.getName().equals(augmentsChild.getName())) { |
| 86 | throw new LinkerException("target node " + targetNode.getName() |
| 87 | + " contains augmented child node" + augmentsChild.getName() + " in module " |
| 88 | + parent.getName()); |
| 89 | } |
| 90 | targetNodesChild = targetNodesChild.getNextSibling(); |
| 91 | } |
| 92 | augmentsChild = augmentsChild.getNextSibling(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Detects collision between target nodes and its all leaf/leaf-list or child node with |
| 99 | * augmented leaf/leaf-list or child node. |
| 100 | * |
| 101 | * @param targetNode target node |
| 102 | * @param augment augment node |
| 103 | */ |
| 104 | public static void detectCollisionForAugmentedNode(YangNode targetNode, YangAugment augment) { |
| 105 | // Detect collision for target node and augment node. |
| 106 | detectCollision(targetNode, augment); |
| 107 | List<YangAugmentedInfo> yangAugmentedInfo = ((YangAugmentableNode) targetNode).getAugmentedInfoList(); |
| 108 | // Detect collision for target augment node and current augment node. |
| 109 | for (YangAugmentedInfo info : yangAugmentedInfo) { |
| 110 | detectCollision((YangAugment) info, augment); |
| 111 | } |
| 112 | } |
| 113 | } |