idmanager remove usage of @deprecated CacheUtil 82/48682/3
authorMichael Vorburger <vorburger@redhat.com>
Thu, 24 Nov 2016 20:08:38 +0000 (21:08 +0100)
committerMichael Vorburger <vorburger@redhat.com>
Fri, 25 Nov 2016 15:51:23 +0000 (16:51 +0100)
Specifically, this also solves a problem in the
InterfaceManagerConfigurationTest (InterfaceManagerTest) related to
stale content surviving between tests in this cache, due to static (that
problem could have been "resolved" via CacheUtil.destroyCache() as well,
but this is much cleaner).

Because the IdManagerCacheCli cannot use the CacheUtil with static
methods anymore now, we introduce a simple IdManagerMonitor interface,
have IdManager implement that to expose the ConcurrentMap<String,
IdLocalPool> localPool, just as Map<String,String> so as to not leak the
IdLocalPool out of the implementation.

With a little bit of blueprint XML wiring magic, the IdManagerCacheCli
can lookup that IdManagerMonitor from the OSGi service registry, and do
the same as it did before.

FTR: I've manually non-regression tested this, and verified that
genius/karaf with this change still comes up without errors, and that
the "idmanager:show" command does not blow up when used.

Change-Id: I47b92ece98fae0bce4390472c18f6833b43e7843
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
idmanager/idmanager-api/src/main/java/org/opendaylight/genius/idmanager/api/IdManagerMonitor.java [new file with mode: 0644]
idmanager/idmanager-impl/src/main/java/org/opendaylight/genius/idmanager/IdManager.java
idmanager/idmanager-impl/src/main/java/org/opendaylight/genius/idmanager/IdUtils.java
idmanager/idmanager-impl/src/main/resources/org/opendaylight/blueprint/idmanager.xml
idmanager/idmanager-shell/src/main/java/org/opendaylight/genius/idmanager/shell/IdManagerCacheCli.java
idmanager/idmanager-shell/src/main/resources/OSGI-INF/blueprint/blueprint.xml

diff --git a/idmanager/idmanager-api/src/main/java/org/opendaylight/genius/idmanager/api/IdManagerMonitor.java b/idmanager/idmanager-api/src/main/java/org/opendaylight/genius/idmanager/api/IdManagerMonitor.java
new file mode 100644 (file)
index 0000000..17a89ab
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2016 Red Hat, Inc. 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.genius.idmanager.api;
+
+import java.util.Map;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
+
+/**
+ * Monitoring for {@link IdManagerService}.
+ *
+ * @author Michael Vorburger
+ */
+public interface IdManagerMonitor {
+
+    /**
+     * Obtain description of ID pool/s. Suitable for usage e.g. by a CLI tool.
+     *
+     * @return Map with poolName as key, and a String describing the pool
+     *         (e.g. with information about availableIds &amp; releasedIds)
+     */
+    Map<String, String> getLocalPoolsDetails();
+
+}
index 6337a09a6deba697f6655eb6550a4c140a85bf3c..8f79490c2094709187a154185d68cbd5f76059e5 100644 (file)
@@ -9,16 +9,20 @@
 package org.opendaylight.genius.idmanager;
 
 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
+
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
@@ -33,13 +37,13 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
 import org.opendaylight.genius.idmanager.ReleasedIdHolder.DelayedIdEntry;
+import org.opendaylight.genius.idmanager.api.IdManagerMonitor;
 import org.opendaylight.genius.idmanager.jobs.CleanUpJob;
 import org.opendaylight.genius.idmanager.jobs.IdHolderSyncJob;
 import org.opendaylight.genius.idmanager.jobs.LocalPoolCreateJob;
 import org.opendaylight.genius.idmanager.jobs.LocalPoolDeleteJob;
 import org.opendaylight.genius.idmanager.jobs.UpdateIdEntryJob;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
