Adding upload and validation of yang model
authorniamhcore <niamh.core@est.tech>
Wed, 23 Sep 2020 10:36:33 +0000 (11:36 +0100)
committerniamhcore <niamh.core@est.tech>
Thu, 24 Sep 2020 16:21:36 +0000 (17:21 +0100)
Issue-ID: CCSDK-2718

https: //jira.onap.org/browse/CCSDK-2718
Change-Id: I919525595e28d46f20c1adb560232c31025687e3

15 files changed:
cps/cps-rest/pom.xml
cps/cps-rest/src/main/java/org/onap/cps/rest/config/JerseyConfig.java
cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java [new file with mode: 0644]
cps/cps-ri/pom.xml
cps/cps-ri/src/main/java/org/onap/cps/spi/dummy.txt [deleted file]
cps/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java [new file with mode: 0644]
cps/cps-ri/src/main/java/org/onap/cps/spi/impl/entities/ModuleEntity.java [new file with mode: 0644]
cps/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java [new file with mode: 0644]
cps/cps-service/pom.xml
cps/cps-service/src/main/java/org/onap/cps/api/CPService.java [new file with mode: 0644]
cps/cps-service/src/main/java/org/onap/cps/api/dummy.txt [deleted file]
cps/cps-service/src/main/java/org/onap/cps/api/impl/CPServiceImpl.java [new file with mode: 0644]
cps/cps-service/src/main/java/org/onap/cps/spi/ModelPersistencyService.java [new file with mode: 0644]
cps/cps-service/src/main/java/org/onap/cps/spi/dummy.txt [deleted file]
cps/pom.xml

index 0e8317b..bd9be8d 100644 (file)
             <version>${project.version}</version>\r
         </dependency>\r
 \r
+        <dependency>\r
+            <groupId>org.glassfish.jersey.media</groupId>\r
+            <artifactId>jersey-media-multipart</artifactId>\r
+        </dependency>\r
+\r
         <dependency>\r
             <groupId>org.springframework.boot</groupId>\r
             <artifactId>spring-boot-starter-jersey</artifactId>\r
@@ -39,6 +44,7 @@
             <artifactId>spring-boot-starter-jetty</artifactId>\r
         </dependency>\r
 \r
+\r
         <dependency>\r
             <groupId>org.springframework.boot</groupId>\r
             <artifactId>spring-boot-starter-test</artifactId>\r
index a7c4307..44487fe 100644 (file)
@@ -22,8 +22,9 @@ package org.onap.cps.rest.config;
 \r
 import javax.annotation.PostConstruct;\r
 import javax.ws.rs.ApplicationPath;\r
+import org.glassfish.jersey.media.multipart.MultiPartFeature;\r
 import org.glassfish.jersey.server.ResourceConfig;\r
-import org.onap.cps.rest.controller.ModelController;\r
+import org.onap.cps.rest.controller.RestController;\r
 import org.springframework.context.annotation.Configuration;\r
 \r
 @Configuration\r
@@ -32,6 +33,7 @@ public class JerseyConfig extends ResourceConfig {
 \r
     @PostConstruct\r
     public void init() {\r
-        register(ModelController.class);\r
+        register(RestController.class);\r
+        register(MultiPartFeature.class);\r
     }\r
 }
