[ONOS-4650][ONOS-4726][ONOS-4727] [ONOS-4728]Implement must parser + when parser + feature + if-feature + revision defect fix

Change-Id: I0a3aee6c1c6b72ef7da7f7f565fd0f149fe3fd42
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/FeatureListener.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/FeatureListener.java
new file mode 100644
index 0000000..6a70f31
--- /dev/null
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/FeatureListener.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2016-present 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;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ *  feature-stmt        = feature-keyword sep identifier-arg-str optsep
+ *                        (";" /
+ *                         "{" stmtsep
+ *                             ;; these stmts can appear in any order
+ *                             *(if-feature-stmt stmtsep)
+ *                             [status-stmt stmtsep]
+ *                             [description-stmt stmtsep]
+ *                             [reference-stmt stmtsep]
+ *                         "}")
+ *
+ *
+ *
+ * ANTLR grammar rule
+ * featureStatement : FEATURE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE featureBody RIGHT_CURLY_BRACE);
+ */
+
+import org.onosproject.yangutils.datamodel.YangFeature;
+import org.onosproject.yangutils.datamodel.YangFeatureHolder;
+import org.onosproject.yangutils.datamodel.utils.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.YangConstructType.FEATURE_DATA;
+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.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.ListenerUtil.getValidIdentifier;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/**
+ * Represents listener based call back function corresponding to the "feature"
+ * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class FeatureListener {
+
+    /**
+     * Creates a new feature listener.
+     */
+    private FeatureListener() {
+    }
+
+    /**
+     * Performs validation and updates the data model tree.It is called when parser receives
+     * an input matching the grammar rule (feature).
+     *
+     * @param listener listener's object
+     * @param ctx      context object of the grammar rule
+     */
+    public static void processFeatureEntry(TreeWalkListener listener,
+                                           GeneratedYangParser.FeatureStatementContext ctx) {
+
+        // Check for stack to be non empty.
+        checkStackIsNotEmpty(listener, MISSING_HOLDER, FEATURE_DATA, ctx.string().getText(), ENTRY);
+
+        String identifier = getValidIdentifier(ctx.string().getText(), FEATURE_DATA, ctx);
+
+        // Obtain the node of the stack.
+        Parsable tmpNode = listener.getParsedDataStack().peek();
+        if (tmpNode instanceof YangFeatureHolder) {
+            YangFeatureHolder featureHolder = (YangFeatureHolder) tmpNode;
+
+            YangFeature feature = new YangFeature();
+            feature.setName(identifier);
+
+            featureHolder.addFeatureList(feature);
+            listener.getParsedDataStack().push(feature);
+        } else {
+            throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, FEATURE_DATA,
+                    ctx.string().getText(), ENTRY));
+        }
+    }
+
+    /**
+     * Perform validations and updates the data model tree.It is called when parser exits from
+     * grammar rule(feature).
+     *
+     * @param listener listener's object
+     * @param ctx      context object of the grammar rule
+     */
+    public static void processFeatureExit(TreeWalkListener listener,
+                                          GeneratedYangParser.FeatureStatementContext ctx) {
+
+        // Check for stack to be non empty.
+        checkStackIsNotEmpty(listener, MISSING_HOLDER, FEATURE_DATA, ctx.string().getText(), EXIT);
+
+        if (listener.getParsedDataStack().peek() instanceof YangFeature) {
+            listener.getParsedDataStack().pop();
+        } else {
+            throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, FEATURE_DATA,
+                    ctx.string().getText(), EXIT));
+        }
+    }
+}
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IfFeatureListener.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IfFeatureListener.java
new file mode 100644
index 0000000..29899de
--- /dev/null
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IfFeatureListener.java
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2016-present 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;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ *
+ * if-feature-stmt     = if-feature-keyword sep identifier-ref-arg-str
+ *                        optsep stmtend
+ *
+ * ANTLR grammar rule
+ * ifFeatureStatement : IF_FEATURE_KEYWORD string STMTEND;
+ */
+
+import org.onosproject.yangutils.datamodel.YangFeature;
+import org.onosproject.yangutils.datamodel.YangIfFeature;
+import org.onosproject.yangutils.datamodel.YangIfFeatureHolder;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
+import org.onosproject.yangutils.datamodel.YangResolutionInfo;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.datamodel.utils.Parsable;
+import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
+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.DataModelUtils.addResolutionInfo;
+import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.UNRESOLVED;
+import static org.onosproject.yangutils.datamodel.utils.YangConstructType.IF_FEATURE_DATA;
+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_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/**
+ * Represents listener based call back function corresponding to the "if-feature"
+ * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class IfFeatureListener {
+
+    /**
+     * Creates a new IfFeature listener.
+     */
+    private IfFeatureListener() {
+    }
+
+    /**
+     * Performs validation and updates the data model tree.It is called when parser receives
+     * an input matching the grammar rule (if-feature).
+     *
+     * @param listener listener's object
+     * @param ctx      context object of the grammar rule
+     */
+    public static void processIfFeatureEntry(TreeWalkListener listener,
+                                             GeneratedYangParser.IfFeatureStatementContext ctx) {
+        // Check for stack to be non empty.
+        checkStackIsNotEmpty(listener, MISSING_HOLDER, IF_FEATURE_DATA, ctx.string().getText(), ENTRY);
+
+        // Validate if-feature argument string
+        YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(),
+                IF_FEATURE_DATA, ctx);
+
+        YangIfFeature ifFeature = new YangIfFeature();
+        ifFeature.setName(nodeIdentifier);
+        ifFeature.setResolvableStatus(UNRESOLVED);
+        YangIfFeatureHolder ifFeatureHolder;
+
+        // Obtain the node of the stack.
+        Parsable tmpNode = listener.getParsedDataStack().peek();
+        if (tmpNode instanceof YangIfFeatureHolder) {
+            ifFeatureHolder = (YangIfFeatureHolder) tmpNode;
+            ifFeatureHolder.addIfFeatureList(ifFeature);
+        } else {
+            throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IF_FEATURE_DATA,
+                    ctx.string().getText(), ENTRY));
+        }
+
+        // Add resolution information to the list
+        Parsable parentNode;
+        if (tmpNode instanceof YangLeafList || tmpNode instanceof YangLeaf
+                || tmpNode instanceof YangFeature) {
+            Parsable leafData = listener.getParsedDataStack().pop();
+            parentNode = listener.getParsedDataStack().peek();
+            listener.getParsedDataStack().push(leafData);
+        } else {
+            parentNode = tmpNode;
+        }
+
+        // Verify parent node of leaf
+        if (!(parentNode instanceof YangNode)) {
+            throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IF_FEATURE_DATA,
+                    ctx.string().getText(), EXIT));
+        }
+
+        int errorLine = ctx.getStart().getLine();
+        int errorPosition = ctx.getStart().getCharPositionInLine();
+        YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangIfFeature>(ifFeature,
+                (YangNode) parentNode, errorLine,
+                errorPosition);
+        addToResolutionList(resolutionInfo, ctx);
+    }
+
+    /**
+     * Add to resolution list.
+     *
+     * @param resolutionInfo resolution information.
+     * @param ctx            context object of the grammar rule
+     */
+    private static void addToResolutionList(YangResolutionInfo<YangIfFeature> resolutionInfo,
+                                            GeneratedYangParser.IfFeatureStatementContext ctx) {
+
+        try {
+            addResolutionInfo(resolutionInfo);
+        } catch (DataModelException e) {
+            throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
+                    IF_FEATURE_DATA, ctx.string().getText(), EXIT, e.getMessage()));
+        }
+    }
+}
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
index ef1af2d..05cfdb3 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
@@ -16,11 +16,13 @@
 
 package org.onosproject.yangutils.parser.impl.listeners;
 
