Merge "Fix two neutron service defects"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.xtend
index 2218023cafe16ff1b53ad6131c21b390f87882b8..624178569d4e71551507f7733cec7c93bf4b032c 100644 (file)
@@ -24,9 +24,14 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
 import org.opendaylight.yangtools.yang.model.api.SchemaContext
 
 import static com.google.common.base.Preconditions.*
+import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition
+import java.util.concurrent.ConcurrentHashMap
+
+class ControllerContext implements SchemaServiceListener {
+
+    val static ControllerContext INSTANCE = new ControllerContext
 
-class ControllerContext {
-    
     val static NULL_VALUE = "null"
 
     @Property
@@ -34,6 +39,18 @@ class ControllerContext {
 
     private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
     private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
+    private val Map<QName,RpcDefinition> qnameToRpc = new ConcurrentHashMap();
+    
+
+    private new() {
+        if (INSTANCE != null) {
+            throw new IllegalStateException("Already instantiated");
+        }
+    }
+
+    static def getInstance() {
+        return INSTANCE
+    }
 
     public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
         val ret = InstanceIdentifier.builder();
@@ -196,6 +213,7 @@ class ControllerContext {
         if (targetNode instanceof ListSchemaNode) {
             val listNode = targetNode as ListSchemaNode;
             val keysSize = listNode.keyDefinition.size
+
             // every key has to be filled
             if ((strings.length - consumed) < keysSize) {
                 return null;
@@ -205,6 +223,7 @@ class ControllerContext {
             var i = 0;
             for (key : listNode.keyDefinition) {
                 val uriKeyValue = uriKeyValues.get(i);
+
                 // key value cannot be NULL
                 if (uriKeyValue.equals(NULL_VALUE)) {
                     return null
@@ -215,6 +234,7 @@ class ControllerContext {
             consumed = consumed + i;
             builder.nodeWithKey(targetNode.QName, keyValues);
         } else {
+
             // Only one instance of node is allowed
             builder.node(targetNode.QName);
         }
@@ -256,6 +276,27 @@ class ControllerContext {
         }
     }
 
-    public def QName toRpcQName(String name) {
+    public def QName toQName(String name) {
+        val module = name.toModuleName;
+        val node = name.toNodeName;
+        val namespace = moduleNameToUri.get(module);
+        return new QName(namespace,null,node);
+    }
+
+    override onGlobalContextUpdated(SchemaContext context) {
+        this.schemas = context;
+        for(operation : context.operations) {
+            val qname = new QName(operation.QName.namespace,null,operation.QName.localName);
+            qnameToRpc.put(qname,operation);
+        }
     }
+    
+    def ContainerSchemaNode getRpcOutputSchema(QName name) {
+        qnameToRpc.get(name)?.output;
+    }
+    
+    def ContainerSchemaNode getRpcInputSchema(QName name) {
+        qnameToRpc.get(name)?.input;
+    }
+
 }