Merge "CPS Exception handling - extending the CPSValidationException"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / ModelPersistenceServiceImpl.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 static org.onap.cps.exceptions.CpsExceptionBuilder.duplicateSchemaSetException;
24 import static org.onap.cps.exceptions.CpsExceptionBuilder.invalidDataspaceException;
25
26 import com.google.common.collect.ImmutableSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31 import javax.transaction.Transactional;
32 import org.onap.cps.spi.ModelPersistenceService;
33 import org.onap.cps.spi.entities.Dataspace;
34 import org.onap.cps.spi.entities.SchemaSet;
35 import org.onap.cps.spi.entities.YangResource;
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 ModelPersistenceServiceImpl implements ModelPersistenceService {
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     public void storeModule(final String namespace, final String moduleContent, final String revision,
58                             final String dataspaceName) {
59         // TODO this method should be removed as obsolete.
60         // Modules to be processed within schema sets only.
61     }
62
63     @Override
64     @Transactional
65     public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
66                                final Set<String> yangResourcesAsStrings) {
67
68         final Dataspace dataspace = dataspaceRepository.findByName(dataspaceName)
69             .orElseThrow(() -> invalidDataspaceException(dataspaceName));
70
71         final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesAsStrings);
72         final SchemaSet schemaSet = new SchemaSet();
73         schemaSet.setName(schemaSetName);
74         schemaSet.setDataspace(dataspace);
75         schemaSet.setYangResources(yangResources);
76         try {
77             schemaSetRepository.save(schemaSet);
78         } catch (final DataIntegrityViolationException e) {
79             throw duplicateSchemaSetException(dataspaceName, schemaSetName);
80         }
81     }
82
83     private Set<YangResource> synchronizeYangResources(final Set<String> yangResourcesAsStrings) {
84         final Map<String, String> checksumToContentMap = yangResourcesAsStrings.stream()
85             .collect(Collectors.toMap(
86                 content -> DigestUtils.md5DigestAsHex(content.getBytes()),
87                 content -> content)
88             );
89
90         final List<YangResource> existingYangResources =
91             yangResourceRepository.findAllByChecksumIn(checksumToContentMap.keySet());
92         existingYangResources.forEach(yangFile -> checksumToContentMap.remove(yangFile.getChecksum()));
93
94         final List<YangResource> newYangResources = checksumToContentMap.entrySet().stream()
95             .map(entry -> {
96                 final YangResource yangResource = new YangResource();
97                 yangResource.setChecksum(entry.getKey());
98                 yangResource.setContent(entry.getValue());
99                 return yangResource;
100             }).collect(Collectors.toList());
101         if (!newYangResources.isEmpty()) {
102             yangResourceRepository.saveAll(newYangResources);
103         }
104
105         return ImmutableSet.<YangResource>builder()
106             .addAll(existingYangResources)
107             .addAll(newYangResources)
108             .build();
109     }
110
111 }