-import org.opendaylight.genius.utils.cache.CacheUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutputBuilder;
@@ -70,7 +74,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class IdManager implements IdManagerService {
+public class IdManager implements IdManagerService, IdManagerMonitor {
 
     private static final Logger LOG = LoggerFactory.getLogger(IdManager.class);
     private static final long DEFAULT_IDLE_TIME = 24 * 60 * 60;
@@ -87,12 +91,17 @@ public class IdManager implements IdManagerService {
         this.broker = db;
         this.lockManager = lockManager;
         this.idUtils = idUtils;
-
-        CacheUtil.createCache(IdUtils.ID_POOL_CACHE);
-        localPool = (ConcurrentMap<String, IdLocalPool>) CacheUtil.getCache(IdUtils.ID_POOL_CACHE);
+        this.localPool = new ConcurrentHashMap<>();
         populateCache();
     }
 
+    @Override
+    public Map<String, String> getLocalPoolsDetails() {
+        Map<String, String> map = new HashMap<>();
+        localPool.entrySet().stream().forEach(entry -> map.put(entry.getKey(), entry.getValue().toString()));
+        return map;
+    }
+
     @PostConstruct
     public void start() {
         LOG.info("{} start", getClass().getSimpleName());
@@ -655,4 +664,5 @@ public class IdManager implements IdManagerService {
         }
         localPool.put(parentPoolName, localPoolCache);
     }
+
 }
index a9df6fd9ce5fafe7243aa27be0df8dc9bed4d603..f16981164bd44a47d415d79f849a24920dac84dc 100644 (file)
@@ -58,7 +58,6 @@ public class IdUtils {
     private static final long DEFAULT_AVAILABLE_ID_COUNT = 0;
     private static final int DEFAULT_BLOCK_SIZE_DIFF = 10;
     public static final int RETRY_COUNT = 6;
-    public static final String ID_POOL_CACHE = "ID_POOL_CACHE";
 
     private final ConcurrentHashMap<String, Integer> poolUpdatedMap = new ConcurrentHashMap<>();
 
index 3c30352c746f2ecec1c0ada0bbc766c1ad4ebb56..3ebed44b27bc91e13078f7cd7bb96750fa12a15a 100644 (file)
@@ -19,4 +19,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
 
     <odl:rpc-implementation ref="idManager" />
 
+    <service ref="idManager" interface="org.opendaylight.genius.idmanager.api.IdManagerMonitor" />
+
 </blueprint>
index f44f42e3a93f167e0cd9540d016453e83a71c61d..0d42b4cae1b290dee4cefa28175a77e15c1d859f 100644 (file)
@@ -8,13 +8,11 @@
 
 package org.opendaylight.genius.idmanager.shell;
 
-import java.util.concurrent.ConcurrentMap;
-
+import java.util.Map;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.commands.Option;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
-import org.opendaylight.genius.idmanager.IdUtils;
-import org.opendaylight.genius.utils.cache.CacheUtil;
+import org.opendaylight.genius.idmanager.api.IdManagerMonitor;
 
 @Command(scope = "idmanager", name = "show", description = "display local pool id cache")
 public class IdManagerCacheCli extends OsgiCommandSupport {
@@ -24,38 +22,40 @@ public class IdManagerCacheCli extends OsgiCommandSupport {
             required = false, multiValued = false)
     String poolName;
 
-    @Override
-    protected Object doExecute() throws Exception {
-        showIdPoolCache();
-        return null;
+    private IdManagerMonitor idManagerMonitor;
+
+    public void setIdManagerMonitor(IdManagerMonitor idManagerMonitor) {
+        this.idManagerMonitor = idManagerMonitor;
     }
 
-    private void showIdPoolCache() {
-        ConcurrentMap<String, Object> cache =  (ConcurrentMap<String, Object>) CacheUtil.getCache(IdUtils.ID_POOL_CACHE);
-        if (cache == null) {
-            session.getConsole().println("No caches available");
-            return;
+    @Override
+    protected Object doExecute() throws Exception {
+        if (idManagerMonitor == null) {
+            session.getConsole().println("No IdManagerMonitor service available");
+            return null;
         }
+        Map<String, String> cache = idManagerMonitor.getLocalPoolsDetails();
         session.getConsole().println("No of pools in cluster " + cache.keySet().size());
         session.getConsole().println(DEMARCATION);
         if (poolName == null) {
-            for (String idPoolName : cache.keySet()) {
+            cache.keySet().stream().forEach(idPoolName -> {
                 print(idPoolName, cache.get(idPoolName));
                 session.getConsole().println(DEMARCATION);
                 session.getConsole().println(DEMARCATION);
+            });
+        } else {
+            Object idPool = cache.get(poolName);
+            if (idPool == null) {
+                session.getConsole().println("Local Id pool not found for " + poolName);
+            } else {
+                print(poolName, idPool);
             }
-            return;
         }
-        Object idPool = cache.get(poolName);
-        if (idPool == null) {
-            session.getConsole().println("Local Id pool not found for " + poolName);
-            return;
-        }
-        print(poolName, idPool);
+        return null;
     }
 
     private void print(String idPoolName, Object idPool) {
-        session.getConsole().println("Pool name : " + idPoolName);
-        session.getConsole().println("IdPool " + idPool);
+        session.getConsole().println("Pool name: " + idPoolName);
+        session.getConsole().println("IdPool: " + idPool);
     }
-}
\ No newline at end of file
+}
index 220f2f3c3b02fac3b201ed6f454a03f479b4fcee..49dce2d50ae7931ebe867d3403894d7720fa79fb 100644 (file)
@@ -6,9 +6,14 @@
 -->
 
 <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+    <reference id="idManagerMonitor" interface="org.opendaylight.genius.idmanager.api.IdManagerMonitor" />
+
     <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
         <command>
-            <action class="org.opendaylight.genius.idmanager.shell.IdManagerCacheCli"/>
+            <action class="org.opendaylight.genius.idmanager.shell.IdManagerCacheCli">
+                <property name="idManagerMonitor" ref="idManagerMonitor" />
+            </action>
         </command>
     </command-bundle>
-</blueprint>
\ No newline at end of file
+</blueprint>