[ONOS-7063]Validation of modelId for valid syntax

Change-Id: Ic2e991b3e9abcaab97985882a8292fa48bd43827
diff --git a/compiler/plugin/utils/pom.xml b/compiler/plugin/utils/pom.xml
new file mode 100644
index 0000000..6d30023
--- /dev/null
+++ b/compiler/plugin/utils/pom.xml
@@ -0,0 +1,36 @@
+<!--
+  ~ Copyright 2016-present Open Networking Foundation
+  ~
+  ~ 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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+        </dependency>
+    </dependencies>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-yang-compiler-plugin</artifactId>
+        <version>2.3-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-yang-compiler-plugin-utils</artifactId>
+    <packaging>bundle</packaging>
+</project>
diff --git a/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/PluginUtils.java b/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/PluginUtils.java
new file mode 100644
index 0000000..b806b84
--- /dev/null
+++ b/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/PluginUtils.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.yang.compiler.plugin.utils;
+
+/**
+ * Representation of model id validation util.
+ */
+public final class PluginUtils {
+
+    // Forbid construction.
+    private PluginUtils() {
+    }
+
+    /**
+     * Returns the valid model id by removing the special character with
+     * underscore.
+     *
+     * @param id user given model id
+     * @return model id
+     * @throws IllegalArgumentException if user defined model id does not
+     *                                  contain at least a alphanumeric character
+     */
+    public static String getValidModelId(String id) throws
+            IllegalArgumentException {
+        // checking weather modelId contains the alphanumeric character or not.
+        if (id.matches(".*[A-Za-z0-9].*")) {
+            // replacing special characters with '_'
+            id = id.replaceAll("[\\s\\/:*?\"\\[\\]<>|$@!#%&(){}';,]", "_");
+            // remove leading and trailing underscore
+            id = id.replaceAll("^_+|_+$", "");
+            // replacing the consecutive underscores '_' to single _
+            id = id.replaceAll("_+", "_");
+            return id;
+        } else {
+            throw new IllegalArgumentException("Invalid model id " + id);
+        }
+    }
+}
diff --git a/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/package-info.java b/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/package-info.java
new file mode 100644
index 0000000..3ed076c
--- /dev/null
+++ b/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/package-info.java
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
+ * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
+ * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
+ * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
+ * Vestibulum commodo. Ut rhoncus gravida arcu.
+ */
+
+/**
+ * File system utilities.
+ */
+package org.onosproject.yang.compiler.plugin.utils;
diff --git a/compiler/plugin/utils/src/test/java/org/onosproject/yang/compiler/plugin/utils/ModelIdValidatorTest.java b/compiler/plugin/utils/src/test/java/org/onosproject/yang/compiler/plugin/utils/ModelIdValidatorTest.java
new file mode 100644
index 0000000..f3e3c60
--- /dev/null
+++ b/compiler/plugin/utils/src/test/java/org/onosproject/yang/compiler/plugin/utils/ModelIdValidatorTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
+ * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
+ * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
+ * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
+ * Vestibulum commodo. Ut rhoncus gravida arcu.
+ */
+
+package org.onosproject.yang.compiler.plugin.utils;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.onosproject.yang.compiler.plugin.utils.PluginUtils.getValidModelId;
+import static org.onosproject.yang.compiler.utils.UtilConstants.REGEX;
+
+public final class ModelIdValidatorTest {
+
+    private static final String E_INVAL = "ERROR: Model Id validation not " +
+            "working";
+    /*
+     * Reference for string values array to provide modelId value for
+     * different-different scenario.
+     */
+    String[] nA = {
+            "list Any data $doller @rate < arrow>end |or (copy)",
+            "onos-yang-runtime",
+            "Logistic.manager.1.",
+            "list Any data []{}",
+            "onos-yang     runtime      ",
+            "[]   xyz  []",
+            "onos_ yang_ runtime",
+            "onos_yang_runtime",
+            "org.onosproject.runtime"
+    };
+
+    private static final String[] EXPECTED = {
+            "list_Any_data_doller_rate_arrow_end_or_copy",
+            "onos-yang-runtime",
+            "Logistic.manager.1.",
+            "list_Any_data",
+            "onos-yang_runtime",
+            "xyz",
+            "onos_yang_runtime",
+            "onos_yang_runtime",
+            "org.onosproject.runtime"
+    };
+
+    /*
+     * Invalid values for testing the negative scenario for getValidModelId.
+     */
+    String[] invalidVal = {
+            ".",
+            "-",
+            "_"
+    };
+
+    /*
+     * Invalid values for testing the negative scenario for user given
+     * model id at the time of registration.
+     */
+    String[] invalidVal1 = {
+            "list Any data $doller @rate < arrow>end |or (copy)",
+            "list Any data []{}",
+            "onos-yang     runtime      ",
+            "[]   xyz  []",
+            "onos_ yang_ runtime",
+    };
+
+    /**
+     * Test positive scenario for getValidModelId functionality.
+     */
+    @Test
+    public void validateGetModelIdTest() {
+        for (int i = 0; i < EXPECTED.length; i++) {
+            assertEquals(EXPECTED[i], getValidModelId(nA[i]));
+        }
+    }
+
+    /**
+     * Test negative scenario for getValidModelId functionality.
+     */
+    @Test
+    public void validateNegativeScenarioGetModelIdTest() {
+        int i = 0;
+        for (; i < invalidVal.length; i++) {
+            try {
+                getValidModelId(invalidVal[i]);
+            } catch (IllegalArgumentException e) {
+                assertEquals(e.getMessage(), "Invalid model id " +
+                        invalidVal[i]);
+            }
+        }
+
+        if (i != invalidVal.length) {
+            throw new RuntimeException(E_INVAL);
+        }
+    }
+
+    /**
+     * Test positive scenario for user supplied model id functionality.
+     */
+    @Test
+    public void validateSuppliedModelIdTest() {
+        for (int i = 0; i < EXPECTED.length; i++) {
+            if (!EXPECTED[i].matches(REGEX)) {
+                throw new RuntimeException(E_INVAL);
+            }
+        }
+    }
+
+    /**
+     * Test negative scenario for user supplied model id functionality.
+     */
+    @Test
+    public void validateNegativeScenarioSuppliedModelIdTest() {
+        for (int i = 0; i < invalidVal1.length; i++) {
+            if (invalidVal1[i].matches(REGEX)) {
+                throw new RuntimeException(E_INVAL);
+            }
+        }
+    }
+}
diff --git a/compiler/plugin/utils/src/test/resources/CopyrightHeader.txt b/compiler/plugin/utils/src/test/resources/CopyrightHeader.txt
new file mode 100644
index 0000000..2cbed45
--- /dev/null
+++ b/compiler/plugin/utils/src/test/resources/CopyrightHeader.txt
@@ -0,0 +1,14 @@
+ *
+ * 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.
+ */
+