Merge "Issue with CPSData API to add an item to an existing list node"
authorNiamh Core <niamh.core@est.tech>
Mon, 16 Aug 2021 15:41:27 +0000 (15:41 +0000)
committerGerrit Code Review <gerrit@onap.org>
Mon, 16 Aug 2021 15:41:27 +0000 (15:41 +0000)
24 files changed:
cps-dependencies/pom.xml
cps-ncmp-rest/pom.xml
cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java
cps-parent/pom.xml
cps-rest/pom.xml
cps-rest/src/main/java/org/onap/cps/rest/controller/AdminRestController.java
cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java
cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java
cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy
cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy
cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy
cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java
cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceModuleReference.java [new file with mode: 0644]
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java
cps-ri/src/main/resources/changelog/changelog-master.yaml
cps-ri/src/main/resources/changelog/db/changes/11-add-column-to-yang-resources-table.yaml [new file with mode: 0644]
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy
cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java
cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java
cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java
cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy
releases/1.1.0.yaml [new file with mode: 0644]

index 8d147ab..1cd36f6 100755 (executable)
                 <artifactId>swagger-annotations</artifactId>
                 <version>2.1.4</version>
             </dependency>
-            <!---To be removed once swagger-codegen-maven-plugin is upgraded-->
-            <dependency>
-                <groupId>io.swagger</groupId>
-                <artifactId>swagger-annotations</artifactId>
-                <version>1.6.2</version>
-            </dependency>
             <dependency>
                 <groupId>io.springfox</groupId>
                 <artifactId>springfox-boot-starter</artifactId>
index e0c4f6f..dbe1af0 100644 (file)
         <groupId>io.springfox</groupId>
         <artifactId>springfox-boot-starter</artifactId>
     </dependency>
-    <dependency>
-        <groupId>io.swagger</groupId>
-        <artifactId>swagger-annotations</artifactId>
-    </dependency>
     <!-- T E S T   D E P E N D E N C I E S -->
     <dependency>
         <groupId>org.codehaus.groovy</groupId>
index e9e0223..3d771b6 100755 (executable)
@@ -27,6 +27,7 @@ import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import java.util.Collection;
 import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
@@ -66,8 +67,8 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
      */
     @Override
     @Deprecated(forRemoval = false)
-    public ResponseEntity<Void> createNode(final String jsonData, final String cmHandle,
-        final String parentNodeXpath) {
+    public ResponseEntity<Void> createNode(final String cmHandle, @Valid final String jsonData,
+        @Valid final String parentNodeXpath) {
         networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.CREATED);
     }
@@ -78,8 +79,8 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
      */
     @Override
     @Deprecated(forRemoval = false)
-    public ResponseEntity<Void> addListNodeElements(final String jsonData, final String parentNodeXpath,
-        final String cmHandle) {
+    public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
+        final String cmHandle, @Valid final String jsonData) {
         networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.CREATED);
     }
@@ -132,7 +133,7 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
      */
     @Override
     @Deprecated(forRemoval = false)
-    public ResponseEntity<Object> replaceNode(@Valid final String jsonData, final String cmHandle,
+    public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final String jsonData,
         @Valid final String parentNodeXpath) {
         networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.OK);
@@ -144,7 +145,7 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
      */
     @Override
     @Deprecated(forRemoval = false)
-    public ResponseEntity<Object> updateNodeLeaves(@Valid final String jsonData, final String cmHandle,
+    public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final String jsonData,
         @Valid final String parentNodeXpath) {
         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.OK);
index 59b4290..938e75e 100755 (executable)
                 <plugin>
                     <groupId>io.swagger.codegen.v3</groupId>
                     <artifactId>swagger-codegen-maven-plugin</artifactId>
-                    <version>3.0.18</version>
+                    <version>3.0.27</version>
                 </plugin>
                 <plugin>
                     <groupId>com.github.spotbugs</groupId>
index 9a13117..9e29074 100755 (executable)
             <groupId>io.springfox</groupId>
             <artifactId>springfox-boot-starter</artifactId>
         </dependency>
-        <dependency>
-            <groupId>io.swagger</groupId>
-            <artifactId>swagger-annotations</artifactId>
-        </dependency>
         <dependency>
             <groupId>org.springdoc</groupId>
             <artifactId>springdoc-openapi-ui</artifactId>