\ No newline at end of file
diff --git a/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java b/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java
new file mode 100644 (file)
index 0000000..d2f1a3e
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.rest.controller;
+
+import java.io.File;
+import java.io.IOException;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import org.glassfish.jersey.media.multipart.FormDataParam;
+import org.onap.cps.api.CPService;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
+import org.springframework.beans.factory.annotation.Autowired;
+
+
+@Path("cps")
+public class RestController {
+
+    @Autowired
+    private CPService cpService;
+
+    @POST
+    @Path("uploadYangFile")
+    @Produces(MediaType.APPLICATION_JSON)
+    @Consumes(MediaType.MULTIPART_FORM_DATA)
+    public Response uploadFile(@FormDataParam("file") File uploadedFile) throws IOException {
+        try {
+            File fileToParse = renameFileIfNeeded(uploadedFile);
+            SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse);
+            cpService.storeSchemaContext(schemaContext);
+            return Response.status(Status.OK).entity("Yang File Parsed").build();
+        } catch (YangParserException e) {
+            return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
+        }
+    }
+
+    private static File renameFileIfNeeded(File originalFile) {
+        if (originalFile.getName().endsWith(".yang")) {
+            return originalFile;
+        }
+        File renamedFile = new File(originalFile.getName() + ".yang");
+        originalFile.renameTo(renamedFile);
+        return renamedFile;
+    }
+}
+
+
+
+
index 0d6dcac..781f8e8 100644 (file)
@@ -1,43 +1,48 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0"\r
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
-    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\r
-    <modelVersion>4.0.0</modelVersion>\r
-    <parent>\r
-        <groupId>org.onap.cps</groupId>\r
-        <artifactId>cps</artifactId>\r
-        <version>0.0.1-SNAPSHOT</version>\r
-    </parent>\r
-    <artifactId>cps-ri</artifactId>\r
-\r
-    <dependencies>\r
-\r
-        <dependency>\r
-            <groupId>org.onap.cps</groupId>\r
-            <artifactId>cps-service</artifactId>\r
-            <version>${project.version}</version>\r
-            <scope>compile</scope>\r
-        </dependency>\r
-\r
-        <dependency>\r
-            <groupId>org.springframework.boot</groupId>\r
-            <artifactId>spring-boot-starter-data-jpa</artifactId>\r
-        </dependency>\r
-\r
-        <dependency>\r
-            <groupId>org.springframework.boot</groupId>\r
-            <artifactId>spring-boot-starter-validation</artifactId>\r
-        </dependency>\r
-\r
-        <dependency>\r
-            <groupId>org.mariadb.jdbc</groupId>\r
-            <artifactId>mariadb-java-client</artifactId>\r
-        </dependency>\r
-\r
-        <dependency>\r
-            <groupId>org.projectlombok</groupId>\r
-            <artifactId>lombok</artifactId>\r
-        </dependency>\r
-\r
-    </dependencies>\r
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\r
+  <modelVersion>4.0.0</modelVersion>\r
+  <parent>\r
+    <groupId>org.onap.cps</groupId>\r
+    <artifactId>cps</artifactId>\r
+    <version>0.0.1-SNAPSHOT</version>\r
+  </parent>\r
+  <artifactId>cps-ri</artifactId>\r
+\r
+  <dependencies>\r
+\r
+    <dependency>\r
+      <groupId>org.onap.cps</groupId>\r
+      <artifactId>cps-service</artifactId>\r
+      <version>${project.version}</version>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <groupId>org.springframework.boot</groupId>\r
+      <artifactId>spring-boot-starter-data-jpa</artifactId>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <groupId>org.springframework.boot</groupId>\r
+      <artifactId>spring-boot-starter-validation</artifactId>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <groupId>org.mariadb.jdbc</groupId>\r
+      <artifactId>mariadb-java-client</artifactId>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <groupId>org.projectlombok</groupId>\r
+      <artifactId>lombok</artifactId>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <!-- Provides mariadb object mapping to an entity -->\r
+      <groupId>jakarta.persistence</groupId>\r
+      <artifactId>jakarta.persistence-api</artifactId>\r
+    </dependency>\r
+\r
+  </dependencies>\r
 \r
 </project>
