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 javax.validation.constraints.NotNull;
26 import org.onap.cps.spi.entities.AnchorEntity;
27 import org.onap.cps.spi.entities.DataspaceEntity;
28 import org.onap.cps.spi.entities.SchemaSetEntity;
29 import org.onap.cps.spi.exceptions.AnchorNotFoundException;
30 import org.springframework.data.jpa.repository.JpaRepository;
31 import org.springframework.data.jpa.repository.Query;
32 import org.springframework.data.repository.query.Param;
33 import org.springframework.stereotype.Repository;
36 public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> {
38 Optional<AnchorEntity> findByDataspaceAndName(@NotNull DataspaceEntity dataspaceEntity, @NotNull String name);
40 default AnchorEntity getByDataspaceAndName(@NotNull DataspaceEntity dataspace,
41 @NotNull String anchorName) {
42 return findByDataspaceAndName(dataspace, anchorName)
43 .orElseThrow(() -> new AnchorNotFoundException(anchorName, dataspace.getName()));
46 Collection<AnchorEntity> findAllByDataspace(@NotNull DataspaceEntity dataspaceEntity);
48 Collection<AnchorEntity> findAllBySchemaSet(@NotNull SchemaSetEntity schemaSetEntity);
50 Integer countByDataspace(@NotNull DataspaceEntity dataspaceEntity);
52 @Query(value = "SELECT anchor.* FROM yang_resource\n"
53 + "JOIN schema_set_yang_resources ON schema_set_yang_resources.yang_resource_id = yang_resource.id\n"
54 + "JOIN schema_set ON schema_set.id = schema_set_yang_resources.schema_set_id\n"
55 + "JOIN anchor ON anchor.schema_set_id = schema_set.id\n"
56 + "WHERE schema_set.dataspace_id = :dataspaceId AND module_name IN (:moduleNames)\n"
57 + "GROUP BY anchor.id, anchor.name, anchor.dataspace_id, anchor.schema_set_id\n"
58 + "HAVING COUNT(DISTINCT module_name) = :sizeOfModuleNames", nativeQuery = true)
59 Collection<AnchorEntity> getAnchorsByDataspaceIdAndModuleNames(@Param("dataspaceId") int dataspaceId,
60 @Param("moduleNames") Collection<String> moduleNames, @Param("sizeOfModuleNames") int sizeOfModuleNames);