Bug 164
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / BrokerFacade.xtend
index 4c6eb3b10658c61b5b0c4de6e423cfad33ed0643..eb1f6165ca23c5bb17deac73f575a06bfc57fc57 100644 (file)
@@ -1,65 +1,71 @@
 package org.opendaylight.controller.sal.restconf.impl
 
+import javax.ws.rs.WebApplicationException
+import javax.ws.rs.core.Response
 import org.opendaylight.controller.md.sal.common.api.data.DataReader
-import java.net.URI
-import org.opendaylight.yangtools.yang.data.api.CompositeNode
-import org.opendaylight.controller.sal.core.api.data.DataBrokerService
-import org.opendaylight.controller.sal.core.api.model.SchemaService
-import static com.google.common.base.Preconditions.*;
 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession
+import org.opendaylight.controller.sal.core.api.data.DataBrokerService
+import org.opendaylight.controller.sal.rest.impl.RestconfProvider
+import org.opendaylight.yangtools.yang.common.QName
 import org.opendaylight.yangtools.yang.common.RpcResult
-import org.opendaylight.controller.md.sal.common.api.data.DataModificationTransactionFactory
+import org.opendaylight.yangtools.yang.data.api.CompositeNode
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
+
+class BrokerFacade implements DataReader<InstanceIdentifier, CompositeNode> {
 
-class BrokerFacade implements DataReader<String, CompositeNode> {
+    val static BrokerFacade INSTANCE = new BrokerFacade
 
     @Property
     private ConsumerSession context;
-    
+
     @Property
     private DataBrokerService dataService;
     
-    @Property
-    private SchemaService schemaService;
-
-    @Property
-    private extension ControllerContext schemaContext;
+    private new() {
+        if (INSTANCE !== null) {
+            throw new IllegalStateException("Already instantiated");
+        }
+    }
 
+    def static BrokerFacade getInstance() {
+        return INSTANCE
+    }
 
-    def void init() {
-        checkState(dataService !== null)
-        checkState(schemaService !== null)
-        schemaContext = new ControllerContext();
-        schemaContext.schemas = schemaService.globalContext;
+    private def void checkPreconditions() {
+        if (context === null || dataService === null) {
+            throw new WebApplicationException(Response.status(Response.Status.SERVICE_UNAVAILABLE)
+                    .entity(RestconfProvider::NOT_INITALIZED_MSG).build())
+        }
     }
 
-    override readConfigurationData(String path) {
-        val processedPath = path.removePrefixes();
-        return dataService.readConfigurationData(processedPath.toInstanceIdentifier);
+    override readConfigurationData(InstanceIdentifier path) {
+        checkPreconditions
+        return dataService.readConfigurationData(path);
     }
 
-    override readOperationalData(String path) {
-        val processedPath = path.removePrefixes();
-        return dataService.readOperationalData(processedPath.toInstanceIdentifier);
+    override readOperationalData(InstanceIdentifier path) {
+        checkPreconditions
+        return dataService.readOperationalData(path);
     }
-    
-    def RpcResult<CompositeNode> invokeRpc(String type,CompositeNode payload) {
-        val future = context.rpc(type.toRpcQName(),payload);
+
+    def RpcResult<CompositeNode> invokeRpc(QName type, CompositeNode payload) {
+        checkPreconditions
+        val future = context.rpc(type, payload);
         return future.get;
     }
-    
-    def commitConfigurationDataUpdate(String path, CompositeNode payload) {
+
+    def commitConfigurationDataPut(InstanceIdentifier path, CompositeNode payload) {
+        checkPreconditions
         val transaction = dataService.beginTransaction;
-        transaction.putConfigurationData(path.toInstanceIdentifier,payload);
+        transaction.putConfigurationData(pathpayload);
         return transaction.commit()
     }
-    
-    def commitConfigurationDataCreate(String path, CompositeNode payload) {
+
+    def commitOperationalDataPut(InstanceIdentifier path, CompositeNode payload) {
+        checkPreconditions
         val transaction = dataService.beginTransaction;
-        transaction.putConfigurationData(path.toInstanceIdentifier,payload);
+        transaction.putOperationalData(path, payload);
         return transaction.commit()
     }
     
-    private def String removePrefixes(String path) {
-        return path;
-    }
 }