\ No newline at end of file
diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/dummy.txt b/cps/cps-ri/src/main/java/org/onap/cps/spi/dummy.txt
deleted file mode 100644 (file)
index ff2b9f3..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Have a good day !
\ No newline at end of file
diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java
new file mode 100644 (file)
index 0000000..6e718c8
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.impl;
+
+
+import org.onap.cps.spi.impl.entities.ModuleEntity;
+import org.onap.cps.spi.ModelPersistencyService;
+import org.onap.cps.spi.repository.ModuleRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ModelPersistencyServiceImpl implements ModelPersistencyService {
+
+
+    private final ModuleRepository moduleRepository;
+
+    @Autowired
+    public ModelPersistencyServiceImpl(final ModuleRepository moduleRepository) {
+        this.moduleRepository = moduleRepository;
+    }
+
+    @Override
+    public void storeModule(final String name, final String moduleContent, final String revision) {
+        final ModuleEntity moduleEntity = new ModuleEntity(name, moduleContent, revision);
+        moduleRepository.save(moduleEntity);
+
+    }
+}
diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/entities/ModuleEntity.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/entities/ModuleEntity.java
new file mode 100644 (file)
index 0000000..be3dfef
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.impl.entities;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+
+/**
+ * Entity to store a yang module.
+ */
+@Getter
+@Setter
+@Entity
+@AllArgsConstructor
+@NoArgsConstructor
+@Table(name = "modules")
+public class ModuleEntity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Integer id;
+
+    @Column
+    private String name;
+
+    @Column
+    private String moduleContent;
+
+    @Column
+    private String revision;
+
+    public ModuleEntity(String name, String moduleContent, String revision) {
+        this.name = name;
+        this.moduleContent = moduleContent;
+        this.revision = revision;
+    }
+}
\ No newline at end of file
diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java
new file mode 100644 (file)
index 0000000..84441cb
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.repository;
+
+
+import org.onap.cps.spi.impl.entities.ModuleEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ModuleRepository extends JpaRepository<ModuleEntity, Integer> {
+
+}
\ No newline at end of file
index a0bf497..8445394 100644 (file)
@@ -1,4 +1,6 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\r
+<project xmlns="http://maven.apache.org/POM/4.0.0"\r
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\r
   <modelVersion>4.0.0</modelVersion>\r
   <parent>\r
     <groupId>org.onap.cps</groupId>\r
@@ -7,36 +9,49 @@
   </parent>\r
   <artifactId>cps-service</artifactId>\r
 \r
-  <properties>\r
-        <org.opendaylight.yangtools.version>5.0.5</org.opendaylight.yangtools.version>\r
-    </properties>\r
-\r
   <dependencies>\r
 \r
-        <dependency>\r
-            <groupId>org.opendaylight.yangtools</groupId>\r
-            <artifactId>yang-parser-api</artifactId>\r
-            <version>${org.opendaylight.yangtools.version}</version>\r
-        </dependency>\r
-\r
-        <dependency>\r
-            <groupId>org.opendaylight.yangtools</groupId>\r
-            <artifactId>yang-parser-impl</artifactId>\r
-            <version>${org.opendaylight.yangtools.version}</version>\r
-        </dependency>\r
-\r
-        <dependency>\r
-            <groupId>org.opendaylight.yangtools</groupId>\r
-            <artifactId>yang-model-util</artifactId>\r
-            <version>${org.opendaylight.yangtools.version}</version>\r
-        </dependency>\r
-\r
-        <dependency>\r
-            <groupId>org.opendaylight.yangtools</groupId>\r
-            <artifactId>yang-data-codec-xml</artifactId>\r
-            <version>${org.opendaylight.yangtools.version}</version>\r
-        </dependency>\r
-\r
-    </dependencies>\r
+    <dependency>\r
+      <groupId>org.opendaylight.yangtools</groupId>\r
+      <artifactId>yang-parser-api</artifactId>\r
+      <version>${org.opendaylight.yangtools.version}</version>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <groupId>org.opendaylight.yangtools</groupId>\r
+      <artifactId>yang-parser-impl</artifactId>\r
+      <version>${org.opendaylight.yangtools.version}</version>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <groupId>org.opendaylight.yangtools</groupId>\r
+      <artifactId>yang-model-util</artifactId>\r
+      <version>${org.opendaylight.yangtools.version}</version>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <groupId>org.opendaylight.yangtools</groupId>\r
+      <artifactId>yang-data-codec-xml</artifactId>\r
+      <version>${org.opendaylight.yangtools.version}</version>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <groupId>org.projectlombok</groupId>\r
+      <artifactId>lombok</artifactId>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <!-- For logging -->\r
+      <groupId>org.slf4j</groupId>\r
+      <artifactId>slf4j-api</artifactId>\r
+    </dependency>\r
+\r
+    <dependency>\r
+      <!-- For dependency injection -->\r
+      <groupId>org.springframework</groupId>\r
+      <artifactId>spring-context</artifactId>\r
+    </dependency>\r
+\r
+  </dependencies>\r
 \r
 </project>