index 879d5dc..a1287b2 100755 (executable)
@@ -28,6 +28,8 @@ import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;
 import java.util.Collection;
 import java.util.List;
 import java.util.stream.Collectors;
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
 import org.modelmapper.ModelMapper;
 import org.onap.cps.api.CpsAdminService;
 import org.onap.cps.api.CpsModuleService;
@@ -63,7 +65,7 @@ public class AdminRestController implements CpsAdminApi {
      * @return a {@Link ResponseEntity} of created dataspace name & {@link HttpStatus} CREATED
      */
     @Override
-    public ResponseEntity<String> createDataspace(final String dataspaceName) {
+    public ResponseEntity<String> createDataspace(@NotNull @Valid final String dataspaceName) {
         cpsAdminService.createDataspace(dataspaceName);
         return new ResponseEntity<>(dataspaceName, HttpStatus.CREATED);
     }
@@ -88,8 +90,8 @@ public class AdminRestController implements CpsAdminApi {
      * @return a {@Link ResponseEntity} of created schemaset name & {@link HttpStatus} CREATED
      */
     @Override
-    public ResponseEntity<String> createSchemaSet(final MultipartFile multipartFile,
-        final String schemaSetName, final String dataspaceName) {
+    public ResponseEntity<String> createSchemaSet(@NotNull @Valid final String schemaSetName,
+        final String dataspaceName, @Valid final MultipartFile multipartFile) {
         cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
         return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
     }
@@ -130,8 +132,8 @@ public class AdminRestController implements CpsAdminApi {
      * @return a ResponseEntity with the anchor name & {@link HttpStatus} CREATED
      */
     @Override
-    public ResponseEntity<String> createAnchor(final String dataspaceName, final String schemaSetName,
-        final String anchorName) {
+    public ResponseEntity<String> createAnchor(final String dataspaceName, @NotNull @Valid final String schemaSetName,
+        @NotNull @Valid final String anchorName) {
         cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
         return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
     }
index 64b4418..bad66dd 100755 (executable)
@@ -21,6 +21,8 @@
 
 package org.onap.cps.rest.controller;
 
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.rest.api.CpsDataApi;
 import org.onap.cps.spi.FetchDescendantsOption;
@@ -41,8 +43,8 @@ public class DataRestController implements CpsDataApi {
     private CpsDataService cpsDataService;
 
     @Override
-    public ResponseEntity<String> createNode(final String jsonData, final String dataspaceName, final String anchorName,
-        final String parentNodeXpath) {
+    public ResponseEntity<String> createNode(final String dataspaceName, final String anchorName,
+        final String jsonData, final String parentNodeXpath) {
         if (isRootXpath(parentNodeXpath)) {
             cpsDataService.saveData(dataspaceName, anchorName, jsonData);
         } else {
@@ -52,8 +54,8 @@ public class DataRestController implements CpsDataApi {
     }
 
     @Override
-    public ResponseEntity<String> addListNodeElements(final String jsonData, final String parentNodeXpath,
-        final String dataspaceName, final String anchorName) {
+    public ResponseEntity<String> addListNodeElements(final String parentNodeXpath,
+        final String dataspaceName, final String anchorName, final String jsonData) {
         cpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.CREATED);
     }
@@ -74,22 +76,22 @@ public class DataRestController implements CpsDataApi {
     }
 
     @Override
-    public ResponseEntity<Object> updateNodeLeaves(final String jsonData, final String dataspaceName,
-        final String anchorName, final String parentNodeXpath) {
+    public ResponseEntity<Object> updateNodeLeaves(final String dataspaceName,
+        final String anchorName, final String jsonData, final String parentNodeXpath) {
         cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.OK);
     }
 
     @Override
-    public ResponseEntity<Object> replaceNode(final String jsonData, final String dataspaceName,
-        final String anchorName, final String parentNodeXpath) {
+    public ResponseEntity<Object> replaceNode(final String dataspaceName,
+        final String anchorName, @Valid final String jsonData, @Valid final String parentNodeXpath) {
         cpsDataService.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.OK);
     }
 
     @Override
-    public ResponseEntity<String> replaceListNodeElements(final String jsonData, final String parentNodeXpath,
-        final String dataspaceName, final String anchorName) {
+    public ResponseEntity<String> replaceListNodeElements(@NotNull @Valid final String parentNodeXpath,
+        final String dataspaceName, final String anchorName, @Valid final String jsonData) {
         cpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.OK);
     }
index b7fc9f7..8aa65a0 100644 (file)
 package org.onap.cps.rest.controller;
 
 import com.google.gson.Gson;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
+import java.util.Map;
 import javax.validation.Valid;
 import org.onap.cps.api.CpsQueryService;
 import org.onap.cps.rest.api.CpsQueryApi;
 import org.onap.cps.spi.FetchDescendantsOption;
 import org.onap.cps.spi.model.DataNode;
+import org.onap.cps.utils.DataMapUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -47,6 +51,10 @@ public class QueryRestController implements CpsQueryApi {
             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
         final Collection<DataNode> dataNodes =
             cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption);
-        return new ResponseEntity<>(new Gson().toJson(dataNodes), HttpStatus.OK);
+        final List<Map<String, Object>> dataNodeList = new ArrayList<>();
+        for (final DataNode dataNode : dataNodes) {
+            dataNodeList.add(DataMapUtils.toDataMap(dataNode));
+        }
+        return new ResponseEntity<>(new Gson().toJson(dataNodeList), HttpStatus.OK);
     }
 }
index 2f97a31..84da2db 100755 (executable)
@@ -49,7 +49,7 @@ import org.springframework.util.LinkedMultiValueMap
 import org.springframework.util.MultiValueMap
 import spock.lang.Specification
 
-@WebMvcTest
+@WebMvcTest(AdminRestController)
 class AdminRestControllerSpec extends Specification {
 
     @SpringBean
@@ -58,12 +58,6 @@ class AdminRestControllerSpec extends Specification {
     @SpringBean
     CpsAdminService mockCpsAdminService = Mock()
 
-    @SpringBean
-    CpsDataService mockCpsDataService = Mock()
-
-    @SpringBean
-    CpsQueryService mockCpsQueryService = Mock()
-
     @SpringBean
     ModelMapper modelMapper = Spy()
 
index ec31c7f..8675f42 100755 (executable)
@@ -46,24 +46,12 @@ import org.springframework.test.web.servlet.MockMvc
 import spock.lang.Shared
 import spock.lang.Specification
 
-@WebMvcTest
+@WebMvcTest(DataRestController)
 class DataRestControllerSpec extends Specification {
 
     @SpringBean
     CpsDataService mockCpsDataService = Mock()
 
-    @SpringBean
-    CpsModuleService mockCpsModuleService = Mock()
-
-    @SpringBean
-    CpsAdminService mockCpsAdminService = Mock()
-
-    @SpringBean
-    CpsQueryService mockCpsQueryService = Mock()
-
-    @SpringBean
-    ModelMapper modelMapper = Mock()
-
     @Autowired
     MockMvc mvc
 
index 6b6bdee..550dec9 100644 (file)
@@ -22,6 +22,8 @@
 
 package org.onap.cps.rest.controller
 
+import org.onap.cps.spi.model.DataNode
+
 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
@@ -41,24 +43,12 @@ import org.springframework.http.HttpStatus
 import org.springframework.test.web.servlet.MockMvc
 import spock.lang.Specification
 
-@WebMvcTest
+@WebMvcTest(QueryRestController)
 class QueryRestControllerSpec extends Specification {
 
-    @SpringBean
-    CpsDataService mockCpsDataService = Mock()
-
-    @SpringBean
-    CpsModuleService mockCpsModuleService = Mock()
-
-    @SpringBean
-    CpsAdminService mockCpsAdminService = Mock()
-
     @SpringBean
     CpsQueryService mockCpsQueryService = Mock()
 
-    @SpringBean
-    ModelMapper modelMapper = Mock()
-
     @Autowired
     MockMvc mvc
 
@@ -67,11 +57,12 @@ class QueryRestControllerSpec extends Specification {
 
     def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() {
         given: 'service method returns a list containing a data node'
-            def dataNode = new DataNodeBuilder().withXpath('/xpath').build()
+             def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
+                    .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
             def dataspaceName = 'my_dataspace'
             def anchorName = 'my_anchor'
             def cpsPath = 'some cps-path'
-            mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode]
+            mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode1]
         and: 'the query endpoint'
             def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
         when: 'query data nodes API is invoked'
@@ -83,7 +74,7 @@ class QueryRestControllerSpec extends Specification {
                             .andReturn().response
         then: 'the response contains the the datanode in json format'
             response.status == HttpStatus.OK.value()
-            response.getContentAsString().contains(new Gson().toJson(dataNode))
+            response.getContentAsString() == '[{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]},{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}]'
         where: 'the following options for include descendants are provided in the request'
             scenario                    | includeDescendantsOption || expectedCpsDataServiceOption
             'no descendants by default' | ''                       || OMIT_DESCENDANTS
index 4763d68..032745b 100644 (file)
@@ -1,6 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Pantheon.tech
+ *  Modifications Copyright (C) 2021 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -61,6 +62,14 @@ public class YangResourceEntity implements Serializable {
     @Column
     private String content;
 
+    @NotNull
+    @Column
+    private String moduleName;
+
+    @NotNull
+    @Column
+    private String revision;
+
     @ManyToMany(mappedBy = "yangResources")
     private Set<SchemaSetEntity> schemaSets;
 
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceModuleReference.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceModuleReference.java
new file mode 100644 (file)
index 0000000..3c39c6b
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 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.entities;
+
+import org.springframework.beans.factory.annotation.Value;
+
+public interface YangResourceModuleReference {
+
+    @Value("#{target.module_name}")
+    String getModuleName();
+
+    String getRevision();
+}
index 1b3dc24..535cbe2 100755 (executable)
 
 package org.onap.cps.spi.impl;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableSet;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -42,14 +49,22 @@ import org.onap.cps.spi.CpsModulePersistenceService;
 import org.onap.cps.spi.entities.AnchorEntity;
 import org.onap.cps.spi.entities.SchemaSetEntity;
 import org.onap.cps.spi.entities.YangResourceEntity;
+import org.onap.cps.spi.entities.YangResourceModuleReference;
 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException;
+import org.onap.cps.spi.exceptions.ModelValidationException;
 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
+import org.onap.cps.spi.model.ModuleReference;
 import org.onap.cps.spi.repository.AnchorRepository;
 import org.onap.cps.spi.repository.DataspaceRepository;
 import org.onap.cps.spi.repository.FragmentRepository;
 import org.onap.cps.spi.repository.SchemaSetRepository;
 import org.onap.cps.spi.repository.YangResourceRepository;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.retry.annotation.Backoff;
@@ -63,6 +78,8 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
 
     private static final String YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME = "yang_resource_checksum_key";
     private static final Pattern CHECKSUM_EXCEPTION_PATTERN = Pattern.compile(".*\\(checksum\\)=\\((\\w+)\\).*");
+    private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN = Pattern
+            .compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE);
 
     @Autowired
     private YangResourceRepository yangResourceRepository;
@@ -82,6 +99,29 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
     @Autowired
     private CpsAdminPersistenceService cpsAdminPersistenceService;
 
+    @Override
+    public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
+        final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+        final var schemaSetEntity =
+            schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
+        return schemaSetEntity.getYangResources().stream().collect(
+            Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent));
+    }
+
+    @Override
+    public Map<String, String> getYangSchemaSetResources(final String dataspaceName, final String anchorName) {
+        final var anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
+        return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
+    }
+
+    @Override
+    public List<ModuleReference> getAllYangResourcesModuleReferences() {
+        final List<YangResourceModuleReference> yangResourceModuleReferenceList =
+                yangResourceRepository.findAllModuleNameAndRevision();
+        return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
+                .collect(Collectors.toList());
+    }
+
     @Override
     @Transactional
     // A retry is made to store the schema set if it fails because of duplicated yang resource exception that
@@ -91,7 +131,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
         final Map<String, String> yangResourcesNameToContentMap) {
 
         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
-        final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap);
+        final var yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap);
         final var schemaSetEntity = new SchemaSetEntity();
         schemaSetEntity.setName(schemaSetName);
         schemaSetEntity.setDataspace(dataspaceEntity);
@@ -103,13 +143,37 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
         }
     }
 
+    @Override
+    @Transactional
+    public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
+        final CascadeDeleteAllowed cascadeDeleteAllowed) {
+        final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+        final var schemaSetEntity =
+            schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
+
+        final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllBySchemaSet(schemaSetEntity);
+        if (!anchorEntities.isEmpty()) {
+            if (cascadeDeleteAllowed != CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) {
+                throw new SchemaSetInUseException(dataspaceName, schemaSetName);
+            }
+            fragmentRepository.deleteByAnchorIn(anchorEntities);
+            anchorRepository.deleteAll(anchorEntities);
+        }
+        schemaSetRepository.delete(schemaSetEntity);
+        yangResourceRepository.deleteOrphans();
+    }
+
     private Set<YangResourceEntity> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
         final Map<String, YangResourceEntity> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
             .map(entry -> {
                 final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8));
+                final Map<String, String> moduleNameAndRevisionMap = createModuleNameAndRevisionMap(entry.getKey(),
+                            entry.getValue());
                 final var yangResourceEntity = new YangResourceEntity();
                 yangResourceEntity.setName(entry.getKey());
                 yangResourceEntity.setContent(entry.getValue());
+                yangResourceEntity.setModuleName(moduleNameAndRevisionMap.get("moduleName"));
+                yangResourceEntity.setRevision(moduleNameAndRevisionMap.get("revision"));
                 yangResourceEntity.setChecksum(checksum);
                 return yangResourceEntity;
             })
@@ -147,6 +211,42 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
             .build();
     }
 
+    private static Map<String, String> createModuleNameAndRevisionMap(final String sourceName, final String source) {
+        final Map<String, String> metaDataMap = new HashMap<>();
+        final var revisionSourceIdentifier =
+                createIdentifierFromSourceName(checkNotNull(sourceName));
+
+        final var tempYangTextSchemaSource = new YangTextSchemaSource(revisionSourceIdentifier) {
+            @Override
+            protected MoreObjects.ToStringHelper addToStringAttributes(
+                    final MoreObjects.ToStringHelper toStringHelper) {
+                return toStringHelper;
+            }
+
+            @Override
+            public InputStream openStream() {
+                return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
+            }
+        };
+        try {
+            final var dependencyInfo = YangModelDependencyInfo.forYangText(tempYangTextSchemaSource);
+            metaDataMap.put("moduleName", dependencyInfo.getName());
+            metaDataMap.put("revision", dependencyInfo.getFormattedRevision());
+        } catch (final YangSyntaxErrorException | IOException e) {
+            throw new ModelValidationException("Yang resource is invalid.",
+                   String.format("Yang syntax validation failed for resource %s:%n%s", sourceName, e.getMessage()), e);
+        }
+        return metaDataMap;
+    }
+
+    private static RevisionSourceIdentifier createIdentifierFromSourceName(final String sourceName) {
+        final var matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName);
+        if (matcher.matches()) {
+            return RevisionSourceIdentifier.create(matcher.group(1), Revision.of(matcher.group(2)));
+        }
+        return RevisionSourceIdentifier.create(sourceName);
+    }
+
     /**
      * Convert the specified data integrity violation exception into a CPS duplicated Yang resource exception
      * if the cause of the error is a yang checksum database constraint violation.
@@ -178,20 +278,6 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
 
     }
 
-    /**
-     * Get the checksum that caused the constraint violation exception.
-     * @param exception the exception having the checksum in error.
-     * @return the checksum in error or null if not found.
-     */
-    private String getDuplicatedChecksumFromException(final ConstraintViolationException exception) {
-        String checksum = null;
-        final var matcher = CHECKSUM_EXCEPTION_PATTERN.matcher(exception.getSQLException().getMessage());
-        if (matcher.find() && matcher.groupCount() == 1) {
-            checksum = matcher.group(1);
-        }
-        return checksum;
-    }
-
     /**
      * Get the name of the yang resource having the specified checksum.
      * @param checksum the checksum. Null is supported.
@@ -208,38 +294,24 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
                         .orElse(null);
     }
 
-    @Override
-    public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
-        final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
-        final var schemaSetEntity =
-            schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
-        return schemaSetEntity.getYangResources().stream().collect(
-            Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent));
-    }
-
-    @Override
-    public Map<String, String> getYangSchemaSetResources(final String dataspaceName, final String anchorName) {
-        final var anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
-        return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
+    /**
+     * Get the checksum that caused the constraint violation exception.
+     * @param exception the exception having the checksum in error.
+     * @return the checksum in error or null if not found.
+     */
+    private String getDuplicatedChecksumFromException(final ConstraintViolationException exception) {
+        String checksum = null;
+        final var matcher = CHECKSUM_EXCEPTION_PATTERN.matcher(exception.getSQLException().getMessage());
+        if (matcher.find() && matcher.groupCount() == 1) {
+            checksum = matcher.group(1);
+        }
+        return checksum;
     }
 
-    @Override
-    @Transactional
-    public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
-        final CascadeDeleteAllowed cascadeDeleteAllowed) {
-        final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
-        final var schemaSetEntity =
-            schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
-
-        final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllBySchemaSet(schemaSetEntity);
-        if (!anchorEntities.isEmpty()) {
-            if (cascadeDeleteAllowed != CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) {
-                throw new SchemaSetInUseException(dataspaceName, schemaSetName);
-            }
-            fragmentRepository.deleteByAnchorIn(anchorEntities);
-            anchorRepository.deleteAll(anchorEntities);
-        }
-        schemaSetRepository.delete(schemaSetEntity);
-        yangResourceRepository.deleteOrphans();
+    private static ModuleReference toModuleReference(final YangResourceModuleReference yangResourceModuleReference) {
+        return ModuleReference.builder()
+                .name(yangResourceModuleReference.getModuleName())
+                .revision(yangResourceModuleReference.getRevision())
+                .build();
     }
 }
index 25d7689..fe8787f 100644 (file)
@@ -1,6 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Pantheon.tech
+ *  Modifications Copyright (C) Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -23,6 +24,7 @@ import java.util.List;
 import java.util.Set;
 import javax.validation.constraints.NotNull;
 import org.onap.cps.spi.entities.YangResourceEntity;
+import org.onap.cps.spi.entities.YangResourceModuleReference;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
@@ -33,6 +35,9 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity
 
     List<YangResourceEntity> findAllByChecksumIn(@NotNull Set<String> checksum);
 
+    @Query(value = "SELECT module_name, revision FROM yang_resource", nativeQuery = true)
+    List<YangResourceModuleReference> findAllModuleNameAndRevision();
+
     @Modifying
     @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS "
         + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true)
