2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021-2023 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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.repository;
23 import java.util.Collection;
24 import java.util.Optional;
25 import org.onap.cps.spi.entities.AnchorEntity;
26 import org.onap.cps.spi.entities.DataspaceEntity;
27 import org.onap.cps.spi.entities.SchemaSetEntity;
28 import org.onap.cps.spi.exceptions.AnchorNotFoundException;
29 import org.springframework.data.jpa.repository.JpaRepository;
30 import org.springframework.data.jpa.repository.Query;
31 import org.springframework.data.repository.query.Param;
32 import org.springframework.stereotype.Repository;
35 public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> {
37 Optional<AnchorEntity> findByDataspaceAndName(DataspaceEntity dataspaceEntity, String name);
39 default AnchorEntity getByDataspaceAndName(DataspaceEntity dataspace, String anchorName) {
40 return findByDataspaceAndName(dataspace, anchorName)
41 .orElseThrow(() -> new AnchorNotFoundException(anchorName, dataspace.getName()));
44 Collection<AnchorEntity> findAllByDataspace(DataspaceEntity dataspaceEntity);
46 Collection<AnchorEntity> findAllBySchemaSet(SchemaSetEntity schemaSetEntity);
48 Collection<AnchorEntity> findAllByDataspaceAndNameIn(DataspaceEntity dataspaceEntity,
49 Collection<String> anchorNames);
51 Collection<AnchorEntity> findAllByDataspaceAndSchemaSetNameIn(DataspaceEntity dataspaceEntity,
52 Collection<String> schemaSetNames);
54 Integer countByDataspace(DataspaceEntity dataspaceEntity);
56 @Query(value = "SELECT anchor.* FROM yang_resource\n"
57 + "JOIN schema_set_yang_resources ON schema_set_yang_resources.yang_resource_id = yang_resource.id\n"
58 + "JOIN schema_set ON schema_set.id = schema_set_yang_resources.schema_set_id\n"
59 + "JOIN anchor ON anchor.schema_set_id = schema_set.id\n"
60 + "WHERE schema_set.dataspace_id = :dataspaceId AND module_name IN (:moduleNames)\n"
61 + "GROUP BY anchor.id, anchor.name, anchor.dataspace_id, anchor.schema_set_id\n"
62 + "HAVING COUNT(DISTINCT module_name) = :sizeOfModuleNames", nativeQuery = true)
63 Collection<AnchorEntity> getAnchorsByDataspaceIdAndModuleNames(@Param("dataspaceId") int dataspaceId,
64 @Param("moduleNames") Collection<String> moduleNames, @Param("sizeOfModuleNames") int sizeOfModuleNames);
66 void deleteAllByDataspaceAndNameIn(DataspaceEntity dataspaceEntity,
67 Collection<String> anchorNames);