\ No newline at end of file
diff --git a/cps/cps-service/src/main/java/org/onap/cps/api/CPService.java b/cps/cps-service/src/main/java/org/onap/cps/api/CPService.java
new file mode 100644 (file)
index 0000000..51622c3
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.api;
+
+import java.io.File;
+import java.io.IOException;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
+
+/**
+ * Configuration and persistency service interface which holds methods for parsing and storing yang models and data.
+ */
+public interface CPService {
+
+    /**
+     * Parse and validate a string representing a yang model to generate a schema context.
+     *
+     * @param yangModelContent the input stream
+     * @return the schema context
+     */
+    SchemaContext parseAndValidateModel(final String yangModelContent) throws IOException, YangParserException;
+
+    /**
+     * Parse and validate a file representing a yang model to generate a schema context.
+     *
+     * @param yangModelFile the yang file
+     * @return the schema context
+     */
+    SchemaContext parseAndValidateModel(final File yangModelFile) throws IOException, YangParserException;
+
+    /**
+     * Store schema context for a yang model.
+     *
+     * @param schemaContext the schema context
+     */
+    void storeSchemaContext(final SchemaContext schemaContext);
+
+}
diff --git a/cps/cps-service/src/main/java/org/onap/cps/api/dummy.txt b/cps/cps-service/src/main/java/org/onap/cps/api/dummy.txt
deleted file mode 100644 (file)
index ff2b9f3..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Have a good day !
\ No newline at end of file
diff --git a/cps/cps-service/src/main/java/org/onap/cps/api/impl/CPServiceImpl.java b/cps/cps-service/src/main/java/org/onap/cps/api/impl/CPServiceImpl.java
new file mode 100644 (file)
index 0000000..5bd6e44
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+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.Iterator;
+import java.util.ServiceLoader;
+import org.onap.cps.api.CPService;
+import org.onap.cps.spi.ModelPersistencyService;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class CPServiceImpl implements CPService {
+
+    private final static Logger LOGGER = LoggerFactory.getLogger(CPServiceImpl.class);
+
+    private static final YangParserFactory PARSER_FACTORY;
+
+    static {
+        final Iterator<YangParserFactory> it =
+            ServiceLoader.load(YangParserFactory.class).iterator();
+        if (!it.hasNext()) {
+            throw new IllegalStateException("No YangParserFactory found");
+        }
+        PARSER_FACTORY = it.next();
+    }
+
+    @Autowired
+    private ModelPersistencyService modelPersistencyService;
+
+    @Override
+    public SchemaContext parseAndValidateModel(final String yangModelContent)
+        throws IOException, YangParserException {
+        final File tempFile = File.createTempFile("yang", ".yang");
+        try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
+            writer.write(yangModelContent);
+        } catch (IOException e) {
+            LOGGER.error("Unable to write to temporary file {}", e.getMessage());
+        }
+        return parseAndValidateModel(tempFile);
+    }
+
+    @Override
+    public SchemaContext parseAndValidateModel(final File yangModelFile) throws IOException, YangParserException {
+        final YangTextSchemaSource yangTextSchemaSource = YangTextSchemaSource.forFile(yangModelFile);
+        final YangParser yangParser = PARSER_FACTORY.createParser(StatementParserMode.DEFAULT_MODE);
+        yangParser.addSource(yangTextSchemaSource);
+        return yangParser.buildEffectiveModel();
+    }
+
+    @Override
+    public void storeSchemaContext(final SchemaContext schemaContext) {
+        for (final Module module : schemaContext.getModules()) {
+            modelPersistencyService.storeModule(module.getName(), module.toString(),
+                module.getRevision().toString());
+        }
+    }
+}
diff --git a/cps/cps-service/src/main/java/org/onap/cps/spi/ModelPersistencyService.java b/cps/cps-service/src/main/java/org/onap/cps/spi/ModelPersistencyService.java
new file mode 100644 (file)
index 0000000..e0286c1
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi;
+
+/**
+ * Defines methods to access and manipulate data using the chosen database solution.
+ */
+public interface ModelPersistencyService {
+
+    /**
+     * Store the module from a yang model in the database.
+     * @param name
+     * @param moduleContent
+     * @param revision
+     */
+    void storeModule(final String name, final String moduleContent, final String revision);
+
+}
diff --git a/cps/cps-service/src/main/java/org/onap/cps/spi/dummy.txt b/cps/cps-service/src/main/java/org/onap/cps/spi/dummy.txt
deleted file mode 100644 (file)
index ff2b9f3..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Have a good day !
\ No newline at end of file
index 94ed29c..1c1b2b1 100644 (file)
 <project xmlns="http://maven.apache.org/POM/4.0.0"\r
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
-    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\r
-    <modelVersion>4.0.0</modelVersion>\r
-    <parent>\r
-        <groupId>org.onap.oparent</groupId>\r
-        <artifactId>oparent</artifactId>\r
-        <version>3.1.0</version>\r
-    </parent>\r
-    <groupId>org.onap.cps</groupId>\r
-    <artifactId>cps</artifactId>\r
-    <version>0.0.1-SNAPSHOT</version>\r
-    <packaging>pom</packaging>\r
-    <name>cps</name>\r
-    <description>ONAP Configuration and Persistency Service</description>\r
-    <organization>\r
-        <name>ONAP - CPS</name>\r
-        <url>http://www.onap.org/</url>\r
-    </organization>\r
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\r
+  <modelVersion>4.0.0</modelVersion>\r
+  <parent>\r
+    <groupId>org.onap.oparent</groupId>\r
+    <artifactId>oparent</artifactId>\r
+    <version>3.1.0</version>\r
+  </parent>\r
+  <groupId>org.onap.cps</groupId>\r
+  <artifactId>cps</artifactId>\r
+  <version>0.0.1-SNAPSHOT</version>\r
+  <packaging>pom</packaging>\r
+  <name>cps</name>\r
+  <description>ONAP Configuration and Persistency Service</description>\r
+  <organization>\r
+    <name>ONAP - CPS</name>\r
+    <url>http://www.onap.org/</url>\r
+  </organization>\r
 \r
