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; |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangChoice; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 27 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 28 | import org.onosproject.yangutils.datamodel.YangNode; |
| 29 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
| 30 | |
| 31 | /** |
| 32 | * Represent utilities for YANG linker. |
| 33 | */ |
| 34 | public final class YangLinkerUtils { |
| 35 | |
| 36 | private YangLinkerUtils() { |
| 37 | } |
| 38 | |
| 39 | /** |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 40 | * Detects collision between target nodes leaf/leaf-list or child node with augmented leaf/leaf-list or child node. |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 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; |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 49 | if (targetNode instanceof YangChoice) { |
| 50 | if (augmentsLeavesHolder.getListOfLeaf() != null |
| 51 | || augmentsLeavesHolder.getListOfLeafList() != null) { |
| 52 | throw new LinkerException("target node " + targetNode.getName() |
| 53 | + "is a instance of choice. it can " + |
| 54 | "only be augmented with leaf using a case node."); |
| 55 | } |
| 56 | } else { |
| 57 | YangLeavesHolder targetNodesLeavesHolder = (YangLeavesHolder) targetNode; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 58 | |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 59 | YangNode parent = targetNode; |
| 60 | if (targetNode instanceof YangAugment) { |
| 61 | parent = targetNode.getParent(); |
| 62 | } else { |
| 63 | while (parent.getParent() != null) { |
| 64 | parent = parent.getParent(); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 65 | } |
| 66 | } |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 67 | if (augmentsLeavesHolder.getListOfLeaf() != null && augmentsLeavesHolder.getListOfLeaf().size() != 0 |
| 68 | && targetNodesLeavesHolder.getListOfLeaf() != null) { |
| 69 | for (YangLeaf leaf : augmentsLeavesHolder.getListOfLeaf()) { |
| 70 | for (YangLeaf targetLeaf : targetNodesLeavesHolder.getListOfLeaf()) { |
| 71 | if (targetLeaf.getName().equals(leaf.getName())) { |
| 72 | throw new LinkerException("target node " + targetNode.getName() |
| 73 | + " contains augmented leaf " + leaf.getName() + " in module " |
| 74 | + parent.getName()); |
| 75 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 76 | } |
| 77 | } |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 78 | } else if (augmentsLeavesHolder.getListOfLeafList() != null |
| 79 | && augmentsLeavesHolder.getListOfLeafList().size() != 0 |
| 80 | && targetNodesLeavesHolder.getListOfLeafList() != null) { |
| 81 | for (YangLeafList leafList : augmentsLeavesHolder.getListOfLeafList()) { |
| 82 | for (YangLeafList targetLeafList : targetNodesLeavesHolder.getListOfLeafList()) { |
| 83 | if (targetLeafList.getName().equals(leafList.getName())) { |
| 84 | throw new LinkerException("target node " + targetNode.getName() |
| 85 | + " contains augmented leaf-list" + leafList.getName() + " in module " |
| 86 | + parent.getName()); |
| 87 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 88 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 89 | } |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 90 | } else { |
| 91 | while (augmentsChild != null) { |
| 92 | while (targetNodesChild != null) { |
| 93 | if (targetNodesChild.getName().equals(augmentsChild.getName())) { |
| 94 | throw new LinkerException("target node " + targetNode.getName() |
| 95 | + " contains augmented child node" + augmentsChild.getName() + " in module " |
| 96 | + parent.getName()); |
| 97 | } |
| 98 | targetNodesChild = targetNodesChild.getNextSibling(); |
| 99 | } |
| 100 | augmentsChild = augmentsChild.getNextSibling(); |
| 101 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 107 | * Detects collision between target nodes and its all leaf/leaf-list or child node with augmented leaf/leaf-list or |
| 108 | * child node. |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 109 | * |
| 110 | * @param targetNode target node |
| 111 | * @param augment augment node |
| 112 | */ |
| 113 | public static void detectCollisionForAugmentedNode(YangNode targetNode, YangAugment augment) { |
| 114 | // Detect collision for target node and augment node. |
| 115 | detectCollision(targetNode, augment); |
| 116 | List<YangAugmentedInfo> yangAugmentedInfo = ((YangAugmentableNode) targetNode).getAugmentedInfoList(); |
| 117 | // Detect collision for target augment node and current augment node. |
| 118 | for (YangAugmentedInfo info : yangAugmentedInfo) { |
| 119 | detectCollision((YangAugment) info, augment); |
| 120 | } |
| 121 | } |
| 122 | } |