Fix issues in CPS integration 42/122742/2
authorkrishnaa96 <krishna.moorthy6@wipro.com>
Tue, 20 Jul 2021 12:14:16 +0000 (17:44 +0530)
committerkrishnaa96 <krishna.moorthy6@wipro.com>
Thu, 22 Jul 2021 12:36:56 +0000 (18:06 +0530)
- 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 <krishna.moorthy6@wipro.com>
Change-Id: I071e96d9d3c09b2a90f455f8056dea8ced0060d8

13 files changed:
cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/AppConfiguration.java
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/CpsConfiguration.java [new file with mode: 0644]
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java
cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java
cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java
cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java
cps-tbdmt-service/src/test/resources/application-test.properties
docker-compose/application.yml

index f4f383f..bb8c872 100644 (file)
@@ -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);
index 879efb5..0d356d8 100644 (file)
@@ -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<String, String> 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<String> entity = new HttpEntity<>(headers);
 
         ResponseEntity<String> 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<String, String> queryParams) {
-        final String baseUrl = appConfiguration.getXnfProxyUrl();
 
         return UriComponentsBuilder
             .fromHttpUrl(baseUrl)
index 90666cd..e9fd290 100644 (file)
@@ -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<String, String> 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 (file)
index 0000000..bcaf161
--- /dev/null
@@ -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;
+
+}
index 97353f2..00ad134 100644 (file)
@@ -51,4 +51,6 @@ public class Template implements Serializable {
 
     private String requestType;
 
+    private Boolean includeDescendants;
+
 }
index c8daf0f..6b08ad8 100644 (file)
@@ -47,4 +47,5 @@ public class TemplateRequest implements Serializable {
     @NotEmpty(message = "request type missing")
     private String requestType;
 
+    private Boolean includeDescendants;
 }
index b83a1f8..b7dd42a 100644 (file)
@@ -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());
         }
index 06c48fa..ae179d3 100644 (file)
@@ -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);
     }
 
index 9742bb1..46f28cb 100644 (file)
@@ -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.<Class<String>>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<String> 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.<Class<String>>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);
     }
 }
index 32dbc27..c3be423 100644 (file)
@@ -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));
index aa7e28a..d0bdf47 100644 (file)
@@ -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));
     }
index c4daedd..fd90f8a 100644 (file)
@@ -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
index f93cfa7..2fc49f1 100644 (file)
@@ -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
+