Create schema set REST API and service level
[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.Collection;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
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.model.ModuleReference;
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.onap.cps.yang.YangTextSchemaSourceSetBuilder;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.dao.DataIntegrityViolationException;
42 import org.springframework.stereotype.Component;
43 import org.springframework.util.DigestUtils;
44
45 @Component
46 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
47
48     @Autowired
49     private YangResourceRepository yangResourceRepository;
50
51     @Autowired
52     private SchemaSetRepository schemaSetRepository;
53
54     @Autowired
55     private DataspaceRepository dataspaceRepository;
56
57     @Override
58     public void storeModule(final String namespace, final String moduleContent, final String revision,
59         final String dataspaceName) {
60         // TODO this method should be removed as obsolete.
61         // Modules to be processed within schema sets only.
62     }
63
64     @Override
65     @Transactional
66     public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
67         final Map<String, String> yangResourcesNameToContentMap) {
68
69         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
70         final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesNameToContentMap);
71         final SchemaSet schemaSet = new SchemaSet();
72         schemaSet.setName(schemaSetName);
73         schemaSet.setDataspace(dataspace);
74         schemaSet.setYangResources(yangResources);
75         try {
76             schemaSetRepository.save(schemaSet);
77         } catch (final DataIntegrityViolationException e) {
78             throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e);
79         }
80     }
81
82     private Set<YangResource> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
83         final Map<String, YangResource> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
84             .map(entry -> {
85                 final YangResource yangResource = new YangResource();
86                 yangResource.setName(entry.getKey());
87                 yangResource.setContent(entry.getValue());
88                 yangResource.setChecksum(DigestUtils.md5DigestAsHex(entry.getValue().getBytes()));
89                 return yangResource;
90             })
91             .collect(Collectors.toMap(
92                 YangResource::getChecksum,
93                 entity -> entity
94             ));
95
96         final List<YangResource> existingYangResources =
97             yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
98         existingYangResources.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
99
100         final Collection<YangResource> newYangResources = checksumToEntityMap.values();
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     @Override
112     public Collection<ModuleReference> getModuleReferences(final String dataspaceName, final String schemaSetName) {
113         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
114         final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName);
115         final Map<String, String> yangResourceNameToContent = schemaSet.getYangResources().stream().collect(
116             Collectors.toMap(YangResource::getName, YangResource::getContent));
117         return YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getModuleReferences();
118     }
119 }