index 6c01583..14f3726 100644 (file)
@@ -35,3 +35,5 @@ databaseChangeLog:
       file: changelog/db/changes/09-loadData-dmi-registry-schema-set.yaml
   - include:
       file: changelog/db/changes/10-loadData-dmi-registry-fragment.yaml
+  - include:
+      file: changelog/db/changes/11-add-column-to-yang-resources-table.yaml
diff --git a/cps-ri/src/main/resources/changelog/db/changes/11-add-column-to-yang-resources-table.yaml b/cps-ri/src/main/resources/changelog/db/changes/11-add-column-to-yang-resources-table.yaml
new file mode 100644 (file)
index 0000000..a2bfb67
--- /dev/null
@@ -0,0 +1,40 @@
+# ============LICENSE_START=======================================================
+# Copyright (C) 2021 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=========================================================
+
+databaseChangeLog:
+  - changeSet:
+      id: 11
+      label: add-module-name-and-revision-column
+      author: cps
+      changes:
+        - addColumn:
+            tableName: yang_resource
+            columns:
+              - column:
+                  name: module_name
+                  type: TEXT
+              - column:
+                  name: revision
+                  type: TEXT
+  - changeSet:
+      id: 11.1
+      label: update-previous-data-module-name-and-revision
+      author: cps
+      changes:
+        - sql:
+            sql: update yang_resource set module_name = 'dummy_module_name', revision = '2021-08-04' where module_name is null and revision is null
index dace82a..f5d5fc6 100644 (file)
@@ -30,6 +30,7 @@ import org.onap.cps.spi.exceptions.DataspaceNotFoundException
 import org.onap.cps.spi.exceptions.AlreadyDefinedException
 import org.onap.cps.spi.exceptions.SchemaSetInUseException
 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
