Merge changes I0e8c20b4,Id6b80c7e
authorVivek Srivastava <vivek.v.srivastava@ericsson.com>
Fri, 17 Apr 2015 06:53:38 +0000 (06:53 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 17 Apr 2015 06:53:38 +0000 (06:53 +0000)
* changes:
  IdManager implementation
  Yang model for IdManager

idmanager/idmanager-api/src/main/yang/id-manager.yang
idmanager/idmanager-impl/pom.xml
idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManager.java [new file with mode: 0644]
idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManagerServiceProvider.java [new file with mode: 0644]

index 7066442aabd8bd0174f5d9ce3035ae2a0e9f27d6..79bd2ffa5ec3c78438f42b2f5ab82c1fba1a8e9f 100644 (file)
@@ -1,4 +1,4 @@
-module odl-id-manager {
+module id-manager {
     namespace "urn:opendaylight:vpnservice:idmanager";
     prefix idmgr;
 
@@ -6,35 +6,38 @@ module odl-id-manager {
         description "ID generator and manager Service module";
     }
 
-    
-       list id-pool {
-               key "pool-name";
-               leaf id-start { type uint32; }
-               leaf pool-size { type uint64; }
-               leaf pool-name { type string; }
-               list generated-ids {
-                       key "id-key";
-                       leaf id-key { type string; }
-                       leaf id-value { type uint32; }
-               }
-       }
-       
-       rpc createIdPool {
-               input {
-                       leaf pool-name { type string; }
-                       leaf id-start { type uint32; }
-                       leaf pool-size { type uint64; }
-               }
-       }
-       
-       rpc getUniqueId {
-               input {
-                       leaf pool-name { type string; }
-                       leaf id-key {type string; }
-               }
-               output {
-                       leaf id-value { type uint32; }
-               }
-       }
-    
+    container pools {
+      description
+        "id pool instances";
+      config false;
+      list id-pool {
+        key "pool-name";
+        leaf id-start { type uint32;}
+        leaf pool-size { type uint64;}
+        leaf pool-name { type string;}
+        list generated-ids {
+          key "id-key";
+          leaf id-key { type string;}
+          leaf id-value { type uint32;}
+        }
+      }
+    }
+
+    rpc createIdPool {
+      input {
+        leaf pool-name { type string; }
+        leaf id-start { type uint32; }
+        leaf pool-size { type uint64; }
+      }
+    }
+
+    rpc getUniqueId {
+      input {
+        leaf pool-name { type string; }
+        leaf id-key {type string; }
+      }
+      output {
+        leaf id-value { type uint32; }
+      }
+    }
 }
index 03ecb36c8ac9b7cae674fdec0d2acd20dcf1c4d0..06b5f88a8e9740e905efd14bd897af81810d5f1b 100644 (file)
@@ -27,12 +27,16 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
-
     <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-all</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.vpnservice</groupId>
+      <artifactId>idmanager-api</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
diff --git a/idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManager.java b/idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManager.java
new file mode 100644 (file)
index 0000000..2f6cec8
--- /dev/null
@@ -0,0 +1,179 @@
+/*
+ * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.idmanager;
+
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+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.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.*;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.pools.*;
+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.List;
+import java.util.concurrent.Future;
+
+
+public class IdManager implements IdManagerService, AutoCloseable{
+    private static final Logger LOG = LoggerFactory.getLogger(IdManager.class);
+    private ListenerRegistration<DataChangeListener> listenerRegistration;
+    private final DataBroker broker;
+
+
+
+    @Override
+    public void close() throws Exception {
+        if (listenerRegistration != null) {
+            try {
+                listenerRegistration.close();
+            } catch (final Exception e) {
+                LOG.error("Error when cleaning up DataChangeListener.", e);
+            }
+            listenerRegistration = null;
+        }
+        LOG.info("IDManager Closed");
+    }
+
+    public IdManager(final DataBroker db) {
+       broker = db;
+    }
+
+    private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
+                                                    InstanceIdentifier<T> path) {
+
+        ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
+
+        Optional<T> result = Optional.absent();
+        try {
+            result = tx.read(datastoreType, path).get();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        return result;
+    }
+
+    private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
+                                                   InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
+        WriteTransaction tx = broker.newWriteOnlyTransaction();
+        tx.put(datastoreType, path, data, true);
+        Futures.addCallback(tx.submit(), callback);
+    }
+
+    @Override
+    public Future<RpcResult<Void>> createIdPool(CreateIdPoolInput input)
+    {
+
+        String poolName = input.getPoolName();
+        long startIndx = input.getIdStart();
+        long poolSize = input.getPoolSize().longValue();
+
+        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);
+
+        }
+
+       // return Futures.immediateFuture(RpcResult<Void>);
+        return null;
+    }
+
+
+    @Override
+    public Future<RpcResult<GetUniqueIdOutput>> getUniqueId(GetUniqueIdInput input){
+
+        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);
+                    }
+
+                }
+            }
+            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);
+                }
+            }
+
+            GetUniqueIdOutputBuilder output = new GetUniqueIdOutputBuilder();
+            output.setIdValue(newIdValue);
+
+        }
+        /*            Collection<RpcError> errors = Collections.emptyList();
+            RpcResult<GetUniqueIdOutput> result = Rpcs.getRpcResult(true, output.build(), errors);
+            return Futures.immediateFuture(result);*/
+        return null;
+    }
+
+
+    private IdPool getPoolInterface(String poolName, long startIndx, long poolSize) {
+        BigInteger size = BigInteger.valueOf(poolSize);
+        return new IdPoolBuilder().setKey(new IdPoolKey(poolName)).setPoolName(poolName).setIdStart(startIndx)
+                .setPoolSize(size).build();
+    }
+
+    private GeneratedIds getIdsInterface(String idKey, long newIdVal) {
+        return new GeneratedIdsBuilder().setKey(new GeneratedIdsKey(idKey)).setIdKey(idKey)
+                .setIdValue(newIdVal).build();
+    }
+
+    private static final FutureCallback<Void> DEFAULT_CALLBACK =
+            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);
+                };
+            };
+
+}
diff --git a/idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManagerServiceProvider.java b/idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManagerServiceProvider.java
new file mode 100644 (file)
index 0000000..f94d59f
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.idmanager;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
+import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class IdManagerServiceProvider implements BindingAwareProvider,
+            AutoCloseable {
+
+        private static final Logger LOG = LoggerFactory.getLogger(IdManagerServiceProvider.class);
+        private IdManager idManager;
+
+        @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);
+            }
+        }
+
+        @Override
+        public void close() throws Exception {
+            idManager.close();
+        }
+    }
+
+
+
+
+