RpcRegistration and RpcResult for IdManager. 90/19590/1
authorDimple Jain <dimple.jain@ericsson.com>
Tue, 5 May 2015 10:00:17 +0000 (15:30 +0530)
committerDimple Jain <dimple.jain@ericsson.com>
Tue, 5 May 2015 10:03:06 +0000 (15:33 +0530)
Signed-off-by: Dimple Jain <dimple.jain@ericsson.com>
Change-Id: I28eb13889a298976a31c89034adafd15cab77f97

idmanager/idmanager-impl/src/main/config/default-config.xml
idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManager.java
idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManagerServiceProvider.java
idmanager/idmanager-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/idmanager/impl/rev150325/IdmanagerImplModule.java
idmanager/idmanager-impl/src/main/yang/idmanager-impl.yang

index eccf8afdaab556072298a57acf4efbdf6d39ffc0..55fbb666e21b580c061a4521522f335f7a01d696 100644 (file)
@@ -23,6 +23,10 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
             <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-broker-osgi-registry</type>
             <name>binding-osgi-broker</name>
           </broker>
             <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-broker-osgi-registry</type>
             <name>binding-osgi-broker</name>
           </broker>
+          <rpc-registry>
+            <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-rpc-registry</type>
+            <name>binding-rpc-broker</name>
+          </rpc-registry>
         </module>
       </modules>
     </data>
         </module>
       </modules>
     </data>
index 2f6cec8ff8af508e7700b905e216b4e946f6fb2d..7eb3bd3a0ad1e16b3132303a7f6c942af5dbd7a0 100644 (file)
@@ -19,8 +19,8 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.*;
@@ -28,8 +28,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.pools.id.pool.*;
 
 import java.math.BigInteger;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.pools.id.pool.*;
 
 import java.math.BigInteger;
-import java.util.Collection;
-import java.util.Collections;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Future;
 
 import java.util.List;
 import java.util.concurrent.Future;
 
@@ -55,7 +54,7 @@ public class IdManager implements IdManagerService, AutoCloseable{
     }
 
     public IdManager(final DataBroker db) {
     }
 
     public IdManager(final DataBroker db) {
-       broker = db;
+        broker = db;
     }
 
     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
     }
 
     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
@@ -80,6 +79,13 @@ public class IdManager implements IdManagerService, AutoCloseable{
         Futures.addCallback(tx.submit(), callback);
     }
 
         Futures.addCallback(tx.submit(), callback);
     }
 