+import org.onap.cps.spi.model.ModuleReference
 import org.onap.cps.spi.repository.AnchorRepository
 import org.onap.cps.spi.repository.SchemaSetRepository
 import org.springframework.beans.factory.annotation.Autowired
@@ -57,10 +58,27 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase
 
     static final Long NEW_RESOURCE_ABSTRACT_ID = 0L
     static final String NEW_RESOURCE_NAME = 'some new resource'
-    static final String NEW_RESOURCE_CONTENT = 'some resource content'
-    static final String NEW_RESOURCE_CHECKSUM = '09002da02ee2683898d2c81c67f9e22cdbf8577d8c2de16c84d724e4ae44a0a6'
+    static final String NEW_RESOURCE_CONTENT = 'module stores {\n' +
+            '    yang-version 1.1;\n' +
+            '    namespace "org:onap:ccsdk:sample";\n' +
+            '\n' +
+            '    prefix book-store;\n' +
+            '\n' +
+            '    revision "2020-09-15" {\n' +
+            '        description\n' +
+            '        "Sample Model";\n' +
+            '    }' +
+            '}'
+    static final String NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
+    static final String NEW_RESOURCE_MODULE_NAME = 'stores'
+    static final String NEW_RESOURCE_REVISION = '2020-09-15'
+    static final ModuleReference newModuleReference = ModuleReference.builder().name(NEW_RESOURCE_MODULE_NAME)
+            .revision(NEW_RESOURCE_REVISION).build()
 
     def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
