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.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;
38 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
41 private DataspaceRepository dataspaceRepository;
44 private FragmentRepository fragmentRepository;
47 private ModuleRepository moduleRepository;
50 public String createAnchor(final Anchor anchor) {
51 final String anchorName = anchor.getAnchorName();
53 final Dataspace dataspace = dataspaceRepository.getByName(anchor.getDataspaceName());
55 moduleRepository.getByDataspaceAndNamespaceAndRevision(dataspace,
56 anchor.getNamespace(), anchor.getRevision());
58 final Fragment fragment = Fragment.builder().xpath(anchorName)
59 .anchorName(anchorName)
60 .dataspace(dataspace).module(module).build();
62 fragmentRepository.save(fragment);
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()));