diff options
author | 2020-11-24 11:13:43 +0200 | |
---|---|---|
committer | 2020-12-03 15:59:03 +0200 | |
commit | cb03544962c994c0cc9d9117a3c6b9182ce22204 (patch) | |
tree | 7fdec9f545c81c55e93376a7d58e6a648d994550 /cps-ri/src/main/java/org/onap | |
parent | 35b2b4414405c0c3f7c06171ae0e1ebca3214e40 (diff) |
Schema Set persistence (store) implementation
Issue-ID: CPS-92
Change-Id: I11257726e4f847e3337d8bafad824853e6a458e2
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-ri/src/main/java/org/onap')
5 files changed, 272 insertions, 15 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/SchemaSet.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/SchemaSet.java new file mode 100644 index 000000000..fe67a6089 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/SchemaSet.java @@ -0,0 +1,71 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Pantheon.tech + * ================================================================================ + * 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.entities; + +import java.io.Serializable; +import java.util.Set; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +/** + * Entity to store a Schema Set. + */ +@Getter +@Setter +@NoArgsConstructor +@Entity +@Table(name = "schema_set") +public class SchemaSet implements Serializable { + + private static final long serialVersionUID = 6665056955069047269L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @NotNull + @Column + private String name; + + @NotNull + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "dataspace_id", referencedColumnName = "ID") + private Dataspace dataspace; + + @NotNull + @ManyToMany(fetch = FetchType.LAZY) + @JoinTable(name = "schema_set_yang_resources", + joinColumns = @JoinColumn(name = "schema_set_id"), + inverseJoinColumns = @JoinColumn(name = "yang_resource_id")) + private Set<YangResource> yangResources; +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResource.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResource.java new file mode 100644 index 000000000..862b7aea6 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResource.java @@ -0,0 +1,63 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Pantheon.tech + * ================================================================================ + * 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.entities; + +import java.io.Serializable; +import java.util.Set; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.Table; +import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +/** + * Entity to store a Yang files. + */ +@Getter +@Setter +@NoArgsConstructor +@Entity +@Table(name = "yang_resource") +public class YangResource implements Serializable { + + private static final long serialVersionUID = -4496883162142106774L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotNull + @Column + private String checksum; + + @NotNull + @Column + private String content; + + @ManyToMany(mappedBy = "yangResources") + private Set<SchemaSet> moduleSets; + +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java index 03679b316..2207f7940 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java @@ -20,39 +20,92 @@ package org.onap.cps.spi.impl; -import org.onap.cps.exceptions.CpsValidationException; +import static org.onap.cps.exceptions.CpsExceptionBuilder.duplicateSchemaSetException; +import static org.onap.cps.exceptions.CpsExceptionBuilder.invalidDataspaceException; + +import com.google.common.collect.ImmutableSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import javax.transaction.Transactional; import org.onap.cps.spi.ModelPersistenceService; import org.onap.cps.spi.entities.Dataspace; -import org.onap.cps.spi.entities.Module; +import org.onap.cps.spi.entities.SchemaSet; +import org.onap.cps.spi.entities.YangResource; import org.onap.cps.spi.repository.DataspaceRepository; -import org.onap.cps.spi.repository.ModuleRepository; +import org.onap.cps.spi.repository.SchemaSetRepository; +import org.onap.cps.spi.repository.YangResourceRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Component; +import org.springframework.util.DigestUtils; @Component public class ModelPersistenceServiceImpl implements ModelPersistenceService { @Autowired - private ModuleRepository moduleRepository; + private YangResourceRepository yangResourceRepository; + + @Autowired + private SchemaSetRepository schemaSetRepository; @Autowired private DataspaceRepository dataspaceRepository; @Override public void storeModule(final String namespace, final String moduleContent, final String revision, - final String dataspaceName) { - final Dataspace dataspace = new Dataspace(dataspaceName); - if (Boolean.FALSE.equals(dataspaceRepository.existsByName(dataspaceName))) { - dataspaceRepository.save(dataspace); - } - dataspace.setId(dataspaceRepository.getByName(dataspaceName).getId()); - final Module module = new Module(namespace, moduleContent, revision, dataspace); + final String dataspaceName) { + // TODO this method should be removed as obsolete. + // Modules to be processed within schema sets only. + } + + @Override + @Transactional + public void storeSchemaSet(final String dataspaceName, final String schemaSetName, + final Set<String> yangResourcesAsStrings) { + + final Dataspace dataspace = dataspaceRepository.findByName(dataspaceName) + .orElseThrow(() -> invalidDataspaceException(dataspaceName)); + + final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesAsStrings); + final SchemaSet schemaSet = new SchemaSet(); + schemaSet.setName(schemaSetName); + schemaSet.setDataspace(dataspace); + schemaSet.setYangResources(yangResources); try { - moduleRepository.save(module); - } catch (final DataIntegrityViolationException ex) { - throw new CpsValidationException("Duplicate Entry", - String.format("Module already exist in dataspace %s.", dataspaceName)); + schemaSetRepository.save(schemaSet); + } catch (final DataIntegrityViolationException e) { + throw duplicateSchemaSetException(dataspaceName, schemaSetName); + } + } + + private Set<YangResource> synchronizeYangResources(final Set<String> yangResourcesAsStrings) { + final Map<String, String> checksumToContentMap = yangResourcesAsStrings.stream() + .collect(Collectors.toMap( + content -> DigestUtils.md5DigestAsHex(content.getBytes()), + content -> content) + ); + + final List<YangResource> existingYangResources = + yangResourceRepository.findAllByChecksumIn(checksumToContentMap.keySet()); + existingYangResources.forEach(yangFile -> checksumToContentMap.remove(yangFile.getChecksum())); + + final List<YangResource> newYangResources = checksumToContentMap.entrySet().stream() + .map(entry -> { + final YangResource yangResource = new YangResource(); + yangResource.setChecksum(entry.getKey()); + yangResource.setContent(entry.getValue()); + return yangResource; + }).collect(Collectors.toList()); + if (!newYangResources.isEmpty()) { + yangResourceRepository.saveAll(newYangResources); } + + return ImmutableSet.<YangResource>builder() + .addAll(existingYangResources) + .addAll(newYangResources) + .build(); } + } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java new file mode 100644 index 000000000..f9746972f --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Pantheon.tech + * ================================================================================ + * 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 java.util.List; +import java.util.Optional; +import javax.validation.constraints.NotNull; +import org.onap.cps.spi.entities.Dataspace; +import org.onap.cps.spi.entities.SchemaSet; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface SchemaSetRepository extends JpaRepository<SchemaSet, Integer> { + + List<SchemaSet> findAllByDataspace(@NotNull Dataspace dataspace); + + Optional<SchemaSet> findByDataspaceAndName(@NotNull Dataspace dataspace, @NotNull String name); +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java new file mode 100644 index 000000000..47d3ea32c --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java @@ -0,0 +1,34 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Pantheon.tech + * ================================================================================ + * 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 java.util.List; +import java.util.Set; +import javax.validation.constraints.NotNull; +import org.onap.cps.spi.entities.YangResource; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface YangResourceRepository extends JpaRepository<YangResource, Long> { + + List<YangResource> findAllByChecksumIn(@NotNull Set<String> checksum); + +} |