+import java.util.Date;
 import org.onosproject.yangutils.datamodel.ResolvableType;
 import org.onosproject.yangutils.datamodel.YangModule;
 import org.onosproject.yangutils.datamodel.YangReferenceResolver;
 import org.onosproject.yangutils.datamodel.YangRevision;
 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.datamodel.utils.Parsable;
 import org.onosproject.yangutils.linker.exceptions.LinkerException;
 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
 import org.onosproject.yangutils.parser.exceptions.ParserException;
@@ -34,8 +36,8 @@
 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.ListenerUtil.getCurrentDateForRevision;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
 import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangModuleNode;
@@ -91,13 +93,6 @@
             yangModule.setVersion((byte) 1);
         }
 
-        if (ctx.moduleBody().revisionStatements().revisionStatement().isEmpty()) {
-            String currentDate = setCurrentDateForRevision();
-            YangRevision currentRevision = new YangRevision();
-            currentRevision.setRevDate(currentDate);
-            yangModule.setRevision(currentRevision);
-        }
-
         listener.getParsedDataStack().push(yangModule);
     }
 
@@ -113,12 +108,23 @@
         // Check for stack to be non empty.
         checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.identifier().getText(), EXIT);
 
-        if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
+        Parsable tmpNode = listener.getParsedDataStack().peek();
+        if (!(tmpNode instanceof YangModule)) {
             throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
                     ctx.identifier().getText(), EXIT));
         }
