blob: 76098fbb96749b281212a2a06b30047d3e721716 [file] [log] [blame]
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301/*
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
17package org.onosproject.yangutils.linker.impl;
18
19import java.util.List;
20
21import org.onosproject.yangutils.datamodel.YangAugment;
22import org.onosproject.yangutils.datamodel.YangAugmentableNode;
23import org.onosproject.yangutils.datamodel.YangAugmentedInfo;
Bharat saraswalb551aae2016-07-14 15:18:20 +053024import org.onosproject.yangutils.datamodel.YangChoice;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangLeafList;
27import org.onosproject.yangutils.datamodel.YangLeavesHolder;
28import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.linker.exceptions.LinkerException;
30
31/**
32 * Represent utilities for YANG linker.
33 */
34public final class YangLinkerUtils {
35
36 private YangLinkerUtils() {
37 }
38
39 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +053040 * Detects collision between target nodes leaf/leaf-list or child node with augmented leaf/leaf-list or child node.
Bharat saraswalb1170bd2016-07-14 13:26:18 +053041 *
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 saraswalb551aae2016-07-14 15:18:20 +053049 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 saraswalb1170bd2016-07-14 13:26:18 +053058
Bharat saraswalb551aae2016-07-14 15:18:20 +053059 YangNode parent = targetNode;
60 if (targetNode instanceof YangAugment) {
61 parent = targetNode.getParent();
62 } else {
63 while (parent.getParent() != null) {
64 parent = parent.getParent();
Bharat saraswalb1170bd2016-07-14 13:26:18 +053065 }
66 }
Bharat saraswalb551aae2016-07-14 15:18:20 +053067 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 saraswalb1170bd2016-07-14 13:26:18 +053076 }
77 }
Bharat saraswalb551aae2016-07-14 15:18:20 +053078 } 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 saraswalb1170bd2016-07-14 13:26:18 +053088 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +053089 }
Bharat saraswalb551aae2016-07-14 15:18:20 +053090 } 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 saraswalb1170bd2016-07-14 13:26:18 +0530102 }
103 }
104 }
105
106 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530107 * Detects collision between target nodes and its all leaf/leaf-list or child node with augmented leaf/leaf-list or
108 * child node.
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530109 *
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}