2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
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.impl;
23 import com.google.common.collect.ImmutableSet;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Collection;
26 import java.util.List;
29 import java.util.stream.Collectors;
30 import javax.transaction.Transactional;
31 import org.apache.commons.codec.digest.DigestUtils;
32 import org.onap.cps.spi.CascadeDeleteAllowed;
33 import org.onap.cps.spi.CpsAdminPersistenceService;
34 import org.onap.cps.spi.CpsModulePersistenceService;
35 import org.onap.cps.spi.entities.AnchorEntity;
36 import org.onap.cps.spi.entities.SchemaSetEntity;
37 import org.onap.cps.spi.entities.YangResourceEntity;
38 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
39 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
40 import org.onap.cps.spi.repository.AnchorRepository;
41 import org.onap.cps.spi.repository.DataspaceRepository;
42 import org.onap.cps.spi.repository.FragmentRepository;
43 import org.onap.cps.spi.repository.SchemaSetRepository;
44 import org.onap.cps.spi.repository.YangResourceRepository;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.dao.DataIntegrityViolationException;
47 import org.springframework.stereotype.Component;
51 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
54 private YangResourceRepository yangResourceRepository;
57 private SchemaSetRepository schemaSetRepository;
60 private DataspaceRepository dataspaceRepository;
63 private AnchorRepository anchorRepository;
66 private FragmentRepository fragmentRepository;
69 private CpsAdminPersistenceService cpsAdminPersistenceService;
73 public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
74 final Map<String, String> yangResourcesNameToContentMap) {
76 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
77 final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap);
78 final var schemaSetEntity = new SchemaSetEntity();
79 schemaSetEntity.setName(schemaSetName);
80 schemaSetEntity.setDataspace(dataspaceEntity);
81 schemaSetEntity.setYangResources(yangResourceEntities);
83 schemaSetRepository.save(schemaSetEntity);
84 } catch (final DataIntegrityViolationException e) {
85 throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e);
89 private Set<YangResourceEntity> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
90 final Map<String, YangResourceEntity> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
92 final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8));
93 final var yangResourceEntity = new YangResourceEntity();
94 yangResourceEntity.setName(entry.getKey());
95 yangResourceEntity.setContent(entry.getValue());
96 yangResourceEntity.setChecksum(checksum);
97 return yangResourceEntity;
99 .collect(Collectors.toMap(
100 YangResourceEntity::getChecksum,
104 final List<YangResourceEntity> existingYangResourceEntities =
105 yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
106 existingYangResourceEntities.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
108 final Collection<YangResourceEntity> newYangResourceEntities = checksumToEntityMap.values();
109 if (!newYangResourceEntities.isEmpty()) {
110 yangResourceRepository.saveAll(newYangResourceEntities);
113 return ImmutableSet.<YangResourceEntity>builder()
114 .addAll(existingYangResourceEntities)
115 .addAll(newYangResourceEntities)
120 public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
121 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
122 final var schemaSetEntity =
123 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
124 return schemaSetEntity.getYangResources().stream().collect(
125 Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent));
129 public Map<String, String> getYangSchemaSetResources(final String dataspaceName, final String anchorName) {
130 final var anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
131 return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
136 public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
137 final CascadeDeleteAllowed cascadeDeleteAllowed) {
138 final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
139 final var schemaSetEntity =
140 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
142 final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllBySchemaSet(schemaSetEntity);
143 if (!anchorEntities.isEmpty()) {
144 if (cascadeDeleteAllowed != CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) {
145 throw new SchemaSetInUseException(dataspaceName, schemaSetName);
147 fragmentRepository.deleteByAnchorIn(anchorEntities);
148 anchorRepository.deleteAll(anchorEntities);
150 schemaSetRepository.delete(schemaSetEntity);
151 yangResourceRepository.deleteOrphans();