+    def allYangResourcesModuleAndRevisionList = [ModuleReference.builder().build(),ModuleReference.builder().build(),
+                                                 ModuleReference.builder().build(),ModuleReference.builder().build(),
+                                                 ModuleReference.builder().build(), newModuleReference]
     def dataspaceEntity
 
     def setup() {
@@ -85,7 +103,7 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase
             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
         then: 'the schema set is persisted correctly'
             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME,
-                    NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM)
+                    NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM, NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION)
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
@@ -100,19 +118,32 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase
              result == newYangResourcesNameToContentMap
     }
 
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Retrieving all yang resources module references.'() {
+        given: 'a new schema set is stored'
+            objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
+        when: 'all yang resources module references are retrieved'
+            def result = objectUnderTest.getAllYangResourcesModuleReferences()
+        then: 'the correct resources are returned'
+            result.sort() == allYangResourcesModuleAndRevisionList.sort()
+    }
+
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Storing duplicate schema content.'() {
         given: 'a new schema set with a resource with the same content as an existing resource'
-            def existingResourceContent = 'CONTENT-001'
+            def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }'
             def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
         when: 'the schema set with duplicate resource is stored'
             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
         then: 'the schema persisted (re)uses the existing id, name and has the same checksum'
-            def existingResourceId = 3001L
+            def existingResourceId = 9L
             def existingResourceName = 'module1@2020-02-02.yang'