+
+        if (((YangModule) tmpNode).getRevision() == null) {
+            Date currentDate = getCurrentDateForRevision();
+            YangRevision currentRevision = new YangRevision();
+            currentRevision.setRevDate(currentDate);
+            ((YangModule) tmpNode).setRevision(currentRevision);
+        }
+
         try {
             ((YangReferenceResolver) listener.getParsedDataStack()
+                    .peek()).resolveSelfFileLinking(ResolvableType.YANG_IF_FEATURE);
+            ((YangReferenceResolver) listener.getParsedDataStack()
                     .peek()).resolveSelfFileLinking(ResolvableType.YANG_USES);
             ((YangReferenceResolver) listener.getParsedDataStack()
                     .peek()).resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/MustListener.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/MustListener.java
new file mode 100644
index 0000000..b6cbf20
--- /dev/null
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/MustListener.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2016-present 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.YangMust;
+import org.onosproject.yangutils.datamodel.YangMustHolder;
+import org.onosproject.yangutils.datamodel.utils.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.YangConstructType.MUST_DATA;
+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.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.ListenerUtil.removeQuotesAndHandleConcat;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ *
+ *  must-stmt           = must-keyword sep string optsep
+ *                        (";" /
+ *                         "{" stmtsep
+ *                             ;; these stmts can appear in any order
+ *                             [error-message-stmt stmtsep]
+ *                             [error-app-tag-stmt stmtsep]
+ *                             [description-stmt stmtsep]
+ *                             [reference-stmt stmtsep]
+ *                          "}")
+ *
+ * ANTLR grammar rule
+ * mustStatement : MUST_KEYWORD string (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
+ */
+
+/**
+ * Represents listener based call back function corresponding to the
+ * "must" rule defined in ANTLR grammar file for corresponding ABNF rule
+ * in RFC 6020.
+ */
+public final class MustListener {
+
+    /**
+     * Creates a new must listener.
+     */
+    private MustListener() {
+    }
+
+    /**
+     * Perform validations and updates the data model tree.It is called when parser
+     * receives an input matching the grammar rule (must).
+     *
+     * @param listener listener's object
+     * @param ctx      context object of the grammar rule
+     */
+    public static void processMustEntry(TreeWalkListener listener,
+                                        GeneratedYangParser.MustStatementContext ctx) {
+
+        // Check for stack to be non empty.
+        checkStackIsNotEmpty(listener, MISSING_HOLDER, MUST_DATA, ctx.string().getText(), ENTRY);
+        String constraint = removeQuotesAndHandleConcat(ctx.string().getText());
+
+        // Obtain the node of the stack.
+        Parsable tmpNode = listener.getParsedDataStack().peek();
+        if (tmpNode instanceof YangMustHolder) {
+
+            YangMust must = new YangMust();
+            must.setConstraint(constraint);
+
+            YangMustHolder mustHolder = (YangMustHolder) tmpNode;
+            mustHolder.addMust(must);
+
+            listener.getParsedDataStack().push(must);
+        } else {
+            throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MUST_DATA,
+                    ctx.string().getText(), ENTRY));
+        }
+
+    }
+
+    /**
+     * Performs validation and updates the data model tree.It is called when parser
+     * exits from grammar rule (must).
+     *
+     * @param listener listener's object
+     * @param ctx      context object of the grammar rule
+     */
+    public static void processMustExit(TreeWalkListener listener,
+                                       GeneratedYangParser.MustStatementContext ctx) {
+
+        // Check for stack to be non empty.
+        checkStackIsNotEmpty(listener, MISSING_HOLDER, MUST_DATA, ctx.string().getText(), EXIT);
+
+        if (listener.getParsedDataStack().peek() instanceof YangMust) {
+            listener.getParsedDataStack().pop();
+        } else {
+            throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MUST_DATA,
+                    ctx.string().getText(), EXIT));
+        }
+    }
+}
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RevisionDateListener.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RevisionDateListener.java
index bb701a6..3ff67fe 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RevisionDateListener.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RevisionDateListener.java
@@ -16,6 +16,7 @@
 
 package org.onosproject.yangutils.parser.impl.listeners;
 
+import java.util.Date;
 import org.onosproject.yangutils.datamodel.YangImport;
 import org.onosproject.yangutils.datamodel.YangInclude;
 import org.onosproject.yangutils.datamodel.utils.Parsable;
