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.util.Collection;
25 import java.util.List;
28 import java.util.stream.Collectors;
29 import javax.transaction.Transactional;
30 import org.onap.cps.spi.CpsModulePersistenceService;
31 import org.onap.cps.spi.entities.Dataspace;
32 import org.onap.cps.spi.entities.SchemaSet;
33 import org.onap.cps.spi.entities.YangResource;
34 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
35 import org.onap.cps.spi.repository.DataspaceRepository;
36 import org.onap.cps.spi.repository.SchemaSetRepository;
37 import org.onap.cps.spi.repository.YangResourceRepository;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.dao.DataIntegrityViolationException;
40 import org.springframework.stereotype.Component;
41 import org.springframework.util.DigestUtils;
44 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
47 private YangResourceRepository yangResourceRepository;
50 private SchemaSetRepository schemaSetRepository;
53 private DataspaceRepository dataspaceRepository;
57 public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
58 final Map<String, String> yangResourcesNameToContentMap) {
60 final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
61 final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesNameToContentMap);
62 final SchemaSet schemaSet = new SchemaSet();
63 schemaSet.setName(schemaSetName);
64 schemaSet.setDataspace(dataspace);
65 schemaSet.setYangResources(yangResources);
67 schemaSetRepository.save(schemaSet);
68 } catch (final DataIntegrityViolationException e) {
69 throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e);
73 private Set<YangResource> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
74 final Map<String, YangResource> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
76 final YangResource yangResource = new YangResource();
77 yangResource.setName(entry.getKey());
78 yangResource.setContent(entry.getValue());
79 yangResource.setChecksum(DigestUtils.md5DigestAsHex(entry.getValue().getBytes()));
82 .collect(Collectors.toMap(
83 YangResource::getChecksum,
87 final List<YangResource> existingYangResources =
88 yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
89 existingYangResources.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
91 final Collection<YangResource> newYangResources = checksumToEntityMap.values();
92 if (!newYangResources.isEmpty()) {
93 yangResourceRepository.saveAll(newYangResources);
96 return ImmutableSet.<YangResource>builder()
97 .addAll(existingYangResources)
98 .addAll(newYangResources)
103 public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
104 final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
105 final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName);
106 return schemaSet.getYangResources().stream().collect(
107 Collectors.toMap(YangResource::getName, YangResource::getContent));