summaryrefslogtreecommitdiffstats
path: root/cps-ri/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'cps-ri/src/main/java/org')
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java5
-rwxr-xr-x[-rw-r--r--]cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java15
-rwxr-xr-x[-rw-r--r--]cps-ri/src/main/java/org/onap/cps/spi/entities/Module.java (renamed from cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java)7
-rwxr-xr-x[-rw-r--r--]cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistenceServiceImpl.java (renamed from cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java)4
-rwxr-xr-xcps-ri/src/main/java/org/onap/cps/spi/impl/FragmentPersistenceServiceImpl.java72
-rwxr-xr-x[-rw-r--r--]cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java (renamed from cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java)31
-rwxr-xr-x[-rw-r--r--]cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java10
-rwxr-xr-xcps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java29
-rwxr-xr-x[-rw-r--r--]cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java30
9 files changed, 179 insertions, 24 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java
index 627a14467d..aeab4f844b 100644
--- a/cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java
@@ -19,6 +19,7 @@
package org.onap.cps.spi.entities;
+import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@@ -41,7 +42,9 @@ import lombok.Setter;
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "dataspace")
-public class Dataspace {
+public class Dataspace implements Serializable {
+
+ private static final long serialVersionUID = 8395254649813051882L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java
index 12422dc5f6..7afdb3e092 100644..100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java
@@ -21,6 +21,7 @@
package org.onap.cps.spi.entities;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
+import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@@ -32,6 +33,7 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
+import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@@ -47,8 +49,11 @@ import org.hibernate.annotations.TypeDefs;
@Entity
@AllArgsConstructor
@NoArgsConstructor
+@Builder
@TypeDefs({@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)})
-public class Fragment {
+public class Fragment implements Serializable {
+
+ private static final long serialVersionUID = 7737669789097119667L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -62,6 +67,10 @@ public class Fragment {
@Column(columnDefinition = "jsonb")
private String attributes;
+ @Column(columnDefinition = "text")
+ private String anchorName;
+
+ @NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dataspace_id")
private Dataspace dataspace;
@@ -73,4 +82,8 @@ public class Fragment {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Fragment parentFragment;
+
+ @OneToOne(fetch = FetchType.LAZY)
+ @JoinColumn(name = "module_id")
+ private Module module;
}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/Module.java
index d2130aeec1..ef6895c7d4 100644..100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/Module.java
@@ -20,6 +20,7 @@
package org.onap.cps.spi.entities;
+import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@@ -45,7 +46,9 @@ import lombok.Setter;
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "module")
-public class ModuleEntity {
+public class Module implements Serializable {
+
+ private static final long serialVersionUID = -748666970938314895L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -75,7 +78,7 @@ public class ModuleEntity {
* @param revision the revision number of the module.
* @param dataspace the dataspace related to the module.
*/
- public ModuleEntity(String namespace, String moduleContent, String revision, Dataspace dataspace) {
+ public Module(String namespace, String moduleContent, String revision, Dataspace dataspace) {
this.namespace = namespace;
this.moduleContent = moduleContent;
this.revision = revision;
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistenceServiceImpl.java
index 2b4f1357de..9ed111271a 100644..100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistenceServiceImpl.java
@@ -19,7 +19,7 @@
package org.onap.cps.spi.impl;
-import org.onap.cps.spi.DataPersistencyService;
+import org.onap.cps.spi.DataPersistenceService;
import org.onap.cps.spi.entities.JsonDataEntity;
import org.onap.cps.spi.repository.DataRepository;
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +27,7 @@ import org.springframework.stereotype.Component;
@Component
-public class DataPersistencyServiceImpl implements DataPersistencyService {
+public class DataPersistenceServiceImpl implements DataPersistenceService {
@Autowired
private DataRepository dataRepository;
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/FragmentPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/FragmentPersistenceServiceImpl.java
new file mode 100755
index 0000000000..0ecf72d772
--- /dev/null
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/FragmentPersistenceServiceImpl.java
@@ -0,0 +1,72 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.impl;
+
+import org.onap.cps.api.model.AnchorDetails;
+import org.onap.cps.exceptions.CpsNotFoundException;
+import org.onap.cps.exceptions.CpsValidationException;
+import org.onap.cps.spi.FragmentPersistenceService;
+import org.onap.cps.spi.entities.Dataspace;
+import org.onap.cps.spi.entities.Fragment;
+import org.onap.cps.spi.entities.Module;
+import org.onap.cps.spi.repository.DataspaceRepository;
+import org.onap.cps.spi.repository.FragmentRepository;
+import org.onap.cps.spi.repository.ModuleRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataIntegrityViolationException;
+import org.springframework.stereotype.Component;
+
+@Component
+public class FragmentPersistenceServiceImpl implements FragmentPersistenceService {
+
+ @Autowired
+ private DataspaceRepository dataspaceRepository;
+
+ @Autowired
+ private FragmentRepository fragmentRepository;
+
+ @Autowired
+ private ModuleRepository moduleRepository;
+
+ @Override
+ public String createAnchor(final AnchorDetails anchorDetails) {
+ try {
+ final Dataspace dataspace = dataspaceRepository.getByName(anchorDetails.getDataspace());
+ final Module module =
+ moduleRepository.getByDataspaceAndNamespaceAndRevision(dataspace,
+ anchorDetails.getNamespace(), anchorDetails.getRevision());
+
+ final Fragment fragment = Fragment.builder().xpath(anchorDetails.getAnchorName())
+ .anchorName(anchorDetails.getAnchorName())
+ .dataspace(dataspace).module(module).build();
+
+ fragmentRepository.save(fragment);
+ return anchorDetails.getAnchorName();
+ } catch (final CpsNotFoundException ex) {
+ throw new CpsValidationException("Validation Error",
+ String.format("Dataspace and/or Module do not exist."));
+ } catch (final DataIntegrityViolationException ex) {
+ throw new CpsValidationException("Duplication Error",
+ String.format("Anchor with name %s already exist in dataspace %s.",
+ anchorDetails.getAnchorName(), anchorDetails.getDataspace()));
+ }
+ }
+} \ No newline at end of file
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java
index 01c7a7b53b..03679b3160 100644..100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java
@@ -20,28 +20,24 @@
package org.onap.cps.spi.impl;
-import org.onap.cps.spi.ModelPersistencyService;
+import org.onap.cps.exceptions.CpsValidationException;
+import org.onap.cps.spi.ModelPersistenceService;
import org.onap.cps.spi.entities.Dataspace;
-import org.onap.cps.spi.entities.ModuleEntity;
+import org.onap.cps.spi.entities.Module;
import org.onap.cps.spi.repository.DataspaceRepository;
import org.onap.cps.spi.repository.ModuleRepository;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
@Component
-public class ModelPersistencyServiceImpl implements ModelPersistencyService {
+public class ModelPersistenceServiceImpl implements ModelPersistenceService {
-
- private final ModuleRepository moduleRepository;
-
- private final DataspaceRepository dataspaceRepository;
+ @Autowired
+ private ModuleRepository moduleRepository;
@Autowired
- public ModelPersistencyServiceImpl(final ModuleRepository moduleRepository,
- final DataspaceRepository dataspaceRepository) {
- this.moduleRepository = moduleRepository;
- this.dataspaceRepository = dataspaceRepository;
- }
+ private DataspaceRepository dataspaceRepository;
@Override
public void storeModule(final String namespace, final String moduleContent, final String revision,
@@ -50,8 +46,13 @@ public class ModelPersistencyServiceImpl implements ModelPersistencyService {
if (Boolean.FALSE.equals(dataspaceRepository.existsByName(dataspaceName))) {
dataspaceRepository.save(dataspace);
}
- dataspace.setId(dataspaceRepository.findByName(dataspaceName).getId());
- final ModuleEntity moduleEntity = new ModuleEntity(namespace, moduleContent, revision, dataspace);
- moduleRepository.save(moduleEntity);
+ dataspace.setId(dataspaceRepository.getByName(dataspaceName).getId());
+ final Module module = new Module(namespace, moduleContent, revision, dataspace);
+ try {
+ moduleRepository.save(module);
+ } catch (final DataIntegrityViolationException ex) {
+ throw new CpsValidationException("Duplicate Entry",
+ String.format("Module already exist in dataspace %s.", dataspaceName));
+ }
}
}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java
index 46a5266103..ad8004c07d 100644..100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java
@@ -20,6 +20,9 @@
package org.onap.cps.spi.repository;
+import java.util.Optional;
+import javax.validation.constraints.NotNull;
+import org.onap.cps.exceptions.CpsNotFoundException;
import org.onap.cps.spi.entities.Dataspace;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@@ -28,5 +31,10 @@ import org.springframework.stereotype.Repository;
public interface DataspaceRepository extends JpaRepository<Dataspace, Integer> {
Boolean existsByName(String name); //Checks if there are any records by name()
- Dataspace findByName(String name);
+ Optional<Dataspace> findByName(@NotNull String name);
+
+ default Dataspace getByName(@NotNull String name) {
+ return findByName(name).orElseThrow(
+ () -> new CpsNotFoundException("Not Found", "Dataspace " + name + " does not exist."));
+ }
} \ No newline at end of file
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
new file mode 100755
index 0000000000..ba83f15881
--- /dev/null
+++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
@@ -0,0 +1,29 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.repository;
+
+import org.onap.cps.spi.entities.Fragment;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface FragmentRepository extends JpaRepository<Fragment, Integer> {
+} \ No newline at end of file
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java
index f9078d7c14..d3a8bc1f09 100644..100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java
@@ -20,10 +20,36 @@
package org.onap.cps.spi.repository;
-import org.onap.cps.spi.entities.ModuleEntity;
+import java.util.Optional;
+import javax.validation.constraints.NotNull;
+import org.onap.cps.exceptions.CpsNotFoundException;
+import org.onap.cps.spi.entities.Dataspace;
+import org.onap.cps.spi.entities.Module;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
-public interface ModuleRepository extends JpaRepository<ModuleEntity, Integer> {
+public interface ModuleRepository extends JpaRepository<Module, Integer> {
+
+ Optional<Module> findByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace,
+ @NotNull String namespace,
+ @NotNull String revision);
+
+ /**
+ * This method gets a Module by dataspace, namespace and revision.
+ *
+ * @param dataspace the dataspace
+ * @param namespace the namespace
+ * @param revision the revision
+ * @return the Module
+ * @throws CpsNotFoundException if Module not found
+ */
+ default Module getByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace, @NotNull String namespace,
+ @NotNull String revision) {
+ return findByDataspaceAndNamespaceAndRevision(dataspace, namespace,
+ revision)
+ .orElseThrow(() -> new CpsNotFoundException("Validation Error", String.format(
+ "Module with dataspace %s, revision %s does not exist in namespace %s.",
+ dataspace.getName(), revision, namespace)));
+ }
} \ No newline at end of file