@@ -28,8 +29,7 @@
 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_HOLDER;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.isDateValid;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidDateFromString;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
 
 /*
@@ -87,13 +87,7 @@
         checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.dateArgumentString().getText(),
                 ENTRY);
 
-        String date = removeQuotesAndHandleConcat(ctx.dateArgumentString().getText());
-        if (!isDateValid(date)) {
-            ParserException parserException = new ParserException("YANG file error: Input date is not correct");
-            parserException.setLine(ctx.getStart().getLine());
-            parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
-            throw parserException;
-        }
+        Date date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
 
         // Obtain the node of the stack.
         Parsable tmpNode = listener.getParsedDataStack().peek();
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RevisionListener.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RevisionListener.java
index 36d028a..cb633b8 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RevisionListener.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RevisionListener.java
@@ -16,6 +16,7 @@
 
 package org.onosproject.yangutils.parser.impl.listeners;
 
+import java.util.Date;
 import org.onosproject.yangutils.datamodel.YangModule;
 import org.onosproject.yangutils.datamodel.YangRevision;
 import org.onosproject.yangutils.datamodel.YangSubModule;
@@ -31,8 +32,7 @@
 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.ListenerUtil.isDateValid;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidDateFromString;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
 
 /*
@@ -88,20 +88,7 @@
         // Check for stack to be non empty.
         checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), ENTRY);
 
-        // Validate for reverse chronological order of revision & for revision
-        // value.
-        if (!validateRevision(listener, ctx)) {
-            return;
-            // TODO to be implemented.
-        }
-
-        String date = removeQuotesAndHandleConcat(ctx.dateArgumentString().getText());
-        if (!isDateValid(date)) {
-            ParserException parserException = new ParserException("YANG file error: Input date is not correct");
-            parserException.setLine(ctx.getStart().getLine());
-            parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
-            throw parserException;
-        }
+        Date date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
 
         YangRevision revisionNode = new YangRevision();
         revisionNode.setRevDate(date);
@@ -134,12 +121,26 @@
             switch (tmpNode.getYangConstructType()) {
                 case MODULE_DATA: {
                     YangModule module = (YangModule) tmpNode;
-                    module.setRevision((YangRevision) tmpRevisionNode);
+                    if (module.getRevision() != null) {
+                        Date curRevisionDate = module.getRevision().getRevDate();
+                        if (curRevisionDate.before(((YangRevision) tmpRevisionNode).getRevDate())) {
+                            module.setRevision((YangRevision) tmpRevisionNode);
+                        }
+                    } else {
+                        module.setRevision((YangRevision) tmpRevisionNode);
+                    }
                     break;
                 }
                 case SUB_MODULE_DATA: {
                     YangSubModule subModule = (YangSubModule) tmpNode;
-                    subModule.setRevision((YangRevision) tmpRevisionNode);
+                    if (subModule.getRevision() != null) {
+                        Date curRevisionDate = subModule.getRevision().getRevDate();
+                        if (curRevisionDate.before(((YangRevision) tmpRevisionNode).getRevDate())) {
+                            subModule.setRevision((YangRevision) tmpRevisionNode);
+                        }
+                    } else {
+                        subModule.setRevision((YangRevision) tmpRevisionNode);
+                    }
                     break;
                 }
                 default:
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListener.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListener.java
index 639906a..b2223b8 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListener.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListener.java
@@ -16,11 +16,13 @@
 
 package org.onosproject.yangutils.parser.impl.listeners;
 
+import java.util.Date;
 import org.onosproject.yangutils.datamodel.ResolvableType;
 import org.onosproject.yangutils.datamodel.YangReferenceResolver;
 import org.onosproject.yangutils.datamodel.YangRevision;
 import org.onosproject.yangutils.datamodel.YangSubModule;
 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.datamodel.utils.Parsable;
 import org.onosproject.yangutils.linker.exceptions.LinkerException;
 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
 import org.onosproject.yangutils.parser.exceptions.ParserException;
@@ -34,8 +36,8 @@
 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.ListenerUtil.getCurrentDateForRevision;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
 import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangSubModuleNode;
@@ -94,13 +96,6 @@
             yangSubModule.setVersion((byte) 1);
         }
 
-        if (ctx.submoduleBody().revisionStatements().revisionStatement().isEmpty()) {
-            String currentDate = setCurrentDateForRevision();
-            YangRevision currentRevision = new YangRevision();
-            currentRevision.setRevDate(currentDate);
-            yangSubModule.setRevision(currentRevision);
-        }
-
         listener.getParsedDataStack().push(yangSubModule);
     }
 
@@ -118,12 +113,23 @@
         checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
                 EXIT);
 
-        if (!(listener.getParsedDataStack().peek() instanceof YangSubModule)) {
+        Parsable tmpNode = listener.getParsedDataStack().peek();
+        if (!(tmpNode instanceof YangSubModule)) {
             throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SUB_MODULE_DATA,
                     ctx.identifier().getText(), EXIT));
         }
+
+        if (((YangSubModule) tmpNode).getRevision() == null) {
+            Date currentDate = getCurrentDateForRevision();
+            YangRevision currentRevision = new YangRevision();
+            currentRevision.setRevDate(currentDate);
+            ((YangSubModule) tmpNode).setRevision(currentRevision);
+        }
+
         try {
             ((YangReferenceResolver) listener.getParsedDataStack().peek())
+                    .resolveSelfFileLinking(ResolvableType.YANG_IF_FEATURE);
+            ((YangReferenceResolver) listener.getParsedDataStack().peek())
                     .resolveSelfFileLinking(ResolvableType.YANG_USES);
             ((YangReferenceResolver) listener.getParsedDataStack().peek())
                     .resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/WhenListener.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/WhenListener.java
new file mode 100644
index 0000000..15092a1
--- /dev/null
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/WhenListener.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2016-present 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.YangWhen;
+import org.onosproject.yangutils.datamodel.YangWhenHolder;
+import org.onosproject.yangutils.datamodel.utils.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.YangConstructType.WHEN_DATA;
+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.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.ListenerUtil.removeQuotesAndHandleConcat;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ *
+ *  when-stmt           = when-keyword sep string optsep
+ *                        (";" /
+ *                         "{" stmtsep
+ *                             ;; these stmts can appear in any order
+ *                             [description-stmt stmtsep]
+ *                             [reference-stmt stmtsep]
+ *                          "}")
+ *
+ * ANTLR grammar rule
+ * whenStatement : WHEN_KEYWORD string (STMTEND | LEFT_CURLY_BRACE ((descriptionStatement? referenceStatement?)
+ *       | (referenceStatement? descriptionStatement?)) RIGHT_CURLY_BRACE);
+ */
+
+/**
+ * Represents listener based call back function corresponding to the
+ * "when" rule defined in ANTLR grammar file for corresponding ABNF rule
+ * in RFC 6020.
+ */
+public final class WhenListener {
+
+    /**
+     * Creates a new when listener.
+     */
+    private WhenListener() {
+    }
+
+    /**
+     * Perform validations and updates the data model tree.It is called when parser
+     * receives an input matching the grammar rule (when).
+     *
+     * @param listener listener's object
+     * @param ctx      context object of the grammar rule
+     */
+    public static void processWhenEntry(TreeWalkListener listener,
+                                        GeneratedYangParser.WhenStatementContext ctx) {
+
+        // Check for stack to be non empty.
+        checkStackIsNotEmpty(listener, MISSING_HOLDER, WHEN_DATA, ctx.string().getText(), ENTRY);
+        String condition = removeQuotesAndHandleConcat(ctx.string().getText());
+
+        YangWhenHolder whenHolder;
+
+        // Obtain the node of the stack.
+        Parsable tmpNode = listener.getParsedDataStack().peek();
+        if (tmpNode instanceof YangWhenHolder) {
+            whenHolder = (YangWhenHolder) tmpNode;
+
+            YangWhen when = new YangWhen();
+            when.setCondition(condition);
+
+            whenHolder.setWhen(when);
+            listener.getParsedDataStack().push(when);
+        } else {
+            throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
+                    WHEN_DATA, ctx.string().getText(), ENTRY));
+        }
+    }
+
+    /**
+     * Performs validation and updates the data model tree.It is called when parser
+     * exits from grammar rule (when).
+     *
+     * @param listener listener's object
+     * @param ctx      context object of the grammar rule
+     */
+    public static void processWhenExit(TreeWalkListener listener,
+                                       GeneratedYangParser.WhenStatementContext ctx) {
+
+        // Check for stack to be non empty.
+        checkStackIsNotEmpty(listener, MISSING_HOLDER, WHEN_DATA, ctx.string().getText(), EXIT);
+
+        if (listener.getParsedDataStack().peek() instanceof YangWhen) {
+            listener.getParsedDataStack().pop();
+        } else {
+            throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, WHEN_DATA,
+                    ctx.string().getText(), EXIT));
+        }
+    }
+}