Retrieve All anchors for a given Dataspace
[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  *  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  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.impl;
23
24 import java.lang.reflect.Type;
25 import java.util.Collection;
26 import org.modelmapper.ModelMapper;
27 import org.modelmapper.TypeToken;
28 import org.onap.cps.spi.CpsAdminPersistenceService;
29 import org.onap.cps.spi.entities.Dataspace;
30 import org.onap.cps.spi.entities.Fragment;
31 import org.onap.cps.spi.entities.Module;
32 import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException;
33 import org.onap.cps.spi.model.Anchor;
34 import org.onap.cps.spi.repository.DataspaceRepository;
35 import org.onap.cps.spi.repository.FragmentRepository;
36 import org.onap.cps.spi.repository.ModuleRepository;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.dao.DataIntegrityViolationException;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
43
44     @Autowired
45     private DataspaceRepository dataspaceRepository;
46
47     @Autowired
48     private FragmentRepository fragmentRepository;
49
50     @Autowired
51     private ModuleRepository moduleRepository;
52
53     @Override
54     public String createAnchor(final Anchor anchor) {
55         final String anchorName = anchor.getAnchorName();
56         try {
57             final Dataspace dataspace = dataspaceRepository.getByName(anchor.getDataspaceName());
58             final Module module = moduleRepository
59                 .getByDataspaceAndNamespaceAndRevision(dataspace, anchor.getNamespace(), anchor.getRevision());
60             final Fragment fragment =
61                 Fragment.builder().xpath(anchorName).anchorName(anchorName).dataspace(dataspace).module(module).build();
62
63             fragmentRepository.save(fragment);
64             return anchorName;
65         } catch (final DataIntegrityViolationException ex) {
66             throw new AnchorAlreadyDefinedException(anchor.getDataspaceName(), anchorName, ex);
67         }
68     }
69
70     @Override
71     public Collection<Anchor> getAnchors(final String dataspaceName) {
72         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
73         final Collection<Fragment> fragments = fragmentRepository.findFragmentsThatAreAnchorsByDataspace(dataspace);
74         final Type anchorListType = new TypeToken<Collection<Anchor>>() {}.getType();
75         return new ModelMapper().map(fragments, anchorListType);
76     }
77 }