From e1216505db39f033c8460ca835eb352e27737b9f Mon Sep 17 00:00:00 2001 From: krishnaa96 Date: Tue, 20 Jul 2021 17:44:16 +0530 Subject: Fix issues in CPS integration - Add include descendants to the template - Add authentication for CPS rest client - Update the CPS url - Add configuration for CPS core and NCMP Issue-ID: CPS-512 Signed-off-by: krishnaa96 Change-Id: I071e96d9d3c09b2a90f455f8056dea8ced0060d8 --- .../cps/tbdmt/rest/TemplateControllerTest.java | 4 +-- .../org/onap/cps/tbdmt/client/CpsRestClient.java | 23 ++++++++------ .../org/onap/cps/tbdmt/model/AppConfiguration.java | 7 ++++- .../org/onap/cps/tbdmt/model/CpsConfiguration.java | 36 ++++++++++++++++++++++ .../java/org/onap/cps/tbdmt/model/Template.java | 2 ++ .../org/onap/cps/tbdmt/model/TemplateRequest.java | 1 + .../cps/tbdmt/service/ExecutionBusinessLogic.java | 2 +- .../cps/tbdmt/service/TemplateBusinessLogic.java | 4 +-- .../onap/cps/tbdmt/client/CpsRestClientTest.java | 12 ++++---- .../tbdmt/service/ExecutionBusinessLogicTest.java | 14 ++++----- .../tbdmt/service/TemplateBusinessLogicTest.java | 4 +-- .../src/test/resources/application-test.properties | 8 ++++- docker-compose/application.yml | 12 +++++++- 13 files changed, 97 insertions(+), 32 deletions(-) create mode 100644 cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/CpsConfiguration.java diff --git a/cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java b/cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java index f4f383f..bb8c872 100644 --- a/cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java +++ b/cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java @@ -65,12 +65,12 @@ public class TemplateControllerTest { @Before public void setup() { objectMapper = new ObjectMapper(); - template = new Template("getNbr", "ran-network", "sample", "get"); + template = new Template("getNbr", "ran-network", "sample", "get", true); } @Test public void testCreateTemplate() throws Exception { - final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample", "get"); + final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample", "get", true); final String templateJson = objectMapper.writeValueAsString(templateRequest); Mockito.when(templateBusinessLogic.createTemplate(ArgumentMatchers.any())) .thenReturn(template); diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java index 879efb5..0d356d8 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java @@ -23,6 +23,7 @@ package org.onap.cps.tbdmt.client; import java.util.Arrays; import org.onap.cps.tbdmt.exception.CpsClientException; import org.onap.cps.tbdmt.model.AppConfiguration; +import org.onap.cps.tbdmt.model.CpsConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -38,7 +39,7 @@ import org.springframework.web.util.UriComponentsBuilder; @Component public class CpsRestClient { - private static final String NODES_API_PATH = "/anchors/{anchor}/nodes"; + private static final String NODES_API_PATH = "/anchors/{anchor}/node"; private static final String QUERY_API_PATH = "/anchors/{anchor}/nodes/query"; @@ -56,16 +57,21 @@ public class CpsRestClient { * @return result Response string from CPS */ public String fetchNode(final String anchor, final String xpath, - final String requestType) throws CpsClientException { + final String requestType, final Boolean includeDescendants) throws CpsClientException { final MultiValueMap queryParams = new LinkedMultiValueMap<>(); - queryParams.add("cpsPath", xpath); - String uri = buildCpsUrl(NODES_API_PATH, anchor, queryParams); - if ("query".equals(requestType)) { - uri = buildCpsUrl(QUERY_API_PATH, anchor, queryParams); - } + queryParams.add("xpath", xpath); + queryParams.add("include-descendants", includeDescendants.toString()); + + final CpsConfiguration cpsConfiguration = "cpsCore".equals(appConfiguration.getCpsClient()) + ? appConfiguration.getCpsCoreConfiguration() : appConfiguration.getNcmpConfiguration(); + + final String path = "query".equals(requestType) ? QUERY_API_PATH : NODES_API_PATH; + final String uri = buildCpsUrl(cpsConfiguration.getUrl(), path, anchor, queryParams); final HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + + headers.setBasicAuth(cpsConfiguration.getUsername(), cpsConfiguration.getPassword()); final HttpEntity entity = new HttpEntity<>(headers); ResponseEntity responseEntity = null; @@ -85,9 +91,8 @@ public class CpsRestClient { } } - private String buildCpsUrl(final String path, final String anchor, + private String buildCpsUrl(final String baseUrl, final String path, final String anchor, final MultiValueMap queryParams) { - final String baseUrl = appConfiguration.getXnfProxyUrl(); return UriComponentsBuilder .fromHttpUrl(baseUrl) diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/AppConfiguration.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/AppConfiguration.java index 90666cd..e9fd290 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/AppConfiguration.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/AppConfiguration.java @@ -36,7 +36,12 @@ import org.springframework.web.client.RestTemplate; @ConfigurationProperties(prefix = "app") public class AppConfiguration { - private String xnfProxyUrl; + private CpsConfiguration cpsCoreConfiguration; + + private CpsConfiguration ncmpConfiguration; + + private String cpsClient; + private Map schemaToAnchor; @Bean diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/CpsConfiguration.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/CpsConfiguration.java new file mode 100644 index 0000000..bcaf161 --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/CpsConfiguration.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CpsConfiguration { + + private String url; + + private String username; + + private String password; + +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java index 97353f2..00ad134 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java @@ -51,4 +51,6 @@ public class Template implements Serializable { private String requestType; + private Boolean includeDescendants; + } diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java index c8daf0f..6b08ad8 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java @@ -47,4 +47,5 @@ public class TemplateRequest implements Serializable { @NotEmpty(message = "request type missing") private String requestType; + private Boolean includeDescendants; } diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java index b83a1f8..b7dd42a 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java @@ -72,7 +72,7 @@ public class ExecutionBusinessLogic { final String xpath = generateXpath(template.getXpathTemplate(), inputParameters); try { - return cpsRestClient.fetchNode(anchor, xpath, template.getRequestType()); + return cpsRestClient.fetchNode(anchor, xpath, template.getRequestType(), template.getIncludeDescendants()); } catch (final CpsClientException e) { throw new ExecuteException(e.getLocalizedMessage()); } diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java index 06c48fa..ae179d3 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java @@ -47,8 +47,8 @@ public class TemplateBusinessLogic { */ public Template createTemplate(final TemplateRequest templateRequest) { final Template template = new Template(templateRequest.getTemplateId(), - templateRequest.getModel(), - templateRequest.getXpathTemplate(), templateRequest.getRequestType()); + templateRequest.getModel(), templateRequest.getXpathTemplate(), + templateRequest.getRequestType(), templateRequest.getIncludeDescendants()); return templateRepository.save(template); } diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java index 9742bb1..46f28cb 100644 --- a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java @@ -86,13 +86,13 @@ public class CpsRestClientTest { @Test public void testFetchNode() throws Exception { - final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes?cpsPath=sample"; + final String uri = "http://localhost:8000/anchors/coverage-area-onap/node?xpath=sample&include-descendants=true"; Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.>any())) .thenReturn(response); - assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "get")); + assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "get", true)); final ResponseEntity errorResponse = new ResponseEntity<>("sample response", responseHeaders, HttpStatus.NOT_FOUND); @@ -103,19 +103,19 @@ public class CpsRestClientTest { .thenReturn(errorResponse); exception.expect(CpsClientException.class); exception.expectMessage("Response code from CPS other than 200: 404"); - cpsRestClient.fetchNode("coverage-area-onap", "sample", "get"); + cpsRestClient.fetchNode("coverage-area-onap", "sample", "get", true); } @Test public void testQueryApi() throws Exception { - final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes/query?cpsPath=sample"; + final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes/query?xpath=sample&include-descendants=true"; Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.>any())) .thenReturn(response); - assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "query")); + assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "query", true)); } @Test @@ -127,6 +127,6 @@ public class CpsRestClientTest { .thenThrow(new RestClientException("Connection refused")); exception.expect(CpsClientException.class); exception.expectMessage("Connection refused"); - cpsRestClient.fetchNode("coverage-area-onap", "sample", "get"); + cpsRestClient.fetchNode("coverage-area-onap", "sample", "get", true); } } diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java index 32dbc27..c3be423 100644 --- a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java @@ -91,8 +91,8 @@ public class ExecutionBusinessLogicTest { request = new ExecutionRequest(input); final String xpathTemplate = "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" + "/coverage-area[@coverageArea='{{coverageArea}}']"; - template = new Template("getNbr", "ran-network", xpathTemplate, "get"); - queryTemplate = new Template("getNbr", "ran-network", xpathTemplate, "query"); + template = new Template("getNbr", "ran-network", xpathTemplate, "get", true); + queryTemplate = new Template("getNbr", "ran-network", xpathTemplate, "query", true); } @Test @@ -100,7 +100,7 @@ public class ExecutionBusinessLogicTest { final String resultString = "[{\"key\": \"value\"}]"; Mockito.when(cpsRestClient .fetchNode("ran-network", "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" - + "/coverage-area[@coverageArea='Zone 1']", "get")) + + "/coverage-area[@coverageArea='Zone 1']", "get", true)) .thenReturn(resultString); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(template)); @@ -120,7 +120,7 @@ public class ExecutionBusinessLogicTest { final String exceptionMessage = "Response from CPS other than 200: 404"; Mockito.when(cpsRestClient .fetchNode("ran-network", "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" - + "/coverage-area[@coverageArea='Zone 1']", "get")) + + "/coverage-area[@coverageArea='Zone 1']", "get", true)) .thenThrow(new CpsClientException(exceptionMessage)); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(template)); @@ -128,7 +128,7 @@ public class ExecutionBusinessLogicTest { exception.expectMessage(exceptionMessage); executionBusinessLogic.executeTemplate("ran-network", "getNbr", request); - final Template template1 = new Template("getNbr", "ran-net", "sample", "get"); + final Template template1 = new Template("getNbr", "ran-net", "sample", "get", true); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(template1)); exception.expect(ExecuteException.class); @@ -139,7 +139,7 @@ public class ExecutionBusinessLogicTest { @Test public void testExecuteTemplateNoAnchor() { - final Template template = new Template("getNbr", "ran-net", "sample", "get"); + final Template template = new Template("getNbr", "ran-net", "sample", "get", true); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(template)); exception.expect(ExecuteException.class); @@ -152,7 +152,7 @@ public class ExecutionBusinessLogicTest { final String resultString = "[{\"key\": \"value\"}]"; Mockito.when(cpsRestClient .fetchNode("ran-network", "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" - + "/coverage-area[@coverageArea='Zone 1']", "query")) + + "/coverage-area[@coverageArea='Zone 1']", "query", true)) .thenReturn(resultString); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(queryTemplate)); diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java index aa7e28a..d0bdf47 100644 --- a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java @@ -71,13 +71,13 @@ public class TemplateBusinessLogicTest { @Before public void setup() { - template = new Template("getNbr", "ran-network", "sample", "get"); + template = new Template("getNbr", "ran-network", "sample", "get", true); final TemplateKey templateKey = new TemplateKey("getNbr", "ran-network"); } @Test public void testCreateTemplate() throws Exception { - final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample", "get"); + final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample", "get", true); Mockito.when(templateRepository.save(ArgumentMatchers.any())).thenReturn(template); assertEquals(template, templateBusinessLogic.createTemplate(templateRequest)); } diff --git a/cps-tbdmt-service/src/test/resources/application-test.properties b/cps-tbdmt-service/src/test/resources/application-test.properties index c4daedd..fd90f8a 100644 --- a/cps-tbdmt-service/src/test/resources/application-test.properties +++ b/cps-tbdmt-service/src/test/resources/application-test.properties @@ -1,3 +1,9 @@ -app.xnfProxyUrl=http://localhost:8000/ +app.cpsCoreConfiguration.url=http://localhost:8000/ +app.cpsCoreConfiguration.username=cpsuser +app.cpsCoreConfiguration.password=cpspass +app.ncmpConfiguration.url=http://localhost:8000/ +app.ncmpConfiguration.username=cpsuser +app.ncmpConfiguration.password=cpspass +app.cpsClient=cpsCore app.schemaToAnchor.ran-coverage-area=coverage-area-onap app.schemaToAnchor.ran-network=ran-network \ No newline at end of file diff --git a/docker-compose/application.yml b/docker-compose/application.yml index f93cfa7..2fc49f1 100644 --- a/docker-compose/application.yml +++ b/docker-compose/application.yml @@ -36,6 +36,16 @@ spring: use_jdbc_metadata_defaults: false database-platform: org.hibernate.dialect.PostgreSQLDialect app: - xnfProxyUrl: http://localhost:8000/ + cpsCoreConfiguration: + url: http://192.168.1.5:8883/cps/api/v1/dataspaces/E2EDemo + username: cpsuser + password: cpsr0cks! + ncmpConfiguration: + url: http://192.168.1.5:8883/cps/api/v1 + username: cpsuser + password: cpsr0cks! + cpsClient: cpsCore schemaToAnchor: ran-coverage-area: coverage-area-onap + e2e-cavsta-schemaset: e2e-cavsta1 + -- cgit 1.2.3-korg