[ONOS-3898/4069] Yang Utils Extension to support RPC/Notification/Choice/Case/Union
Change-Id: I405852caff3464719e8e586fa8e9ae9b6ed043ff
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangInput.java b/src/main/java/org/onosproject/yangutils/datamodel/YangInput.java
new file mode 100644
index 0000000..10a095b
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangInput.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.datamodel;
+
+import java.util.LinkedList;
+import java.util.List;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import org.onosproject.yangutils.utils.YangConstructType;
+
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
+
+/*
+ * Reference RFC 6020.
+ *
+ * The "input" statement, which is optional, is used to define input
+ * parameters to the RPC operation. It does not take an argument. The
+ * substatements to "input" define nodes under the RPC's input node.
+ *
+ * If a leaf in the input tree has a "mandatory" statement with the
+ * value "true", the leaf MUST be present in a NETCONF RPC invocation.
+ * Otherwise, the server MUST return a "missing-element" error.
+ *
+ * If a leaf in the input tree has a default value, the NETCONF server
+ * MUST use this value in the same cases as described in Section 7.6.1.
+ * In these cases, the server MUST operationally behave as if the leaf
+ * was present in the NETCONF RPC invocation with the default value as
+ * its value.
+ *
+ * If a "config" statement is present for any node in the input tree,
+ * the "config" statement is ignored.
+ *
+ * If any node has a "when" statement that would evaluate to false, then
+ * this node MUST NOT be present in the input tree.
+ *
+ * The input substatements
+ *
+ * +--------------+---------+-------------+------------------+
+ * | substatement | section | cardinality |data model mapping|
+ * +--------------+---------+-------------+------------------+
+ * | anyxml | 7.10 | 0..n | -not supported |
+ * | choice | 7.9 | 0..n | -child nodes |
+ * | container | 7.5 | 0..n | -child nodes |
+ * | grouping | 7.11 | 0..n | -child nodes |
+ * | leaf | 7.6 | 0..n | -YangLeaf |
+ * | leaf-list | 7.7 | 0..n | -YangLeafList |
+ * | list | 7.8 | 0..n | -child nodes |
+ * | typedef | 7.3 | 0..n | -child nodes |
+ * | uses | 7.12 | 0..n | -child nodes |
+ * +--------------+---------+-------------+------------------+
+ */
+
+/**
+ * Data model node to maintain information defined in YANG input.
+ */
+public class YangInput extends YangNode implements YangLeavesHolder, Parsable, CollisionDetector {
+
+ /**
+ * Name of the input.
+ */
+ private String name;
+
+ /**
+ * List of leaves contained.
+ */
+ private List<YangLeaf> listOfLeaf;
+
+ /**
+ * List of leaf-lists contained.
+ */
+ private List<YangLeafList> listOfLeafList;
+
+ /**
+ * Create a rpc input node.
+ */
+ public YangInput() {
+ super(YangNodeType.INPUT_NODE);
+ listOfLeaf = new LinkedList<YangLeaf>();
+ listOfLeafList = new LinkedList<YangLeafList>();
+ }
+
+ @Override
+ public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
+ // Detect colliding child.
+ detectCollidingChildUtil(identifierName, dataType, this);
+ }
+
+ @Override
+ public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
+ if (this.getName().equals(identifierName)) {
+ throw new DataModelException("YANG file error: Duplicate input identifier detected, same as input \""
+ + this.getName() + "\"");
+ }
+ }
+
+ @Override
+ public YangConstructType getYangConstructType() {
+ return YangConstructType.INPUT_DATA;
+ }
+
+ @Override
+ public void validateDataOnEntry() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public void validateDataOnExit() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public List<YangLeaf> getListOfLeaf() {
+ return listOfLeaf;
+ }
+
+ @Override
+ public void addLeaf(YangLeaf leaf) {
+ getListOfLeaf().add(leaf);
+ }
+
+ @Override
+ public List<YangLeafList> getListOfLeafList() {
+ return listOfLeafList;
+ }
+
+ @Override
+ public void addLeafList(YangLeafList leafList) {
+ getListOfLeafList().add(leafList);
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangNodeType.java b/src/main/java/org/onosproject/yangutils/datamodel/YangNodeType.java
index f3798f8..4716065 100644
--- a/src/main/java/org/onosproject/yangutils/datamodel/YangNodeType.java
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangNodeType.java
@@ -75,6 +75,26 @@
CONTAINER_NODE,
/**
+ * Node contains "YANG's notification" information.
+ */
+ NOTIFICATION_NODE,
+
+ /**
+ * Node contains "YANG's input" information.
+ */
+ INPUT_NODE,
+
+ /**
+ * Node contains "YANG's output" information.
+ */
+ OUTPUT_NODE,
+
+ /**
+ * Node contains "YANG's rpc" information.
+ */
+ RPC_NODE,
+
+ /**
* Node contains "YANG's list" information.
*/
LIST_NODE
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangNotification.java b/src/main/java/org/onosproject/yangutils/datamodel/YangNotification.java
new file mode 100644
index 0000000..73c7ddc
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangNotification.java
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.datamodel;
+
+import java.util.LinkedList;
+import java.util.List;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import org.onosproject.yangutils.utils.YangConstructType;
+
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
+
+/*
+ * Reference RFC 6020.
+ *
+ * YANG allows the definition of notifications suitable for NETCONF.
+ * YANG data definition statements are used to model the content of the
+ * notification.
+ *
+ * The "notification" statement is used to define a NETCONF
+ * notification. It takes one argument, which is an identifier,
+ * followed by a block of substatements that holds detailed notification
+ * information. The "notification" statement defines a notification
+ * node in the schema tree.
+ *
+ * If a leaf in the notification tree has a "mandatory" statement with
+ * the value "true", the leaf MUST be present in a NETCONF notification.
+ *
+ * If a leaf in the notification tree has a default value, the NETCONF
+ * client MUST use this value in the same cases as described in
+ * Section 7.6.1. In these cases, the client MUST operationally behave
+ * as if the leaf was present in the NETCONF notification with the
+ * default value as its value.
+ *
+ * If a "config" statement is present for any node in the notification
+ * tree, the "config" statement is ignored.
+ *
+ * The notification's substatements
+ *
+ * +--------------+---------+-------------+------------------+
+ * | substatement | section | cardinality |data model mapping|
+ * +--------------+---------+-------------+------------------+
+ * | anyxml | 7.10 | 0..n | -not supported |
+ * | choice | 7.9 | 0..n | -child nodes |
+ * | container | 7.5 | 0..n | -child nodes |
+ * | description | 7.19.3 | 0..1 | -string |
+ * | grouping | 7.11 | 0..n | -child nodes |
+ * | if-feature | 7.18.2 | 0..n | -TODO |
+ * | leaf | 7.6 | 0..n | -YangLeaf |
+ * | leaf-list | 7.7 | 0..n | -YangLeafList |
+ * | list | 7.8 | 0..n | -child nodes |
+ * | reference | 7.19.4 | 0..1 | -string |
+ * | status | 7.19.2 | 0..1 | -YangStatus |
+ * | typedef | 7.3 | 0..n | -child nodes |
+ * | uses | 7.12 | 0..n | -child nodes |
+ * +--------------+---------+-------------+------------------+
+ */
+
+/**
+ * Data model node to maintain information defined in YANG notification.
+ */
+public class YangNotification extends YangNode implements YangLeavesHolder, YangCommonInfo, Parsable,
+ CollisionDetector {
+
+ /**
+ * Name of the notification.
+ */
+ private String name;
+
+ /**
+ * Description of notification.
+ */
+ private String description;
+
+ /**
+ * List of leaves contained.
+ */
+ private List<YangLeaf> listOfLeaf;
+
+ /**
+ * List of leaf-lists contained.
+ */
+ private List<YangLeafList> listOfLeafList;
+
+ /**
+ * Reference of the module.
+ */
+ private String reference;
+
+ /**
+ * Status of the node.
+ */
+ private YangStatusType status = YangStatusType.CURRENT;
+
+ /**
+ * Create a notification node.
+ */
+ public YangNotification() {
+ super(YangNodeType.NOTIFICATION_NODE);
+ listOfLeaf = new LinkedList<YangLeaf>();
+ listOfLeafList = new LinkedList<YangLeafList>();
+ }
+
+ @Override
+ public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
+ // Detect colliding child.
+ detectCollidingChildUtil(identifierName, dataType, this);
+ }
+
+ @Override
+ public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
+ if (this.getName().equals(identifierName)) {
+ throw new DataModelException("YANG file error: Duplicate input identifier detected, same as notification \""
+ + this.getName() + "\"");
+ }
+ }
+
+ @Override
+ public YangConstructType getYangConstructType() {
+ return YangConstructType.NOTIFICATION_DATA;
+ }
+
+ @Override
+ public void validateDataOnEntry() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public void validateDataOnExit() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public String getDescription() {
+ return description;
+ }
+
+ @Override
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ @Override
+ public List<YangLeaf> getListOfLeaf() {
+ return listOfLeaf;
+ }
+
+ @Override
+ public void addLeaf(YangLeaf leaf) {
+ getListOfLeaf().add(leaf);
+ }
+
+ @Override
+ public List<YangLeafList> getListOfLeafList() {
+ return listOfLeafList;
+ }
+
+ @Override
+ public void addLeafList(YangLeafList leafList) {
+ getListOfLeafList().add(leafList);
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String getReference() {
+ return reference;
+ }
+
+ @Override
+ public void setReference(String reference) {
+ this.reference = reference;
+ }
+
+ @Override
+ public YangStatusType getStatus() {
+ return status;
+ }
+
+ @Override
+ public void setStatus(YangStatusType status) {
+ this.status = status;
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangOutput.java b/src/main/java/org/onosproject/yangutils/datamodel/YangOutput.java
new file mode 100644
index 0000000..94ec601
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangOutput.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.datamodel;
+
+import java.util.LinkedList;
+import java.util.List;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import org.onosproject.yangutils.utils.YangConstructType;
+
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
+
+/*
+ * Reference RFC 6020.
+ *
+ * The "output" statement, which is optional, is used to define output
+ * parameters to the RPC operation. It does not take an argument. The
+ * substatements to "output" define nodes under the RPC's output node.
+ *
+ * If a leaf in the output tree has a "mandatory" statement with the
+ * value "true", the leaf MUST be present in a NETCONF RPC reply.
+ *
+ * If a leaf in the output tree has a default value, the NETCONF client
+ * MUST use this value in the same cases as described in Section 7.6.1.
+ * In these cases, the client MUST operationally behave as if the leaf
+ * was present in the NETCONF RPC reply with the default value as its
+ * value.
+ *
+ * If a "config" statement is present for any node in the output tree,
+ * the "config" statement is ignored.
+ *
+ * If any node has a "when" statement that would evaluate to false, then
+ * this node MUST NOT be present in the output tree.
+ *
+ * The output substatements
+ *
+ * +--------------+---------+-------------+------------------+
+ * | substatement | section | cardinality |data model mapping|
+ * +--------------+---------+-------------+------------------+
+ * | anyxml | 7.10 | 0..n | -not supported |
+ * | choice | 7.9 | 0..n | -child nodes |
+ * | container | 7.5 | 0..n | -child nodes |
+ * | grouping | 7.11 | 0..n | -child nodes |
+ * | leaf | 7.6 | 0..n | -YangLeaf |
+ * | leaf-list | 7.7 | 0..n | -YangLeafList |
+ * | list | 7.8 | 0..n | -child nodes |
+ * | typedef | 7.3 | 0..n | -child nodes |
+ * | uses | 7.12 | 0..n | -child nodes |
+ * +--------------+---------+-------------+------------------+
+ */
+
+/**
+ * Data model node to maintain information defined in YANG output.
+ */
+public class YangOutput extends YangNode implements YangLeavesHolder, Parsable, CollisionDetector {
+
+ /**
+ * Name of the output.
+ */
+ private String name;
+
+ /**
+ * List of leaves contained.
+ */
+ private List<YangLeaf> listOfLeaf;
+
+ /**
+ * List of leaf-lists contained.
+ */
+ private List<YangLeafList> listOfLeafList;
+
+ /**
+ * Create a rpc output node.
+ */
+ public YangOutput() {
+ super(YangNodeType.OUTPUT_NODE);
+ listOfLeaf = new LinkedList<YangLeaf>();
+ listOfLeafList = new LinkedList<YangLeafList>();
+ }
+
+ @Override
+ public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
+ // Detect colliding child.
+ detectCollidingChildUtil(identifierName, dataType, this);
+ }
+
+ @Override
+ public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
+ if (this.getName().equals(identifierName)) {
+ throw new DataModelException("YANG file error: Duplicate identifier detected, same as output \""
+ + this.getName() + "\"");
+ }
+ }
+
+ @Override
+ public YangConstructType getYangConstructType() {
+ return YangConstructType.OUTPUT_DATA;
+ }
+
+ @Override
+ public void validateDataOnEntry() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public void validateDataOnExit() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public List<YangLeaf> getListOfLeaf() {
+ return listOfLeaf;
+ }
+
+ @Override
+ public void addLeaf(YangLeaf leaf) {
+ getListOfLeaf().add(leaf);
+ }
+
+ @Override
+ public List<YangLeafList> getListOfLeafList() {
+ return listOfLeafList;
+ }
+
+ @Override
+ public void addLeafList(YangLeafList leafList) {
+ getListOfLeafList().add(leafList);
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangRpc.java b/src/main/java/org/onosproject/yangutils/datamodel/YangRpc.java
new file mode 100644
index 0000000..fd6b3be
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangRpc.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.datamodel;
+
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import org.onosproject.yangutils.utils.YangConstructType;
+
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
+
+/*
+ * Reference RFC 6020.
+ *
+ * The "rpc" statement is used to define a NETCONF RPC operation. It
+ * takes one argument, which is an identifier, followed by a block of
+ * substatements that holds detailed rpc information. This argument is
+ * the name of the RPC, and is used as the element name directly under
+ * the <rpc> element, as designated by the substitution group
+ * "rpcOperation" in [RFC4741].
+ *
+ * The "rpc" statement defines an rpc node in the schema tree. Under
+ * the rpc node, a schema node with the name "input", and a schema node
+ * with the name "output" are also defined. The nodes "input" and
+ * "output" are defined in the module's namespace.
+ *
+ * The rpc substatements
+ *
+ * +--------------+---------+-------------+------------------+
+ * | substatement | section | cardinality |data model mapping|
+ * +--------------+---------+-------------+------------------+
+ * | description | 7.19.3 | 0..1 | -string |
+ * | grouping | 7.11 | 0..n | -child nodes |
+ * | if-feature | 7.18.2 | 0..n | -TODO |
+ * | input | 7.13.2 | 0..1 | -child nodes |
+ * | output | 7.13.3 | 0..1 | -child nodes |
+ * | reference | 7.19.4 | 0..1 | -string |
+ * | status | 7.19.2 | 0..1 | -YangStatus |
+ * | typedef | 7.3 | 0..n | -child nodes |
+ * +--------------+---------+-------------+------------------+
+ */
+
+/**
+ * Data model node to maintain information defined in YANG rpc.
+ */
+public class YangRpc extends YangNode implements YangCommonInfo, Parsable,
+ CollisionDetector {
+
+ /**
+ * Name of the rpc.
+ */
+ private String name;
+
+ /**
+ * Description of rpc.
+ */
+ private String description;
+
+ /**
+ * Reference of the module.
+ */
+ private String reference;
+
+ /**
+ * Status of the node.
+ */
+ private YangStatusType status = YangStatusType.CURRENT;
+
+ /**
+ * Create a rpc node.
+ */
+ public YangRpc() {
+ super(YangNodeType.RPC_NODE);
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
+ // Detect colliding child.
+ detectCollidingChildUtil(identifierName, dataType, this);
+ }
+
+ @Override
+ public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
+ if (this.getName().equals(identifierName)) {
+ throw new DataModelException("YANG file error: Duplicate input identifier detected, same as rpc \""
+ + this.getName() + "\"");
+ }
+ }
+
+ @Override
+ public YangConstructType getYangConstructType() {
+ return YangConstructType.RPC_DATA;
+ }
+
+ @Override
+ public void validateDataOnEntry() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public void validateDataOnExit() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public String getDescription() {
+ return description;
+ }
+
+ @Override
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ @Override
+ public String getReference() {
+ return reference;
+ }
+
+ @Override
+ public void setReference(String reference) {
+ this.reference = reference;
+ }
+
+ @Override
+ public YangStatusType getStatus() {
+ return status;
+ }
+
+ @Override
+ public void setStatus(YangStatusType status) {
+ this.status = status;
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java b/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java
new file mode 100644
index 0000000..c61ce80
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.datamodel;
+
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import org.onosproject.yangutils.utils.YangConstructType;
+
+import java.util.List;
+
+/*
+ * Reference RFC 6020.
+ *
+ * The union built-in type represents a value that corresponds to one of
+ * its member types.
+ *
+ * When the type is "union", the "type" statement (Section 7.4) MUST be
+ * present. It is used to repeatedly specify each member type of the
+ * union. It takes as an argument a string that is the name of a member
+ * type.
+ *
+ * A member type can be of any built-in or derived type, except it MUST
+ * NOT be one of the built-in types "empty" or "leafref".
+ *
+ * When a string representing a union data type is validated, the string
+ * is validated against each member type, in the order they are
+ * specified in the "type" statement, until a match is found.
+ *
+ * Any default value or "units" property defined in the member types is
+ * not inherited by the union type.
+ */
+
+/**
+ * Data model node to maintain information defined in YANG union.
+ */
+public class YangUnion implements Parsable {
+
+ // List of YANG type.
+ private List<YangType<?>> typeList;
+
+ // Name of the union.
+ private String unionName;
+
+ /**
+ * Create a YANG union node.
+ */
+ public YangUnion() {
+ }
+
+ /**
+ * Returns list of YANG type.
+ *
+ * @return the list of YANG type
+ */
+ public List<YangType<?>> getTypeList() {
+ return typeList;
+ }
+
+ /**
+ * Returns union name.
+ *
+ * @return the union name
+ */
+ public String getUnionName() {
+ return unionName;
+ }
+
+ /**
+ * Set the list of YANG type.
+ *
+ * @param typeList list of YANG type.
+ */
+ public void setTypeList(List<YangType<?>> typeList) {
+ this.typeList = typeList;
+ }
+
+ /**
+ * Set the union name.
+ *
+ * @param unionName name of the union.
+ */
+ public void setUnionName(String unionName) {
+ this.unionName = unionName;
+ }
+
+ @Override
+ public YangConstructType getYangConstructType() {
+ return YangConstructType.UNION_DATA;
+ }
+
+ @Override
+ public void validateDataOnEntry() throws DataModelException {
+ //TODO: implement the method.
+ }
+
+ @Override
+ public void validateDataOnExit() throws DataModelException {
+ //TODO: implement the method.
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/TreeWalkListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/TreeWalkListener.java
index 11ed0e4..2436908 100644
--- a/src/main/java/org/onosproject/yangutils/parser/impl/TreeWalkListener.java
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/TreeWalkListener.java
@@ -29,6 +29,8 @@
import org.onosproject.yangutils.parser.impl.listeners.BelongsToListener;
import org.onosproject.yangutils.parser.impl.listeners.BitListener;
import org.onosproject.yangutils.parser.impl.listeners.BitsListener;
+import org.onosproject.yangutils.parser.impl.listeners.CaseListener;
+import org.onosproject.yangutils.parser.impl.listeners.ChoiceListener;
import org.onosproject.yangutils.parser.impl.listeners.ConfigListener;
import org.onosproject.yangutils.parser.impl.listeners.ContactListener;
import org.onosproject.yangutils.parser.impl.listeners.ContainerListener;
@@ -54,6 +56,7 @@
import org.onosproject.yangutils.parser.impl.listeners.ReferenceListener;
import org.onosproject.yangutils.parser.impl.listeners.RevisionDateListener;
import org.onosproject.yangutils.parser.impl.listeners.RevisionListener;
+import org.onosproject.yangutils.parser.impl.listeners.ShortCaseListener;
import org.onosproject.yangutils.parser.impl.listeners.StatusListener;
import org.onosproject.yangutils.parser.impl.listeners.SubModuleListener;
import org.onosproject.yangutils.parser.impl.listeners.TypeDefListener;
@@ -914,32 +917,32 @@
@Override
public void enterChoiceStatement(GeneratedYangParser.ChoiceStatementContext ctx) {
- // TODO: implement the method.
+ ChoiceListener.processChoiceEntry(this, ctx);
}
@Override
public void exitChoiceStatement(GeneratedYangParser.ChoiceStatementContext ctx) {
- // TODO: implement the method.
+ ChoiceListener.processChoiceExit(this, ctx);
}
@Override
public void enterShortCaseStatement(GeneratedYangParser.ShortCaseStatementContext ctx) {
- // TODO: implement the method.
+ ShortCaseListener.processShortCaseEntry(this, ctx);
}
@Override
public void exitShortCaseStatement(GeneratedYangParser.ShortCaseStatementContext ctx) {
- // TODO: implement the method.
+ ShortCaseListener.processShortCaseExit(this, ctx);
}
@Override
public void enterCaseStatement(GeneratedYangParser.CaseStatementContext ctx) {
- // TODO: implement the method.
+ CaseListener.processCaseEntry(this, ctx);
}
@Override
public void exitCaseStatement(GeneratedYangParser.CaseStatementContext ctx) {
- // TODO: implement the method.
+ CaseListener.processCaseExit(this, ctx);
}
@Override
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/CaseListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/CaseListener.java
new file mode 100644
index 0000000..b98bd12
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/CaseListener.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2014-2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.onosproject.yangutils.datamodel.YangCase;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+
+import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
+import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangCaseNode;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
+import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.WHEN_DATA;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ * case-stmt = case-keyword sep identifier-arg-str optsep
+ * (";" /
+ * "{" stmtsep
+ * ;; these stmts can appear in any order
+ * [when-stmt stmtsep]
+ * *(if-feature-stmt stmtsep)
+ * [status-stmt stmtsep]
+ * [description-stmt stmtsep]
+ * [reference-stmt stmtsep]
+ * *(data-def-stmt stmtsep)
+ * "}")
+ *
+ * ANTLR grammar rule
+ * caseStatement : CASE_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement
+ * | statusStatement | descriptionStatement | referenceStatement | dataDefStatement)* RIGHT_CURLY_BRACE);
+ */
+
+/**
+ * Implements listener based call back function corresponding to the "case" rule
+ * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class CaseListener {
+
+ /**
+ * Create a new case listener.
+ */
+ private CaseListener() {
+ }
+
+ /**
+ * It is called when parser enters grammar rule (case), it perform
+ * validations and updates the data model tree.
+ *
+ * @param listener listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processCaseEntry(TreeWalkListener listener,
+ GeneratedYangParser.CaseStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, CASE_DATA, ctx.identifier().getText(), ENTRY);
+
+ // Check validity of identifier and remove double quotes.
+ String identifier = getValidIdentifier(ctx.identifier().getText(), CASE_DATA, ctx);
+
+ // Validate sub statement cardinality.
+ validateSubStatementsCardinality(ctx);
+
+ Parsable curData = listener.getParsedDataStack().peek();
+
+ // Check for identifier collision
+ int line = ctx.getStart().getLine();
+ int charPositionInLine = ctx.getStart().getCharPositionInLine();
+ detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CASE_DATA);
+
+ if (curData instanceof YangChoice) {
+ YangCase caseNode = getYangCaseNode(JAVA_GENERATION);
+ caseNode.setName(identifier);
+ YangNode curNode = (YangNode) curData;
+ try {
+ curNode.addChild(caseNode);
+ } catch (DataModelException e) {
+ throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
+ CASE_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
+ }
+ listener.getParsedDataStack().push(caseNode);
+ } else {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CASE_DATA,
+ ctx.identifier().getText(), ENTRY));
+ }
+ }
+
+ /**
+ * It is called when parser exits from grammar rule (case), it perform
+ * validations and update the data model tree.
+ *
+ * @param listener Listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processCaseExit(TreeWalkListener listener,
+ GeneratedYangParser.CaseStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, CASE_DATA, ctx.identifier().getText(), EXIT);
+
+ if (listener.getParsedDataStack().peek() instanceof YangCase) {
+ listener.getParsedDataStack().pop();
+ } else {
+ throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CASE_DATA,
+ ctx.identifier().getText(), EXIT));
+ }
+ }
+
+ /**
+ * Validates the cardinality of case sub-statements as per grammar.
+ *
+ * @param ctx context object of the grammar rule
+ */
+ private static void validateSubStatementsCardinality(GeneratedYangParser.CaseStatementContext ctx) {
+
+ validateCardinalityMaxOne(ctx.whenStatement(), WHEN_DATA, CASE_DATA, ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, CASE_DATA, ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, CASE_DATA, ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, CASE_DATA, ctx.identifier().getText());
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListener.java
new file mode 100644
index 0000000..ddac61b
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListener.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2014-2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.onosproject.yangutils.datamodel.YangAugment;
+import org.onosproject.yangutils.datamodel.YangCase;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangGrouping;
+import org.onosproject.yangutils.datamodel.YangInput;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNotification;
+import org.onosproject.yangutils.datamodel.YangOutput;
+import org.onosproject.yangutils.datamodel.YangSubModule;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+
+import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
+import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangChoiceNode;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
+import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.CHOICE_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.DEFAULT_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.MANDATORY_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.SHORT_CASE_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.WHEN_DATA;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ * choice-stmt = choice-keyword sep identifier-arg-str optsep
+ * (";" /
+ * "{" stmtsep
+ * ;; these stmts can appear in any order
+ * [when-stmt stmtsep]
+ * *(if-feature-stmt stmtsep)
+ * [default-stmt stmtsep]
+ * [config-stmt stmtsep]
+ * [mandatory-stmt stmtsep]
+ * [status-stmt stmtsep]
+ * [description-stmt stmtsep]
+ * [reference-stmt stmtsep]
+ * *((short-case-stmt / case-stmt) stmtsep)
+ * "}")
+ *
+ * ANTLR grammar rule
+ * choiceStatement : CHOICE_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement
+ * | defaultStatement | configStatement | mandatoryStatement | statusStatement | descriptionStatement
+ * | referenceStatement | shortCaseStatement | caseStatement)* RIGHT_CURLY_BRACE);
+ */
+
+/**
+ * Implements listener based call back function corresponding to the "choice"
+ * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class ChoiceListener {
+
+ /**
+ * Create a new choice listener.
+ */
+ private ChoiceListener() {
+ }
+
+ /**
+ * It is called when parser receives an input matching the grammar rule
+ * (choice), perform validations and update the data model tree.
+ *
+ * @param listener Listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processChoiceEntry(TreeWalkListener listener,
+ GeneratedYangParser.ChoiceStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, CHOICE_DATA, ctx.identifier().getText(), ENTRY);
+
+ // Check validity of identifier and remove double quotes.
+ String identifier = getValidIdentifier(ctx.identifier().getText(), CHOICE_DATA, ctx);
+
+ // Validate sub statement cardinality.
+ validateSubStatementsCardinality(ctx);
+
+ Parsable curData = listener.getParsedDataStack().peek();
+
+ // Check for identifier collision
+ int line = ctx.getStart().getLine();
+ int charPositionInLine = ctx.getStart().getCharPositionInLine();
+ detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CHOICE_DATA);
+
+ if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangContainer
+ || curData instanceof YangList || curData instanceof YangCase || curData instanceof YangGrouping
+ || curData instanceof YangAugment || curData instanceof YangInput || curData instanceof YangOutput
+ || curData instanceof YangNotification) {
+
+ YangChoice choiceNode = getYangChoiceNode(JAVA_GENERATION);
+ choiceNode.setName(identifier);
+
+ YangNode curNode = (YangNode) curData;
+ try {
+ curNode.addChild(choiceNode);
+ } catch (DataModelException e) {
+ throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
+ CHOICE_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
+ }
+ listener.getParsedDataStack().push(choiceNode);
+ } else {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
+ CHOICE_DATA, ctx.identifier().getText(), ENTRY));
+ }
+ }
+
+ /**
+ * It is called when parser exits from grammar rule (choice), it perform
+ * validations and update the data model tree.
+ *
+ * @param listener Listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processChoiceExit(TreeWalkListener listener,
+ GeneratedYangParser.ChoiceStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, CHOICE_DATA, ctx.identifier().getText(), EXIT);
+
+ if (listener.getParsedDataStack().peek() instanceof YangChoice) {
+ listener.getParsedDataStack().pop();
+ } else {
+ throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CHOICE_DATA,
+ ctx.identifier().getText(), EXIT));
+ }
+ }
+
+ /**
+ * Validates the cardinality of choice sub-statements as per grammar.
+ *
+ * @param ctx context object of the grammar rule.
+ */
+ private static void validateSubStatementsCardinality(GeneratedYangParser.ChoiceStatementContext ctx) {
+
+ validateCardinalityMaxOne(ctx.whenStatement(), WHEN_DATA, CHOICE_DATA, ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.defaultStatement(), DEFAULT_DATA, CHOICE_DATA, ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, CHOICE_DATA, ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.mandatoryStatement(), MANDATORY_DATA, CHOICE_DATA, ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, CHOICE_DATA, ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, CHOICE_DATA,
+ ctx.identifier().getText());
+ validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, CHOICE_DATA, ctx.identifier().getText());
+ validateMutuallyExclusiveChilds(ctx.shortCaseStatement(), SHORT_CASE_DATA, ctx.caseStatement(), CASE_DATA,
+ CHOICE_DATA, ctx.identifier().getText());
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ShortCaseListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ShortCaseListener.java
new file mode 100644
index 0000000..c3b0161
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ShortCaseListener.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.antlr.v4.runtime.ParserRuleContext;
+import org.antlr.v4.runtime.tree.ParseTree;
+import org.onosproject.yangutils.datamodel.YangCase;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+
+import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
+import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangCaseNode;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CHILD;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
+import static org.onosproject.yangutils.utils.YangConstructType.SHORT_CASE_DATA;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ * short-case-stmt = container-stmt /
+ * leaf-stmt /
+ * leaf-list-stmt /
+ * list-stmt /
+ * anyxml-stmt
+ *
+ * ANTLR grammar rule
+ * shortCaseStatement : containerStatement | leafStatement | leafListStatement | listStatement;
+ */
+
+/**
+ * Implements listener based call back function corresponding to the "short
+ * case" rule defined in ANTLR grammar file for corresponding ABNF rule in RFC
+ * 6020.
+ */
+public final class ShortCaseListener {
+
+ /**
+ * Create a new short case listener.
+ */
+ private ShortCaseListener() {
+ }
+
+ /**
+ * It is called when parser enters grammar rule (short case), it perform
+ * validations and updates the data model tree.
+ *
+ * @param listener listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processShortCaseEntry(TreeWalkListener listener,
+ GeneratedYangParser.ShortCaseStatementContext ctx) {
+
+ ParseTree errorConstructContext;
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, SHORT_CASE_DATA, "", ENTRY);
+
+ YangCase caseNode = getYangCaseNode(JAVA_GENERATION);
+
+ if (ctx.containerStatement() != null) {
+ caseNode.setName(getValidIdentifier(ctx.containerStatement().identifier().getText(), CASE_DATA, ctx));
+ errorConstructContext = ctx.containerStatement();
+ } else if (ctx.listStatement() != null) {
+ caseNode.setName(getValidIdentifier(ctx.listStatement().identifier().getText(), CASE_DATA, ctx));
+ errorConstructContext = ctx.listStatement();
+ } else if (ctx.leafListStatement() != null) {
+ caseNode.setName(getValidIdentifier(ctx.leafListStatement().identifier().getText(), CASE_DATA, ctx));
+ errorConstructContext = ctx.leafListStatement();
+ } else if (ctx.leafStatement() != null) {
+ caseNode.setName(getValidIdentifier(ctx.leafStatement().identifier().getText(), CASE_DATA, ctx));
+ errorConstructContext = ctx.leafStatement();
+ } else {
+ throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, SHORT_CASE_DATA, "", ENTRY));
+ }
+ // TODO implement for augment.
+
+ int line = ((ParserRuleContext) errorConstructContext).getStart().getLine();
+ int charPositionInLine = ((ParserRuleContext) errorConstructContext).getStart().getCharPositionInLine();
+
+ // Check for identifier collision
+ detectCollidingChildUtil(listener, line, charPositionInLine, caseNode.getName(), CASE_DATA);
+
+ if ((listener.getParsedDataStack().peek()) instanceof YangChoice) {
+ try {
+ ((YangChoice) listener.getParsedDataStack().peek()).addChild(caseNode);
+ } catch (DataModelException e) {
+ throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
+ SHORT_CASE_DATA, caseNode.getName(), ENTRY, e.getMessage()));
+ }
+ listener.getParsedDataStack().push(caseNode);
+ } else {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, SHORT_CASE_DATA,
+ caseNode.getName(), ENTRY));
+ }
+ }
+
+ /**
+ * It is called when parser exits from grammar rule (short case), it perform
+ * validations and update the data model tree.
+ *
+ * @param listener Listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processShortCaseExit(TreeWalkListener listener,
+ GeneratedYangParser.ShortCaseStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, SHORT_CASE_DATA, "", EXIT);
+
+ if (listener.getParsedDataStack().peek() instanceof YangCase) {
+ listener.getParsedDataStack().pop();
+ } else {
+ throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SHORT_CASE_DATA,
+ "", EXIT));
+ }
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaInput.java b/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaInput.java
new file mode 100644
index 0000000..ee12366
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaInput.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.javamodel;
+
+import java.io.IOException;
+import org.onosproject.yangutils.datamodel.YangInput;
+import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
+import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
+import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
+import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.JavaImportData;
+import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
+
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
+import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
+
+/**
+ * Input information extended to support java code generation.
+ */
+public class YangJavaInput extends YangInput
+ implements JavaCodeGenerator, HasJavaFileInfo,
+ HasJavaImportData, HasTempJavaCodeFragmentFiles {
+
+ /**
+ * Contains information of the java file being generated.
+ */
+ private JavaFileInfo javaFileInfo;
+
+ /**
+ * Contains information of the imports to be inserted in the java file
+ * generated.
+ */
+ private JavaImportData javaImportData;
+
+ /**
+ * File handle to maintain temporary java code fragments as per the code
+ * snippet types.
+ */
+ private TempJavaCodeFragmentFiles tempFileHandle;
+
+ /**
+ * Creates an instance of java input.
+ */
+ public YangJavaInput() {
+ super();
+ setJavaFileInfo(new JavaFileInfo());
+ setJavaImportData(new JavaImportData());
+ getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
+ }
+
+ /**
+ * Returns the generated java file information.
+ *
+ * @return generated java file information
+ */
+ @Override
+ public JavaFileInfo getJavaFileInfo() {
+
+ if (javaFileInfo == null) {
+ throw new RuntimeException("Missing java info in java datamodel node");
+ }
+ return javaFileInfo;
+ }
+
+ /**
+ * Set the java file info object.
+ *
+ * @param javaInfo java file info object
+ */
+ @Override
+ public void setJavaFileInfo(JavaFileInfo javaInfo) {
+
+ javaFileInfo = javaInfo;
+ }
+
+ /**
+ * Returns the data of java imports to be included in generated file.
+ *
+ * @return data of java imports to be included in generated file
+ */
+ @Override
+ public JavaImportData getJavaImportData() {
+
+ return javaImportData;
+ }
+
+ /**
+ * Set the data of java imports to be included in generated file.
+ *
+ * @param javaImportData data of java imports to be included in generated
+ * file
+ */
+ @Override
+ public void setJavaImportData(JavaImportData javaImportData) {
+
+ this.javaImportData = javaImportData;
+ }
+
+ /**
+ * Returns the temporary file handle.
+ *
+ * @return temporary file handle
+ */
+ @Override
+ public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
+
+ if (tempFileHandle == null) {
+ throw new RuntimeException("Missing temporary file handle for" +
+ "current node " + getJavaFileInfo().getJavaName());
+ }
+ return tempFileHandle;
+ }
+
+ /**
+ * Set temporary file handle.
+ *
+ * @param fileHandle temporary file handle
+ */
+ @Override
+ public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
+
+ tempFileHandle = fileHandle;
+ }
+
+ /**
+ * Prepare the information for java code generation corresponding to YANG
+ * container info.
+ *
+ * @param codeGenDir code generation directory
+ * @throws IOException IO operation fail
+ */
+ @Override
+ public void generateCodeEntry(String codeGenDir) throws IOException {
+
+ getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
+ getJavaFileInfo().setPackage(getCurNodePackage(this));
+ getJavaFileInfo().setPackageFilePath(
+ getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
+ getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
+
+ String absloutePath = getAbsolutePackagePath(
+ getJavaFileInfo().getBaseCodeGenPath(),
+ getJavaFileInfo().getPackageFilePath());
+ createPackage(absloutePath, getName());
+ setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
+ getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
+ getJavaFileInfo().getJavaName()));
+
+ getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
+
+ getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
+ }
+
+ /**
+ * Create a java file using the YANG grouping info.
+ *
+ * @throws IOException IO operation fail
+ */
+ @Override
+ public void generateCodeExit() throws IOException {
+
+ generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
+ getTempJavaCodeFragmentFiles().close();
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaOutput.java b/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaOutput.java
new file mode 100644
index 0000000..4a9cedc
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaOutput.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.javamodel;
+
+import java.io.IOException;
+import org.onosproject.yangutils.datamodel.YangOutput;
+import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
+import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
+import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
+import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.JavaImportData;
+import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
+
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
+import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
+
+/**
+ * Output information extended to support java code generation.
+ */
+public class YangJavaOutput extends YangOutput
+ implements JavaCodeGenerator, HasJavaFileInfo,
+ HasJavaImportData, HasTempJavaCodeFragmentFiles {
+
+ /**
+ * Contains information of the java file being generated.
+ */
+ private JavaFileInfo javaFileInfo;
+
+ /**
+ * Contains information of the imports to be inserted in the java file
+ * generated.
+ */
+ private JavaImportData javaImportData;
+
+ /**
+ * File handle to maintain temporary java code fragments as per the code
+ * snippet types.
+ */
+ private TempJavaCodeFragmentFiles tempFileHandle;
+
+ /**
+ * Creates an instance of java output.
+ */
+ public YangJavaOutput() {
+ super();
+ setJavaFileInfo(new JavaFileInfo());
+ setJavaImportData(new JavaImportData());
+ getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
+ }
+
+ /**
+ * Returns the generated java file information.
+ *
+ * @return generated java file information
+ */
+ @Override
+ public JavaFileInfo getJavaFileInfo() {
+
+ if (javaFileInfo == null) {
+ throw new RuntimeException("Missing java info in java datamodel node");
+ }
+ return javaFileInfo;
+ }
+
+ /**
+ * Set the java file info object.
+ *
+ * @param javaInfo java file info object
+ */
+ @Override
+ public void setJavaFileInfo(JavaFileInfo javaInfo) {
+
+ javaFileInfo = javaInfo;
+ }
+
+ /**
+ * Returns the data of java imports to be included in generated file.
+ *
+ * @return data of java imports to be included in generated file
+ */
+ @Override
+ public JavaImportData getJavaImportData() {
+
+ return javaImportData;
+ }
+
+ /**
+ * Set the data of java imports to be included in generated file.
+ *
+ * @param javaImportData data of java imports to be included in generated
+ * file
+ */
+ @Override
+ public void setJavaImportData(JavaImportData javaImportData) {
+
+ this.javaImportData = javaImportData;
+ }
+
+ /**
+ * Returns the temporary file handle.
+ *
+ * @return temporary file handle
+ */
+ @Override
+ public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
+
+ if (tempFileHandle == null) {
+ throw new RuntimeException("Missing temporary file handle for" +
+ "current node " + getJavaFileInfo().getJavaName());
+ }
+ return tempFileHandle;
+ }
+
+ /**
+ * Set temporary file handle.
+ *
+ * @param fileHandle temporary file handle
+ */
+ @Override
+ public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
+
+ tempFileHandle = fileHandle;
+ }
+
+ /**
+ * Prepare the information for java code generation corresponding to YANG
+ * container info.
+ *
+ * @param codeGenDir code generation directory
+ * @throws IOException IO operation fail
+ */
+ @Override
+ public void generateCodeEntry(String codeGenDir) throws IOException {
+
+ getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
+ getJavaFileInfo().setPackage(getCurNodePackage(this));
+ getJavaFileInfo().setPackageFilePath(
+ getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
+ getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
+
+ String absloutePath = getAbsolutePackagePath(
+ getJavaFileInfo().getBaseCodeGenPath(),
+ getJavaFileInfo().getPackageFilePath());
+ createPackage(absloutePath, getName());
+ setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
+ getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
+ getJavaFileInfo().getJavaName()));
+
+ getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
+
+ getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
+ }
+
+ /**
+ * Create a java file using the YANG grouping info.
+ *
+ * @throws IOException IO operation fail
+ */
+ @Override
+ public void generateCodeExit() throws IOException {
+
+ generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
+ getTempJavaCodeFragmentFiles().close();
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/utils/YangConstructType.java b/src/main/java/org/onosproject/yangutils/utils/YangConstructType.java
index 89fb449..5281c53 100644
--- a/src/main/java/org/onosproject/yangutils/utils/YangConstructType.java
+++ b/src/main/java/org/onosproject/yangutils/utils/YangConstructType.java
@@ -235,6 +235,41 @@
DATA_DEF_DATA,
/**
+ * Identifies the YANG union element parsed data.
+ */
+ UNION_DATA,
+
+ /**
+ * Identifies the YANG notification element parsed data.
+ */
+ NOTIFICATION_DATA,
+
+ /**
+ * Identifies the YANG when element parsed data.
+ */
+ WHEN_DATA,
+
+ /**
+ * Identifies the YANG input element parsed data.
+ */
+ INPUT_DATA,
+
+ /**
+ * Identifies the YANG output element parsed data.
+ */
+ OUTPUT_DATA,
+
+ /**
+ * Identifies the YANG rpc element parsed data.
+ */
+ RPC_DATA,
+
+ /**
+ * Identifies the YANG short case element parsed data.
+ */
+ SHORT_CASE_DATA,
+
+ /**
* Identifies the derived data type.
*/
DERIVED;
@@ -334,8 +369,22 @@
return "default";
case DATA_DEF_DATA:
return "data-def-substatements";
+ case WHEN_DATA:
+ return "when";
+ case INPUT_DATA:
+ return "input";
+ case OUTPUT_DATA:
+ return "ouput";
+ case RPC_DATA:
+ return "rpc";
+ case SHORT_CASE_DATA:
+ return "short-case";
case DERIVED:
return "derived";
+ case NOTIFICATION_DATA:
+ return "notification";
+ case UNION_DATA:
+ return "union";
default:
return "yang";
}
diff --git a/src/test/java/org/onosproject/yangutils/parser/impl/listeners/CaseListenerTest.java b/src/test/java/org/onosproject/yangutils/parser/impl/listeners/CaseListenerTest.java
new file mode 100644
index 0000000..2a132f9
--- /dev/null
+++ b/src/test/java/org/onosproject/yangutils/parser/impl/listeners/CaseListenerTest.java
@@ -0,0 +1,205 @@
+/*
+ * Copyright 2014-2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangCase;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+/**
+ * Test cases for case listener.
+ */
+public class CaseListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks multiple case statement.
+ */
+ @Test
+ public void processCaseStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/CaseStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+
+ YangCase yangCase1 = (YangCase) yangChoice.getChild();
+ assertThat(yangCase1.getName(), is("sports-arena"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
+ YangLeaf leafInfo1 = leafIterator1.next();
+
+ assertThat(leafInfo1.getLeafName(), is("pretzel"));
+
+ YangCase yangCase2 = (YangCase) yangCase1.getNextSibling();
+ assertThat(yangCase2.getName(), is("late-night"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator();
+ YangLeaf leafInfo2 = leafIterator2.next();
+
+ assertThat(leafInfo2.getLeafName(), is("chocolate"));
+ }
+
+ /**
+ * Checks duplicate case in choice.
+ */
+ @Test(expected = ParserException.class)
+ public void processDuplicateCaseInChoice() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateCaseInChoice.yang");
+ }
+
+ /**
+ * Checks duplicate leaf at different hierarchy.
+ */
+ @Test(expected = ParserException.class)
+ public void processDuplicateLeafInHierarchy() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInHierarchy.yang");
+ }
+
+ /**
+ * Checks duplicate leaf in different case within choice.
+ */
+ @Test(expected = ParserException.class)
+ public void processDuplicateLeafInChoice() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInChoice.yang");
+ }
+
+ /**
+ * Checks same case within different choice.
+ */
+ @Test
+ public void processCaseStatementSameEntryDifferentChoice() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/CaseStatementSameEntryDifferentChoice.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+
+ YangCase yangCase1 = (YangCase) yangChoice.getChild();
+ assertThat(yangCase1.getName(), is("sports-arena"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
+ YangLeaf leafInfo1 = leafIterator1.next();
+
+ assertThat(leafInfo1.getLeafName(), is("pretzel"));
+
+ YangChoice yangChoice2 = (YangChoice) yangChoice.getNextSibling();
+ assertThat(yangChoice2.getName(), is("lunch"));
+
+ YangCase yangCase2 = (YangCase) yangChoice2.getChild();
+ assertThat(yangCase2.getName(), is("sports-arena"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator();
+ YangLeaf leafInfo2 = leafIterator2.next();
+
+ assertThat(leafInfo2.getLeafName(), is("chocolate"));
+ }
+
+ /**
+ * Checks case choice hierarchy.
+ */
+ @Test
+ public void processCaseChoiceHierarchy() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/CaseChoiceHierarchy.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice1 = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice1.getName(), is("snack"));
+
+ YangCase yangCase1 = (YangCase) yangChoice1.getChild();
+ assertThat(yangCase1.getName(), is("sports-arena"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
+ YangLeaf leafInfo1 = leafIterator1.next();
+
+ assertThat(leafInfo1.getLeafName(), is("pretzel"));
+
+ YangCase yangCase2 = (YangCase) yangCase1.getNextSibling();
+ assertThat(yangCase2.getName(), is("late-night"));
+
+ YangChoice yangChoice2 = (YangChoice) yangCase2.getChild();
+ assertThat(yangChoice2.getName(), is("dinner"));
+
+ YangCase yangCase3 = (YangCase) yangChoice2.getChild();
+ assertThat(yangCase3.getName(), is("late-night"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator2 = yangCase3.getListOfLeaf().listIterator();
+ YangLeaf leafInfo2 = leafIterator2.next();
+ assertThat(leafInfo2.getLeafName(), is("beer"));
+ }
+}
diff --git a/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListenerTest.java b/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListenerTest.java
new file mode 100644
index 0000000..68400b9
--- /dev/null
+++ b/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListenerTest.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2014-2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+/**
+ * Test cases for choice listener.
+ */
+public class ChoiceListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks choice statement without body.
+ */
+ @Test
+ public void processChoiceStatementWithoutBody() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementWithoutBody.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+ }
+
+ /**
+ * Checks choice statement with stmt end.
+ */
+ @Test
+ public void processChoiceStatementWithStmtend() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementWithStmtend.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+ }
+
+ /**
+ * Checks choice statement duplicate entry.
+ */
+ @Test(expected = ParserException.class)
+ public void processChoiceStatementDuplicateEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementDuplicateEntry.yang");
+ }
+
+ /**
+ * Checks choice statement with same entry in two different container.
+ */
+ @Test
+ public void processChoiceStatementSameEntryDifferentContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementSameEntryDifferentContainer.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer1 = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer1.getName(), is("food1"));
+
+ YangChoice yangChoice1 = (YangChoice) yangContainer1.getChild();
+ assertThat(yangChoice1.getName(), is("snack"));
+
+ YangContainer yangContainer2 = (YangContainer) yangNode.getChild().getNextSibling();
+ assertThat(yangContainer2.getName(), is("food2"));
+
+ YangChoice yangChoice2 = (YangChoice) yangContainer2.getChild();
+ assertThat(yangChoice2.getName(), is("snack"));
+ }
+}
diff --git a/src/test/resources/CaseChoiceHierarchy.yang b/src/test/resources/CaseChoiceHierarchy.yang
new file mode 100644
index 0000000..16b4047
--- /dev/null
+++ b/src/test/resources/CaseChoiceHierarchy.yang
@@ -0,0 +1,23 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ case sports-arena {
+ leaf pretzel {
+ type empty;
+ }
+ }
+ case late-night {
+ choice dinner {
+ case late-night {
+ leaf beer {
+ type empty;
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/resources/CaseStatement.yang b/src/test/resources/CaseStatement.yang
new file mode 100644
index 0000000..bb3f6c9
--- /dev/null
+++ b/src/test/resources/CaseStatement.yang
@@ -0,0 +1,26 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ case sports-arena {
+ leaf pretzel {
+ type empty;
+ }
+ leaf beer {
+ type empty;
+ }
+ }
+ case late-night {
+ leaf chocolate {
+ type enumeration {
+ enum dark;
+ enum milk;
+ enum first-available;
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/resources/CaseStatementSameEntryDifferentChoice.yang b/src/test/resources/CaseStatementSameEntryDifferentChoice.yang
new file mode 100644
index 0000000..b42cdf9
--- /dev/null
+++ b/src/test/resources/CaseStatementSameEntryDifferentChoice.yang
@@ -0,0 +1,28 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ case sports-arena {
+ leaf pretzel {
+ type empty;
+ }
+ leaf beer {
+ type empty;
+ }
+ }
+ }
+ choice lunch {
+ case sports-arena {
+ leaf chocolate {
+ type enumeration {
+ enum dark;
+ enum milk;
+ enum first-available;
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/resources/ChoiceStatementDuplicateEntry.yang b/src/test/resources/ChoiceStatementDuplicateEntry.yang
new file mode 100644
index 0000000..d2a6371
--- /dev/null
+++ b/src/test/resources/ChoiceStatementDuplicateEntry.yang
@@ -0,0 +1,10 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack;
+ choice lunch;
+ choice snack;
+ }
+}
diff --git a/src/test/resources/ChoiceStatementSameEntryDifferentContainer.yang b/src/test/resources/ChoiceStatementSameEntryDifferentContainer.yang
new file mode 100644
index 0000000..39ba626
--- /dev/null
+++ b/src/test/resources/ChoiceStatementSameEntryDifferentContainer.yang
@@ -0,0 +1,13 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food1 {
+ choice snack;
+ choice lunch;
+ }
+ container food2 {
+ choice snack;
+ choice lunch;
+ }
+}
diff --git a/src/test/resources/ChoiceStatementWithStmtend.yang b/src/test/resources/ChoiceStatementWithStmtend.yang
new file mode 100644
index 0000000..4b85f59
--- /dev/null
+++ b/src/test/resources/ChoiceStatementWithStmtend.yang
@@ -0,0 +1,8 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack;
+ }
+}
diff --git a/src/test/resources/ChoiceStatementWithoutBody.yang b/src/test/resources/ChoiceStatementWithoutBody.yang
new file mode 100644
index 0000000..2de7787
--- /dev/null
+++ b/src/test/resources/ChoiceStatementWithoutBody.yang
@@ -0,0 +1,9 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ }
+ }
+}
diff --git a/src/test/resources/DuplicateCaseInChoice.yang b/src/test/resources/DuplicateCaseInChoice.yang
new file mode 100644
index 0000000..a7b6b50
--- /dev/null
+++ b/src/test/resources/DuplicateCaseInChoice.yang
@@ -0,0 +1,26 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ case sports-arena {
+ leaf pretzel {
+ type empty;
+ }
+ leaf beer {
+ type empty;
+ }
+ }
+ case sports-arena {
+ leaf chocolate {
+ type enumeration {
+ enum dark;
+ enum milk;
+ enum first-available;
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/resources/DuplicateLeafInChoice.yang b/src/test/resources/DuplicateLeafInChoice.yang
new file mode 100644
index 0000000..f951c7f
--- /dev/null
+++ b/src/test/resources/DuplicateLeafInChoice.yang
@@ -0,0 +1,21 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ case sports-arena {
+ leaf pretzel {
+ type empty;
+ }
+ leaf beer {
+ type empty;
+ }
+ }
+ case late-night {
+ leaf pretzel {
+ type empty;
+ }
+ }
+ }
+}
diff --git a/src/test/resources/DuplicateLeafInHierarchy.yang b/src/test/resources/DuplicateLeafInHierarchy.yang
new file mode 100644
index 0000000..c727cb7
--- /dev/null
+++ b/src/test/resources/DuplicateLeafInHierarchy.yang
@@ -0,0 +1,23 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ case sports-arena {
+ leaf pretzel {
+ type empty;
+ }
+ }
+ case late-night {
+ choice lunch {
+ case late {
+ leaf pretzel {
+ type empty;
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/resources/ShortCaseListenerWithContainer.yang b/src/test/resources/ShortCaseListenerWithContainer.yang
new file mode 100644
index 0000000..0e4ff2d
--- /dev/null
+++ b/src/test/resources/ShortCaseListenerWithContainer.yang
@@ -0,0 +1,15 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ container sports-arena {
+ leaf pretzel {
+ type empty;
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/resources/ShortCaseListenerWithList.yang b/src/test/resources/ShortCaseListenerWithList.yang
new file mode 100644
index 0000000..2ba25d9
--- /dev/null
+++ b/src/test/resources/ShortCaseListenerWithList.yang
@@ -0,0 +1,16 @@
+module Test {
+ yang-version 1;
+ namespace http://huawei.com;
+ prefix Ant;
+ container food {
+ choice snack {
+ list sports-arena {
+ key "pretzel";
+ leaf pretzel {
+ type int32;
+ }
+ }
+ }
+ }
+ }
+}