-            def existingResourceChecksum = 'e8bdda931099310de66532e08c3fafec391db29f55c81927b168f6aa8f81b73b'
+            def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a'
+            def existingResourceModelName = 'test'
+            def existingResourceRevision = '2020-09-15'
             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
-                    existingResourceId, existingResourceName, existingResourceContent, existingResourceChecksum)
+                    existingResourceId, existingResourceName, existingResourceContent, existingResourceChecksum,
+                    existingResourceModelName, existingResourceRevision)
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
@@ -163,11 +194,13 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase
     }
 
     def assertSchemaSetPersisted(expectedDataspaceName,
-                             expectedSchemaSetName,
-                             expectedYangResourceId,
-                             expectedYangResourceName,
-                             expectedYangResourceContent,
-                             expectedYangResourceChecksum) {
+                                 expectedSchemaSetName,
+                                 expectedYangResourceId,
+                                 expectedYangResourceName,
+                                 expectedYangResourceContent,
+                                 expectedYangResourceChecksum,
+                                 expectedYangResourceModuleName,
+                                 expectedYangResourceRevision) {
         // assert the schema set is persisted
         def schemaSetEntity = schemaSetRepository
                 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
@@ -188,6 +221,8 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase
         yangResourceEntity.name == expectedYangResourceName
         yangResourceEntity.content == expectedYangResourceContent
         yangResourceEntity.checksum == expectedYangResourceChecksum
+        yangResourceEntity.moduleName == expectedYangResourceModuleName
+        yangResourceEntity.revision == expectedYangResourceRevision
     }
 
 }
