Merge "Create Preliminary Documentation for CPS-Core & NCMP"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / YangResourceRepository.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Pantheon.tech
4  *  Modifications Copyright (C) Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.spi.repository;
22
23 import java.util.List;
24 import java.util.Set;
25 import javax.validation.constraints.NotNull;
26 import org.onap.cps.spi.entities.YangResourceEntity;
27 import org.onap.cps.spi.entities.YangResourceModuleReference;
28 import org.springframework.data.jpa.repository.JpaRepository;
29 import org.springframework.data.jpa.repository.Modifying;
30 import org.springframework.data.jpa.repository.Query;
31 import org.springframework.data.repository.query.Param;
32 import org.springframework.stereotype.Repository;
33
34 @Repository
35 public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Long>,
36         SchemaSetYangResourceRepository {
37
38     List<YangResourceEntity> findAllByChecksumIn(@NotNull Set<String> checksum);
39
40     @Query(value = "SELECT DISTINCT\n"
41         + "yr.module_name AS module_name,\n"
42         + "yr.revision AS revision\n"
43         + "FROM\n"
44         + "dataspace d\n"
45         + "JOIN schema_set ss ON ss.dataspace_id = d.id\n"
46         + "JOIN schema_set_yang_resources ssyr ON ssyr.schema_set_id = ss.id\n"
47         + "JOIN yang_resource yr ON yr.id = ssyr.yang_resource_id\n"
48         + "WHERE\n"
49         + "d.name = :dataspaceName", nativeQuery = true)
50     Set<YangResourceModuleReference> findAllModuleReferences(@Param("dataspaceName") String dataspaceName);
51
52     @Query(value = "SELECT DISTINCT\n"
53         + "yr.module_Name AS module_name,\n"
54         + "yr.revision AS revision\n"
55         + "FROM\n"
56         + "dataspace d\n"
57         + "JOIN anchor a ON a.dataspace_id = d.id\n"
58         + "JOIN schema_set ss ON ss.dataspace_id = a.dataspace_id\n"
59         + "JOIN schema_set_yang_resources ssyr ON ssyr.schema_set_id = ss.id\n"
60         + "JOIN yang_resource yr ON yr.id = ssyr.yang_resource_id\n"
61         + "WHERE\n"
62         + "d.name = :dataspaceName AND\n"
63         + "a.name =:anchorName", nativeQuery = true)
64     Set<YangResourceModuleReference> findAllModuleReferences(
65         @Param("dataspaceName") String dataspaceName, @Param("anchorName") String anchorName);
66
67     @Query(value = "SELECT id FROM yang_resource WHERE module_name=:name and revision=:revision", nativeQuery = true)
68     Long getIdByModuleNameAndRevision(@Param("name") String moduleName, @Param("revision") String revision);
69
70     @Modifying
71     @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS "
72         + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true)
73     void deleteOrphans();
74 }