Introduce Instrumentation
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsModuleServiceImpl.java
index b4890f4..ccd0fcc 100644 (file)
@@ -1,8 +1,9 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2020-2022 Nordix Foundation
+ *  Copyright (C) 2020-2023 Nordix Foundation
  *  Modifications Copyright (C) 2020-2021 Pantheon.tech
  *  Modifications Copyright (C) 2022 Bell Canada
+ *  Modifications Copyright (C) 2022 TechMahindra Ltd
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -22,6 +23,7 @@
 
 package org.onap.cps.api.impl;
 
+import io.micrometer.core.annotation.Timed;
 import java.util.Collection;
 import java.util.Map;
 import lombok.RequiredArgsConstructor;
@@ -35,7 +37,8 @@ import org.onap.cps.spi.model.ModuleDefinition;
 import org.onap.cps.spi.model.ModuleReference;
 import org.onap.cps.spi.model.SchemaSet;
 import org.onap.cps.spi.utils.CpsValidator;
-import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
+import org.onap.cps.yang.TimedYangTextSchemaSourceSetBuilder;
+import org.onap.cps.yang.YangTextSchemaSourceSet;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -47,13 +50,16 @@ public class CpsModuleServiceImpl implements CpsModuleService {
     private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
     private final CpsAdminService cpsAdminService;
     private final CpsValidator cpsValidator;
+    private final TimedYangTextSchemaSourceSetBuilder timedYangTextSchemaSourceSetBuilder;
 
     @Override
+    @Timed(value = "cps.module.service.schemaset.create",
+        description = "Time taken to create (and store) a schemaset")
     public void createSchemaSet(final String dataspaceName, final String schemaSetName,
         final Map<String, String> yangResourcesNameToContentMap) {
         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
-        final var yangTextSchemaSourceSet
-            = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap);
+        final YangTextSchemaSourceSet yangTextSchemaSourceSet =
+            timedYangTextSchemaSourceSetBuilder.getYangTextSchemaSourceSet(yangResourcesNameToContentMap);
         cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
         yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
     }
@@ -77,6 +83,15 @@ public class CpsModuleServiceImpl implements CpsModuleService {
             .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
     }
 
+    @Override
+    public Collection<SchemaSet> getSchemaSets(final String dataspaceName) {
+        cpsValidator.validateNameCharacters(dataspaceName);
+        final Collection<SchemaSet> schemaSets =
+            cpsModulePersistenceService.getSchemaSetsByDataspaceName(dataspaceName);
+        schemaSets.forEach(schemaSet -> setModuleReferences(schemaSet, dataspaceName));
+        return schemaSets;
+    }
+
     @Override
     @Transactional
     public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
@@ -124,4 +139,9 @@ public class CpsModuleServiceImpl implements CpsModuleService {
         return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
     }
 
+    private void setModuleReferences(final SchemaSet schemaSet, final String dataspaceName) {
+        final YangTextSchemaSourceSet yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
+            .get(dataspaceName, schemaSet.getName());
+        schemaSet.setModuleReferences(yangTextSchemaSourceSet.getModuleReferences());
+    }
 }