index 4455a6f..8bd7f86 100644 (file)
@@ -45,11 +45,21 @@ class CpsModulePersistenceServiceSpec extends Specification {
 
     // Constants
     def yangResourceName = 'my-yang-resource-name'
-    def yangResourceContent = 'my-yang-resource-content'
+    def yangResourceContent = 'module stores {\n' +
+            '    yang-version 1.1;\n' +
+            '    namespace "org:onap:ccsdk:sample";\n' +
+            '\n' +
+            '    prefix book-store;\n' +
+            '\n' +
+            '    revision "2020-09-15" {\n' +
+            '        description\n' +
+            '        "Sample Model";\n' +
+            '    }' +
+            '}'
 
     // Scenario data
     @Shared
-    yangResourceChecksum = 'ac2352cc20c10467528b2390bbf2d72d48b0319152ebaabcda207786b4a641c2'
+    yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
     @Shared
     yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
     @Shared
index cd156d6..fee4daa 100644 (file)
 
 package org.onap.cps.api;
 
+import java.util.List;
 import java.util.Map;
 import org.checkerframework.checker.nullness.qual.NonNull;
 import org.onap.cps.spi.CascadeDeleteAllowed;
 import org.onap.cps.spi.exceptions.DataInUseException;
+import org.onap.cps.spi.model.ModuleReference;
 import org.onap.cps.spi.model.SchemaSet;
 
 /**
@@ -63,4 +65,11 @@ public interface CpsModuleService {
      */
     void deleteSchemaSet(@NonNull String dataspaceName, @NonNull String schemaSetName,
         @NonNull CascadeDeleteAllowed cascadeDeleteAllowed);
+
+    /**
+     * Retrieve all modules and revisions known by CPS for all Yang Resources.
+     *
+     * @return a list of ModuleReference objects
+     */
+    List<ModuleReference> getAllYangResourcesModuleReferences();
 }
