Create Common Model objects in SPI
[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.exceptions.CpsNotFoundException;
24 import org.onap.cps.exceptions.CpsValidationException;
25 import org.onap.cps.spi.CpsAdminPersistenceService;
26 import org.onap.cps.spi.entities.Dataspace;
27 import org.onap.cps.spi.entities.Fragment;
28 import org.onap.cps.spi.entities.Module;
29 import org.onap.cps.spi.model.Anchor;
30 import org.onap.cps.spi.repository.DataspaceRepository;
31 import org.onap.cps.spi.repository.FragmentRepository;
32 import org.onap.cps.spi.repository.ModuleRepository;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.dao.DataIntegrityViolationException;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
39
40     @Autowired
41     private DataspaceRepository dataspaceRepository;
42
43     @Autowired
44     private FragmentRepository fragmentRepository;
45
46     @Autowired
47     private ModuleRepository moduleRepository;
48
49     @Override
50     public String createAnchor(final Anchor anchor) {
51         final String anchorName = anchor.getAnchorName();
52         try {
53             final Dataspace dataspace = dataspaceRepository.getByName(anchor.getDataspaceName());
54             final Module module =
55                 moduleRepository.getByDataspaceAndNamespaceAndRevision(dataspace,
56                     anchor.getNamespace(), anchor.getRevision());
57
58             final Fragment fragment = Fragment.builder().xpath(anchorName)
59                 .anchorName(anchorName)
60                 .dataspace(dataspace).module(module).build();
61
62             fragmentRepository.save(fragment);
63             return anchorName;
64         } catch (final CpsNotFoundException ex) {
65             throw new CpsValidationException("Validation Error",
66                 "Dataspace and/or Module do not exist.");
67         } catch (final DataIntegrityViolationException ex) {
68             throw new CpsValidationException("Duplication Error",
69                 String.format("Anchor with name %s already exist in dataspace %s.",
70                     anchorName, anchor.getDataspaceName()));
71         }
72     }
73 }