2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021 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;
34 public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> {
36 Optional<AnchorEntity> findByDataspaceAndName(@NotNull DataspaceEntity dataspaceEntity, @NotNull String name);
38 default AnchorEntity getByDataspaceAndName(@NotNull DataspaceEntity dataspace,
39 @NotNull String anchorName) {
40 return findByDataspaceAndName(dataspace, anchorName)
41 .orElseThrow(() -> new AnchorNotFoundException(anchorName, dataspace.getName()));
44 Collection<AnchorEntity> findAllByDataspace(@NotNull DataspaceEntity dataspaceEntity);
46 Collection<AnchorEntity> findAllBySchemaSet(@NotNull SchemaSetEntity schemaSetEntity);
48 @Query(value = "SELECT DISTINCT\n"
52 + "JOIN schema_set_yang_resources ON "
53 + "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 + "JOIN dataspace ON dataspace.id = anchor.dataspace_id AND dataspace.name = :dataspaceName\n"
57 + "WHERE yang_resource.module_Name IN (:moduleNames)", nativeQuery = true)
58 Collection<AnchorEntity> getAnchorsByDataspaceNameAndModuleNames(@Param("dataspaceName") String dataspaceName,
59 @Param("moduleNames") Collection<String> moduleNames);