895937b60ace93dd8dc481e6d41ed2e66cb9cd13
[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) 2021-2022 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.Collection;
24 import java.util.List;
25 import java.util.Set;
26 import javax.validation.constraints.NotNull;
27 import org.onap.cps.spi.entities.YangResourceEntity;
28 import org.onap.cps.spi.entities.YangResourceModuleReference;
29 import org.springframework.data.jpa.repository.JpaRepository;
30 import org.springframework.data.jpa.repository.Modifying;
31 import org.springframework.data.jpa.repository.Query;
32 import org.springframework.data.repository.query.Param;
33 import org.springframework.stereotype.Repository;
34
35 @Repository
36 public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Long>,
37         SchemaSetYangResourceRepository {
38
39     List<YangResourceEntity> findAllByChecksumIn(@NotNull Set<String> checksum);
40
41     @Query(value = "SELECT DISTINCT\n"
42         + "yang_resource.module_name AS module_name,\n"
43         + "yang_resource.revision AS revision\n"
44         + "FROM\n"
45         + "dataspace\n"
46         + "JOIN schema_set ON schema_set.dataspace_id = dataspace.id\n"
47         + "JOIN schema_set_yang_resources ON schema_set_yang_resources.schema_set_id = "
48         + "schema_set.id\n"
49         + "JOIN yang_resource ON yang_resource.id = schema_set_yang_resources.yang_resource_id\n"
50         + "WHERE\n"
51         + "dataspace.name = :dataspaceName", nativeQuery = true)
52     Set<YangResourceModuleReference> findAllModuleReferences(@Param("dataspaceName") String dataspaceName);
53
54     @Query(value = "SELECT DISTINCT\n"
55         + "yang_resource.module_Name AS module_name,\n"
56         + "yang_resource.revision AS revision\n"
57         + "FROM\n"
58         + "dataspace\n"
59         + "JOIN anchor ON anchor.dataspace_id = dataspace.id\n"
60         + "JOIN schema_set ON schema_set.id = anchor.schema_set_id\n"
61         + "JOIN schema_set_yang_resources ON schema_set_yang_resources.schema_set_id = "
62         + "schema_set.id\n"
63         + "JOIN yang_resource ON yang_resource.id = schema_set_yang_resources.yang_resource_id\n"
64         + "WHERE\n"
65         + "dataspace.name = :dataspaceName AND\n"
66         + "anchor.name =:anchorName", nativeQuery = true)
67     Set<YangResourceModuleReference> findAllModuleReferences(
68         @Param("dataspaceName") String dataspaceName, @Param("anchorName") String anchorName);
69
70     @Query(value = "SELECT DISTINCT\n"
71         + "yang_resource.*\n"
72         + "FROM\n"
73         + "dataspace\n"
74         + "JOIN schema_set ON schema_set.dataspace_id = dataspace.id\n"
75         + "JOIN schema_set_yang_resources ON schema_set_yang_resources.schema_set_id = "
76         + "schema_set.id\n"
77         + "JOIN yang_resource ON yang_resource.id = schema_set_yang_resources.yang_resource_id\n"
78         + "WHERE\n"
79         + "dataspace.name = :dataspaceName and yang_resource.module_Name IN (:moduleNames)", nativeQuery = true)
80     Set<YangResourceModuleReference> findAllModuleReferences(@Param("dataspaceName") String dataspaceName,
81         @Param("moduleNames") Collection<String> moduleNames);
82
83
84     @Query(value = "SELECT id FROM yang_resource WHERE module_name=:name and revision=:revision", nativeQuery = true)
85     Long getIdByModuleNameAndRevision(@Param("name") String moduleName, @Param("revision") String revision);
86
87     @Modifying
88     @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS "
89         + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true)
90     void deleteOrphans();
91 }