[ONOS-6764][ONOS-6673] YANG Runtime: GET API's for YANG Models and YANG Compiler & Runtime: Identification of YANG Model with a name.

Change-Id: I01d6ed970b98125d3ae9f8b42f51d25d7087a69a
diff --git a/serializers/xml/pom.xml b/serializers/xml/pom.xml
index 7af054f..7d77764 100644
--- a/serializers/xml/pom.xml
+++ b/serializers/xml/pom.xml
@@ -54,6 +54,7 @@
                 <version>${project.version}</version>
                 <configuration>
                     <yangFilesDir>src/test/resources</yangFilesDir>
+                    <modelId>xml</modelId>
                 </configuration>
                 <executions>
                     <execution>
diff --git a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/DefaultYangModelRegistryTest.java b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/DefaultYangModelRegistryTest.java
new file mode 100644
index 0000000..99a97b4
--- /dev/null
+++ b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/DefaultYangModelRegistryTest.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2017-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.yang.serializers.xml;
+
+import com.google.common.collect.ImmutableSet;
+import org.junit.Test;
+import org.onosproject.yang.compiler.datamodel.YangNode;
+import org.onosproject.yang.compiler.datamodel.YangSchemaNode;
+import org.onosproject.yang.model.DefaultYangModuleId;
+import org.onosproject.yang.model.YangModel;
+import org.onosproject.yang.model.YangModule;
+import org.onosproject.yang.model.YangModuleId;
+import org.onosproject.yang.runtime.impl.DefaultYangModelRegistry;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.processSchemaRegistry;
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.registry;
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.unRegister;
+
+/**
+ * Unit test for model registry with model id configured using maven as "xml".
+ */
+public class DefaultYangModelRegistryTest {
+
+    private static final String SCHEMA_NAME_3 = "animal";
+    private static final String SCHEMA_NAME_4_15 = "Logistics-manager@2016-05-24";
+    private static final String SCHEMA_NAME_4 = "Logistics-manager";
+    private static final String SCHEMA_NAME_5 = "attributes";
+    private static final String MODEL_ID = "xml";
+
+    private DefaultYangModelRegistry setUp() {
+        processSchemaRegistry();
+        return registry();
+    }
+
+    /**
+     * Unit test case in which schema node should be present.
+     *
+     * @throws IOException when fails to do IO operation
+     */
+    @Test
+    public void testForGetSchemaNode()
+            throws IOException {
+
+        List<YangNode> nodes = new ArrayList<>();
+        DefaultYangModelRegistry registry = setUp();
+
+        YangModel model = registry.getModel(MODEL_ID);
+        assertThat(true, is(model != null));
+
+        Set<YangModule> modules = getYangModules(model, MODEL_ID);
+        assertThat(true, is(modules.size() != 0));
+        assertThat(true, is(modules != null));
+
+        YangModuleId moduleId = new DefaultYangModuleId("animal",
+                                                        "2016-06-24");
+
+        YangModule module = registry.getModule(moduleId);
+        assertThat(true, is(moduleId.equals(module.getYangModuleId())));
+
+        YangSchemaNode yangNode = registry.getForSchemaName(SCHEMA_NAME_3);
+        assertThat(true, is(SCHEMA_NAME_3.equals(yangNode.getName())));
+
+        //Unregister service
+        nodes.add((YangNode) yangNode);
+        unRegister(nodes);
+
+        yangNode = registry.getForSchemaName(SCHEMA_NAME_3);
+        assertThat(true, is(yangNode == null));
+
+        module = registry.getModule(moduleId);
+        assertThat(true, is(module == null));
+
+        Set<YangModel> models = registry.getModels();
+        assertThat(true, is(models.size() == 0));
+
+        model = registry.getModel(MODEL_ID);
+        assertThat(true, is(model == null));
+
+        modules = getYangModules(model, MODEL_ID);
+        assertThat(true, is(modules.size() == 0));
+    }
+
+    /**
+     * Unit test case in which schema node should be present with multi
+     * revisions.
+     *
+     * @throws IOException when fails to do IO operation
+     */
+    @Test
+    public void testForGetSchemaNodeWhenNoRevision()
+            throws IOException {
+
+        DefaultYangModelRegistry registry = setUp();
+
+        List<YangNode> nodes = new ArrayList<>();
+
+        YangModel model = registry.getModel(MODEL_ID);
+        assertThat(true, is(model != null));
+
+        Set<YangModule> modules = getYangModules(model, MODEL_ID);
+        assertThat(true, is(modules.size() != 0));
+        assertThat(true, is(modules != null));
+
+        YangModuleId moduleId = new DefaultYangModuleId("Logistics-manager",
+                                                        "2016-05-24");
+
+        YangModule module = registry.getModule(moduleId);
+        assertThat(true, is(moduleId.equals(module.getYangModuleId())));
+
+        //Service with rev.
+        YangSchemaNode yangNode = registry.getForSchemaName(SCHEMA_NAME_4_15);
+        assertThat(true, is(SCHEMA_NAME_4.equals(yangNode.getName())));
+
+        yangNode = registry.getForSchemaName(SCHEMA_NAME_4);
+        assertThat(true, is(SCHEMA_NAME_4.equals(yangNode.getName())));
+
+        //unregister SERVICE_NAME_REV_15.
+        nodes.add((YangNode) yangNode);
+        unRegister(nodes);
+
+        yangNode = registry.getForSchemaName(SCHEMA_NAME_4);
+        assertThat(true, is(yangNode == null));
+
+        //Here the yangNode should be the node which does not have revision.
+        // asset should pass with false.
+        yangNode = registry.getForSchemaName(SCHEMA_NAME_5);
+        assertThat(true, is(((YangNode) yangNode).getRevision() == null));
+
+        //---------------------------------------------------------------//
+
+        module = registry.getModule(moduleId);
+        assertThat(true, is(module == null));
+
+        Set<YangModel> models = registry.getModels();
+        assertThat(true, is(models.size() == 0));
+
+        model = registry.getModel(MODEL_ID);
+        assertThat(true, is(model == null));
+
+        modules = getYangModules(model, MODEL_ID);
+        assertThat(true, is(modules.size() == 0));
+    }
+
+    private Set<YangModule> getYangModules(YangModel model, String modelId) {
+        Set<org.onosproject.yang.model.YangModule> modules =
+                new LinkedHashSet<>();
+        if (model != null) {
+            modules.addAll(model.getYangModules());
+        }
+        return ImmutableSet.copyOf(modules);
+    }
+}
diff --git a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/MockYangSchemaNodeProvider.java b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/MockYangSchemaNodeProvider.java
deleted file mode 100644
index f71f2c8..0000000
--- a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/MockYangSchemaNodeProvider.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * Copyright 2017-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.yang.serializers.xml;
-
-import org.onosproject.yang.compiler.datamodel.YangNode;
-import org.onosproject.yang.compiler.datamodel.YangSchemaNode;
-import org.onosproject.yang.model.YangModel;
-import org.onosproject.yang.runtime.AppModuleInfo;
-import org.onosproject.yang.runtime.DefaultAppModuleInfo;
-import org.onosproject.yang.runtime.DefaultModelRegistrationParam;
-import org.onosproject.yang.runtime.ModelRegistrationParam;
-import org.onosproject.yang.runtime.YangModelRegistry;
-import org.onosproject.yang.runtime.impl.DefaultYangModelRegistry;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-
-import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.deSerializeDataModel;
-import static org.onosproject.yang.compiler.utils.UtilConstants.TEMP;
-import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.deleteDirectory;
-import static org.onosproject.yang.runtime.helperutils.YangApacheUtils.processModuleId;
-import static org.onosproject.yang.runtime.helperutils.YangApacheUtils.processYangModel;
-import static org.onosproject.yang.runtime.RuntimeHelper.addLinkerAndJavaInfo;
-import static org.onosproject.yang.runtime.RuntimeHelper.getInterfaceClassName;
-
-/**
- * Represents mock bundle context. provides bundle context for YSR to do unit
- * testing.
- */
-public class MockYangSchemaNodeProvider {
-
-    private static final String FS = File.separator;
-    private static final String PATH = System.getProperty("user.dir") +
-            FS + "target" + FS + "classes" + FS;
-    private static final String SER_FILE_PATH = "yang" + FS + "resources" +
-            FS + "YangMetaData.ser";
-    private static final String META_PATH = PATH + SER_FILE_PATH;
-    private static final String TEMP_FOLDER_PATH = PATH + TEMP;
-    private YangModelRegistry reg = new DefaultYangModelRegistry();
-    private List<YangNode> nodes = new ArrayList<>();
-
-    /**
-     * Creates an instance of mock bundle context.
-     */
-    public MockYangSchemaNodeProvider() {
-    }
-
-    /**
-     * Process YANG schema node for a application.
-     */
-    public void processSchemaRegistry() {
-        try {
-            //Need to deserialize generated meta data file for unit tests.
-            Set<YangNode> appNode = deSerializeDataModel(META_PATH);
-            addLinkerAndJavaInfo(appNode);
-            nodes.addAll(appNode);
-            reg.registerModel(prepareParam(nodes));
-            deleteDirectory(TEMP_FOLDER_PATH);
-        } catch (IOException e) {
-        }
-    }
-
-    /**
-     * Unregister given nodes from runtime service.
-     *
-     * @param nodes list of nodes
-     */
-    public void unRegister(List<YangNode> nodes) {
-        reg.unregisterModel(prepareParam(nodes));
-    }
-
-    /**
-     * Prepares model registration parameter.
-     *
-     * @param nodes list of nodes
-     * @return model registration parameter
-     */
-    private ModelRegistrationParam prepareParam(List<YangNode> nodes) {
-        //Process loading class file.
-        String appName;
-        ClassLoader classLoader = getClass().getClassLoader();
-
-        //Create model registration param.
-        ModelRegistrationParam.Builder b =
-                DefaultModelRegistrationParam.builder();
-
-        //create a new YANG model
-        YangModel model = processYangModel(META_PATH, nodes);
-        //set YANG model
-        b.setYangModel(model);
-
-        Iterator<YangNode> it = nodes.iterator();
-        while (it.hasNext()) {
-            YangSchemaNode node = it.next();
-
-            //If service class is not generated then use
-            // interface file to load this class.
-            appName = getInterfaceClassName(node);
-            Class<?> cls;
-            try {
-                cls = classLoader.loadClass(appName);
-            } catch (ClassNotFoundException e) {
-                continue;
-            }
-
-            //generate app info.
-            AppModuleInfo info = new DefaultAppModuleInfo(cls, null);
-            b.addAppModuleInfo(processModuleId((YangNode) node), info);
-        }
-        return b.build();
-    }
-
-    /**
-     * Returns schema registry.
-     *
-     * @return schema registry
-     */
-    public DefaultYangModelRegistry registry() {
-        return (DefaultYangModelRegistry) reg;
-    }
-}
diff --git a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/MockYangSerializerContext.java b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/MockYangSerializerContext.java
index c89700b..56125d8 100644
--- a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/MockYangSerializerContext.java
+++ b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/MockYangSerializerContext.java
@@ -20,18 +20,18 @@
 import org.onosproject.yang.runtime.Annotation;
 import org.onosproject.yang.runtime.DefaultAnnotation;
 import org.onosproject.yang.runtime.YangSerializerContext;
-import org.onosproject.yang.runtime.impl.DefaultYangModelRegistry;
 
 import java.util.LinkedList;
 import java.util.List;
 
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.processSchemaRegistry;
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.registry;
+
 /**
  * Tests the default schema context provider methods.
  */
 public class MockYangSerializerContext implements YangSerializerContext {
 
-    private static MockYangSchemaNodeProvider schemaProvider =
-            new MockYangSchemaNodeProvider();
     private static final String NETCONF_NS =
             "urn:ietf:params:xml:ns:netconf:base:1.0";
     private static final String XMNLS_NC = "xmlns:xc";
@@ -39,9 +39,8 @@
 
     @Override
     public SchemaContext getContext() {
-        schemaProvider.processSchemaRegistry();
-        DefaultYangModelRegistry registry = schemaProvider.registry();
-        return registry;
+        processSchemaRegistry();
+        return registry();
     }
 
     @Override