From 9ddce2b0e23655371aa311c94ce9605dcc74f74e Mon Sep 17 00:00:00 2001 From: "aravind.est" Date: Mon, 22 Apr 2024 12:29:37 +0100 Subject: [PATCH] Modify sme invoker to discover the endpoints by itself SME invoker modified to call the discovery endpoint in every 5 seconds. Issue-ID: NONRTRIC-981 Change-Id: I05e9a56fb510bd63919d77a4f1953348ac898a6d Signed-off-by: aravind.est --- sample-services/hello-world-sme-invoker/Dockerfile | 2 - sample-services/hello-world-sme-invoker/README.md | 14 +- .../hello-world-sme-invoker-build-start.sh | 7 - .../hello-world-sme-invoker/values.yaml | 4 +- .../sample/HelloWorldSmeInvokerApplication.java | 2 + .../sample/rest/HelloWorldSmeInvokerComponent.java | 161 ++++++++++++++++ .../rest/HelloWorldSmeInvokerController.java | 203 --------------------- 7 files changed, 167 insertions(+), 226 deletions(-) create mode 100644 sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerComponent.java delete mode 100644 sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerController.java diff --git a/sample-services/hello-world-sme-invoker/Dockerfile b/sample-services/hello-world-sme-invoker/Dockerfile index 4885a483..05b4db21 100644 --- a/sample-services/hello-world-sme-invoker/Dockerfile +++ b/sample-services/hello-world-sme-invoker/Dockerfile @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# This source code is part of the near-RT RIC (RAN Intelligent Controller) -# platform project (RICP). #================================================================================== diff --git a/sample-services/hello-world-sme-invoker/README.md b/sample-services/hello-world-sme-invoker/README.md index 95822a71..be6cf1e6 100644 --- a/sample-services/hello-world-sme-invoker/README.md +++ b/sample-services/hello-world-sme-invoker/README.md @@ -1,6 +1,6 @@ # Hello World Sme Invoker Service -This repository contains a Spring Boot application serving few Hello World SME endpoints. +This repository contains a Spring Boot application serving as Hello World SME invoker application. The application can be built and run using the provided script - ``hello-world-sme-invoker-build-start.sh``. ## Prerequisites @@ -16,13 +16,7 @@ Run the script: The script will build a Docker image and run a container with the Hello World SME service. After the container starts, wait for a few seconds to ensure the Spring Boot application is fully initialized. Next, it will make an HTTP request to the -Hello World SME endpoint and display the response: - -```bash - response=$(curl -s http://localhost:8080/helloworld/v1/sme) - echo "Response from the Hello World SME endpoint:" - echo "$response" -``` +Hello World SME endpoint on the interval of 5 seconds and displays the response: To stop and remove the Docker container: @@ -31,7 +25,3 @@ To stop and remove the Docker container: docker rm hello-world-sme-invoker ``` -## Additional Information - -- The Hello World SME endpoint is available at http://localhost:8080/helloworld/v1/sme. - diff --git a/sample-services/hello-world-sme-invoker/hello-world-sme-invoker-build-start.sh b/sample-services/hello-world-sme-invoker/hello-world-sme-invoker-build-start.sh index a20bd35e..1baf2c01 100644 --- a/sample-services/hello-world-sme-invoker/hello-world-sme-invoker-build-start.sh +++ b/sample-services/hello-world-sme-invoker/hello-world-sme-invoker-build-start.sh @@ -26,10 +26,3 @@ IMAGE_NAME="o-ran-sc/nonrtric-sample-helloworld-sme-invoker" docker build -t $IMAGE_NAME:latest . docker run --rm -d -p 8080:8080 --name $NAME $IMAGE_NAME - -sleep 10 - -echo "Make an HTTP request to the Hello World Sme Invoker endpoint and display the response" -response=$(curl -s http://localhost:8080/helloworld/v1/sme) -echo "Response from the /helloworld/v1/sme endpoint: " -echo "$response" diff --git a/sample-services/hello-world-sme-invoker/hello-world-sme-invoker/values.yaml b/sample-services/hello-world-sme-invoker/hello-world-sme-invoker/values.yaml index 8d86d801..69c367e2 100644 --- a/sample-services/hello-world-sme-invoker/hello-world-sme-invoker/values.yaml +++ b/sample-services/hello-world-sme-invoker/hello-world-sme-invoker/values.yaml @@ -29,7 +29,7 @@ service: port: 8080 environment: - appId: Invoker_Rapp_Id - smeDiscoveryEndpoint: capifcore.nonrtric.svc.cluster.local:8090/service-apis/v1/allServiceAPIs + appId: + smeDiscoveryEndpoint: diff --git a/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/HelloWorldSmeInvokerApplication.java b/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/HelloWorldSmeInvokerApplication.java index 0a384f42..163eab94 100644 --- a/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/HelloWorldSmeInvokerApplication.java +++ b/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/HelloWorldSmeInvokerApplication.java @@ -24,9 +24,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.client.RestTemplate; @SpringBootApplication(scanBasePackages = "org.oransc.nonrtric.sample") +@EnableScheduling public class HelloWorldSmeInvokerApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldSmeInvokerApplication.class, args); diff --git a/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerComponent.java b/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerComponent.java new file mode 100644 index 00000000..7d76b4f9 --- /dev/null +++ b/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerComponent.java @@ -0,0 +1,161 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2024 OpenInfra Foundation Europe. + * %% + * 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.oransc.nonrtric.sample.rest; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; +import java.util.List; +import org.oransc.nonrtric.sample.exception.CapifAccessException; +import org.oransc.nonrtric.sample.rest.response.ApiResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Component +public class HelloWorldSmeInvokerComponent { + + private final RestTemplate restTemplate; + private final ObjectMapper objectMapper; + private static final Logger logger = LoggerFactory.getLogger(HelloWorldSmeInvokerComponent.class); + + public HelloWorldSmeInvokerComponent(RestTemplate restTemplate, ObjectMapper objectMapper) { + this.restTemplate = restTemplate; + this.objectMapper = objectMapper; + } + + @Scheduled(fixedRate = 5000) + public void accessHelloWorldByInvoker() { + String capifUrl = createCapifUrl(); + if (capifUrl != null && !capifUrl.isEmpty()) { + String baseUrl = ""; + ApiResponse apiResponse; + try { + apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class); + logger.info("Discovery endpoint response is {}", + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(apiResponse)); + baseUrl = getBaseUrl(apiResponse); + } catch (IllegalArgumentException e) { + throw new CapifAccessException("Error accessing the URL :- " + capifUrl); + } catch (Exception e) { + throw new CapifAccessException("Unexpected error"); + } + + //TODO The below should be uncommented once SME Manager provides an accessible URI + +// String helloWorldEndpoint = ""; +// List apiSetEndpoints = getApiSetEndpoints(apiResponse, baseUrl); +// if (apiSetEndpoints != null && !apiSetEndpoints.isEmpty()) { +// helloWorldEndpoint = apiSetEndpoints.get(0); +// } +// +// if (helloWorldEndpoint != null && !helloWorldEndpoint.isEmpty()) { +// try { +// String responseHelloWorld = restTemplate.getForObject(helloWorldEndpoint, String.class); +// logger.info("Response :- ", responseHelloWorld); +// } catch (IllegalArgumentException e) { +// throw new CapifAccessException("Error accessing the URL :- " + helloWorldEndpoint); +// } catch (Exception e) { +// throw new CapifAccessException("Unexpected error"); +// } +// } + } + } + + private String createCapifUrl() { + String appId = System.getenv("APP_ID"); + String invokerId = ""; + String capifUrl = ""; + if (appId != null) { + logger.info("APP_ID: " + appId); + invokerId = "api_invoker_id_" + appId; + logger.info("invokerId: " + invokerId); + } else { + logger.info("APP_ID environment variable is not set. "); + } + + String smeDiscoveryEndpoint = System.getenv("SME_DISCOVERY_ENDPOINT"); + if (smeDiscoveryEndpoint != null) { + logger.info("SME_DISCOVERY_ENDPOINT: " + smeDiscoveryEndpoint); + capifUrl = smeDiscoveryEndpoint + "?api-invoker-id=" + invokerId; + logger.info("capifUrl: " + capifUrl); + } else { + logger.info("SME_DISCOVERY_ENDPOINT environment variable is not set."); + } + return capifUrl; + } + + private static String getBaseUrl(ApiResponse apiResponse) { + if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions() + .isEmpty()) { + + ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0); + + if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) { + + ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0); + + if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) { + ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0); + return "http://" + interfaceDescription.getIpv4Addr() + ":" + interfaceDescription.getPort(); + } + } + } + return ""; + } + + private static List getApiSetEndpoints(ApiResponse apiResponse, String baseUrl) { + + String helloWorldEndpoint = ""; + List apiSetEndpoints = new ArrayList<>(5); + + if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions() + .isEmpty()) { + + ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0); + + if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) { + + ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0); + + if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) { + + if (aefProfile.getVersions() != null && !aefProfile.getVersions().isEmpty()) { + + ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0); + + if (apiVersion.getResources() != null && !apiVersion.getResources().isEmpty()) { + + for (ApiResponse.Resource resource : apiVersion.getResources()) { + helloWorldEndpoint = baseUrl + resource.getUri(); + logger.info("Complete URL for resource " + helloWorldEndpoint); + apiSetEndpoints.add(helloWorldEndpoint); + } + } + } + } + } + } + return apiSetEndpoints; + } + +} diff --git a/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerController.java b/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerController.java deleted file mode 100644 index 57820927..00000000 --- a/sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerController.java +++ /dev/null @@ -1,203 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * O-RAN-SC - * %% - * Copyright (C) 2024 OpenInfra Foundation Europe. - * %% - * 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.oransc.nonrtric.sample.rest; - -import jakarta.servlet.http.HttpServletRequest; -import java.util.ArrayList; -import java.util.List; -import org.oransc.nonrtric.sample.exception.CapifAccessException; -import org.oransc.nonrtric.sample.rest.response.ApiResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; - -@RestController -public class HelloWorldSmeInvokerController { - - private final RestTemplate restTemplate; - private static final Logger logger = LoggerFactory.getLogger(HelloWorldSmeInvokerController.class); - - public HelloWorldSmeInvokerController(RestTemplate restTemplate) { - this.restTemplate = restTemplate; - } - - @RequestMapping("/helloworld/v1/sme") - public String helloWorld(HttpServletRequest request) { - String path = logRequestPath(request); - return "Hello from " + path; - } - - @RequestMapping("/helloworld/sme/invoker/v1/apiset") - public ResponseEntity helloWorldSmeInvoker(HttpServletRequest request) { - String path = logRequestPath(request); - String capifUrl = createCapifUrl(); - try { - ApiResponse apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class); - return ResponseEntity.ok(apiResponse); - } catch (IllegalArgumentException e) { - throw new CapifAccessException("Error accessing the URL :- "+capifUrl); - } catch (Exception e) { - throw new CapifAccessException("Unexpected error"); - } - } - - @RequestMapping("/helloworld/sme/invoker/v1") - public ResponseEntity accessHelloWorldByInvoker(HttpServletRequest request) { - String path = logRequestPath(request); - String capifUrl = createCapifUrl(); - String baseUrl = ""; - ApiResponse apiResponse = new ApiResponse(); - try { - apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class); - baseUrl = getBaseUrl(apiResponse); - } catch (IllegalArgumentException e) { - throw new CapifAccessException("Error accessing the URL :- "+capifUrl); - } catch (Exception e) { - throw new CapifAccessException("Unexpected error"); - } - - String helloWorldEndpoint = ""; - List apiSetEndpoints = getApiSetEndpoints(apiResponse, baseUrl); - if(apiSetEndpoints != null && !apiSetEndpoints.isEmpty()){ - helloWorldEndpoint = apiSetEndpoints.get(0); - } - - try { - String responseHelloWorld = restTemplate.getForObject(helloWorldEndpoint, String.class); - logger.info("Response :- ", responseHelloWorld); - return ResponseEntity.ok(responseHelloWorld); - } catch (IllegalArgumentException e) { - throw new CapifAccessException("Error accessing the URL :- "+helloWorldEndpoint); - } catch (Exception e) { - throw new CapifAccessException("Unexpected error"); - } - } - - private String logRequestPath(HttpServletRequest request) { - String path = request.getRequestURI(); - logger.info("Received request for path: {}", path); - return path; - } - - private String createCapifUrl() { - String appId = System.getenv("APP_ID"); - if (appId != null) { - logger.info("APP_ID: " + appId); - } else { - logger.info("APP_ID environment variable is not set. "); - appId = "Invoker_App_1"; - logger.info("APP_ID default value :- " + appId); - } - - String smeDiscoveryEndpoint = System.getenv("SME_DISCOVERY_ENDPOINT"); - if (smeDiscoveryEndpoint != null) { - logger.info("SME_DISCOVERY_ENDPOINT: " + smeDiscoveryEndpoint); - } else { - logger.info("SME_DISCOVERY_ENDPOINT environment variable is not set."); - smeDiscoveryEndpoint = "capifcore.nonrtric.svc.cluster.local:8090/service-apis/v1/allServiceAPIs"; - logger.info("SME_DISCOVERY_ENDPOINT default value :- " + smeDiscoveryEndpoint); - } - - String invokerId = "api_invoker_id_Invoker_App_1"; - if (appId != null) { - invokerId = "api_invoker_id_" + appId; - } - logger.info("invokerId: " + invokerId); - - String capifUrl = - "http://capifcore.nonrtric.svc.cluster.local:8090/service-apis/v1/allServiceAPIs" + "?api-invoker-id=" + - invokerId; - if (smeDiscoveryEndpoint != null) { - capifUrl = smeDiscoveryEndpoint + "?api-invoker-id=" + invokerId; - } - logger.info("capifUrl: " + capifUrl); - - return capifUrl; - } - - private static String getBaseUrl(ApiResponse apiResponse) { - if (apiResponse != null && - apiResponse.getServiceAPIDescriptions() != null && - !apiResponse.getServiceAPIDescriptions().isEmpty()) { - - ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0); - - if (serviceAPIDescription.getAefProfiles() != null && - !serviceAPIDescription.getAefProfiles().isEmpty()) { - - ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0); - - if (aefProfile.getInterfaceDescriptions() != null && - !aefProfile.getInterfaceDescriptions().isEmpty()) { - ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0); - return "http://" + interfaceDescription.getIpv4Addr() + ":" + interfaceDescription.getPort(); - } - } - } - return ""; - } - - private static List getApiSetEndpoints(ApiResponse apiResponse, String baseUrl){ - - String helloWorldEndpoint = ""; - List apiSetEndpoints = new ArrayList<>(5); - - if (apiResponse != null && - apiResponse.getServiceAPIDescriptions() != null && - !apiResponse.getServiceAPIDescriptions().isEmpty()) { - - ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0); - - if (serviceAPIDescription.getAefProfiles() != null && - !serviceAPIDescription.getAefProfiles().isEmpty()) { - - ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0); - - if (aefProfile.getInterfaceDescriptions() != null && - !aefProfile.getInterfaceDescriptions().isEmpty()) { - - ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0); - - if (aefProfile.getVersions() != null && - !aefProfile.getVersions().isEmpty()) { - - ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0); - - if (apiVersion.getResources() != null && - !apiVersion.getResources().isEmpty()) { - - for(ApiResponse.Resource resource : apiVersion.getResources()) { - helloWorldEndpoint = baseUrl + resource.getUri(); - logger.info("Complete URL for resource " + helloWorldEndpoint); - apiSetEndpoints.add(helloWorldEndpoint); - } - } - } - } - } - } - return apiSetEndpoints; - } - -} -- 2.16.6