-    <properties>\r
-        <version.java.compiler>11</version.java.compiler>\r
-        <springboot.version>2.3.3.RELEASE</springboot.version>\r
-        <onap.logging.version>1.6.7</onap.logging.version>\r
-        <oparent.version>3.1.0</oparent.version>\r
-    </properties>\r
+  <properties>\r
+    <version.java.compiler>11</version.java.compiler>\r
+    <springboot.version>2.3.3.RELEASE</springboot.version>\r
+    <oparent.version>3.1.0</oparent.version>\r
+    <org.opendaylight.yangtools.version>5.0.5</org.opendaylight.yangtools.version>\r
+  </properties>\r
 \r
-    <dependencyManagement>\r
-        <dependencies>\r
-            <dependency>\r
-                <groupId>org.springframework.boot</groupId>\r
-                <artifactId>spring-boot-dependencies</artifactId>\r
-                <version>${springboot.version}</version>\r
-                <type>pom</type>\r
-                <scope>import</scope>\r
-            </dependency>\r
-        </dependencies>\r
-    </dependencyManagement>\r
+  <dependencyManagement>\r
+    <dependencies>\r
+      <dependency>\r
+        <groupId>org.springframework.boot</groupId>\r
+        <artifactId>spring-boot-dependencies</artifactId>\r
+        <version>${springboot.version}</version>\r
+        <type>pom</type>\r
+        <scope>import</scope>\r
+      </dependency>\r
+    </dependencies>\r
+  </dependencyManagement>\r
 \r
-    <build>\r
-        <resources>\r
-            <resource>\r
-                <directory>src/main/resources</directory>\r
-                <filtering>true</filtering>\r
-            </resource>\r
+  <build>\r
+    <resources>\r
+      <resource>\r
+        <directory>src/main/resources</directory>\r
+        <filtering>true</filtering>\r
+      </resource>\r
 \r
-            <resource>\r
-                <directory>target/generated-sources/license</directory>\r
-                <includes>\r
-                    <include>third-party-licenses.txt</include>\r
-                </includes>\r
-            </resource>\r
+      <resource>\r
+        <directory>target/generated-sources/license</directory>\r
+        <includes>\r
+          <include>third-party-licenses.txt</include>\r
+        </includes>\r
+      </resource>\r
 \r
