CPS-508: Create anchor/schemaset from new modules and existing modules
[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 module_name, revision FROM yang_resource", nativeQuery = true)
41     List<YangResourceModuleReference> findAllModuleNameAndRevision();
42
43     @Query(value = "SELECT id FROM yang_resource WHERE module_name=:name and revision=:revision", nativeQuery = true)
44     Long getIdByModuleNameAndRevision(@Param("name") String moduleName, @Param("revision") String revision);
45
46     @Modifying
47     @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS "
48         + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true)
49     void deleteOrphans();
50 }