index e4e6d1b..34735f8 100644 (file)
 
 package org.onap.cps.api.impl;
 
+import java.util.List;
 import java.util.Map;
 import org.onap.cps.api.CpsModuleService;
 import org.onap.cps.spi.CascadeDeleteAllowed;
 import org.onap.cps.spi.CpsModulePersistenceService;
+import org.onap.cps.spi.model.ModuleReference;
 import org.onap.cps.spi.model.SchemaSet;
 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -61,4 +63,10 @@ public class CpsModuleServiceImpl implements CpsModuleService {
         final CascadeDeleteAllowed cascadeDeleteAllowed) {
         cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName, cascadeDeleteAllowed);
     }
+
+    @Override
+    public List<ModuleReference> getAllYangResourcesModuleReferences() {
+        return cpsModulePersistenceService.getAllYangResourcesModuleReferences();
+    }
+
 }
index ec8a53c..bc62a23 100755 (executable)
 
 package org.onap.cps.spi;
 
+import java.util.List;
 import java.util.Map;
 import org.checkerframework.checker.nullness.qual.NonNull;
 import org.onap.cps.spi.exceptions.DataInUseException;
+import org.onap.cps.spi.model.ModuleReference;
 
 /**
  * Service to manage modules.
@@ -73,4 +75,11 @@ public interface CpsModulePersistenceService {
     @NonNull
     Map<String, String> getYangSchemaSetResources(@NonNull String dataspaceName,
         @NonNull String anchorName);
+
+    /**
+     * Returns all YANG resources module references.
+     *
+     * @return List of all YANG resources module information in the database
+     */
+    List<ModuleReference> getAllYangResourcesModuleReferences();
 }
index 579415f..b8bfd45 100644 (file)
@@ -103,4 +103,12 @@ class CpsModuleServiceImplSpec extends Specification {
             'dataspace-1' | 'schemas-set-1' | CASCADE_DELETE_ALLOWED
             'dataspace-2' | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED
     }
+
+    def 'Get all yang resources module references.'(){
+        given: 'an already present module reference'
+            def moduleReferences = [new ModuleReference()]
+            mockModuleStoreService.getAllYangResourcesModuleReferences() >> moduleReferences
+        expect: 'the list provided by persistence service is returned as result'
+            objectUnderTest.getAllYangResourcesModuleReferences() == moduleReferences
+    }
 }
diff --git a/releases/1.1.0.yaml b/releases/1.1.0.yaml
new file mode 100644 (file)
index 0000000..4767cc4
--- /dev/null
@@ -0,0 +1,4 @@
+distribution_type: maven
+log_dir: cps-maven-stage-master/283/
+project: cps
+version: 1.1.0