8a149262552ddacd4c890c4e910a007080ae9bd4
[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.io.IOException;
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.Dataspace;
33 import org.onap.cps.spi.entities.SchemaSet;
34 import org.onap.cps.spi.entities.YangResource;
35 import org.onap.cps.spi.exceptions.CpsException;
36 import org.onap.cps.spi.exceptions.ModelValidationException;
37 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
38 import org.onap.cps.spi.model.ModuleReference;
39 import org.onap.cps.spi.repository.DataspaceRepository;
40 import org.onap.cps.spi.repository.SchemaSetRepository;
41 import org.onap.cps.spi.repository.YangResourceRepository;
42 import org.onap.cps.yang.YangTextSchemaSourceSet;
43 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
44 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.dao.DataIntegrityViolationException;
48 import org.springframework.stereotype.Component;
49 import org.springframework.util.DigestUtils;
50
51 @Component
52 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
53
54     @Autowired
55     private YangResourceRepository yangResourceRepository;
56
57     @Autowired
58     private SchemaSetRepository schemaSetRepository;
59
60     @Autowired
61     private DataspaceRepository dataspaceRepository;
62
63     @Override
64     public void storeModule(final String namespace, final String moduleContent, final String revision,
65                             final String dataspaceName) {
66         // TODO this method should be removed as obsolete.
67         // Modules to be processed within schema sets only.
68     }
69
70     @Override
71     @Transactional
72     public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
73                                final Map<String, String> yangResourcesNameToContentMap) {
74
75         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
76         final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesNameToContentMap);
77         final SchemaSet schemaSet = new SchemaSet();
78         schemaSet.setName(schemaSetName);
79         schemaSet.setDataspace(dataspace);
80         schemaSet.setYangResources(yangResources);
81         try {
82             schemaSetRepository.save(schemaSet);
83         } catch (final DataIntegrityViolationException e) {
84             throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e);
85         }
86     }
87
88     private Set<YangResource> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
89         final Map<String, YangResource> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
90             .map(entry -> {
91                 final YangResource yangResource = new YangResource();
92                 yangResource.setName(entry.getKey());
93                 yangResource.setContent(entry.getValue());
94                 yangResource.setChecksum(DigestUtils.md5DigestAsHex(entry.getValue().getBytes()));
95                 return yangResource;
96             })
97             .collect(Collectors.toMap(
98                 YangResource::getChecksum,
99                 entity -> entity
100             ));
101
102         final List<YangResource> existingYangResources =
103             yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet());
104         existingYangResources.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum()));
105
106         final Collection<YangResource> newYangResources = checksumToEntityMap.values();
107         if (!newYangResources.isEmpty()) {
108             yangResourceRepository.saveAll(newYangResources);
109         }
110
111         return ImmutableSet.<YangResource>builder()
112             .addAll(existingYangResources)
113             .addAll(newYangResources)
114             .build();
115     }
116
117     @Override
118     public Collection<ModuleReference> getModuleReferences(final String dataspaceName, final String schemaSetName) {
119         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
120         final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName);
121         final Map<String, String> yangResourceNameToContent = schemaSet.getYangResources().stream().collect(
122                 Collectors.toMap(YangResource::getName, YangResource::getContent));
123         try {
124             final YangTextSchemaSourceSet schemaSourceSet = YangTextSchemaSourceSetBuilder
125                 .of(yangResourceNameToContent);
126             return schemaSourceSet.getModuleReferences();
127         } catch (final ReactorException | YangSyntaxErrorException e) {
128             throw new ModelValidationException("Yang file validation failed", e.getMessage(), e);
129         }
130     }
131 }