From d0a26533fa70b9c50ba3d374ab4f32d522a5234d Mon Sep 17 00:00:00 2001 From: Dimple Jain Date: Wed, 15 Apr 2015 16:54:52 +0530 Subject: [PATCH 1/1] IdManager implementation Signed-off-by: Dimple Jain Change-Id: I0e8c20b43406ecddaac8b84a8d3beec7eae826fe --- .../src/main/yang/id-manager.yang | 67 ++++--- idmanager/idmanager-impl/pom.xml | 11 +- .../org/opendaylight/idmanager/IdManager.java | 179 ++++++++++++++++++ .../idmanager/IdManagerServiceProvider.java | 45 +++++ 4 files changed, 262 insertions(+), 40 deletions(-) create mode 100644 idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManager.java create mode 100644 idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManagerServiceProvider.java diff --git a/idmanager/idmanager-api/src/main/yang/id-manager.yang b/idmanager/idmanager-api/src/main/yang/id-manager.yang index 45bdcf20..79bd2ffa 100644 --- a/idmanager/idmanager-api/src/main/yang/id-manager.yang +++ b/idmanager/idmanager-api/src/main/yang/id-manager.yang @@ -1,4 +1,4 @@ -module odl-id-manager { +module id-manager { namespace "urn:opendaylight:vpnservice:idmanager"; prefix idmgr; @@ -6,39 +6,38 @@ module odl-id-manager { description "ID generator and manager Service module"; } - 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; } - } - } + 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 getUniqueId { - input { - leaf pool-name { type string; } - leaf id-key {type string; } - } - output { - 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; } + } + } } diff --git a/idmanager/idmanager-impl/pom.xml b/idmanager/idmanager-impl/pom.xml index 3f01a5be..06b5f88a 100644 --- a/idmanager/idmanager-impl/pom.xml +++ b/idmanager/idmanager-impl/pom.xml @@ -27,17 +27,16 @@ and is available at http://www.eclipse.org/legal/epl-v10.html junit test - org.mockito mockito-all test - - org.opendaylight.vpnservice - idmanager-api - 0.0.1-SNAPSHOT - + + org.opendaylight.vpnservice + idmanager-api + 0.0.1-SNAPSHOT + 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 index 00000000..2f6cec8f --- /dev/null +++ b/idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManager.java @@ -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 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 Optional read(LogicalDatastoreType datastoreType, + InstanceIdentifier path) { + + ReadOnlyTransaction tx = broker.newReadOnlyTransaction(); + + Optional result = Optional.absent(); + try { + result = tx.read(datastoreType, path).get(); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return result; + } + + private void asyncWrite(LogicalDatastoreType datastoreType, + InstanceIdentifier path, T data, FutureCallback callback) { + WriteTransaction tx = broker.newWriteOnlyTransaction(); + tx.put(datastoreType, path, data, true); + Futures.addCallback(tx.submit(), callback); + } + + @Override + public Future> 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 idBuilder = + InstanceIdentifier.builder(Pools.class).child(IdPool.class, new IdPoolKey(poolName)); + InstanceIdentifier id = idBuilder.build(); + Optional 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); + return null; + } + + + @Override + public Future> getUniqueId(GetUniqueIdInput input){ + + String poolName = input.getPoolName(); + String idKey = input.getIdKey(); + + LOG.debug("poolName: %s ,idKey: %s", poolName, idKey); + + InstanceIdentifier.InstanceIdentifierBuilder idBuilder = + InstanceIdentifier.builder(Pools.class).child(IdPool.class, new IdPoolKey(poolName)); + InstanceIdentifier id = idBuilder.build(); + Optional globalPool = read(LogicalDatastoreType.OPERATIONAL, id); + Long newIdValue = null; + GeneratedIds newGenId = null; + if (globalPool.isPresent()) { + IdPool pool = globalPool.get(); + List 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 errors = Collections.emptyList(); + RpcResult 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 DEFAULT_CALLBACK = + new FutureCallback() { + 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 index 00000000..f94d59f1 --- /dev/null +++ b/idmanager/idmanager-impl/src/main/java/org/opendaylight/idmanager/IdManagerServiceProvider.java @@ -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(); + } + } + + + + + -- 2.36.6