82554b3d0a2ff3dcb8fd87306623511f30e0f352
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / FragmentPersistenceServiceImpl.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
4  * ================================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  *\r
17  * SPDX-License-Identifier: Apache-2.0\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.cps.spi.impl;\r
22 \r
23 import org.onap.cps.api.model.AnchorDetails;\r
24 import org.onap.cps.exceptions.CpsNotFoundException;\r
25 import org.onap.cps.exceptions.CpsValidationException;\r
26 import org.onap.cps.spi.FragmentPersistenceService;\r
27 import org.onap.cps.spi.entities.Dataspace;\r
28 import org.onap.cps.spi.entities.Fragment;\r
29 import org.onap.cps.spi.entities.Module;\r
30 import org.onap.cps.spi.repository.DataspaceRepository;\r
31 import org.onap.cps.spi.repository.FragmentRepository;\r
32 import org.onap.cps.spi.repository.ModuleRepository;\r
33 import org.springframework.beans.factory.annotation.Autowired;\r
34 import org.springframework.dao.DataIntegrityViolationException;\r
35 import org.springframework.stereotype.Component;\r
36 \r
37 @Component\r
38 public class FragmentPersistenceServiceImpl implements FragmentPersistenceService {\r
39 \r
40     @Autowired\r
41     private DataspaceRepository dataspaceRepository;\r
42 \r
43     @Autowired\r
44     private FragmentRepository fragmentRepository;\r
45 \r
46     @Autowired\r
47     private ModuleRepository moduleRepository;\r
48 \r
49     @Override\r
50     public String createAnchor(final AnchorDetails anchorDetails) {\r
51         try {\r
52             final Dataspace dataspace = dataspaceRepository.getByName(anchorDetails.getDataspace());\r
53             final Module module =\r
54                 moduleRepository.getByDataspaceAndNamespaceAndRevision(dataspace,\r
55                 anchorDetails.getNamespace(), anchorDetails.getRevision());\r
56 \r
57             final Fragment fragment = Fragment.builder().xpath(anchorDetails.getAnchorName())\r
58                 .anchorName(anchorDetails.getAnchorName())\r
59                 .dataspace(dataspace).module(module).build();\r
60 \r
61             fragmentRepository.save(fragment);\r
62             return anchorDetails.getAnchorName();\r
63         } catch (final CpsNotFoundException ex) {\r
64             throw new CpsValidationException("Validation Error",\r
65                 "Dataspace and/or Module do not exist.");\r
66         } catch (final DataIntegrityViolationException ex) {\r
67             throw new CpsValidationException("Duplication Error",\r
68                 String.format("Anchor with name %s already exist in dataspace %s.",\r
69                     anchorDetails.getAnchorName(), anchorDetails.getDataspace()));\r
70         }\r
71     }\r
72 }\r