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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.impl;
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;
37 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
40 private DataspaceRepository dataspaceRepository;
43 private FragmentRepository fragmentRepository;
46 private ModuleRepository moduleRepository;
49 public String createAnchor(final Anchor anchor) {
50 final String anchorName = anchor.getAnchorName();
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();
58 fragmentRepository.save(fragment);
60 } catch (final DataIntegrityViolationException ex) {
61 throw new AnchorAlreadyDefinedException(anchor.getDataspaceName(), anchorName, ex);