NONRTRIC Improved traces

Improved traces

Issue-ID: CCSDK-3560
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Change-Id: I1ef76b859e24f4ea503c8e28e503528c98de4342
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Lock.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Lock.java
index 18507ac..46033a4 100644
--- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Lock.java
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Lock.java
@@ -45,6 +45,7 @@
     private int lockCounter = 0;
     final Queue<LockRequest> lockRequestQueue = new LinkedList<>();
     private static AsynchCallbackExecutor callbackProcessor = new AsynchCallbackExecutor();
+    private final String label;
 
     public enum LockType {
         EXCLUSIVE, SHARED
@@ -63,6 +64,7 @@
         Grant(Lock lock, String label) {
             this.lock = lock;
             this.label = label;
+            logger.trace("Lock granted {}:{}", lock.label, this.label);
         }
 
         /**
@@ -71,27 +73,42 @@
          * @return the lock
          */
         public Mono<Lock> unlock() {
-            check();
-            return this.lock.unlock();
+            if (!isUnlocked()) {
+                logger.trace("Unlocking lock {}:{}", lock.label, this.label);
+                return this.lock.unlock();
+            }
+            return Mono.just(this.lock);
         }
 
         /**
          * Synchronuous unlocking
          */
         public void unlockBlocking() {
-            check();
-            this.lock.unlockBlocking();
+            if (!isUnlocked()) {
+                logger.trace("Unlocking lock {}:{}", lock.label, this.label);
+                this.lock.unlockBlocking();
+            }
         }
 
-        private void check() {
+        private boolean isUnlocked() {
             if (unlocked) {
-                logger.error("Lock already unlocked");
+                logger.debug("Lock {}:{} already unlocked", lock.label, this.label);
+                return true;
             }
             unlocked = true;
+            return false;
         }
     }
 
     /**
+     *
+     * @param label a label attached to the lock. For troubleshooting.
+     */
+    public Lock(String label) {
+        this.label = label;
+    }
+
+    /**
      * Reactive lock. The Lock will be emitted when the lock is granted
      *
      * @param lockType type of lock (exclusive/shared)
@@ -140,7 +157,7 @@
 
     @Override
     public synchronized String toString() {
-        return "Lock cnt: " + this.lockCounter + " exclusive: " + this.isExclusive + " queued: "
+        return "Lock " + this.label + ", cnt: " + this.lockCounter + ", exclusive: " + this.isExclusive + ", queued: "
                 + this.lockRequestQueue.size();
     }
 
@@ -164,6 +181,7 @@
     }
 
     private synchronized void addToQueue(MonoSink<Grant> callback, LockType lockType, String label) {
+        logger.trace("Lock request queued {}:{}", label, this.label);
         lockRequestQueue.add(new LockRequest(callback, lockType, this, label));
     }
 
@@ -172,7 +190,7 @@
         try {
             this.wait();
         } catch (InterruptedException e) {
-            logger.warn("waitForUnlock interrupted", e);
+            logger.warn("waitForUnlock interrupted " + this.label, e);
             Thread.currentThread().interrupt();
         }
     }
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Ric.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Ric.java
index 8eca6be..f43737f 100644
--- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Ric.java
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Ric.java
@@ -45,7 +45,7 @@
     private A1ProtocolType protocolVersion = A1ProtocolType.UNKNOWN;
 
     @Getter
-    private final Lock lock = new Lock();
+    private final Lock lock;
 
     /**
      * Creates the Ric. Initial state is {@link RicState.UNDEFINED}.
@@ -54,6 +54,7 @@
      */
     public Ric(RicConfig ricConfig) {
         this.ricConfig = ricConfig;
+        this.lock = new Lock(ricConfig.ricId());
     }
 
     public String id() {
diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/LockTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/LockTest.java
index 7987944..3ea0fe5 100644
--- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/LockTest.java
+++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/LockTest.java
@@ -65,7 +65,7 @@
 
     @Test
     void testLock() throws IOException, ServiceException {
-        Lock lock = new Lock();
+        Lock lock = new Lock("l1");
         Lock.Grant grant = lock.lockBlocking(LockType.SHARED, "test");
         grant.unlockBlocking();
         assertThat(grant.getLabel()).isEqualTo("test");
@@ -81,7 +81,7 @@
 
     @Test
     void testReactiveLock() {
-        Lock lock = new Lock();
+        Lock lock = new Lock("l1");
 
         Mono<?> l0 = lock.lock(LockType.EXCLUSIVE, "1").doOnNext(grant -> asynchUnlock(grant, lock));
         Mono<?> l1 = lock.lock(LockType.SHARED, "2").doOnNext(grant -> asynchUnlock(grant, lock));