Attach a (JSON) data instance for a container with children to a given Anchor
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsModuleServiceImpl.java
index 87ffdd3..990b7bb 100644 (file)
 
 package org.onap.cps.api.impl;
 
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.Optional;
+import java.util.Map;
 import org.onap.cps.api.CpsModuleService;
-import org.onap.cps.exceptions.CpsException;
-import org.onap.cps.exceptions.CpsValidationException;
+import org.onap.cps.spi.CascadeDeleteAllowed;
 import org.onap.cps.spi.CpsModulePersistenceService;
-import org.onap.cps.utils.YangUtils;
-import org.opendaylight.yangtools.yang.common.Revision;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
+import org.onap.cps.spi.model.SchemaSet;
+import org.onap.cps.yang.YangTextSchemaSourceSet;
+import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
 
-@Component("CpsModuleServiceImpl")
+@Service("CpsModuleServiceImpl")
 public class CpsModuleServiceImpl implements CpsModuleService {
 
     @Autowired
     private CpsModulePersistenceService cpsModulePersistenceService;
 
+    @Autowired
+    private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
+
     @Override
-    public SchemaContext parseAndValidateModel(final String yangModelContent) {
-        try {
-            final File tempFile = File.createTempFile("yang", ".yang");
-            try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
-                writer.write(yangModelContent);
-            }
-            return parseAndValidateModel(tempFile);
-        } catch (final IOException e) {
-            throw new CpsException(e);
-        }
+    public void createSchemaSet(final String dataspaceName, final String schemaSetName,
+        final Map<String, String> yangResourcesNameToContentMap) {
+        final YangTextSchemaSourceSet yangTextSchemaSourceSet
+            = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap);
+        cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
+        yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
     }
 
     @Override
-    public SchemaContext parseAndValidateModel(final File yangModelFile) {
-        try {
-            return YangUtils.parseYangModelFile(yangModelFile);
-        } catch (final YangParserException e) {
-            throw new CpsValidationException("Yang file validation failed", e.getMessage());
-        } catch (final IOException e) {
-            throw new CpsException(e);
-        }
+    public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
+        final YangTextSchemaSourceSet yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
+            .get(dataspaceName, schemaSetName);
+        return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
+            .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
     }
 
     @Override
-    public void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName) {
-        for (final Module module : schemaContext.getModules()) {
-            final Optional<Revision> optionalRevision = module.getRevision();
-            final String revisionValue = optionalRevision.map(Object::toString).orElse(null);
-            cpsModulePersistenceService.storeModule(module.getNamespace().toString(), module.toString(),
-                revisionValue, dataspaceName);
-        }
+    public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
+        final CascadeDeleteAllowed cascadeDeleteAllowed) {
+        cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName, cascadeDeleteAllowed);
     }
 }