* Copyright (C) 2021 highstreet technologies GmbH
* Modifications Copyright (C) 2021 Nordix Foundation
* Modifications Copyright (C) 2021 Pantheon.tech
+ * Modifications Copyright (C) 2021 Bell Canada
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
+import java.time.OffsetDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
+ private static final OffsetDateTime NO_TIMESTAMP = null;
+
private CpsDataService cpsDataService;
private ObjectMapper objectMapper;
@Override
public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) {
if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) {
- cpsDataService.saveData(getDataspaceName(), cmHandle, jsonData);
+ cpsDataService.saveData(getDataspaceName(), cmHandle, jsonData, NO_TIMESTAMP);
} else {
- cpsDataService.saveData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
+ cpsDataService.saveData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
}
}
@Override
public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) {
- cpsDataService.saveListNodeData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
+ cpsDataService.saveListNodeData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
}
@Override
public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) {
- cpsDataService.updateNodeLeaves(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
+ cpsDataService.updateNodeLeaves(getDataspaceName(), cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
}
@Override
public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) {
- cpsDataService.replaceNodeTree(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
+ cpsDataService.replaceNodeTree(getDataspaceName(), cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
}
@Override
persistenceCmHandlesList.setCmHandles(createdPersistenceCmHandles);
final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry",
- cmHandleJsonData);
+ cmHandleJsonData, NO_TIMESTAMP);
} catch (final JsonProcessingException e) {
log.error("Parsing error occurred while converting Object to JSON for Dmi Registry.");
throw new DataValidationException(
persistenceCmHandlesList.setCmHandles(updatedPersistenceCmHandles);
final String cmHandlesJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
cpsDataService.updateNodeLeavesAndExistingDescendantLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
- "/dmi-registry", cmHandlesJsonData);
+ "/dmi-registry", cmHandlesJsonData, NO_TIMESTAMP);
} catch (final JsonProcessingException e) {
log.error("Parsing error occurred while converting Object to JSON Dmi Registry.");
throw new DataValidationException(
for (final String cmHandle: dmiPluginRegistration.getRemovedCmHandles()) {
try {
cpsDataService.deleteListNodeData(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
- "/dmi-registry/cm-handles[@id='" + cmHandle + "']");
+ "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
} catch (final DataNodeNotFoundException e) {
log.warn("Datanode {} not deleted message {}", cmHandle, e.getMessage());
}
* ============LICENSE_START=======================================================
* Copyright (C) 2021 Nordix Foundation
* Modifications Copyright (C) 2021 Pantheon.tech
+ * Modifications Copyright (C) 2021 Bell Canada
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockDmiOperations, mockCpsDataService, mockCpsQueryService, new ObjectMapper())
def cmHandle = 'some handle'
+ def noTimestamp = null
def expectedDataspaceName = 'NFP-Operational'
def 'Query data nodes by cps path with #fetchDescendantsOption.'() {
when: 'createDataNode is invoked'
objectUnderTest.createDataNode(cmHandle, xpath, jsonData)
then: 'the CPS service method is invoked once with the expected parameters'
- 1 * mockCpsDataService.saveData(expectedDataspaceName, cmHandle, jsonData)
+ 1 * mockCpsDataService.saveData(expectedDataspaceName, cmHandle, jsonData, noTimestamp)
where: 'following parameters were used'
scenario | xpath
'no xpath' | ''
when: 'createDataNode is invoked'
objectUnderTest.createDataNode(cmHandle, xpath, jsonData)
then: 'the CPS service method is invoked once with the expected parameters'
- 1 * mockCpsDataService.saveData(expectedDataspaceName, cmHandle, xpath, jsonData)
+ 1 * mockCpsDataService.saveData(expectedDataspaceName, cmHandle, xpath, jsonData, noTimestamp)
}
def 'Add list-node elements.'() {
given: 'a cm handle and parent node xpath'
when: 'addListNodeElements is invoked'
objectUnderTest.addListNodeElements(cmHandle, xpath, jsonData)
then: 'the CPS service method is invoked once with the expected parameters'
- 1 * mockCpsDataService.saveListNodeData(expectedDataspaceName, cmHandle, xpath, jsonData)
+ 1 * mockCpsDataService.saveListNodeData(expectedDataspaceName, cmHandle, xpath, jsonData, noTimestamp)
}
def 'Update data node leaves.'() {
given: 'a cm Handle and a cps path'
when: 'updateNodeLeaves is invoked'
objectUnderTest.updateNodeLeaves(cmHandle, xpath, jsonData)
then: 'the persistence service is called once with the correct parameters'
- 1 * mockCpsDataService.updateNodeLeaves(expectedDataspaceName, cmHandle, xpath, jsonData)
+ 1 * mockCpsDataService.updateNodeLeaves(expectedDataspaceName, cmHandle, xpath, jsonData, noTimestamp)
}
def 'Replace data node tree.'() {
given: 'a cm Handle and a cps path'
when: 'replaceNodeTree is invoked'
objectUnderTest.replaceNodeTree(cmHandle, xpath, jsonData)
then: 'the persistence service is called once with the correct parameters'
- 1 * mockCpsDataService.replaceNodeTree(expectedDataspaceName, cmHandle, xpath, jsonData)
+ 1 * mockCpsDataService.replaceNodeTree(expectedDataspaceName, cmHandle, xpath, jsonData, noTimestamp)
}
def 'Register or re-register a DMI Plugin with #scenario cm handles.'() {
when: 'registration is updated'
objectUnderTest.updateDmiPluginRegistration(dmiPluginRegistration)
then: 'the CPS save list node data is invoked with the expected parameters'
- expectedCallsToSaveNode * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData)
+ expectedCallsToSaveNode * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry',
+ '/dmi-registry', expectedJsonData, noTimestamp)
and: 'update Node and Child Data Nodes is invoked with correct parameters'
- expectedCallsToUpdateNode * mockCpsDataService.updateNodeLeavesAndExistingDescendantLeaves('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData)
+ expectedCallsToUpdateNode * mockCpsDataService.updateNodeLeavesAndExistingDescendantLeaves('NCMP-Admin',
+ 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData, noTimestamp)
and : 'delete list data node is invoked with the correct parameters'
- expectedCallsToDeleteListDataNode * mockCpsDataService.deleteListNodeData('NCMP-Admin', 'ncmp-dmi-registry', "/dmi-registry/cm-handles[@id='cmHandle001']")
+ expectedCallsToDeleteListDataNode * mockCpsDataService.deleteListNodeData('NCMP-Admin',
+ 'ncmp-dmi-registry', "/dmi-registry/cm-handles[@id='cmHandle001']", noTimestamp)
+
where:
scenario | createdCmHandles | updatedCmHandles | removedCmHandles || expectedCallsToSaveNode | expectedCallsToUpdateNode | expectedCallsToDeleteListDataNode
'create' | [persistenceCmHandle ] | [] | [] || 1 | 0 | 0
when: 'registration is updated'
objectUnderTest.updateDmiPluginRegistration(dmiPluginRegistration)
then: 'the CPS save list node data is invoked with the expected parameters'
- 1 * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData)
+ 1 * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry',
+ '/dmi-registry', expectedJsonData, noTimestamp)
}
def 'Get resource data for pass-through operational from dmi.'() {
'testFieldQuery',
5,
'testAcceptParam',
- '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}') >> new ResponseEntity<>('result-json', HttpStatus.OK)
+ '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}') >>
+ new ResponseEntity<>('result-json', HttpStatus.OK)
and: 'dmi returns ok response'
response == 'result-json'
}
/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2020 Bell Canada.
+ * Copyright (C) 2020-2021 Bell Canada.
* Modifications Copyright (C) 2021 Pantheon.tech
* Modifications Copyright (C) 2021 Nordix Foundation
* ================================================================================
* 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.
package org.onap.cps.rest.controller;
-import javax.validation.Valid;
-import javax.validation.constraints.NotNull;
+import java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+import javax.validation.ValidationException;
+import org.apache.commons.lang3.StringUtils;
import org.onap.cps.api.CpsDataService;
import org.onap.cps.rest.api.CpsDataApi;
import org.onap.cps.spi.FetchDescendantsOption;
public class DataRestController implements CpsDataApi {
private static final String ROOT_XPATH = "/";
+ private static final String ISO_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
+ private static final DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_FORMAT);
@Autowired
private CpsDataService cpsDataService;
@Override
public ResponseEntity<String> createNode(final String dataspaceName, final String anchorName,
- final String jsonData, final String parentNodeXpath) {
+ final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
if (isRootXpath(parentNodeXpath)) {
- cpsDataService.saveData(dataspaceName, anchorName, jsonData);
+ cpsDataService.saveData(dataspaceName, anchorName, jsonData, toOffsetDateTime(observedTimestamp));
} else {
- cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData);
+ cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData,
+ toOffsetDateTime(observedTimestamp));
}
return new ResponseEntity<>(HttpStatus.CREATED);
}
@Override
public ResponseEntity<String> addListNodeElements(final String parentNodeXpath,
- final String dataspaceName, final String anchorName, final String jsonData) {
- cpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData);
+ final String dataspaceName, final String anchorName, final String jsonData, final String observedTimestamp) {
+ cpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData,
+ toOffsetDateTime(observedTimestamp));
return new ResponseEntity<>(HttpStatus.CREATED);
}
@Override
public ResponseEntity<Object> updateNodeLeaves(final String dataspaceName,
- final String anchorName, final String jsonData, final String parentNodeXpath) {
- cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData);
+ final String anchorName, final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
+ cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData,
+ toOffsetDateTime(observedTimestamp));
return new ResponseEntity<>(HttpStatus.OK);
}
@Override
- public ResponseEntity<Object> replaceNode(final String dataspaceName,
- final String anchorName, @Valid final String jsonData, @Valid final String parentNodeXpath) {
- cpsDataService.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData);
+ public ResponseEntity<Object> replaceNode(final String dataspaceName, final String anchorName,
+ final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
+ cpsDataService
+ .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData, toOffsetDateTime(observedTimestamp));
return new ResponseEntity<>(HttpStatus.OK);
}
@Override
- public ResponseEntity<String> replaceListNodeElements(@NotNull @Valid final String parentNodeXpath,
- final String dataspaceName, final String anchorName, @Valid final String jsonData) {
- cpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData);
+ public ResponseEntity<String> replaceListNodeElements(final String parentNodeXpath,
+ final String dataspaceName, final String anchorName, final String jsonData,
+ final String observedTimestamp) {
+ cpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData,
+ toOffsetDateTime(observedTimestamp));
return new ResponseEntity<>(HttpStatus.OK);
}
@Override
public ResponseEntity<Void> deleteListNodeElements(final String dataspaceName, final String anchorName,
- final String listNodeXpath) {
- cpsDataService.deleteListNodeData(dataspaceName, anchorName, listNodeXpath);
+ final String listNodeXpath, final String observedTimestamp) {
+ cpsDataService
+ .deleteListNodeData(dataspaceName, anchorName, listNodeXpath, toOffsetDateTime(observedTimestamp));
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
private static boolean isRootXpath(final String xpath) {
return ROOT_XPATH.equals(xpath);
}
+
+ private OffsetDateTime toOffsetDateTime(final String datetTimestamp) {
+ try {
+ return StringUtils.isEmpty(datetTimestamp)
+ ? null : OffsetDateTime.parse(datetTimestamp, ISO_TIMESTAMP_FORMATTER);
+ } catch (final Exception exception) {
+ throw new ValidationException(
+ String.format("observed-timestamp must be in '%s' format", ISO_TIMESTAMP_FORMAT));
+ }
+ }
}
package org.onap.cps.rest.exceptions;
import javax.servlet.http.HttpServletRequest;
+import javax.validation.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.onap.cps.rest.controller.AdminRestController;
import org.onap.cps.rest.controller.DataRestController;
return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
}
+ @ExceptionHandler({ValidationException.class})
+ public static ResponseEntity<Object> handleBadRequestExceptions(final ValidationException validationException) {
+ return buildErrorResponse(HttpStatus.BAD_REQUEST, validationException);
+ }
+
@ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception,
final HttpServletRequest request) {
schema:
type: boolean
default: false
+ observedTimestampInQuery:
+ name: observed-timestamp
+ in: query
+ description: observed-timestamp
+ required: false
+ schema:
+ type: string
+ example: '2021-03-21T00:10:34.030-0100'
responses:
NotFound:
- $ref: 'components.yml#/components/parameters/dataspaceNameInPath'
- $ref: 'components.yml#/components/parameters/anchorNameInPath'
- $ref: 'components.yml#/components/parameters/requiredXpathInQuery'
+ - $ref: 'components.yml#/components/parameters/observedTimestampInQuery'
requestBody:
required: true
content:
- $ref: 'components.yml#/components/parameters/dataspaceNameInPath'
- $ref: 'components.yml#/components/parameters/anchorNameInPath'
- $ref: 'components.yml#/components/parameters/requiredXpathInQuery'
+ - $ref: 'components.yml#/components/parameters/observedTimestampInQuery'
requestBody:
required: true
content:
- $ref: 'components.yml#/components/parameters/dataspaceNameInPath'
- $ref: 'components.yml#/components/parameters/anchorNameInPath'
- $ref: 'components.yml#/components/parameters/requiredXpathInQuery'
+ - $ref: 'components.yml#/components/parameters/observedTimestampInQuery'
responses:
'204':
$ref: 'components.yml#/components/responses/NoContent'
- $ref: 'components.yml#/components/parameters/dataspaceNameInPath'
- $ref: 'components.yml#/components/parameters/anchorNameInPath'
- $ref: 'components.yml#/components/parameters/xpathInQuery'
+ - $ref: 'components.yml#/components/parameters/observedTimestampInQuery'
requestBody:
required: true
content:
- $ref: 'components.yml#/components/parameters/dataspaceNameInPath'
- $ref: 'components.yml#/components/parameters/anchorNameInPath'
- $ref: 'components.yml#/components/parameters/xpathInQuery'
+ - $ref: 'components.yml#/components/parameters/observedTimestampInQuery'
requestBody:
required: true
content:
- $ref: 'components.yml#/components/parameters/dataspaceNameInPath'
- $ref: 'components.yml#/components/parameters/anchorNameInPath'
- $ref: 'components.yml#/components/parameters/xpathInQuery'
+ - $ref: 'components.yml#/components/parameters/observedTimestampInQuery'
requestBody:
required: true
content:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
-import org.modelmapper.ModelMapper
-import org.onap.cps.api.CpsAdminService
import org.onap.cps.api.CpsDataService
-import org.onap.cps.api.CpsModuleService
-import org.onap.cps.api.CpsQueryService
import org.onap.cps.spi.model.DataNode
import org.onap.cps.spi.model.DataNodeBuilder
+import org.onap.cps.utils.DateTimeUtility
import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
def dataNodeBaseEndpoint
def dataspaceName = 'my_dataspace'
def anchorName = 'my_anchor'
+ def noTimestamp = null
@Shared
static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
- .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
+ .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
@Shared
static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
- .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
+ .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
def setup() {
dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
def json = 'some json (this is not validated)'
when: 'post is invoked with datanode endpoint and json'
def response =
- mvc.perform(
- post(endpoint)
- .contentType(MediaType.APPLICATION_JSON)
- .param('xpath', parentNodeXpath)
- .content(json)
- ).andReturn().response
+ mvc.perform(
+ post(endpoint)
+ .contentType(MediaType.APPLICATION_JSON)
+ .param('xpath', parentNodeXpath)
+ .content(json)
+ ).andReturn().response
then: 'a created response is returned'
response.status == HttpStatus.CREATED.value()
then: 'the java API was called with the correct parameters'
- 1 * mockCpsDataService.saveData(dataspaceName, anchorName, json)
+ 1 * mockCpsDataService.saveData(dataspaceName, anchorName, json, noTimestamp)
where: 'following xpath parameters are are used'
scenario | parentNodeXpath
'no xpath parameter' | ''
'xpath parameter point root' | '/'
}
+ def 'Create a node with observed-timestamp'() {
+ given: 'some json to create a data node'
+ def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
+ def json = 'some json (this is not validated)'
+ when: 'post is invoked with datanode endpoint and json'
+ def response =
+ mvc.perform(
+ post(endpoint)
+ .contentType(MediaType.APPLICATION_JSON)
+ .param('xpath', '')
+ .param('observed-timestamp', observedTimestamp)
+ .content(json)
+ ).andReturn().response
+ then: 'a created response is returned'
+ response.status == expectedHttpStatus.value()
+ then: 'the java API was called with the correct parameters'
+ expectedApiCount * mockCpsDataService.saveData(dataspaceName, anchorName, json,
+ { it == DateTimeUtility.toOffsetDateTime(observedTimestamp) })
+ where:
+ scenario | observedTimestamp || expectedApiCount | expectedHttpStatus
+ 'with observed-timestamp' | '2021-03-03T23:59:59.999-0400' || 1 | HttpStatus.CREATED
+ 'with invalid observed-timestamp' | 'invalid' || 0 | HttpStatus.BAD_REQUEST
+ }
+
def 'Create a child node'() {
given: 'some json to create a data node'
def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
and: 'parent node xpath'
def parentNodeXpath = 'some xpath'
when: 'post is invoked with datanode endpoint and json'
+ def postRequestBuilder = post(endpoint)
+ .contentType(MediaType.APPLICATION_JSON)
+ .param('xpath', parentNodeXpath)
+ .content(json)
+ if (observedTimestamp != null)
+ postRequestBuilder.param('observed-timestamp', observedTimestamp)
def response =
- mvc.perform(
- post(endpoint)
- .contentType(MediaType.APPLICATION_JSON)
- .param('xpath', parentNodeXpath)
- .content(json)
- ).andReturn().response
+ mvc.perform(postRequestBuilder).andReturn().response
then: 'a created response is returned'
response.status == HttpStatus.CREATED.value()
then: 'the java API was called with the correct parameters'
- 1 * mockCpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, json)
+ 1 * mockCpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, json,
+ DateTimeUtility.toOffsetDateTime(observedTimestamp))
+ where:
+ scenario | observedTimestamp
+ 'with observed-timestamp' | '2021-03-03T23:59:59.999-0400'
+ 'without observed-timestamp' | null
}
- def 'Create list node child elements.'() {
+ def 'Create list node child elements #scenario.'() {
given: 'parent node xpath and json data inputs'
def parentNodeXpath = 'parent node xpath'
def jsonData = 'json data'
when: 'post is invoked list-node endpoint'
- def response = mvc.perform(
- post("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
- .contentType(MediaType.APPLICATION_JSON)
- .param('xpath', parentNodeXpath)
- .content(jsonData)
- ).andReturn().response
+ def postRequestBuilder = post("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
+ .contentType(MediaType.APPLICATION_JSON)
+ .param('xpath', parentNodeXpath)
+ .content(jsonData)
+ if (observedTimestamp != null)
+ postRequestBuilder.param('observed-timestamp', observedTimestamp)
+ def response = mvc.perform(postRequestBuilder).andReturn().response
then: 'a created response is returned'
- response.status == HttpStatus.CREATED.value()
+ response.status == expectedHttpStatus.value()
then: 'the java API was called with the correct parameters'
- 1 * mockCpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData)
+ expectedApiCount * mockCpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData,
+ { it == DateTimeUtility.toOffsetDateTime(observedTimestamp) })
+ where:
+ scenario | observedTimestamp || expectedApiCount | expectedHttpStatus
+ 'with observed-timestamp' | '2021-03-03T23:59:59.999-0400' || 1 | HttpStatus.CREATED
+ 'without observed-timestamp' | null || 1 | HttpStatus.CREATED
+ 'with invalid observed-timestamp' | 'invalid' || 0 | HttpStatus.BAD_REQUEST
}
def 'Get data node with leaves'() {
mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS) >> dataNodeWithLeavesNoChildren
when: 'get request is performed through REST API'
def response =
- mvc.perform(get(endpoint).param('xpath', xpath))
- .andReturn().response
+ mvc.perform(get(endpoint).param('xpath', xpath))
+ .andReturn().response
then: 'a success response is returned'
response.status == HttpStatus.OK.value()
and: 'response contains expected leaf and value'
mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, expectedCpsDataServiceOption) >> dataNode
when: 'get request is performed through REST API'
def response =
- mvc.perform(
- get(endpoint)
- .param('xpath', xpath)
- .param('include-descendants', includeDescendantsOption))
- .andReturn().response
+ mvc.perform(
+ get(endpoint)
+ .param('xpath', xpath)
+ .param('include-descendants', includeDescendantsOption))
+ .andReturn().response
then: 'a success response is returned'
response.status == HttpStatus.OK.value()
and: 'the response contains child is #expectChildInResponse'
def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
when: 'patch request is performed'
def response =
- mvc.perform(
- patch(endpoint)
- .contentType(MediaType.APPLICATION_JSON)
- .content(jsonData)
- .param('xpath', inputXpath)
- ).andReturn().response
+ mvc.perform(
+ patch(endpoint)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(jsonData)
+ .param('xpath', inputXpath)
+ ).andReturn().response
then: 'the service method is invoked with expected parameters'
- 1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData)
+ 1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData, null)
and: 'response status indicates success'
response.status == HttpStatus.OK.value()
where:
'some xpath by parent' | '/some/xpath' || '/some/xpath'
}
+ def 'Update data node leaves with observedTimestamp'() {
+ given: 'json data'
+ def jsonData = 'json data'
+ def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
+ when: 'patch request is performed'
+ def response =
+ mvc.perform(
+ patch(endpoint)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(jsonData)
+ .param('xpath', '/')
+ .param('observed-timestamp', observedTimestamp)
+ ).andReturn().response
+ then: 'the service method is invoked with expected parameters'
+ expectedApiCount * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, '/', jsonData,
+ { it == DateTimeUtility.toOffsetDateTime(observedTimestamp) })
+ and: 'response status indicates success'
+ response.status == expectedHttpStatus.value()
+ where:
+ scenario | observedTimestamp || expectedApiCount | expectedHttpStatus
+ 'with observed-timestamp' | '2021-03-03T23:59:59.999-0400' || 1 | HttpStatus.OK
+ 'with invalid observed-timestamp' | 'invalid' || 0 | HttpStatus.BAD_REQUEST
+ }
+
def 'Replace data node tree: #scenario.'() {
given: 'json data'
def jsonData = 'json data'
def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
when: 'put request is performed'
def response =
- mvc.perform(
- put(endpoint)
- .contentType(MediaType.APPLICATION_JSON)
- .content(jsonData)
- .param('xpath', inputXpath))
- .andReturn().response
+ mvc.perform(
+ put(endpoint)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(jsonData)
+ .param('xpath', inputXpath))
+ .andReturn().response
then: 'the service method is invoked with expected parameters'
- 1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData)
+ 1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData, noTimestamp)
and: 'response status indicates success'
response.status == HttpStatus.OK.value()
where:
'some xpath by parent' | '/some/xpath' || '/some/xpath'
}
+ def 'Replace data node tree with observedTimestamp.'() {
+ given: 'json data'
+ def jsonData = 'json data'
+ def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
+ when: 'put request is performed'
+ def response =
+ mvc.perform(
+ put(endpoint)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(jsonData)
+ .param('xpath', '')
+ .param('observed-timestamp', observedTimestamp))
+ .andReturn().response
+ then: 'the service method is invoked with expected parameters'
+ expectedApiCount * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, '/', jsonData,
+ { it == DateTimeUtility.toOffsetDateTime(observedTimestamp) })
+ and: 'response status indicates success'
+ response.status == expectedHttpStatus.value()
+ where:
+ scenario | observedTimestamp || expectedApiCount | expectedHttpStatus
+ 'with observed-timestamp' | '2021-03-03T23:59:59.999-0400' || 1 | HttpStatus.OK
+ 'with invalid observed-timestamp' | 'invalid' || 0 | HttpStatus.BAD_REQUEST
+ }
+
def 'Replace list node child elements.'() {
given: 'parent node xpath and json data inputs'
def parentNodeXpath = 'parent node xpath'
def jsonData = 'json data'
when: 'patch is invoked list-node endpoint'
- def response = mvc.perform(
- patch("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
- .contentType(MediaType.APPLICATION_JSON)
- .param('xpath', parentNodeXpath)
- .content(jsonData)
- ).andReturn().response
+ def patchRequestBuilder = patch("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
+ .contentType(MediaType.APPLICATION_JSON)
+ .param('xpath', parentNodeXpath)
+ .content(jsonData)
+ if (observedTimestamp != null)
+ patchRequestBuilder.param('observed-timestamp', observedTimestamp)
+ def response = mvc.perform(patchRequestBuilder).andReturn().response
then: 'a success response is returned'
- response.status == HttpStatus.OK.value()
- then: 'the java API was called with the correct parameters'
- 1 * mockCpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData)
+ response.status == expectedHttpStatus.value()
+ and: 'the java API was called with the correct parameters'
+ expectedApiCount * mockCpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData,
+ { it == DateTimeUtility.toOffsetDateTime(observedTimestamp) })
+ where:
+ scenario | observedTimestamp || expectedApiCount | expectedHttpStatus
+ 'with observed-timestamp' | '2021-03-03T23:59:59.999-0400' || 1 | HttpStatus.OK
+ 'without observed-timestamp' | null || 1 | HttpStatus.OK
+ 'with invalid observed-timestamp' | 'invalid' || 0 | HttpStatus.BAD_REQUEST
}
- def 'Delete list node child elements.'() {
+ def 'Delete list node child elements. #scenario'() {
given: 'list node xpath'
def listNodeXpath = 'list node xpath'
when: 'delete is invoked list-node endpoint'
- def response = mvc.perform(
- delete("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
- .param('xpath', listNodeXpath)
- ).andReturn().response
+ def deleteRequestBuilder = delete("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
+ .param('xpath', listNodeXpath)
+ if (observedTimestamp != null)
+ deleteRequestBuilder.param('observed-timestamp', observedTimestamp)
+ def response = mvc.perform(deleteRequestBuilder).andReturn().response
then: 'a success response is returned'
- response.status == HttpStatus.NO_CONTENT.value()
- then: 'the java API was called with the correct parameters'
- 1 * mockCpsDataService.deleteListNodeData(dataspaceName, anchorName, listNodeXpath)
+ response.status == expectedHttpStatus.value()
+ and: 'the java API was called with the correct parameters'
+ expectedApiCount * mockCpsDataService.deleteListNodeData(dataspaceName, anchorName, listNodeXpath,
+ { it == DateTimeUtility.toOffsetDateTime(observedTimestamp) })
+ where:
+ scenario | observedTimestamp || expectedApiCount | expectedHttpStatus
+ 'with observed-timestamp' | '2021-03-03T23:59:59.999-0400' || 1 | HttpStatus.NO_CONTENT
+ 'without observed-timestamp' | null || 1 | HttpStatus.NO_CONTENT
+ 'with invalid observed-timestamp' | 'invalid' || 0 | HttpStatus.BAD_REQUEST
}
}
def response = performTestRequest()
then: 'an HTTP Not Found response is returned with correct message and details'
assertTestResponse(response, NOT_FOUND, 'Object not found',
- 'Description does not exist in dataspace MyDataSpace.')
+ 'Description does not exist in dataspace MyDataSpace.')
}
def 'Request with an object already defined exception returns HTTP Status Conflict.'() {
def response = performTestRequest()
then: 'a HTTP conflict response is returned with correct message an details'
assertTestResponse(response, CONFLICT,
- "Already defined exception",
- "Anchor with name ${existingObjectName} already exists for ${dataspaceName}.")
+ "Already defined exception",
+ "Anchor with name ${existingObjectName} already exists for ${dataspaceName}.")
}
def 'Get request with a #exceptionThrown.class.simpleName returns HTTP Status Bad Request'() {
* NB. This method tests the expected behavior for POST request only;
* testing of PUT and PATCH requests omitted due to same NOT 'GET' condition is being used.
*/
+
def 'Post request with #exceptionThrown.class.simpleName returns HTTP Status Bad Request.'() {
given: '#exception is thrown the service indicating data is not found'
- mockCpsDataService.saveData(_, _, _, _) >> { throw exceptionThrown }
+ mockCpsDataService.saveData(_, _, _, _, _) >> { throw exceptionThrown }
when: 'data update request is performed'
def response = mvc.perform(
- post("$basePath/v1/dataspaces/dataspace-name/anchors/anchor-name/nodes")
- .contentType(MediaType.APPLICATION_JSON)
- .param('xpath', 'parent node xpath')
- .content('json data')
+ post("$basePath/v1/dataspaces/dataspace-name/anchors/anchor-name/nodes")
+ .contentType(MediaType.APPLICATION_JSON)
+ .param('xpath', 'parent node xpath')
+ .content('json data')
).andReturn().response
then: 'response code indicates bad input parameters'
response.status == BAD_REQUEST.value()
def performTestRequest() {
return mvc.perform(
- get("$basePath/v1/dataspaces/dataspace-name/anchors"))
- .andReturn().response
+ get("$basePath/v1/dataspaces/dataspace-name/anchors"))
+ .andReturn().response
}
static void assertTestResponse(response, expectedStatus, expectedErrorMessage, expectedErrorDetails) {
--- /dev/null
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (c) 2021 Bell Canada.
+ * ================================================================================
+ * 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.utils;
+
+import java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+import org.springframework.util.StringUtils;
+
+public interface DateTimeUtility {
+
+ String ISO_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
+ DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_PATTERN);
+
+ static OffsetDateTime toOffsetDateTime(String datetTimestampAsString) {
+ return ! StringUtils.hasLength(datetTimestampAsString)
+ ? null : OffsetDateTime.parse(datetTimestampAsString, ISO_TIMESTAMP_FORMATTER);
+ }
+
+ static String toString(OffsetDateTime offsetDateTime) {
+ return offsetDateTime != null ? ISO_TIMESTAMP_FORMATTER.format(offsetDateTime) : null;
+ }
+}
* ============LICENSE_START=======================================================
* Copyright (C) 2020 Nordix Foundation
* Modifications Copyright (C) 2021 Pantheon.tech
+ * Modifications Copyright (C) 2021 Bell Canada
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.cps.api;
+import java.time.OffsetDateTime;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.onap.cps.spi.FetchDescendantsOption;
import org.onap.cps.spi.model.DataNode;
* @param dataspaceName dataspace name
* @param anchorName anchor name
* @param jsonData json data
+ * @param observedTimestamp observedTimestamp
*/
- void saveData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String jsonData);
+ void saveData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String jsonData,
+ OffsetDateTime observedTimestamp);
/**
* Persists child data fragment under existing data node for the given anchor and dataspace.
* @param anchorName anchor name
* @param parentNodeXpath parent node xpath
* @param jsonData json data
+ * @param observedTimestamp observedTimestamp
*/
void saveData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath,
- @NonNull String jsonData);
+ @NonNull String jsonData, OffsetDateTime observedTimestamp);
/**
- * Persists child data fragment representing list-node (with one or more elements) under existing data node
- * for the given anchor and dataspace.
+ * Persists child data fragment representing list-node (with one or more elements) under existing data node for the
+ * given anchor and dataspace.
*
* @param dataspaceName dataspace name
* @param anchorName anchor name
* @param parentNodeXpath parent node xpath
* @param jsonData json data representing list element
+ * @param observedTimestamp observedTimestamp
*/
void saveListNodeData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath,
- @NonNull String jsonData);
+ @NonNull String jsonData, OffsetDateTime observedTimestamp);
/**
* Retrieves datanode by XPath for given dataspace and anchor.
* @param anchorName anchor name
* @param parentNodeXpath xpath to parent node
* @param jsonData json data
+ * @param observedTimestamp observedTimestamp
*/
void updateNodeLeaves(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath,
- @NonNull String jsonData);
+ @NonNull String jsonData, OffsetDateTime observedTimestamp);
/**
* Replaces existing data node content including descendants.
* @param anchorName anchor name
* @param parentNodeXpath xpath to parent node
* @param jsonData json data
+ * @param observedTimestamp observedTimestamp
*/
void replaceNodeTree(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath,
- @NonNull String jsonData);
+ @NonNull String jsonData, OffsetDateTime observedTimestamp);
/**
- * Replaces (if exists) child data fragment representing list-node (with one or more elements)
- * under existing data node for the given anchor and dataspace.
+ * Replaces (if exists) child data fragment representing list-node (with one or more elements) under existing data
+ * node for the given anchor and dataspace.
*
- * @param dataspaceName dataspace name
- * @param anchorName anchor name
- * @param parentNodeXpath parent node xpath
- * @param jsonData json data representing list element
+ * @param dataspaceName dataspace name
+ * @param anchorName anchor name
+ * @param parentNodeXpath parent node xpath
+ * @param jsonData json data representing list element
+ * @param observedTimestamp observedTimestamp
*/
void replaceListNodeData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath,
- @NonNull String jsonData);
+ @NonNull String jsonData, OffsetDateTime observedTimestamp);
/**
- * Deletes (if exists) child data fragment representing list-node (with one or more elements)
- * under existing data node for the given anchor and dataspace.
+ * Deletes (if exists) child data fragment representing list-node (with one or more elements) under existing data
+ * node for the given anchor and dataspace.
*
- * @param dataspaceName dataspace name
- * @param anchorName anchor name
- * @param listNodeXpath list node xpath
+ * @param dataspaceName dataspace name
+ * @param anchorName anchor name
+ * @param listNodeXpath list node xpath
+ * @param observedTimestamp observedTimestamp
*/
- void deleteListNodeData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String listNodeXpath);
+ void deleteListNodeData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String listNodeXpath,
+ OffsetDateTime observedTimestamp);
/**
- * Updates leaves of DataNode for given dataspace and anchor using xpath,
- * along with the leaves of each Child Data Node which already exists.
- * This method will throw an exception if data node update or any descendant update does not exist.
+ * Updates leaves of DataNode for given dataspace and anchor using xpath, along with the leaves of each Child Data
+ * Node which already exists. This method will throw an exception if data node update or any descendant update does
+ * not exist.
*
- * @param dataspaceName dataspace name
- * @param anchorName anchor name
- * @param parentNodeXpath xpath
+ * @param dataspaceName dataspace name
+ * @param anchorName anchor name
+ * @param parentNodeXpath xpath
* @param dataNodeUpdatesAsJson json data representing data node updates
+ * @param observedTimestamp observedTimestamp
*/
void updateNodeLeavesAndExistingDescendantLeaves(String dataspaceName, String anchorName, String parentNodeXpath,
- String dataNodeUpdatesAsJson);
+ String dataNodeUpdatesAsJson, OffsetDateTime observedTimestamp);
}
package org.onap.cps.api.impl;
+import java.time.OffsetDateTime;
import java.util.Collection;
import lombok.extern.slf4j.Slf4j;
import org.onap.cps.api.CpsAdminService;
private NotificationService notificationService;
@Override
- public void saveData(final String dataspaceName, final String anchorName, final String jsonData) {
+ public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
+ final OffsetDateTime observedTimestamp) {
final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
- processDataUpdatedEventAsync(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
}
@Override
public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
- final String jsonData) {
+ final String jsonData, final OffsetDateTime observedTimestamp) {
final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
- processDataUpdatedEventAsync(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
}
@Override
public void saveListNodeData(final String dataspaceName, final String anchorName,
- final String parentNodeXpath, final String jsonData) {
+ final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
final Collection<DataNode> dataNodesCollection =
buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodesCollection);
- processDataUpdatedEventAsync(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
}
@Override
@Override
public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
- final String jsonData) {
+ final String jsonData, final OffsetDateTime observedTimestamp) {
final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService
.updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
- processDataUpdatedEventAsync(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
}
@Override
public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
- final String parentNodeXpath,
- final String dataNodeUpdatesAsJson) {
+ final String parentNodeXpath,
+ final String dataNodeUpdatesAsJson,
+ final OffsetDateTime observedTimestamp) {
final Collection<DataNode> dataNodeUpdates =
buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, dataNodeUpdatesAsJson);
for (final DataNode dataNodeUpdate : dataNodeUpdates) {
processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
}
- notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
}
@Override
public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
- final String jsonData) {
+ final String jsonData, final OffsetDateTime observedTimestamp) {
final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
- processDataUpdatedEventAsync(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
}
@Override
public void replaceListNodeData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
- final String jsonData) {
+ final String jsonData, final OffsetDateTime observedTimestamp) {
final Collection<DataNode> dataNodes =
buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodes);
- processDataUpdatedEventAsync(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
}
@Override
- public void deleteListNodeData(final String dataspaceName, final String anchorName, final String listNodeXpath) {
+ public void deleteListNodeData(final String dataspaceName, final String anchorName, final String listNodeXpath,
+ final OffsetDateTime observedTimestamp) {
cpsDataPersistenceService.deleteListDataNodes(dataspaceName, anchorName, listNodeXpath);
- processDataUpdatedEventAsync(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
}
}
- private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName) {
+ private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
+ final OffsetDateTime observedTimestamp) {
try {
- notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
+ notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp);
} catch (final Exception exception) {
log.error("Failed to send message to notification service", exception);
}
private static final URI EVENT_SCHEMA;
private static final URI EVENT_SOURCE;
private static final String EVENT_TYPE = "org.onap.cps.data-updated-event";
- private static final DateTimeFormatter dateTimeFormatter =
+ private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
static {
}
/**
- * Generates CPS Data Updated event.
+ * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used.
*
- * @param dataspaceName dataspaceName
- * @param anchorName anchorName
+ * @param dataspaceName dataspaceName
+ * @param anchorName anchorName
+ * @param observedTimestamp observedTimestamp
* @return CpsDataUpdatedEvent
*/
- public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName) {
+ public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName,
+ final OffsetDateTime observedTimestamp) {
final var dataNode = cpsDataService
.getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
- return toCpsDataUpdatedEvent(anchor, dataNode);
+ return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp);
}
- private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode) {
+ private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode,
+ final OffsetDateTime observedTimestamp) {
final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
- cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode));
+ cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp));
cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
cpsDataUpdatedEvent.withSchema(EVENT_SCHEMA);
cpsDataUpdatedEvent.withSource(EVENT_SOURCE);
return data;
}
- private Content createContent(final Anchor anchor, final DataNode dataNode) {
+ private Content createContent(final Anchor anchor, final DataNode dataNode,
+ final OffsetDateTime observedTimestamp) {
final var content = new Content();
content.withAnchorName(anchor.getName());
content.withDataspaceName(anchor.getDataspaceName());
content.withSchemaSetName(anchor.getSchemaSetName());
content.withData(createData(dataNode));
- content.withObservedTimestamp(dateTimeFormatter.format(OffsetDateTime.now()));
+ content.withObservedTimestamp(
+ DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp));
return content;
}
}
package org.onap.cps.notification;
+import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
*
* @param dataspaceName dataspace name
* @param anchorName anchor name
+ * @param observedTimestamp observedTimestamp
* @return future
*/
@Async("notificationExecutor")
- public Future<Void> processDataUpdatedEvent(final String dataspaceName, final String anchorName) {
+ public Future<Void> processDataUpdatedEvent(final String dataspaceName, final String anchorName,
+ final OffsetDateTime observedTimestamp) {
log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName);
try {
if (shouldSendNotification(dataspaceName)) {
final var cpsDataUpdatedEvent =
- cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, anchorName);
+ cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp);
log.debug("data updated event to be published {}", cpsDataUpdatedEvent);
notificationPublisher.sendNotification(cpsDataUpdatedEvent);
}
package org.onap.cps.api.impl
+import java.time.OffsetDateTime
import org.onap.cps.TestUtils
import org.onap.cps.api.CpsAdminService
import org.onap.cps.api.CpsModuleService
def dataspaceName = 'some dataspace'
def anchorName = 'some anchor'
def schemaSetName = 'some schema set'
+ def observedTimestamp = OffsetDateTime.now()
def 'Saving json data.'() {
given: 'schema set for given anchor and dataspace references test-tree model'
setupSchemaSetMocks('test-tree.yang')
when: 'save data method is invoked with test-tree json data'
def jsonData = TestUtils.getResourceFileContent('test-tree.json')
- objectUnderTest.saveData(dataspaceName, anchorName, jsonData)
+ objectUnderTest.saveData(dataspaceName, anchorName, jsonData, observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
- { dataNode -> dataNode.xpath == '/test-tree' })
+ { dataNode -> dataNode.xpath == '/test-tree' })
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
}
def 'Saving child data fragment under existing node.'() {
setupSchemaSetMocks('test-tree.yang')
when: 'save data method is invoked with test-tree json data'
def jsonData = '{"branch": [{"name": "New"}]}'
- objectUnderTest.saveData(dataspaceName, anchorName, '/test-tree', jsonData)
+ objectUnderTest.saveData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree',
- { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' })
+ { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' })
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
}
def 'Saving list-node data fragment under existing node.'() {
setupSchemaSetMocks('test-tree.yang')
when: 'save data method is invoked with list-node json data'
def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
- objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
+ objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, '/test-tree',
- { dataNodeCollection ->
- {
- assert dataNodeCollection.size() == 2
- assert dataNodeCollection.collect { it.getXpath() }
- .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
- }
+ { dataNodeCollection ->
+ {
+ assert dataNodeCollection.size() == 2
+ assert dataNodeCollection.collect { it.getXpath() }
+ .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
}
+ }
)
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
}
def 'Saving empty list-node data fragment.'() {
setupSchemaSetMocks('test-tree.yang')
when: 'save data method is invoked with empty list-node data fragment'
def jsonData = '{"branch": []}'
- objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
+ objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
then: 'invalid data exception is thrown'
thrown(DataValidationException)
}
given: 'schema set for given anchor and dataspace references test-tree model'
setupSchemaSetMocks('test-tree.yang')
when: 'update data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
- objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData)
+ objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData, observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves)
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
where: 'following parameters were used'
scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves
'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap()
given: 'schema set for given anchor and dataspace references bookstore model'
setupSchemaSetMocks('bookstore.yang')
when: 'update data method is invoked with json data #jsonData and parent node xpath'
- objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, '/bookstore/categories[@code=2]', jsonData)
+ objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, '/bookstore/categories[@code=2]',
+ jsonData, observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
thrown(DataValidationException)
where: 'following parameters were used'
and: 'the expected json string'
def jsonData = '{"cm-handles":[{"id":"cmHandle001", "additional-properties":[{"name":"P1"}]}]}'
when: 'update data method is invoked with json data and parent node xpath'
- objectUnderTest.updateNodeLeavesAndExistingDescendantLeaves(dataspaceName, anchorName, '/dmi-registry', jsonData)
+ objectUnderTest.updateNodeLeavesAndExistingDescendantLeaves(dataspaceName, anchorName,
+ '/dmi-registry', jsonData, observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
- 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, "/dmi-registry/cm-handles[@id='cmHandle001']", ['id': 'cmHandle001'])
+ 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName,
+ "/dmi-registry/cm-handles[@id='cmHandle001']", ['id': 'cmHandle001'])
and: 'the data updated event is sent to the notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
}
def 'Replace data node: #scenario.'() {
given: 'schema set for given anchor and dataspace references test-tree model'
setupSchemaSetMocks('test-tree.yang')
when: 'replace data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
- objectUnderTest.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData)
+ objectUnderTest.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData, observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
- { dataNode -> dataNode.xpath == expectedNodeXpath })
+ { dataNode -> dataNode.xpath == expectedNodeXpath })
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
where: 'following parameters were used'
scenario | parentNodeXpath | jsonData || expectedNodeXpath
'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree'
setupSchemaSetMocks('test-tree.yang')
when: 'replace list data method is invoked with list-node json data'
def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
- objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
+ objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, '/test-tree',
- { dataNodeCollection ->
- {
- assert dataNodeCollection.size() == 2
- assert dataNodeCollection.collect { it.getXpath() }
- .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
- }
+ { dataNodeCollection ->
+ {
+ assert dataNodeCollection.size() == 2
+ assert dataNodeCollection.collect { it.getXpath() }
+ .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
}
+ }
)
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
}
def 'Replace with empty list-node data fragment.'() {
setupSchemaSetMocks('test-tree.yang')
when: 'replace list data method is invoked with empty list-node data fragment'
def jsonData = '{"branch": []}'
- objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
+ objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
then: 'invalid data exception is thrown'
thrown(DataValidationException)
}
given: 'schema set for given anchor and dataspace references test-tree model'
setupSchemaSetMocks('test-tree.yang')
when: 'delete list data method is invoked with list-node json data'
- objectUnderTest.deleteListNodeData(dataspaceName, anchorName, '/test-tree/branch')
+ objectUnderTest.deleteListNodeData(dataspaceName, anchorName, '/test-tree/branch', observedTimestamp)
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.deleteListDataNodes(dataspaceName, anchorName, '/test-tree/branch')
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
}
def setupSchemaSetMocks(String... yangResources) {
\r
package org.onap.cps.api.impl\r
\r
+import java.time.OffsetDateTime\r
import org.onap.cps.TestUtils\r
import org.onap.cps.api.CpsAdminService\r
import org.onap.cps.notification.NotificationService\r
def dataspaceName = 'someDataspace'\r
def anchorName = 'someAnchor'\r
def schemaSetName = 'someSchemaSet'\r
+ def noTimestamp = null\r
\r
def setup() {\r
cpsDataServiceImpl.cpsDataPersistenceService = mockDataStoreService\r
YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)\r
mockModuleStoreService.getYangSchemaResources(dataspaceName, schemaSetName) >> schemaContext\r
when: 'saveData method is invoked'\r
- cpsDataServiceImpl.saveData(dataspaceName, anchorName, jsonData)\r
+ cpsDataServiceImpl.saveData(dataspaceName, anchorName, jsonData, noTimestamp)\r
then: 'Parameters are validated and processing is delegated to persistence service'\r
1 * mockDataStoreService.storeDataNode('someDataspace', 'someAnchor', _) >>\r
{ args -> dataNodeStored = args[2]}\r
mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)\r
mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> schemaContext\r
when: 'saveData method is invoked'\r
- cpsDataServiceImpl.saveData('someDataspace', 'someAnchor', jsonData)\r
+ cpsDataServiceImpl.saveData('someDataspace', 'someAnchor', jsonData, noTimestamp)\r
then: 'parameters are validated and processing is delegated to persistence service'\r
1 * mockDataStoreService.storeDataNode('someDataspace', 'someAnchor', _) >>\r
{ args -> dataNodeStored = args[2]}\r
/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2021 Bell Canada. All rights reserved.
+ * Copyright (c) 2021 Bell Canada.
* ================================================================================
* 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.
package org.onap.cps.notification
+import java.time.OffsetDateTime
+import java.time.format.DateTimeFormatter
+import org.onap.cps.utils.DateTimeUtility
import org.onap.cps.api.CpsAdminService
import org.onap.cps.api.CpsDataService
import org.onap.cps.event.model.Data
import org.springframework.util.StringUtils
import spock.lang.Specification
-import java.time.format.DateTimeFormatter
-
class CpsDataUpdateEventFactorySpec extends Specification {
def mockCpsDataService = Mock(CpsDataService)
def mySchemasetName = 'my-schemaset-name'
def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'
- def 'Create a CPS data updated event successfully.'() {
+ def 'Create a CPS data updated event successfully: #scenario'() {
given: 'cps admin service is able to return anchor details'
mockCpsAdminService.getAnchor(myDataspaceName, myAnchorName) >>
def xpath = '/'
def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
mockCpsDataService.getDataNode(
- myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+ myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
when: 'CPS data updated event is created'
- def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName)
+ def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName,
+ myAnchorName, DateTimeUtility.toOffsetDateTime(inputObservedTimestamp))
+
+ then: 'CPS data updated event is created with correct envelope'
- then: 'CPS data updated event is created with expected values'
with(cpsDataUpdatedEvent) {
type == 'org.onap.cps.data-updated-event'
source == new URI('urn:cps:org.onap.cps')
StringUtils.hasText(id)
content != null
}
+ and: 'correct content'
with(cpsDataUpdatedEvent.content) {
assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format"
- anchorName == myAnchorName
- dataspaceName == myDataspaceName
- schemaSetName == mySchemasetName
- data == new Data().withAdditionalProperty('leafName', 'leafValue')
+ if (inputObservedTimestamp != null)
+ assert observedTimestamp == inputObservedTimestamp
+ else
+ assert OffsetDateTime.now().minusSeconds(20).isBefore(
+ DateTimeUtility.toOffsetDateTime(observedTimestamp))
+ assert anchorName == myAnchorName
+ assert dataspaceName == myDataspaceName
+ assert schemaSetName == mySchemasetName
+ assert data == new Data().withAdditionalProperty('leafName', 'leafValue')
}
+ where:
+ scenario | inputObservedTimestamp
+ 'with observed timestamp -0400' | '2021-01-01T23:00:00.345-0400'
+ 'with observed timestamp +0400' | '2021-01-01T23:00:00.345+0400'
+ 'missing observed timestamp' | null
}
def isExpectedDateTimeFormat(String observedTimestamp) {
package org.onap.cps.notification
+import java.time.OffsetDateTime
import org.onap.cps.config.AsyncConfig
import org.onap.cps.event.model.CpsDataUpdatedEvent
import org.spockframework.spring.SpringBean
@Shared
def myDataspacePublishedName = 'my-dataspace-published'
def myAnchorName = 'my-anchorname'
+ def myObservedTimestamp = OffsetDateTime.now()
def 'Skip sending notification when disabled.'() {
given: 'notification is disabled'
spyNotificationProperties.isEnabled() >> false
when: 'dataUpdatedEvent is received'
- objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName)
+ objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp)
then: 'the notification is not sent'
0 * mockNotificationPublisher.sendNotification(_)
}
spyNotificationProperties.isEnabled() >> true
and: 'event factory can create event successfully'
def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
- mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName) >> cpsDataUpdatedEvent
+ mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp) >>
+ cpsDataUpdatedEvent
when: 'dataUpdatedEvent is received'
- def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName)
- and: 'wait for async processing is completed'
+ def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp)
+ and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
then: 'async process completed successfully'
future.isDone()
given: 'notification is enabled'
spyNotificationProperties.isEnabled() >> true
and: 'event factory can not create event successfully'
- mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName) >>
+ mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp) >>
{ throw new Exception("Could not create event") }
when: 'event is sent for processing'
- def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName)
- and: 'wait for async processing is completed'
+ def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp)
+ and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
then: 'async process completed successfully'
future.isDone()
--- /dev/null
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (c) 2021 Bell Canada.
+ * ================================================================================
+ * 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.utils;
+
+import java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+import org.springframework.util.StringUtils;
+
+public interface DateTimeUtility {
+
+ String ISO_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
+ DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_PATTERN);
+
+ static OffsetDateTime toOffsetDateTime(String datetTimestampAsString) {
+ return ! StringUtils.hasLength(datetTimestampAsString)
+ ? null : OffsetDateTime.parse(datetTimestampAsString, ISO_TIMESTAMP_FORMATTER);
+ }
+
+ static String toString(OffsetDateTime offsetDateTime) {
+ return offsetDateTime != null ? ISO_TIMESTAMP_FORMATTER.format(offsetDateTime) : null;
+ }
+}