-            <resource>\r
-                <directory>target/generated-resources/licenses</directory>\r
-                <includes>\r
-                    <include>*.*</include>\r
-                </includes>\r
-                <targetPath>third-party-licenses</targetPath>\r
-            </resource>\r
-        </resources>\r
+      <resource>\r
+        <directory>target/generated-resources/licenses</directory>\r
+        <includes>\r
+          <include>*.*</include>\r
+        </includes>\r
+        <targetPath>third-party-licenses</targetPath>\r
+      </resource>\r
+    </resources>\r
 \r
-        <plugins>\r
-            <plugin>\r
-                <groupId>org.apache.maven.plugins</groupId>\r
-                <artifactId>maven-compiler-plugin</artifactId>\r
-                <configuration>\r
-                    <source>${version.java.compiler}</source>\r
-                    <target>${version.java.compiler}</target>\r
-                </configuration>\r
-            </plugin>\r
+    <plugins>\r
+      <plugin>\r
+        <groupId>org.apache.maven.plugins</groupId>\r
+        <artifactId>maven-compiler-plugin</artifactId>\r
+        <configuration>\r
+          <source>${version.java.compiler}</source>\r
+          <target>${version.java.compiler}</target>\r
+        </configuration>\r
+      </plugin>\r
 \r
-            <plugin>\r
-                <groupId>org.apache.maven.plugins</groupId>\r
-                <artifactId>maven-checkstyle-plugin</artifactId>\r
-                <executions>\r
-                    <execution>\r
-                        <id>onap-java-style</id>\r
-                        <goals>\r
-                            <goal>check</goal>\r
-                        </goals>\r
-                        <phase>process-sources</phase>\r
-                        <configuration>\r
-                            <configLocation>onap-checkstyle/onap-java-style.xml</configLocation>\r
-                            <sourceDirectories>${project.build.sourceDirectory}</sourceDirectories>\r
-                            <includeResources>true</includeResources>\r
-                            <includeTestSourceDirectory>true</includeTestSourceDirectory>\r
-                            <includeTestResources>true</includeTestResources>\r
-                            <consoleOutput>true</consoleOutput>\r
-                            <violationSeverity>warning</violationSeverity>\r
-                            <failsOnViolation>true</failsOnViolation>\r
-                        </configuration>\r
-                    </execution>\r
-                </executions>\r
+      <plugin>\r
+        <groupId>org.apache.maven.plugins</groupId>\r
+        <artifactId>maven-checkstyle-plugin</artifactId>\r
+        <executions>\r
+          <execution>\r
+            <id>onap-java-style</id>\r
+            <goals>\r
+              <goal>check</goal>\r
+            </goals>\r
+            <phase>process-sources</phase>\r
+            <configuration>\r
+              <configLocation>onap-checkstyle/onap-java-style.xml</configLocation>\r
+              <sourceDirectories>${project.build.sourceDirectory}</sourceDirectories>\r
+              <includeResources>true</includeResources>\r
+              <includeTestSourceDirectory>true</includeTestSourceDirectory>\r
+              <includeTestResources>true</includeTestResources>\r
+              <consoleOutput>true</consoleOutput>\r
+              <violationSeverity>warning</violationSeverity>\r
+              <failsOnViolation>true</failsOnViolation>\r
+            </configuration>\r
+          </execution>\r
+        </executions>\r
 \r
-                <dependencies>\r
-                    <dependency>\r
-                        <groupId>org.onap.oparent</groupId>\r
-                        <artifactId>checkstyle</artifactId>\r
-                        <version>${oparent.version}</version>\r
-                        <scope>compile</scope>\r
-                    </dependency>\r
-                </dependencies>\r
+        <dependencies>\r
+          <dependency>\r
+            <groupId>org.onap.oparent</groupId>\r
+            <artifactId>checkstyle</artifactId>\r
+            <version>${oparent.version}</version>\r
+          </dependency>\r
+        </dependencies>\r
 \r
-            </plugin>\r
-        </plugins>\r
-    </build>\r
+      </plugin>\r
+    </plugins>\r
+  </build>\r
 \r
-    <modules>\r
-        <module>cps-service</module>\r
-        <module>cps-rest</module>\r
-        <module>cps-ri</module>\r
-    </modules>\r
+  <modules>\r
+    <module>cps-service</module>\r
+    <module>cps-rest</module>\r
+    <module>cps-ri</module>\r
+  </modules>\r
 \r
 </project>
\ No newline at end of file