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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.impl;
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;
42 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
45 private DataspaceRepository dataspaceRepository;
48 private FragmentRepository fragmentRepository;
51 private ModuleRepository moduleRepository;
54 public String createAnchor(final Anchor anchor) {
55 final String anchorName = anchor.getAnchorName();
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();
63 fragmentRepository.save(fragment);
65 } catch (final DataIntegrityViolationException ex) {
66 throw new AnchorAlreadyDefinedException(anchor.getDataspaceName(), anchorName, ex);
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);