Fixing Merge Issue
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsAdminPersistenceServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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 org.onap.cps.spi.CpsAdminPersistenceService;
24 import org.onap.cps.spi.entities.Dataspace;
25 import org.onap.cps.spi.entities.Fragment;
26 import org.onap.cps.spi.entities.Module;
27 import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException;
28 import org.onap.cps.spi.model.Anchor;
29 import org.onap.cps.spi.repository.DataspaceRepository;
30 import org.onap.cps.spi.repository.FragmentRepository;
31 import org.onap.cps.spi.repository.ModuleRepository;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.dao.DataIntegrityViolationException;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
38
39     @Autowired
40     private DataspaceRepository dataspaceRepository;
41
42     @Autowired
43     private FragmentRepository fragmentRepository;
44
45     @Autowired
46     private ModuleRepository moduleRepository;
47
48     @Override
49     public String createAnchor(final Anchor anchor) {
50         final String anchorName = anchor.getAnchorName();
51         try {
52             final Dataspace dataspace = dataspaceRepository.getByName(anchor.getDataspaceName());
53             final Module module = moduleRepository
54                 .getByDataspaceAndNamespaceAndRevision(dataspace, anchor.getNamespace(), anchor.getRevision());
55             final Fragment fragment =
56                 Fragment.builder().xpath(anchorName).anchorName(anchorName).dataspace(dataspace).module(module).build();
57
58             fragmentRepository.save(fragment);
59             return anchorName;
60         } catch (final DataIntegrityViolationException ex) {
61             throw new AnchorAlreadyDefinedException(anchor.getDataspaceName(), anchorName, ex);
62         }
63     }
64 }