Merge "handling of value with ':' in URI in json serilazer "
diff --git a/compiler/base/datamodel/src/test/java/org/onosproject/yang/compiler/datamodel/CheckValidationTest.java b/compiler/base/datamodel/src/test/java/org/onosproject/yang/compiler/datamodel/CheckValidationTest.java
index 1827aba..a442189 100644
--- a/compiler/base/datamodel/src/test/java/org/onosproject/yang/compiler/datamodel/CheckValidationTest.java
+++ b/compiler/base/datamodel/src/test/java/org/onosproject/yang/compiler/datamodel/CheckValidationTest.java
@@ -21,13 +21,12 @@
 import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangUint16;
 import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangUint32;
 import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangUint64;
+import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangUint8;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
 
 import static org.junit.Assert.assertEquals;
-import static org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangInt8.MAX_VALUE;
-import static org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangInt8.MIN_VALUE;
 
 /**
  * Utility class to check error messages.
@@ -35,8 +34,6 @@
 final class CheckValidationTest {
     private static final String E_MESS = "Exception has not occurred for " +
             "invalid value with type ";
-    private static boolean expOccurred;
-
     private CheckValidationTest() {
     }
 
@@ -48,6 +45,7 @@
      */
     static void dataValidation(YangType node, String value)
             throws DataModelException {
+        boolean expOccurred = false;
         YangDataTypes type = node.getDataType();
         try {
             node.isValidValue(value);
@@ -66,6 +64,7 @@
      */
     static void rangeCheck(YangType node, String value)
             throws DataModelException {
+        boolean expOccurred = false;
         YangDataTypes type = node.getDataType();
         try {
             node.isValidValue(value);
@@ -88,16 +87,16 @@
         StringBuilder msg = new StringBuilder();
         switch (type) {
             case UINT8:
-                if (Integer.valueOf(value) < MIN_VALUE) {
+                if (Integer.valueOf(value) < YangUint8.MIN_VALUE) {
                     msg.append("YANG file error : ")
                             .append(value)
                             .append(" is lesser than minimum value ")
-                            .append(MIN_VALUE).append(".");
-                } else if (Integer.valueOf(value) > MAX_VALUE) {
+                            .append(YangUint8.MIN_VALUE).append(".");
+                } else if (Integer.valueOf(value) > YangUint8.MAX_VALUE) {
                     msg.append("YANG file error : ")
                             .append(value)
                             .append(" is greater than maximum value ")
-                            .append(MAX_VALUE).append(".");
+                            .append(YangUint8.MAX_VALUE).append(".");
                 }
                 break;
             case UINT16:
@@ -194,6 +193,7 @@
             case BINARY:
             case STRING:
             case BOOLEAN:
+            case UNION:
                 msg.append("YANG file error : Input value ").append("\"")
                         .append(value).append("\"")
                         .append(" is not a valid ").append(dataType);
diff --git a/compiler/base/datamodel/src/test/java/org/onosproject/yang/compiler/datamodel/DataValidator.java b/compiler/base/datamodel/src/test/java/org/onosproject/yang/compiler/datamodel/DataValidator.java
index 5d6ac49..2fe012b 100644
--- a/compiler/base/datamodel/src/test/java/org/onosproject/yang/compiler/datamodel/DataValidator.java
+++ b/compiler/base/datamodel/src/test/java/org/onosproject/yang/compiler/datamodel/DataValidator.java
@@ -17,6 +17,8 @@
 
 import org.junit.Test;
 import org.onosproject.yang.compiler.datamodel.exceptions.DataModelException;
+import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangDataTypes;
+import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangInt8;
 
 import static org.onosproject.yang.compiler.datamodel.CheckValidationTest.dataValidation;
 import static org.onosproject.yang.compiler.datamodel.CheckValidationTest.rangeCheck;
@@ -207,10 +209,12 @@
 
         YangType<?> newNode = new YangType<>();
         newNode.setDataType(BINARY);
-        dataValidation(newNode, "  ");
+        dataValidation(newNode, "");
     }
 
-   /* TODO: need to verify .
+    /**
+     * Creating nodes of type ENUMERATION and testing the data validation.
+     */
     @Test
     public void negativeEnum() throws DataModelException {
 
@@ -218,12 +222,11 @@
         newNode.setDataType(YangDataTypes.ENUMERATION);
         YangEnum enum1 = new YangEnum();
         enum1.setNamedValue("sample1");
-
+        enum1.setValue(10);
         YangEnum enum2 = new YangEnum();
         enum2.setNamedValue("sample2");
         enum2.setValue(20);
-
-        YangEnumeration YEnumeration = new YangEnumeration() {
+        YangEnumeration yEnumeration = new YangEnumeration() {
             @Override
             public String getJavaPackage() {
                 return null;
@@ -239,19 +242,21 @@
                 return null;
             }
         };
-        YEnumeration.addEnumInfo(enum1);
-        YEnumeration.addEnumInfo(enum2);
-        newNode.setDataTypeExtendedInfo(YEnumeration);
-        dataValidation(newNode," ");
-        dataValidation(newNode,"123");
-        dataValidation(newNode,"sample1");
+        yEnumeration.addEnumInfo(enum1);
+        yEnumeration.addEnumInfo(enum2);
+        newNode.setDataTypeExtendedInfo(yEnumeration);
+        dataValidation(newNode, " ");
+        dataValidation(newNode, "123");
     }
 
+    /**
+     * Creating nodes of type UNION and testing the data validation.
+     */
     @Test
     public void negativeUnion() throws DataModelException {
-        YangType<?> newNode = new YangType<>();
+
+        YangType<YangUnion> newNode = new YangType<>();
         newNode.setDataType(YangDataTypes.UNION);
-        List<YangType<?>> list = new LinkedList<>();
         YangUnion union = new YangUnion() {
             @Override
             public String getJavaPackage() {
@@ -268,13 +273,14 @@
                 return null;
             }
         };
-        YangUnion YUnion = union;
         YangType<YangInt8> typeInt1 = new YangType<>();
+        typeInt1.setDataType(INT8);
         YangType<YangDecimal64> typeInt2 = new YangType<>();
-        YUnion.addType(typeInt1);
-        YUnion.addType(typeInt2);
-        //  newNode.setDataTypeExtendedInfo(YUnion);
-        dataValidation(newNode,"INT8");
+        typeInt2.setDataType(BOOLEAN);
+        union.addType(typeInt1);
+        union.addType(typeInt2);
+        newNode.setDataTypeExtendedInfo(union);
+        dataValidation(newNode, "abcd");
+        dataValidation(newNode, "-129");
     }
-    */
 }
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/DefaultModelRegistrationParam.java b/runtime/src/main/java/org/onosproject/yang/runtime/DefaultModelRegistrationParam.java
index 9c0b2e3..3451112 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/DefaultModelRegistrationParam.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/DefaultModelRegistrationParam.java
@@ -54,6 +54,11 @@
     }
 
     @Override
+    public boolean ifAppInfoPresent() {
+        return !appInfoMap.isEmpty();
+    }
+
+    @Override
     public int hashCode() {
         return Objects.hash(appInfoMap, model);
     }
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/ModelRegistrationParam.java b/runtime/src/main/java/org/onosproject/yang/runtime/ModelRegistrationParam.java
index 5e69adb..559daab 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/ModelRegistrationParam.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/ModelRegistrationParam.java
@@ -40,6 +40,14 @@
     AppModuleInfo getAppModuleInfo(YangModuleId id);
 
     /**
+     * Returns true for empty map. When application is being registered using
+     * model demo application then app info map will be empty.
+     *
+     * @return true for empty map
+     */
+    boolean ifAppInfoPresent();
+
+    /**
      * Abstraction of an entity that represents builder of model registration
      * parameters.
      */
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java b/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java
index 6ec9f6d..e2d470f 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java
@@ -29,6 +29,7 @@
 import org.onosproject.yang.model.YangModel;
 import org.onosproject.yang.model.YangModuleId;
 import org.onosproject.yang.runtime.AppModuleInfo;
+import org.onosproject.yang.runtime.DefaultModelRegistrationParam;
 import org.onosproject.yang.runtime.ModelRegistrationParam;
 import org.onosproject.yang.runtime.YangModelRegistry;
 import org.slf4j.Logger;
@@ -135,15 +136,42 @@
         Set<YangNode> curNodes = getNodes(model);
         models.add(model);
         AppModuleInfo info;
-        for (YangModuleId id : model.getYangModulesId()) {
-            info = param.getAppModuleInfo(id);
-            if (info != null) {
-                registerModule(curNodes, info);
+        //TODO: changing for models-demo app to work.
+        if (checkForAppInfo(param)) {
+            for (YangModuleId id : model.getYangModulesId()) {
+                info = param.getAppModuleInfo(id);
+                if (info != null) {
+                    registerModule(curNodes, info);
+                }
             }
+        } else {
+            //TODO: check this after demo1:
+            registerWhenAppInfoNull(curNodes);
         }
         updateChildContext(curNodes);
     }
 
+    /**
+     * Registerer all the model if app info is null.
+     *
+     * @param curNodes current nodes
+     */
+    private void registerWhenAppInfoNull(Set<YangNode> curNodes) {
+        String name;
+        for (YangNode node : curNodes) {
+            name = getInterfaceClassName(node);
+            if (!interfaceNameKeyStore.containsKey(name)) {
+                processApplicationContext(node, name);
+            }
+        }
+    }
+
+    /**
+     * Register specific model.
+     *
+     * @param curNodes current nodes
+     * @param info     application info
+     */
     private void registerModule(Set<YangNode> curNodes, AppModuleInfo info) {
         Class<?> service;
         service = info.getModuleClass();
@@ -164,23 +192,44 @@
             YangModel model = checkNotNull(param.getYangModel(), "Model must not be null");
             models.remove(model);
             AppModuleInfo info;
-            for (YangModuleId id : model.getYangModulesId()) {
-                info = param.getAppModuleInfo(id);
-                if (info != null) {
-                    unregisterModule(param, info);
+            //TODO: changing for models-demo app to work
+            if (checkForAppInfo(param)) {
+                for (YangModuleId id : model.getYangModulesId()) {
+                    info = param.getAppModuleInfo(id);
+                    if (info != null) {
+                        unregisterModule(info);
+                    }
+                }
+            } else {
+                Set<YangNode> curNodes = getNodes(model);
+                for (YangNode cur : curNodes) {
+                    processUnReg(getInterfaceClassName(cur));
                 }
             }
         }
     }
 
-    private void unregisterModule(ModelRegistrationParam param, AppModuleInfo info) {
-        YangSchemaNode curNode;
+    /**
+     * Checks if current application has any application info.
+     *
+     * @param param model param
+     * @return if current application has any application info
+     */
+    private boolean checkForAppInfo(ModelRegistrationParam param) {
+        return ((DefaultModelRegistrationParam) param).ifAppInfoPresent();
+    }
+
+    private void unregisterModule(AppModuleInfo info) {
         Class<?> sClass = info.getModuleClass();
         String serviceName = sClass.getName();
         //Remove registered class from store.
         registerClassStore.remove(serviceName);
         //check if service is in app store.
-        curNode = appNameKeyStore.get(serviceName);
+        processUnReg(serviceName);
+    }
+
+    private void processUnReg(String serviceName) {
+        YangSchemaNode curNode = appNameKeyStore.get(serviceName);
         if (curNode == null) {
             curNode = interfaceNameKeyStore.get(serviceName);
         }
@@ -196,13 +245,12 @@
             appNameKeyStore.remove(serviceName);
             nameSpaceSchemaStore.remove(
                     curNode.getNameSpace().getModuleNamespace());
-            log.info(" service class {} of model {} is " +
-                             "unregistered.", sClass
-                             .getSimpleName(), param);
+            log.info(" service class {} of model is " +
+                             "unregistered.", serviceName);
         } else {
             log.error("Either {} service was not registered or " +
                               "already unregistered from model " +
-                              "registry.", sClass.getSimpleName());
+                              "registry.", serviceName);
         }
     }