Improve CPS Exception handling
[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.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import javax.transaction.Transactional;
29 import org.onap.cps.spi.CpsModulePersistenceService;
30 import org.onap.cps.spi.entities.Dataspace;
31 import org.onap.cps.spi.entities.SchemaSet;
32 import org.onap.cps.spi.entities.YangResource;
33 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
34 import org.onap.cps.spi.repository.DataspaceRepository;
35 import org.onap.cps.spi.repository.SchemaSetRepository;
36 import org.onap.cps.spi.repository.YangResourceRepository;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.dao.DataIntegrityViolationException;
39 import org.springframework.stereotype.Component;
40 import org.springframework.util.DigestUtils;
41
42 @Component
43 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
44
45     @Autowired
46     private YangResourceRepository yangResourceRepository;
47
48     @Autowired
49     private SchemaSetRepository schemaSetRepository;
50
51     @Autowired
52     private DataspaceRepository dataspaceRepository;
53
54     @Override
55     public void storeModule(final String namespace, final String moduleContent, final String revision,
56                             final String dataspaceName) {
57         // TODO this method should be removed as obsolete.
58         // Modules to be processed within schema sets only.
59     }
60
61     @Override
62     @Transactional
63     public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
64                                final Set<String> yangResourcesAsStrings) {
65
66         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
67         final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesAsStrings);
68         final SchemaSet schemaSet = new SchemaSet();
69         schemaSet.setName(schemaSetName);
70         schemaSet.setDataspace(dataspace);
71         schemaSet.setYangResources(yangResources);
72         try {
73             schemaSetRepository.save(schemaSet);
74         } catch (final DataIntegrityViolationException e) {
75             throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e);
76         }
77     }
78
79     private Set<YangResource> synchronizeYangResources(final Set<String> yangResourcesAsStrings) {
80         final Map<String, String> checksumToContentMap = yangResourcesAsStrings.stream()
81             .collect(Collectors.toMap(
82                 content -> DigestUtils.md5DigestAsHex(content.getBytes()),
83                 content -> content)
84             );
85
86         final List<YangResource> existingYangResources =
87             yangResourceRepository.findAllByChecksumIn(checksumToContentMap.keySet());
88         existingYangResources.forEach(yangFile -> checksumToContentMap.remove(yangFile.getChecksum()));
89
90         final List<YangResource> newYangResources = checksumToContentMap.entrySet().stream()
91             .map(entry -> {
92                 final YangResource yangResource = new YangResource();
93                 yangResource.setChecksum(entry.getKey());
94                 yangResource.setContent(entry.getValue());
95                 return yangResource;
96             }).collect(Collectors.toList());
97         if (!newYangResources.isEmpty()) {
98             yangResourceRepository.saveAll(newYangResources);
99         }
100
101         return ImmutableSet.<YangResource>builder()
102             .addAll(existingYangResources)
103             .addAll(newYangResources)
104             .build();
105     }
106
107 }