CmSubscription minor code refactoring
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / cmsubscription / service / CmNotificationSubscriptionPersistenceServiceImpl.java
 package org.onap.cps.ncmp.api.impl.events.cmsubscription.service;
 
 import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.onap.cps.api.CpsDataService;
+import org.onap.cps.api.CpsQueryService;
 import org.onap.cps.ncmp.api.impl.operations.DatastoreType;
 import org.onap.cps.spi.FetchDescendantsOption;
 import org.onap.cps.spi.model.DataNode;
@@ -32,23 +34,33 @@ import org.springframework.stereotype.Service;
 @Slf4j
 @Service
 @RequiredArgsConstructor
-public class CmSubscriptionServiceImpl implements CmSubscriptionService {
+public class CmNotificationSubscriptionPersistenceServiceImpl implements CmNotificationSubscriptionPersistenceService {
 
     private static final String IS_ONGOING_CM_SUBSCRIPTION_CPS_PATH_QUERY = """
             /datastores/datastore[@name='%s']/cm-handles/cm-handle[@id='%s']/filters/filter[@xpath='%s']""";
 
-    private final CpsDataService cpsDataService;
+    private final CpsQueryService cpsQueryService;
 
     @Override
-    public boolean isOngoingCmSubscription(final DatastoreType datastoreType, final String cmHandleId,
+    public boolean isOngoingCmNotificationSubscription(final DatastoreType datastoreType, final String cmHandleId,
             final String xpath) {
+        return !getOngoingCmNotificationSubscriptionIds(datastoreType, cmHandleId, xpath).isEmpty();
+    }
+
+    @Override
+    public Collection<String> getOngoingCmNotificationSubscriptionIds(final DatastoreType datastoreType,
+            final String cmHandleId, final String xpath) {
+
         final String isOngoingCmSubscriptionCpsPathQuery =
                 IS_ONGOING_CM_SUBSCRIPTION_CPS_PATH_QUERY.formatted(datastoreType.getDatastoreName(), cmHandleId,
                         escapeQuotesByDoublingThem(xpath));
         final Collection<DataNode> existingNodes =
-                cpsDataService.getDataNodes(NCMP_DATASPACE_NAME, CM_SUBSCRIPTIONS_ANCHOR_NAME,
+                cpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, CM_SUBSCRIPTIONS_ANCHOR_NAME,
                         isOngoingCmSubscriptionCpsPathQuery, FetchDescendantsOption.OMIT_DESCENDANTS);
-        return !existingNodes.isEmpty();
+        if (existingNodes.isEmpty()) {
+            return Collections.emptyList();
+        }
+        return (List<String>) existingNodes.iterator().next().getLeaves().get("subscribers");
     }
 
     private static String escapeQuotesByDoublingThem(final String inputXpath) {