+    private <T extends DataObject> void asyncUpdate(LogicalDatastoreType datastoreType,
+                                                    InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
+        WriteTransaction tx = broker.newWriteOnlyTransaction();
+        tx.merge(datastoreType, path, data, true);
+        Futures.addCallback(tx.submit(), callback);
+    }
+
     @Override
     public Future<RpcResult<Void>> createIdPool(CreateIdPoolInput input)
     {
     @Override
     public Future<RpcResult<Void>> createIdPool(CreateIdPoolInput input)
     {
@@ -87,22 +93,31 @@ public class IdManager implements IdManagerService, AutoCloseable{
         String poolName = input.getPoolName();
         long startIndx = input.getIdStart();
         long poolSize = input.getPoolSize().longValue();
         String poolName = input.getPoolName();
         long startIndx = input.getIdStart();
         long poolSize = input.getPoolSize().longValue();
+        RpcResultBuilder<Void> rpcResultBuilder;
 
         LOG.debug("poolName: %s, startIndx: %d , poolSize: %d ", poolName, startIndx,  poolSize);
 
 
         LOG.debug("poolName: %s, startIndx: %d , poolSize: %d ", poolName, startIndx,  poolSize);
 
-        InstanceIdentifier.InstanceIdentifierBuilder<IdPool> idBuilder =
-                InstanceIdentifier.builder(Pools.class).child(IdPool.class, new IdPoolKey(poolName));
-        InstanceIdentifier<IdPool> id = idBuilder.build();
-        Optional<IdPool> pool = read(LogicalDatastoreType.OPERATIONAL, id);
-        if (!pool.isPresent()) {
-            LOG.debug("Creating a new global pool: %s " ,poolName);
-            IdPool newPool = getPoolInterface(poolName, startIndx, poolSize);
-            asyncWrite(LogicalDatastoreType.OPERATIONAL, id, newPool, DEFAULT_CALLBACK);
+        try {
+            InstanceIdentifier.InstanceIdentifierBuilder<IdPool> idBuilder =
+                    InstanceIdentifier.builder(Pools.class).child(IdPool.class, new IdPoolKey(poolName));
+            InstanceIdentifier<IdPool> id = idBuilder.build();
+            Optional<IdPool> pool = read(LogicalDatastoreType.OPERATIONAL, id);
+            if (!pool.isPresent()) {
+                LOG.debug("Creating a new global pool: {} ", poolName);
+                IdPool newPool = getPoolInterface(poolName, startIndx, poolSize);
+                LOG.debug("NewPool: {}", newPool);
+                asyncWrite(LogicalDatastoreType.OPERATIONAL, id, newPool, DEFAULT_CALLBACK);
 
 
+            }
+
+            rpcResultBuilder = RpcResultBuilder.success();
+        }
+        catch(Exception e){
+            LOG.error("Creation of global pool {} failed due to {}" ,poolName, e);
+            rpcResultBuilder = RpcResultBuilder.failed();
         }
 
         }
 
-       // return Futures.immediateFuture(RpcResult<Void>);
-        return null;
+        return Futures.immediateFuture(rpcResultBuilder.build());
     }
 
 
     }
 
 
@@ -112,45 +127,54 @@ public class IdManager implements IdManagerService, AutoCloseable{
         String poolName = input.getPoolName();
         String idKey = input.getIdKey();
 
         String poolName = input.getPoolName();
         String idKey = input.getIdKey();
 
-        LOG.debug("poolName: %s ,idKey: %s", poolName, idKey);
-
-        InstanceIdentifier.InstanceIdentifierBuilder<IdPool> idBuilder =
-                InstanceIdentifier.builder(Pools.class).child(IdPool.class, new IdPoolKey(poolName));
-        InstanceIdentifier<IdPool> id = idBuilder.build();
-        Optional<IdPool> globalPool = read(LogicalDatastoreType.OPERATIONAL, id);
-        Long newIdValue = null;
-        GeneratedIds newGenId = null;
-        if (globalPool.isPresent()) {
-            IdPool pool = globalPool.get();
-            List<GeneratedIds> generatedIds = pool.getGeneratedIds();
-
-            if (!generatedIds.isEmpty()) {
-                for (GeneratedIds gen_id : generatedIds) {
-                    if (gen_id.getIdKey().equals(idKey)) {
-                        newIdValue = gen_id.getIdValue();
-                        LOG.debug("Existing id for the key %s ", idKey);
-                    }
+        LOG.debug("poolName: {} ,idKey: {}", poolName, idKey);
+        RpcResultBuilder<GetUniqueIdOutput> rpcResultBuilder;
 
 
+        try {
+            InstanceIdentifier.InstanceIdentifierBuilder<IdPool> idBuilder =
+                    InstanceIdentifier.builder(Pools.class).child(IdPool.class, new IdPoolKey(poolName));
+            InstanceIdentifier<IdPool> id = idBuilder.build();
+            Optional<IdPool> globalPool = read(LogicalDatastoreType.OPERATIONAL, id);
+            GetUniqueIdOutputBuilder output = new GetUniqueIdOutputBuilder();
+            Long newIdValue = null;
+            GeneratedIds newGenId = null;
+            if (globalPool.isPresent()) {
+                IdPool pool = globalPool.get();
+                List<GeneratedIds> generatedIds = pool.getGeneratedIds();
+
+                if (generatedIds == null) {
+                    generatedIds = new ArrayList<GeneratedIds>();
                 }
                 }
-            }
-            synchronized(this){
-                if (newIdValue == null) {
-                    LOG.debug("Creating a new id for the pool: %s ", poolName);
-                    newIdValue = (long) generatedIds.size() + 1;
-                    newGenId = getIdsInterface(idKey, newIdValue);
-                    generatedIds.add(newGenId);
-                    asyncWrite(LogicalDatastoreType.OPERATIONAL, id, pool, DEFAULT_CALLBACK);
+                if (!generatedIds.isEmpty()) {
+                    for (GeneratedIds gen_id : generatedIds) {
+                        if (gen_id.getIdKey().equals(idKey)) {
+                            newIdValue = gen_id.getIdValue();
+                            LOG.debug("Existing id {} for the key {} ", idKey, newIdValue);
+                        }
+
+                    }
+                }
+                synchronized (this) {
+                    if (newIdValue == null) {
+                        newIdValue = (long) generatedIds.size() + 1;
+                        LOG.debug("Creating a new id {} for the pool: {} ", newIdValue, poolName);
+                        newGenId = getIdsInterface(idKey, newIdValue);
+                        generatedIds.add(newGenId);
+                        pool = new IdPoolBuilder(pool).setGeneratedIds(generatedIds).build();
+                        asyncUpdate(LogicalDatastoreType.OPERATIONAL, id, pool, DEFAULT_CALLBACK);
+                    }
                 }
                 }
+                output.setIdValue(newIdValue);
             }
 
             }
 
-            GetUniqueIdOutputBuilder output = new GetUniqueIdOutputBuilder();
-            output.setIdValue(newIdValue);
-
+            rpcResultBuilder = RpcResultBuilder.success();
+            rpcResultBuilder.withResult(output.build());
+        }
+        catch(Exception e){
+            LOG.error("Creation of id for the key {} , global pool {} failed due to {}" ,idKey, poolName, e);
+            rpcResultBuilder = RpcResultBuilder.failed();
         }
         }
-        /*            Collection<RpcError> errors = Collections.emptyList();
-            RpcResult<GetUniqueIdOutput> result = Rpcs.getRpcResult(true, output.build(), errors);
-            return Futures.immediateFuture(result);*/
-        return null;
+        return Futures.immediateFuture(rpcResultBuilder.build());
     }
 
 
     }
 
 
