From b5d573b02c7d1376e92e59887371965a2fb43c25 Mon Sep 17 00:00:00 2001 From: niamhcore Date: Fri, 26 Feb 2021 10:13:48 +0000 Subject: CPS-240 - Create REST End-point on NF-Proxy for DataNode Update & cpsPath Query Issue-ID: CPS-240 Signed-off-by: niamhcore Change-Id: I2aed92f8ab34282b12e23ae7807a391446165eb0 --- cps-nf-proxy-rest/docs/openapi/components.yaml | 9 +++ cps-nf-proxy-rest/docs/openapi/openapi.yml | 5 ++ cps-nf-proxy-rest/docs/openapi/xnfProxy.yml | 77 ++++++++++++++++++++++ .../nfproxy/rest/controller/NfProxyController.java | 28 +++++++- .../rest/controller/NfProxyControllerSpec.groovy | 72 ++++++++++++++++++-- 5 files changed, 183 insertions(+), 8 deletions(-) (limited to 'cps-nf-proxy-rest') diff --git a/cps-nf-proxy-rest/docs/openapi/components.yaml b/cps-nf-proxy-rest/docs/openapi/components.yaml index 0b5d52a3a..af95723cb 100644 --- a/cps-nf-proxy-rest/docs/openapi/components.yaml +++ b/cps-nf-proxy-rest/docs/openapi/components.yaml @@ -43,6 +43,15 @@ components: schema: type: boolean default: false + cpsPathInQuery: + name: cps-path + in: query + description: cps-path + required: false + schema: + type: string + default: / + responses: NotFound: diff --git a/cps-nf-proxy-rest/docs/openapi/openapi.yml b/cps-nf-proxy-rest/docs/openapi/openapi.yml index efa8e7253..a6d0949f2 100755 --- a/cps-nf-proxy-rest/docs/openapi/openapi.yml +++ b/cps-nf-proxy-rest/docs/openapi/openapi.yml @@ -9,3 +9,8 @@ paths: /v1/cm-handles/{cm-handle}/node: $ref: 'xnfProxy.yml#/nodeByCmHandleAndXpath' + /v1/cm-handles/{cm-handle}/nodes/query: + $ref: 'xnfProxy.yml#/nodesByCmHandleAndCpsPath' + + /v1/cm-handles/{cm-handle}/nodes: + $ref: 'xnfProxy.yml#/nodesByCmHandleAndXpath' \ No newline at end of file diff --git a/cps-nf-proxy-rest/docs/openapi/xnfProxy.yml b/cps-nf-proxy-rest/docs/openapi/xnfProxy.yml index 4abe81aff..c39d2dff0 100644 --- a/cps-nf-proxy-rest/docs/openapi/xnfProxy.yml +++ b/cps-nf-proxy-rest/docs/openapi/xnfProxy.yml @@ -22,3 +22,80 @@ nodeByCmHandleAndXpath: $ref: 'components.yaml#/components/responses/NotFound' 501: $ref: 'components.yaml#/components/responses/NotImplemented' + +nodesByCmHandleAndCpsPath: + get: + description: Query nodes for the given cps path and cm Handle + tags: + - nf-proxy + summary: Query data nodes + operationId: queryNodesByCmHandleAndCpsPath + parameters: + - $ref: 'components.yaml#/components/parameters/cmHandleInPath' + - $ref: 'components.yaml#/components/parameters/cpsPathInQuery' + responses: + 200: + $ref: 'components.yaml#/components/responses/Ok' + 400: + $ref: 'components.yaml#/components/responses/BadRequest' + 401: + $ref: 'components.yaml#/components/responses/Unauthorized' + 403: + $ref: 'components.yaml#/components/responses/Forbidden' + 404: + $ref: 'components.yaml#/components/responses/NotFound' + +nodesByCmHandleAndXpath: + patch: + description: Update node leaves for the given cps path and cm Handle + tags: + - nf-proxy + summary: Update node leaves + operationId: updateNodeLeaves + parameters: + - $ref: 'components.yaml#/components/parameters/cmHandleInPath' + - $ref: 'components.yaml#/components/parameters/xpathInQuery' + requestBody: + required: true + content: + application/json: + schema: + type: string + responses: + 200: + $ref: 'components.yaml#/components/responses/Ok' + 400: + $ref: 'components.yaml#/components/responses/BadRequest' + 401: + $ref: 'components.yaml#/components/responses/Unauthorized' + 403: + $ref: 'components.yaml#/components/responses/Forbidden' + 404: + $ref: 'components.yaml#/components/responses/NotFound' + + put: + description: Replace a node with descendants for the given cps path and cm Handle + tags: + - nf-proxy + summary: Replace a node with descendants + operationId: replaceNode + parameters: + - $ref: 'components.yaml#/components/parameters/cmHandleInPath' + - $ref: 'components.yaml#/components/parameters/xpathInQuery' + requestBody: + required: true + content: + application/json: + schema: + type: string + responses: + 200: + $ref: 'components.yaml#/components/responses/Ok' + 400: + $ref: 'components.yaml#/components/responses/BadRequest' + 401: + $ref: 'components.yaml#/components/responses/Unauthorized' + 403: + $ref: 'components.yaml#/components/responses/Forbidden' + 404: + $ref: 'components.yaml#/components/responses/NotFound' \ No newline at end of file diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java index 494e7f659..93ed06580 100644 --- a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech - * ================================================================================ + * Modifications (C) 2021 Nordix Foundation * Modification Copyright (C) 2021 highstreet technologies GmbH * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,6 +21,9 @@ package org.onap.cps.nfproxy.rest.controller; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.util.Collection; import javax.validation.Valid; import org.onap.cps.nfproxy.api.NfProxyDataService; import org.onap.cps.nfproxy.rest.api.NfProxyApi; @@ -38,6 +41,7 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("${rest.api.xnf-base-path}") public class NfProxyController implements NfProxyApi { + private static final Gson GSON = new GsonBuilder().create(); private static final String XPATH_ROOT = "/"; @Autowired @@ -45,7 +49,7 @@ public class NfProxyController implements NfProxyApi { @Override public ResponseEntity getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath, - @Valid final Boolean includeDescendants) { + @Valid final Boolean includeDescendants) { if (XPATH_ROOT.equals(xpath)) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } @@ -54,4 +58,24 @@ public class NfProxyController implements NfProxyApi { final DataNode dataNode = nfProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption); return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK); } + + @Override + public ResponseEntity queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath) { + final Collection dataNodes = nfProxyDataService.queryDataNodes(cmHandle, cpsPath); + return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK); + } + + @Override + public ResponseEntity replaceNode(@Valid final String jsonData, final String cmHandle, + @Valid final String parentNodeXpath) { + nfProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData); + return new ResponseEntity<>(HttpStatus.OK); + } + + @Override + public ResponseEntity updateNodeLeaves(@Valid final String jsonData, final String cmHandle, + @Valid final String parentNodeXpath) { + nfProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData); + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy index 3cd6b9a9f..742a643fa 100644 --- a/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy +++ b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy @@ -1,8 +1,8 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech - * ================================================================================ * Modification Copyright (C) 2021 highstreet technologies GmbH + * Modification Copyright (C) 2021 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,19 +21,22 @@ package org.onap.cps.nfproxy.rest.controller +import com.google.common.collect.ImmutableMap +import com.google.gson.Gson import org.onap.cps.nfproxy.api.NfProxyDataService +import org.onap.cps.spi.model.DataNode import org.onap.cps.spi.model.DataNodeBuilder import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest import org.springframework.http.HttpStatus +import org.springframework.http.MediaType import org.springframework.test.web.servlet.MockMvc import spock.lang.Specification -import spock.lang.Unroll import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* @WebMvcTest class NfProxyControllerSpec extends Specification { @@ -53,14 +56,70 @@ class NfProxyControllerSpec extends Specification { dataNodeBaseEndpoint = "$basePath/v1" } - @Unroll + def cmHandle = 'some handle' + def xpath = 'some xpath' + + def 'Query data node by cps path for the given cm handle.'() { + given: 'service method returns a list containing a data node' + def cpsPath = '/xpath/leaves[@leaf=\'value\']' + def dataNode = new DataNodeBuilder().withXpath("/xpath") + .withLeaves(ImmutableMap.of("leaf", "value")).build() + ArrayList dataNodeList = new ArrayList(); + dataNodeList.add(dataNode) + mockNfProxyDataService.queryDataNodes(cmHandle, cpsPath) >> dataNodeList + and: 'the query endpoint' + def dataNodeEndpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes/query" + when: 'query data nodes API is invoked' + def response = mvc.perform(get(dataNodeEndpoint).param('cps-path', cpsPath)).andReturn().response + then: 'the response contains the the datanode in json format' + response.status == HttpStatus.OK.value() + def expectedJsonContent = new Gson().toJson(dataNode) + response.getContentAsString().contains(expectedJsonContent) + } + + def 'Update data node leaves.'() { + given: 'json data' + def jsonData = 'json data' + and: 'the query endpoint' + def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes" + when: 'patch request is performed' + def response = mvc.perform( + patch(endpoint) + .contentType(MediaType.APPLICATION_JSON) + .content(jsonData) + .param('xpath', xpath) + ).andReturn().response + then: 'the service method is invoked once with expected parameters' + 1 * mockNfProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData) + and: 'response status indicates success' + response.status == HttpStatus.OK.value() + } + + def 'Replace data node tree.'() { + given: 'json data' + def jsonData = 'json data' + and: 'the query endpoint' + def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes" + when: 'put request is performed' + def response = mvc.perform( + put(endpoint) + .contentType(MediaType.APPLICATION_JSON) + .content(jsonData) + .param('xpath', xpath) + ).andReturn().response + then: 'the service method is invoked once with expected parameters' + 1 * mockNfProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData) + and: 'response status indicates success' + response.status == HttpStatus.OK.value() + } + def 'Get data node.'() { given: 'the service returns a data node' def xpath = 'some xpath' - def cmHandle = 'some handle' def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build() - def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/node" mockNfProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode + and: 'the query endpoint' + def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/node" when: 'get request is performed through REST API' def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response then: 'a success response is returned' @@ -69,3 +128,4 @@ class NfProxyControllerSpec extends Specification { response.contentAsString.contains('"leaf":"value"') } } + -- cgit 1.2.3-korg