Rename entity artifacts, remove obsolete entities
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsModulePersistenceServiceImpl.java
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.spi.impl;
22
23 import com.google.common.collect.ImmutableSet;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30 import javax.transaction.Transactional;
31 import org.onap.cps.spi.CpsModulePersistenceService;
32 import org.onap.cps.spi.entities.DataspaceEntity;
33 import org.onap.cps.spi.entities.SchemaSetEntity;
34 import org.onap.cps.spi.entities.YangResourceEntity;
35 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
36 import org.onap.cps.spi.repository.DataspaceRepository;
37 import org.onap.cps.spi.repository.SchemaSetRepository;
38 import org.onap.cps.spi.repository.YangResourceRepository;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.dao.DataIntegrityViolationException;
41 import org.springframework.stereotype.Component;
42 import org.springframework.util.DigestUtils;
43
44 @Component
45 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
46
47     @Autowired
48     private YangResourceRepository yangResourceRepository;
49
50     @Autowired
51     private SchemaSetRepository schemaSetRepository;
52
53     @Autowired
54     private DataspaceRepository dataspaceRepository;
55
56     @Override
57     @Transactional
58     public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
59         final Map<String, String> yangResourcesNameToContentMap) {
60
61         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
62         final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap);
63         final SchemaSetEntity schemaSetEntity = new SchemaSetEntity();
64         schemaSetEntity.setName(schemaSetName);
65         schemaSetEntity.setDataspace(dataspaceEntity);
66         schemaSetEntity.setYangResources(yangResourceEntities);
67         try {
68             schemaSetRepository.save(schemaSetEntity);
69         } catch (final DataIntegrityViolationException e) {
70             throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e);
71         }
72     }
73
74     private Set<YangResourceEntity> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
75         final Map<String, YangResourceEntity> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
76             .map(entry -> {
77                 final String checksum = DigestUtils.md5DigestAsHex(entry.getValue().getBytes(StandardCharsets.UTF_8));
78                 final YangResourceEntity yangResourceEntity = new YangResourceEntity();
79                 yangResourceEntity.setName(entry.getKey());
80                 yangResourceEntity.setContent(entry.getValue());
81                 yangResourceEntity.setChecksum(checksum);
82                 return yangResourceEntity;
83             })
84             .collect(Collectors.toMap(
85                 YangResourceEntity::getChecksum,
86                 entity -> entity
87             ));
88
89         final List<YangResourceEntity> existingYangResourceEntities =
90             yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
91         existingYangResourceEntities.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
92
93         final Collection<YangResourceEntity> newYangResourceEntities = checksumToEntityMap.values();
94         if (!newYangResourceEntities.isEmpty()) {
95             yangResourceRepository.saveAll(newYangResourceEntities);
96         }
97
98         return ImmutableSet.<YangResourceEntity>builder()
99             .addAll(existingYangResourceEntities)
100             .addAll(newYangResourceEntities)
101             .build();
102     }
103
104     @Override
105     public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
106         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
107         final SchemaSetEntity schemaSetEntity =
108             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
109         return schemaSetEntity.getYangResources().stream().collect(
110             Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent));
111     }
112 }