Implementation of SchemaContextProvider interface

Change-Id: Id3a6b53aecbb1982c0fbef076cca1f2b119c9d0d
diff --git a/model/src/main/java/org/onosproject/yang/model/SchemaContextProvider.java b/model/src/main/java/org/onosproject/yang/model/SchemaContextProvider.java
index a891818..98bd5d9 100644
--- a/model/src/main/java/org/onosproject/yang/model/SchemaContextProvider.java
+++ b/model/src/main/java/org/onosproject/yang/model/SchemaContextProvider.java
@@ -36,13 +36,11 @@
 
     /**
      * Returns rpc context corresponding to a given resource identifier.
-     * It returns null if module is not registered.
      *
      * @param id absolute resource identifier
      * @return rpc context
-     * @throws IllegalArgumentException when module as per resource
-     *                                  identifier is registered, but given
-     *                                  rpc is invalid
+     * @throws IllegalArgumentException when module is not reqistered or rpc
+     *                                  is invalid
      */
     RpcContext getRpcContext(ResourceId id);
 }
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultSchemaContextProvider.java b/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultSchemaContextProvider.java
new file mode 100644
index 0000000..799c995
--- /dev/null
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultSchemaContextProvider.java
@@ -0,0 +1,83 @@
+/*
+ * 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.runtime.impl;
+
+import org.onosproject.yang.compiler.datamodel.YangSchemaNode;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.RpcContext;
+import org.onosproject.yang.model.SchemaContext;
+import org.onosproject.yang.model.SchemaContextProvider;
+import org.onosproject.yang.model.SchemaId;
+import org.slf4j.Logger;
+
+import static org.onosproject.yang.compiler.utils.UtilConstants.PERIOD;
+import static org.onosproject.yang.compiler.utils.UtilConstants.SERVICE;
+import static org.onosproject.yang.runtime.RuntimeHelper.getCapitalCase;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Represents Schema Context Provider implementation.
+ */
+public class DefaultSchemaContextProvider implements SchemaContextProvider {
+    private static final Logger log =
+            getLogger(DefaultSchemaContextProvider.class);
+    public DefaultYangModelRegistry reg;
+
+    /**
+     * Creates an instance of Default Schema Context Provider.
+     *
+     * @param registry an instance of Default Yanf Model Registry.
+     */
+    public DefaultSchemaContextProvider(DefaultYangModelRegistry registry) {
+        reg = registry;
+    }
+
+    @Override
+    public SchemaContext getSchemaContext(ResourceId id) {
+        return null;
+    }
+
+    @Override
+    public RpcContext getRpcContext(ResourceId id) {
+        SchemaId schemaId = id.nodeKeys().get(1).schemaId();
+        String rpcname;
+
+        SchemaContext childContext = reg.getChildContext(schemaId);
+        if (childContext == null) {
+            throw new IllegalArgumentException("Module is not registered or " +
+                                                       "RPC name doesn't " +
+                                                       "exist in module.");
+        }
+        YangSchemaNode childNode = (YangSchemaNode) childContext;
+        rpcname = childNode.getJavaAttributeName();
+
+        YangSchemaNode moduleNode = (YangSchemaNode) childNode.getNameSpace();
+        String pkg = moduleNode.getJavaPackage();
+        String moduleClassName = moduleNode.getJavaClassNameOrBuiltInType();
+        String moduleQlName = pkg + PERIOD + getCapitalCase(moduleClassName);
+
+        Class<?> moduleClass = reg.getRegisteredClass(moduleNode);
+        ClassLoader classLoader = moduleClass.getClassLoader();
+        Class rpcServiceIntf = null;
+        try {
+            rpcServiceIntf = classLoader.loadClass(moduleQlName + SERVICE);
+        } catch (ClassNotFoundException e) {
+            log.error("Not able to load service interface class");
+        }
+        return new RpcContext(rpcname, rpcServiceIntf);
+    }
+}
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/RpcContextTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/RpcContextTest.java
new file mode 100644
index 0000000..6f61321
--- /dev/null
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/RpcContextTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.runtime.impl;
+
+import org.junit.Test;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.RpcContext;
+
+import static org.junit.Assert.assertEquals;
+import static org.onosproject.yang.runtime.SerializerHelper.addToResourceId;
+import static org.onosproject.yang.runtime.SerializerHelper.initializeResourceId;
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.processSchemaRegistry;
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.registry;
+
+/**
+ * Test cases for YANG runtime service interface file generation.
+ */
+public class RpcContextTest {
+    ModIdToRscIdConverter builder;
+    DefaultYangModelRegistry reg;
+    public static final String NS = "urn:params:xml:ns:yang:hello";
+    String value = null;
+    TestYangSerializerContext context = new TestYangSerializerContext();
+    DataNode.Builder dBlr;
+
+    /**
+     * Sets up all prerequisite.
+     */
+    private void setUp() {
+        processSchemaRegistry();
+        reg = registry();
+        builder = new ModIdToRscIdConverter(reg);
+    }
+
+    /**
+     * Check for service interface file.
+     */
+    @Test
+    public void checkRpcContext() {
+        setUp();
+        ResourceId.Builder rIdBlr = initializeResourceId(context);
+        rIdBlr = addToResourceId(rIdBlr, "hello-world", NS, value);
+
+        DefaultSchemaContextProvider scp = new DefaultSchemaContextProvider(
+                reg);
+
+        RpcContext context = scp.getRpcContext(rIdBlr.build());
+        assertEquals(context.rpcName(), "helloWorld");
+        assertEquals(context.serviceIntf().toString(), "interface org.onosproject" +
+                ".yang.gen.v1.hello.rev20150105.HelloService");
+    }
+}
diff --git a/runtime/src/test/resources/schemaProviderTestYangFiles/rpc_test.yang b/runtime/src/test/resources/schemaProviderTestYangFiles/rpc_test.yang
new file mode 100644
index 0000000..c1968a3
--- /dev/null
+++ b/runtime/src/test/resources/schemaProviderTestYangFiles/rpc_test.yang
@@ -0,0 +1,23 @@
+module hello {
+    yang-version 1;
+    namespace "urn:params:xml:ns:yang:hello";
+    prefix "hello";
+
+    revision "2015-01-05" {
+        description "Initial revision of hello model";
+    }
+
+    rpc hello-world {
+        input {
+            leaf x {
+                type string;
+            }
+        }
+
+        output {
+            leaf greeting {
+                type string;
+            }
+        }
+    }
+}
\ No newline at end of file