summaryrefslogtreecommitdiffstats
path: root/cps-service
diff options
context:
space:
mode:
Diffstat (limited to 'cps-service')
-rwxr-xr-x[-rw-r--r--]cps-service/src/main/java/org/onap/cps/api/CpService.java26
-rwxr-xr-x[-rw-r--r--]cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java39
-rwxr-xr-xcps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java42
-rw-r--r--cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java3
-rw-r--r--cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java3
-rwxr-xr-x[-rw-r--r--]cps-service/src/main/java/org/onap/cps/spi/DataPersistenceService.java (renamed from cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java)2
-rwxr-xr-xcps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java34
-rwxr-xr-x[-rw-r--r--]cps-service/src/main/java/org/onap/cps/spi/ModelPersistenceService.java (renamed from cps-service/src/main/java/org/onap/cps/spi/ModelPersistencyService.java)2
-rwxr-xr-x[-rw-r--r--]cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy72
9 files changed, 187 insertions, 36 deletions
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
index 4d94a46547..726ca0f28d 100644..100755
--- 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,15 +44,16 @@ 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.
*
* @param schemaContext the schema context
* @param dataspaceName the dataspace name
+ * @throws CpsValidationException if input data already exists.
*/
- void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName);
+ void storeSchemaContext(SchemaContext schemaContext, String dataspaceName);
/**
* Store the JSON structure in the database.
@@ -60,7 +61,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 +69,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
index c33746e861..93ed8b8468 100644..100755
--- 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,10 +26,12 @@ 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.ModelPersistencyService;
+import org.onap.cps.spi.DataPersistenceService;
+import org.onap.cps.spi.FragmentPersistenceService;
+import org.onap.cps.spi.ModelPersistenceService;
import org.onap.cps.utils.YangUtils;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.Module;
@@ -38,15 +40,17 @@ import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
-
@Component
public class CpServiceImpl implements CpService {
@Autowired
- private ModelPersistencyService modelPersistencyService;
+ private ModelPersistenceService modelPersistenceService;
+
+ @Autowired
+ private DataPersistenceService dataPersistenceService;
@Autowired
- private DataPersistencyService dataPersistencyService;
+ private FragmentPersistenceService fragmentPersistenceService;
@Override
public final SchemaContext parseAndValidateModel(final String yangModelContent) {
@@ -57,7 +61,7 @@ public class CpServiceImpl implements CpService {
writer.write(yangModelContent);
}
return parseAndValidateModel(tempFile);
- } catch (IOException e) {
+ } catch (final IOException e) {
throw new CpsException(e);
}
}
@@ -66,35 +70,40 @@ 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);
}
}
@Override
public final Integer storeJsonStructure(final String jsonStructure) {
- return dataPersistencyService.storeJsonStructure(jsonStructure);
+ return dataPersistenceService.storeJsonStructure(jsonStructure);
}
@Override
public final String getJsonById(final int jsonObjectId) {
- return dataPersistencyService.getJsonById(jsonObjectId);
+ return dataPersistenceService.getJsonById(jsonObjectId);
}
@Override
public void deleteJsonById(int jsonObjectId) {
- dataPersistencyService.deleteJsonById(jsonObjectId);
+ dataPersistenceService.deleteJsonById(jsonObjectId);
}
@Override
public final void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName) {
for (final Module module : schemaContext.getModules()) {
- Optional<Revision> optionalRevision = module.getRevision();
- String revisionValue = optionalRevision.isPresent() ? optionalRevision.get().toString() : null;
- modelPersistencyService.storeModule(module.getNamespace().toString(), module.toString(),
+ final Optional<Revision> optionalRevision = module.getRevision();
+ final String revisionValue = optionalRevision.map(Object::toString).orElse(null);
+ modelPersistenceService.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/DataPersistencyService.java b/cps-service/src/main/java/org/onap/cps/spi/DataPersistenceService.java
index ce51f40ac6..a3cbc28c55 100644..100755
--- a/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java
+++ b/cps-service/src/main/java/org/onap/cps/spi/DataPersistenceService.java
@@ -22,7 +22,7 @@ package org.onap.cps.spi;
/**
* Defines methods to access and manipulate data using the chosen database solution.
*/
-public interface DataPersistencyService {
+public interface DataPersistenceService {
/**
* Store the JSON structure in the database.
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/main/java/org/onap/cps/spi/ModelPersistencyService.java b/cps-service/src/main/java/org/onap/cps/spi/ModelPersistenceService.java
index 2afdaa82a8..3f0b3c1109 100644..100755
--- a/cps-service/src/main/java/org/onap/cps/spi/ModelPersistencyService.java
+++ b/cps-service/src/main/java/org/onap/cps/spi/ModelPersistenceService.java
@@ -23,7 +23,7 @@ package org.onap.cps.spi;
/**
* Defines methods to access and manipulate data using the chosen database solution.
*/
-public interface ModelPersistencyService {
+public interface ModelPersistenceService {
/**
* Store the module from a yang model in the database.
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
index 5f42810bd5..709378ebee 100644..100755
--- 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,24 +21,28 @@
package org.onap.cps.api.impl
import org.onap.cps.TestUtils
+import org.onap.cps.api.model.AnchorDetails
import org.onap.cps.exceptions.CpsValidationException
-import org.onap.cps.spi.DataPersistencyService
+import org.onap.cps.spi.DataPersistenceService
+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
class CpServiceImplSpec extends Specification {
- def mockDataPersistencyService = Mock(DataPersistencyService)
+ def mockDataPersistenceService = Mock(DataPersistenceService)
+ def mockFragmentPersistenceService = Mock(FragmentPersistenceService)
def objectUnderTest = new CpServiceImpl()
def setup() {
- objectUnderTest.dataPersistencyService = mockDataPersistencyService
+ objectUnderTest.dataPersistenceService = mockDataPersistenceService
+ objectUnderTest.fragmentPersistenceService = mockFragmentPersistenceService
}
def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() {
- given: 'that data persistency service is giving id 123 to a data structure it is asked to store'
- mockDataPersistencyService.storeJsonStructure(_) >> 123
+ given: 'that data persistence service is giving id 123 to a data structure it is asked to store'
+ mockDataPersistenceService.storeJsonStructure(_) >> 123
expect: 'Cps service returns the same id when storing data structure'
objectUnderTest.storeJsonStructure('') == 123
}
@@ -83,7 +87,7 @@ class CpServiceImplSpec extends Specification {
def 'Read a JSON object with a valid identifier'(){
given: 'that the data persistence service returns a JSON structure for identifier 1'
- mockDataPersistencyService.getJsonById(1) >> '{name : hello}'
+ mockDataPersistenceService.getJsonById(1) >> '{name : hello}'
expect: 'that the same JSON structure is returned by CPS'
objectUnderTest.getJsonById(1) == '{name : hello}'
}
@@ -91,7 +95,7 @@ class CpServiceImplSpec extends Specification {
def 'Read a JSON object with an identifier that does not exist'(){
given: 'that the data persistence service throws an exception'
def exceptionThrownByPersistenceService = new IllegalStateException()
- mockDataPersistencyService.getJsonById(_) >> {throw exceptionThrownByPersistenceService}
+ mockDataPersistenceService.getJsonById(_) >> {throw exceptionThrownByPersistenceService}
when: 'we try to get the JSON structure'
objectUnderTest.getJsonById(1);
then: 'the same exception is thrown by CPS'
@@ -100,17 +104,67 @@ class CpServiceImplSpec extends Specification {
def 'Delete a JSON object with a valid identifier'(){
given: 'that the data persistence service can delete a JSON structure for identifier 1'
- mockDataPersistencyService.deleteJsonById(1)
+ mockDataPersistenceService.deleteJsonById(1)
expect: 'No exception is thrown when we delete a JSON structure with identifier 1'
objectUnderTest.deleteJsonById(1)
}
def 'Delete a JSON object with an identifier that does not exist'(){
given: 'that the data persistence service throws an exception'
- mockDataPersistencyService.deleteJsonById(_) >> {throw new IllegalStateException()}
+ mockDataPersistenceService.deleteJsonById(_) >> {throw new IllegalStateException()}
when: 'we try to delete a JSON structure'
objectUnderTest.deleteJsonById(100);
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