From 48830f146f6776afa180fefa101788b169bc841a Mon Sep 17 00:00:00 2001 From: "Rishi.Chail" Date: Mon, 9 Nov 2020 03:28:44 +0000 Subject: VSE: Create an anchor in the given dataspace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue-ID: CPS-42 https://jira.onap.org/browse/CPS-42 Signed-off-by: Rishi Chail Change-Id: If67be6f13889808da4d9fe830595766af67e4fdf --- .../src/main/java/org/onap/cps/api/CpService.java | 25 ++++++---- .../java/org/onap/cps/api/impl/CpServiceImpl.java | 22 ++++++--- .../java/org/onap/cps/api/model/AnchorDetails.java | 42 +++++++++++++++++ .../java/org/onap/cps/exceptions/CpsException.java | 3 +- .../onap/cps/exceptions/CpsNotFoundException.java | 3 +- .../onap/cps/spi/FragmentPersistenceService.java | 34 +++++++++++++ .../org/onap/cps/api/impl/CpServiceImplSpec.groovy | 55 ++++++++++++++++++++++ 7 files changed, 168 insertions(+), 16 deletions(-) mode change 100644 => 100755 cps-service/src/main/java/org/onap/cps/api/CpService.java mode change 100644 => 100755 cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java create mode 100755 cps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java create mode 100755 cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java mode change 100644 => 100755 cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy (limited to 'cps-service/src') diff --git a/cps-service/src/main/java/org/onap/cps/api/CpService.java b/cps-service/src/main/java/org/onap/cps/api/CpService.java old mode 100644 new mode 100755 index 4d94a46547..6b59949ca0 --- a/cps-service/src/main/java/org/onap/cps/api/CpService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpService.java @@ -21,9 +21,9 @@ package org.onap.cps.api; import java.io.File; -import java.io.IOException; +import org.onap.cps.api.model.AnchorDetails; +import org.onap.cps.exceptions.CpsValidationException; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.parser.api.YangParserException; /** * Configuration and persistency service interface which holds methods for parsing and storing yang models and data. @@ -36,7 +36,7 @@ public interface CpService { * @param yangModelContent the input stream * @return the schema context */ - SchemaContext parseAndValidateModel(final String yangModelContent); + SchemaContext parseAndValidateModel(String yangModelContent); /** * Parse and validate a file representing a yang model to generate a schema context. @@ -44,7 +44,7 @@ public interface CpService { * @param yangModelFile the yang file * @return the schema context */ - SchemaContext parseAndValidateModel(final File yangModelFile); + SchemaContext parseAndValidateModel(File yangModelFile); /** * Store schema context for a yang model. @@ -52,7 +52,7 @@ public interface CpService { * @param schemaContext the schema context * @param dataspaceName the dataspace name */ - void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName); + void storeSchemaContext(SchemaContext schemaContext, String dataspaceName); /** * Store the JSON structure in the database. @@ -60,7 +60,7 @@ public interface CpService { * @param jsonStructure the JSON structure. * @return entity ID. */ - Integer storeJsonStructure(final String jsonStructure); + Integer storeJsonStructure(String jsonStructure); /** * Read a JSON Object using the object identifier. @@ -68,12 +68,21 @@ public interface CpService { * @param jsonObjectId the JSON object identifier. * @return the JSON structure. */ - String getJsonById(final int jsonObjectId); + String getJsonById(int jsonObjectId); /** * Delete a JSON Object using the object identifier. * * @param jsonObjectId the JSON object identifier. */ - void deleteJsonById(final int jsonObjectId); + void deleteJsonById(int jsonObjectId); + + /** + * Create an anchor using provided anchorDetails object. + * + * @param anchorDetails the anchor details object. + * @return the anchor name. + * @throws CpsValidationException if input data is invalid. + */ + String createAnchor(AnchorDetails anchorDetails); } diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java old mode 100644 new mode 100755 index c33746e861..8cdadbeb55 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java @@ -26,9 +26,11 @@ import java.io.FileWriter; import java.io.IOException; import java.util.Optional; import org.onap.cps.api.CpService; +import org.onap.cps.api.model.AnchorDetails; import org.onap.cps.exceptions.CpsException; import org.onap.cps.exceptions.CpsValidationException; import org.onap.cps.spi.DataPersistencyService; +import org.onap.cps.spi.FragmentPersistenceService; import org.onap.cps.spi.ModelPersistencyService; import org.onap.cps.utils.YangUtils; import org.opendaylight.yangtools.yang.common.Revision; @@ -48,6 +50,9 @@ public class CpServiceImpl implements CpService { @Autowired private DataPersistencyService dataPersistencyService; + @Autowired + private FragmentPersistenceService fragmentPersistenceService; + @Override public final SchemaContext parseAndValidateModel(final String yangModelContent) { @@ -57,7 +62,7 @@ public class CpServiceImpl implements CpService { writer.write(yangModelContent); } return parseAndValidateModel(tempFile); - } catch (IOException e) { + } catch (final IOException e) { throw new CpsException(e); } } @@ -66,9 +71,9 @@ public class CpServiceImpl implements CpService { public final SchemaContext parseAndValidateModel(final File yangModelFile) { try { return YangUtils.parseYangModelFile(yangModelFile); - } catch (YangParserException e) { + } catch (final YangParserException e) { throw new CpsValidationException("Yang file validation failed", e.getMessage()); - } catch (IOException e) { + } catch (final IOException e) { throw new CpsException(e); } } @@ -91,10 +96,15 @@ public class CpServiceImpl implements CpService { @Override public final void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName) { for (final Module module : schemaContext.getModules()) { - Optional optionalRevision = module.getRevision(); - String revisionValue = optionalRevision.isPresent() ? optionalRevision.get().toString() : null; + final Optional optionalRevision = module.getRevision(); + final String revisionValue = optionalRevision.map(Object::toString).orElse(null); modelPersistencyService.storeModule(module.getNamespace().toString(), module.toString(), revisionValue, dataspaceName); } } -} + + @Override + public String createAnchor(AnchorDetails anchorDetails) { + return fragmentPersistenceService.createAnchor(anchorDetails); + } +} \ No newline at end of file diff --git a/cps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java b/cps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java new file mode 100755 index 0000000000..576168ae75 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java @@ -0,0 +1,42 @@ +/*- + * ============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.api.model; + +import java.io.Serializable; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class AnchorDetails implements Serializable { + + private static final long serialVersionUID = 1464791260718603291L; + + private String anchorName; + + private String dataspace; + + private String namespace; + + private String revision; +} \ No newline at end of file diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java b/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java index b54453cd90..4dd19dd82b 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java +++ b/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java @@ -20,13 +20,14 @@ package org.onap.cps.exceptions; import lombok.Getter; -import org.springframework.core.NestedExceptionUtils; /** * CP Service exception. */ public class CpsException extends RuntimeException { + private static final long serialVersionUID = 5573438585188332404L; + @Getter String details; diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java b/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java index f44fe806cf..4613da8f23 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java +++ b/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java @@ -19,13 +19,14 @@ package org.onap.cps.exceptions; -import lombok.Getter; /** * CP Service exception. Indicates the requested data being absent. */ public class CpsNotFoundException extends CpsException { + private static final long serialVersionUID = -1852996415384288431L; + /** * Constructor. * diff --git a/cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java new file mode 100755 index 0000000000..48dbb0cc25 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java @@ -0,0 +1,34 @@ +/*- + * ============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; + +import org.onap.cps.api.model.AnchorDetails; + +public interface FragmentPersistenceService { + + /** + * Create an Anchor. + * + * @param anchorDetails the anchorDetails object. + * @return the anchor name. + */ + String createAnchor(AnchorDetails anchorDetails); +} diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy old mode 100644 new mode 100755 index 5f42810bd5..3c51cca597 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy @@ -21,8 +21,11 @@ package org.onap.cps.api.impl import org.onap.cps.TestUtils +import org.onap.cps.api.model.AnchorDetails +import org.onap.cps.exceptions.CpsNotFoundException import org.onap.cps.exceptions.CpsValidationException import org.onap.cps.spi.DataPersistencyService +import org.onap.cps.spi.FragmentPersistenceService import org.opendaylight.yangtools.yang.common.Revision import org.opendaylight.yangtools.yang.model.api.SchemaContext import spock.lang.Specification @@ -30,10 +33,12 @@ import spock.lang.Specification class CpServiceImplSpec extends Specification { def mockDataPersistencyService = Mock(DataPersistencyService) + def mockFragmentPersistenceService = Mock(FragmentPersistenceService) def objectUnderTest = new CpServiceImpl() def setup() { objectUnderTest.dataPersistencyService = mockDataPersistencyService + objectUnderTest.fragmentPersistenceService = mockFragmentPersistenceService } def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() { @@ -113,4 +118,54 @@ class CpServiceImplSpec extends Specification { then: 'the same exception is thrown by CPS' thrown(IllegalStateException) } + + def 'Create an anchor with a non-existant dataspace'(){ + given: 'that the dataspace does not exist service throws an exception' + AnchorDetails anchorDetails = new AnchorDetails() + anchorDetails.setDataspace('dummyDataspace') + mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)} + when: 'we try to create a anchor with a non-existant dataspace' + objectUnderTest.createAnchor(anchorDetails) + then: 'the same exception is thrown by CPS' + thrown(CpsValidationException) + } + + def 'Create an anchor with invalid dataspace, namespace and revision'(){ + given: 'that the dataspace, namespace and revison combination does not exist service throws an exception' + AnchorDetails anchorDetails = new AnchorDetails() + anchorDetails.setDataspace('dummyDataspace') + anchorDetails.setNamespace('dummyNamespace') + anchorDetails.setRevision('dummyRevision') + mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)} + when: 'we try to create a anchor with a non-existant dataspace, namespace and revison combination' + objectUnderTest.createAnchor(anchorDetails) + then: 'the same exception is thrown by CPS' + thrown(CpsValidationException) + } + + def 'Create a duplicate anchor'(){ + given: 'that the anchor already exist service throws an exception' + AnchorDetails anchorDetails = new AnchorDetails() + anchorDetails.setDataspace('dummyDataspace') + anchorDetails.setNamespace('dummyNamespace') + anchorDetails.setRevision('dummyRevision') + anchorDetails.setRevision('dummyAnchorName') + mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)} + when: 'we try to create a duplicate anchor' + objectUnderTest.createAnchor(anchorDetails) + then: 'the same exception is thrown by CPS' + thrown(CpsValidationException) + } + + def 'Create an anchor with supplied anchor name, dataspace, namespace and revision'(){ + given: 'that the anchor does not pre-exist service creates an anchor' + AnchorDetails anchorDetails = new AnchorDetails() + anchorDetails.setDataspace('dummyDataspace') + anchorDetails.setNamespace('dummyNamespace') + anchorDetails.setRevision('dummyRevision') + anchorDetails.setRevision('dummyAnchorName') + mockFragmentPersistenceService.createAnchor(anchorDetails) >> 'dummyAnchorName' + expect: 'anchor name is returned by service' + objectUnderTest.createAnchor(anchorDetails) == 'dummyAnchorName' + } } \ No newline at end of file -- cgit 1.2.3-korg