Invalid pattern restriction check

Change-Id: I5398cb6e85f06efd7937562ba0d9a51eff23572d
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListener.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListener.java
index ca36525..4f0ce8e 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListener.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListener.java
@@ -16,6 +16,8 @@
 
 package org.onosproject.yangutils.parser.impl.listeners;
 
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
 import org.onosproject.yangutils.datamodel.YangDataTypes;
 import org.onosproject.yangutils.datamodel.YangDerivedInfo;
 import org.onosproject.yangutils.datamodel.YangPatternRestriction;
@@ -98,8 +100,8 @@
      * Sets the pattern restriction to type.
      *
      * @param listener listener's object
-     * @param type Yang type for which pattern restriction to be set
-     * @param ctx  context object of the grammar rule
+     * @param type     Yang type for which pattern restriction to be set
+     * @param ctx      context object of the grammar rule
      */
     private static void setPatternRestriction(TreeWalkListener listener, YangType type,
                                               GeneratedYangParser.PatternStatementContext ctx) {
@@ -114,7 +116,8 @@
             throw parserException;
         }
 
-        String patternArgument = ctx.string().getText().replace("\"", EMPTY_STRING);
+        // Validate and get valid pattern restriction string.
+        String patternArgument = getValidPattern(ctx);
 
         if (type.getDataType() == YangDataTypes.STRING) {
             YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
@@ -145,10 +148,10 @@
      * It is called when parser exits from grammar rule (pattern).
      *
      * @param listener listener's object
-     * @param ctx context object of the grammar rule
+     * @param ctx      context object of the grammar rule
      */
     public static void processPatternRestrictionExit(TreeWalkListener listener,
-                                                    GeneratedYangParser.PatternStatementContext ctx) {
+                                                     GeneratedYangParser.PatternStatementContext ctx) {
 
         // Check for stack to be non empty.
         checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), EXIT);
@@ -164,4 +167,25 @@
                     ctx.string().getText(), EXIT));
         }
     }
+
+    /**
+     * Validates and return the valid pattern.
+     *
+     * @param ctx context object of the grammar rule
+     * @return validated string
+     */
+    private static String getValidPattern(GeneratedYangParser.PatternStatementContext ctx) {
+        String userInputPattern = ctx.string().getText().replace("\"", EMPTY_STRING);
+        try {
+            Pattern.compile(userInputPattern);
+        } catch (PatternSyntaxException exception) {
+            ParserException parserException = new ParserException("YANG file error : " +
+                    YangConstructType.getYangConstructType(PATTERN_DATA) + " name " + ctx.string().getText() +
+                    " is not a valid regular expression");
+            parserException.setLine(ctx.getStart().getLine());
+            parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
+            throw parserException;
+        }
+        return userInputPattern;
+    }
 }
diff --git a/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListenerTest.java b/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListenerTest.java
index 928af5c..69c4452 100644
--- a/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListenerTest.java
+++ b/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListenerTest.java
@@ -18,17 +18,16 @@
 
 import java.io.IOException;
 import java.util.ListIterator;
-
 import org.junit.Test;
-import org.onosproject.yangutils.datamodel.YangNode;
-import org.onosproject.yangutils.datamodel.YangNodeType;
-import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
 import org.onosproject.yangutils.datamodel.YangLeaf;
 import org.onosproject.yangutils.datamodel.YangLeafList;
-import org.onosproject.yangutils.datamodel.YangDataTypes;
-import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
 import org.onosproject.yangutils.datamodel.YangPatternRestriction;
 import org.onosproject.yangutils.datamodel.YangStringRestriction;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
 import org.onosproject.yangutils.parser.exceptions.ParserException;
 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
 
@@ -194,4 +193,12 @@
                 .getPatternList().listIterator();
         assertThat(patternListIterator.next(), is("[a-zA-Z]"));
     }
+
+    /**
+     * Checks invalid pattern sub-statement.
+     */
+    @Test(expected = ParserException.class)
+    public void processInvalidPatternSubStatements() throws IOException, ParserException {
+        YangNode node = manager.getDataModel("src/test/resources/InvalidPatternSubStatements.yang");
+    }
 }
diff --git a/utils/yangutils/src/test/resources/InvalidPatternSubStatements.yang b/utils/yangutils/src/test/resources/InvalidPatternSubStatements.yang
new file mode 100644
index 0000000..76ff75e
--- /dev/null
+++ b/utils/yangutils/src/test/resources/InvalidPatternSubStatements.yang
@@ -0,0 +1,13 @@
+module Test {
+    yang-version 1;
+    namespace http://huawei.com;
+    prefix Ant;
+    leaf invalid-interval {
+        type string {
+            pattern "[a-zA-Z]\" {
+                description "pattern description";
+                reference "pattern reference";
+            }
+         }
+    }
+}