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.DataspaceEntity;
37 import org.onap.cps.spi.entities.SchemaSetEntity;
38 import org.onap.cps.spi.entities.YangResourceEntity;
39 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
40 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
41 import org.onap.cps.spi.model.Anchor;
42 import org.onap.cps.spi.repository.AnchorRepository;
43 import org.onap.cps.spi.repository.DataspaceRepository;
44 import org.onap.cps.spi.repository.FragmentRepository;
45 import org.onap.cps.spi.repository.SchemaSetRepository;
46 import org.onap.cps.spi.repository.YangResourceRepository;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.dao.DataIntegrityViolationException;
49 import org.springframework.stereotype.Component;
53 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
56 private YangResourceRepository yangResourceRepository;
59 private SchemaSetRepository schemaSetRepository;
62 private DataspaceRepository dataspaceRepository;
65 private AnchorRepository anchorRepository;
68 private FragmentRepository fragmentRepository;
71 private CpsAdminPersistenceService cpsAdminPersistenceService;
75 public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
76 final Map<String, String> yangResourcesNameToContentMap) {
78 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
79 final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap);
80 final SchemaSetEntity schemaSetEntity = new SchemaSetEntity();
81 schemaSetEntity.setName(schemaSetName);
82 schemaSetEntity.setDataspace(dataspaceEntity);
83 schemaSetEntity.setYangResources(yangResourceEntities);
85 schemaSetRepository.save(schemaSetEntity);
86 } catch (final DataIntegrityViolationException e) {
87 throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e);
91 private Set<YangResourceEntity> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
92 final Map<String, YangResourceEntity> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
94 final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8));
95 final YangResourceEntity yangResourceEntity = new YangResourceEntity();
96 yangResourceEntity.setName(entry.getKey());
97 yangResourceEntity.setContent(entry.getValue());
98 yangResourceEntity.setChecksum(checksum);
99 return yangResourceEntity;
101 .collect(Collectors.toMap(
102 YangResourceEntity::getChecksum,
106 final List<YangResourceEntity> existingYangResourceEntities =
107 yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
108 existingYangResourceEntities.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
110 final Collection<YangResourceEntity> newYangResourceEntities = checksumToEntityMap.values();
111 if (!newYangResourceEntities.isEmpty()) {
112 yangResourceRepository.saveAll(newYangResourceEntities);
115 return ImmutableSet.<YangResourceEntity>builder()
116 .addAll(existingYangResourceEntities)
117 .addAll(newYangResourceEntities)
122 public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
123 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
124 final SchemaSetEntity schemaSetEntity =
125 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
126 return schemaSetEntity.getYangResources().stream().collect(
127 Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent));
131 public Map<String, String> getYangSchemaSetResources(final String dataspaceName, final String anchorName) {
132 final Anchor anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
133 return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
138 public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
139 final CascadeDeleteAllowed cascadeDeleteAllowed) {
140 final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
141 final SchemaSetEntity schemaSetEntity =
142 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
144 final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllBySchemaSet(schemaSetEntity);
145 if (!anchorEntities.isEmpty()) {
146 if (cascadeDeleteAllowed != CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) {
147 throw new SchemaSetInUseException(dataspaceName, schemaSetName);
149 fragmentRepository.deleteByAnchorIn(anchorEntities);
150 anchorRepository.deleteAll(anchorEntities);
152 schemaSetRepository.delete(schemaSetEntity);
153 yangResourceRepository.deleteOrphans();