@@ -166,14 +190,14 @@ public class IdManager implements IdManagerService, AutoCloseable{
     }
 
     private static final FutureCallback<Void> DEFAULT_CALLBACK =
     }
 
     private static final FutureCallback<Void> DEFAULT_CALLBACK =
-            new FutureCallback<Void>() {
-                public void onSuccess(Void result) {
-                    LOG.debug("Success in Datastore write operation");
-                }
+        new FutureCallback<Void>() {
+            public void onSuccess(Void result) {
+                LOG.debug("Success in Datastore write operation");
+            }
 
 
-                public void onFailure(Throwable error) {
-                    LOG.error("Error in Datastore write operation", error);
-                };
+            public void onFailure(Throwable error) {
+                LOG.error("Error in Datastore write operation", error);
             };
             };
+        };
 
 }
 
 }
index f94d59f123330e0422949a433b539f9714abf219..ab016b1ef26322334da7a603544b827eba04fde6 100644 (file)
@@ -9,9 +9,12 @@
 package org.opendaylight.idmanager;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 package org.opendaylight.idmanager;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
 
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
 
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -21,21 +24,35 @@ public class IdManagerServiceProvider implements BindingAwareProvider,
 
         private static final Logger LOG = LoggerFactory.getLogger(IdManagerServiceProvider.class);
         private IdManager idManager;
 
         private static final Logger LOG = LoggerFactory.getLogger(IdManagerServiceProvider.class);
         private IdManager idManager;
+        private RpcProviderRegistry rpcProviderRegistry;
+
+    public RpcProviderRegistry getRpcProviderRegistry() {
+        return rpcProviderRegistry;
+    }
+
+    public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
+        this.rpcProviderRegistry = rpcProviderRegistry;
+    }
 
         @Override
 
         @Override
-        public void onSessionInitiated(ProviderContext session){
-            LOG.info("IDManagerserviceProvider Session Initiated");
-            try {
-                final  DataBroker dataBroker = session.getSALService(DataBroker.class);
-                idManager = new IdManager(dataBroker);
-            } catch (Exception e) {
-                LOG.error("Error initializing services", e);
-            }
+    public void onSessionInitiated(ProviderContext session){
+        LOG.info("IDManagerserviceProvider Session Initiated");
+        try {
+            final  DataBroker dataBroker = session.getSALService(DataBroker.class);
+            idManager = new IdManager(dataBroker);
+            final BindingAwareBroker.RpcRegistration<IdManagerService> rpcRegistration = getRpcProviderRegistry().addRpcImplementation(IdManagerService.class, idManager);
+        } catch (Exception e) {
+            LOG.error("Error initializing services", e);
         }
         }
+    }
 
 
-        @Override
-        public void close() throws Exception {
-            idManager.close();
+    public IdManagerServiceProvider(RpcProviderRegistry rpcRegistry) {
+        this.rpcProviderRegistry = rpcRegistry;
+    }
+
+    @Override
+    public void close() throws Exception {
+        idManager.close();
         }
     }
 
         }
     }
 
index ba9314d15bebe7c7976099006babe3caaf668890..d141e3eef2271c0f3ae41dbd76a5a409c55f2e01 100644 (file)
@@ -8,7 +8,9 @@
 
 package org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.idmanager.impl.rev150325;
 
 
 package org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.idmanager.impl.rev150325;
 
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.idmanager.IdManagerServiceProvider;
 import org.opendaylight.idmanager.IdManagerServiceProvider;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
 
 public class IdmanagerImplModule extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.idmanager.impl.rev150325.AbstractIdmanagerImplModule {
     public IdmanagerImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
 
 public class IdmanagerImplModule extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.idmanager.impl.rev150325.AbstractIdmanagerImplModule {
     public IdmanagerImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
@@ -20,15 +22,14 @@ public class IdmanagerImplModule extends org.opendaylight.yang.gen.v1.urn.openda
     }
 
     @Override
     }
 
     @Override
-    public void customValidation() {
-        // add custom validation form module attributes here.
+    public java.lang.AutoCloseable createInstance() {
+        IdManagerServiceProvider provider = new IdManagerServiceProvider(getRpcRegistryDependency());
+        getBrokerDependency().registerProvider(provider);
+        return provider;
     }
 
     @Override
     }
 
     @Override
-    public java.lang.AutoCloseable createInstance() {
-          IdManagerServiceProvider provider = new IdManagerServiceProvider();
-          getBrokerDependency().registerProvider(provider);
-          return provider;
+    public void customValidation() {
+        // add custom validation form module attributes here.
     }
     }
-
 }
 }
index ec07c7b5e696de27462b64e63d77bf203a00715b..c1425075100baeae49f7e942715173d802aeed95 100644 (file)
@@ -30,6 +30,15 @@ module idmanager-impl {
                     }
                 }
             }
                     }
                 }
             }
+            container rpc-registry {
+                 uses config:service-ref {
+                      refine type {
+                         mandatory true;
+                         config:required-identity md-sal-binding:binding-rpc-registry;
+                      }
+                 }
+            }
+
         }
     }
 }
         }
     }
 }