Modify sme invoker to discover the endpoints by itself 62/12762/1
authoraravind.est <aravindhan.a@est.tech>
Mon, 22 Apr 2024 11:29:37 +0000 (12:29 +0100)
committeraravind.est <aravindhan.a@est.tech>
Mon, 22 Apr 2024 11:33:36 +0000 (12:33 +0100)
SME invoker modified to call the discovery endpoint in every 5 seconds.

Issue-ID: NONRTRIC-981
Change-Id: I05e9a56fb510bd63919d77a4f1953348ac898a6d
Signed-off-by: aravind.est <aravindhan.a@est.tech>
sample-services/hello-world-sme-invoker/Dockerfile
sample-services/hello-world-sme-invoker/README.md
sample-services/hello-world-sme-invoker/hello-world-sme-invoker-build-start.sh
sample-services/hello-world-sme-invoker/hello-world-sme-invoker/values.yaml
sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/HelloWorldSmeInvokerApplication.java
sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerComponent.java [new file with mode: 0644]
sample-services/hello-world-sme-invoker/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldSmeInvokerController.java [deleted file]

index 4885a48..05b4db2 100644 (file)
@@ -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).
 #==================================================================================
 
 
index 95822a7..be6cf1e 100644 (file)
@@ -1,6 +1,6 @@
 # Hello World Sme Invoker Service\r
 \r
-This repository contains a Spring Boot application serving few Hello World SME endpoints. \r
+This repository contains a Spring Boot application serving as Hello World SME invoker application.\r
 The application can be built and run using the provided script - ``hello-world-sme-invoker-build-start.sh``.\r
 \r
 ## Prerequisites\r
@@ -16,13 +16,7 @@ Run the script:
 \r
 The script will build a Docker image and run a container with the Hello World SME service. After the container starts,\r
 wait for a few seconds to ensure the Spring Boot application is fully initialized. Next, it will make an HTTP request to the\r
-Hello World SME endpoint and display the response:\r
-\r
-```bash\r
-  response=$(curl -s http://localhost:8080/helloworld/v1/sme)\r
-  echo "Response from the Hello World SME endpoint:"\r
-  echo "$response"\r
-```\r
+Hello World SME endpoint on the interval of 5 seconds and displays the response:\r
 \r
 To stop and remove the Docker container:\r
 \r
@@ -31,7 +25,3 @@ To stop and remove the Docker container:
   docker rm hello-world-sme-invoker\r
 ```\r
 \r
-## Additional Information\r
-\r
-- The Hello World SME endpoint is available at http://localhost:8080/helloworld/v1/sme.\r
-\r
index a20bd35..1baf2c0 100644 (file)
@@ -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"
index 8d86d80..69c367e 100644 (file)
@@ -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:
 
 
index 0a384f4..163eab9 100644 (file)
@@ -24,9 +24,11 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;\r
 import org.springframework.boot.web.client.RestTemplateBuilder;\r
 import org.springframework.context.annotation.Bean;\r
+import org.springframework.scheduling.annotation.EnableScheduling;\r
 import org.springframework.web.client.RestTemplate;\r
 \r
 @SpringBootApplication(scanBasePackages = "org.oransc.nonrtric.sample")\r
+@EnableScheduling\r
 public class HelloWorldSmeInvokerApplication {\r
     public static void main(String[] args) {\r
         SpringApplication.run(HelloWorldSmeInvokerApplication.class, args);\r
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 (file)
index 0000000..7d76b4f
--- /dev/null
@@ -0,0 +1,161 @@
+/*-\r
+ * ========================LICENSE_START=================================\r
+ * O-RAN-SC\r
+ * %%\r
+ * Copyright (C) 2024 OpenInfra Foundation Europe.\r
+ * %%\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ========================LICENSE_END===================================\r
+ */\r
+\r
+package org.oransc.nonrtric.sample.rest;\r
+\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import org.oransc.nonrtric.sample.exception.CapifAccessException;\r
+import org.oransc.nonrtric.sample.rest.response.ApiResponse;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.scheduling.annotation.Scheduled;\r
+import org.springframework.stereotype.Component;\r
+import org.springframework.web.client.RestTemplate;\r
+\r
+@Component\r
+public class HelloWorldSmeInvokerComponent {\r
+\r
+    private final RestTemplate restTemplate;\r
+    private final ObjectMapper objectMapper;\r
+    private static final Logger logger = LoggerFactory.getLogger(HelloWorldSmeInvokerComponent.class);\r
+\r
+    public HelloWorldSmeInvokerComponent(RestTemplate restTemplate, ObjectMapper objectMapper) {\r
+        this.restTemplate = restTemplate;\r
+        this.objectMapper = objectMapper;\r
+    }\r
+\r
+    @Scheduled(fixedRate = 5000)\r
+    public void accessHelloWorldByInvoker() {\r
+        String capifUrl = createCapifUrl();\r
+        if (capifUrl != null && !capifUrl.isEmpty()) {\r
+            String baseUrl = "";\r
+            ApiResponse apiResponse;\r
+            try {\r
+                apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class);\r
+                logger.info("Discovery endpoint response is {}",\r
+                        objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(apiResponse));\r
+                baseUrl = getBaseUrl(apiResponse);\r
+            } catch (IllegalArgumentException e) {\r
+                throw new CapifAccessException("Error accessing the URL :- " + capifUrl);\r
+            } catch (Exception e) {\r
+                throw new CapifAccessException("Unexpected error");\r
+            }\r
+\r
+            //TODO The below should be uncommented once SME Manager provides an accessible URI\r
+\r
+//            String helloWorldEndpoint = "";\r
+//            List<String> apiSetEndpoints = getApiSetEndpoints(apiResponse, baseUrl);\r
+//            if (apiSetEndpoints != null && !apiSetEndpoints.isEmpty()) {\r
+//                helloWorldEndpoint = apiSetEndpoints.get(0);\r
+//            }\r
+//\r
+//            if (helloWorldEndpoint != null && !helloWorldEndpoint.isEmpty()) {\r
+//                try {\r
+//                    String responseHelloWorld = restTemplate.getForObject(helloWorldEndpoint, String.class);\r
+//                    logger.info("Response :- ", responseHelloWorld);\r
+//                } catch (IllegalArgumentException e) {\r
+//                    throw new CapifAccessException("Error accessing the URL :- " + helloWorldEndpoint);\r
+//                } catch (Exception e) {\r
+//                    throw new CapifAccessException("Unexpected error");\r
+//                }\r
+//            }\r
+        }\r
+    }\r
+\r
+    private String createCapifUrl() {\r
+        String appId = System.getenv("APP_ID");\r
+        String invokerId = "";\r
+        String capifUrl = "";\r
+        if (appId != null) {\r
+            logger.info("APP_ID: " + appId);\r
+            invokerId = "api_invoker_id_" + appId;\r
+            logger.info("invokerId: " + invokerId);\r
+        } else {\r
+            logger.info("APP_ID environment variable is not set. ");\r
+        }\r
+\r
+        String smeDiscoveryEndpoint = System.getenv("SME_DISCOVERY_ENDPOINT");\r
+        if (smeDiscoveryEndpoint != null) {\r
+            logger.info("SME_DISCOVERY_ENDPOINT: " + smeDiscoveryEndpoint);\r
+            capifUrl = smeDiscoveryEndpoint + "?api-invoker-id=" + invokerId;\r
+            logger.info("capifUrl: " + capifUrl);\r
+        } else {\r
+            logger.info("SME_DISCOVERY_ENDPOINT environment variable is not set.");\r
+        }\r
+        return capifUrl;\r
+    }\r
+\r
+    private static String getBaseUrl(ApiResponse apiResponse) {\r
+        if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions()\r
+                .isEmpty()) {\r
+\r
+            ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
+\r
+            if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
+\r
+                ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
+\r
+                if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
+                    ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0);\r
+                    return "http://" + interfaceDescription.getIpv4Addr() + ":" + interfaceDescription.getPort();\r
+                }\r
+            }\r
+        }\r
+        return "";\r
+    }\r
+\r
+    private static List<String> getApiSetEndpoints(ApiResponse apiResponse, String baseUrl) {\r
+\r
+        String helloWorldEndpoint = "";\r
+        List<String> apiSetEndpoints = new ArrayList<>(5);\r
+\r
+        if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions()\r
+                .isEmpty()) {\r
+\r
+            ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
+\r
+            if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
+\r
+                ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
+\r
+                if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
+\r
+                    if (aefProfile.getVersions() != null && !aefProfile.getVersions().isEmpty()) {\r
+\r
+                        ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0);\r
+\r
+                        if (apiVersion.getResources() != null && !apiVersion.getResources().isEmpty()) {\r
+\r
+                            for (ApiResponse.Resource resource : apiVersion.getResources()) {\r
+                                helloWorldEndpoint = baseUrl + resource.getUri();\r
+                                logger.info("Complete URL for resource " + helloWorldEndpoint);\r
+                                apiSetEndpoints.add(helloWorldEndpoint);\r
+                            }\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+        }\r
+        return apiSetEndpoints;\r
+    }\r
+\r
+}\r
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 (file)
index 5782092..0000000
+++ /dev/null
@@ -1,203 +0,0 @@
-/*-\r
- * ========================LICENSE_START=================================\r
- * O-RAN-SC\r
- * %%\r
- * Copyright (C) 2024 OpenInfra Foundation Europe.\r
- * %%\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ========================LICENSE_END===================================\r
- */\r
-\r
-package org.oransc.nonrtric.sample.rest;\r
-\r
-import jakarta.servlet.http.HttpServletRequest;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import org.oransc.nonrtric.sample.exception.CapifAccessException;\r
-import org.oransc.nonrtric.sample.rest.response.ApiResponse;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-import org.springframework.http.ResponseEntity;\r
-import org.springframework.web.bind.annotation.RequestMapping;\r
-import org.springframework.web.bind.annotation.RestController;\r
-import org.springframework.web.client.RestTemplate;\r
-\r
-@RestController\r
-public class HelloWorldSmeInvokerController {\r
-\r
-    private final RestTemplate restTemplate;\r
-    private static final Logger logger = LoggerFactory.getLogger(HelloWorldSmeInvokerController.class);\r
-\r
-    public HelloWorldSmeInvokerController(RestTemplate restTemplate) {\r
-        this.restTemplate = restTemplate;\r
-    }\r
-\r
-    @RequestMapping("/helloworld/v1/sme")\r
-    public String helloWorld(HttpServletRequest request) {\r
-        String path = logRequestPath(request);\r
-        return "Hello from " + path;\r
-    }\r
-\r
-    @RequestMapping("/helloworld/sme/invoker/v1/apiset")\r
-    public ResponseEntity<ApiResponse> helloWorldSmeInvoker(HttpServletRequest request) {\r
-        String path = logRequestPath(request);\r
-        String capifUrl = createCapifUrl();\r
-        try {\r
-            ApiResponse apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class);\r
-            return ResponseEntity.ok(apiResponse);\r
-        } catch (IllegalArgumentException e) {\r
-            throw new CapifAccessException("Error accessing the URL :- "+capifUrl);\r
-        } catch (Exception e) {\r
-            throw new CapifAccessException("Unexpected error");\r
-        }\r
-    }\r
-\r
-    @RequestMapping("/helloworld/sme/invoker/v1")\r
-    public ResponseEntity<String> accessHelloWorldByInvoker(HttpServletRequest request) {\r
-        String path = logRequestPath(request);\r
-        String capifUrl = createCapifUrl();\r
-        String baseUrl = "";\r
-        ApiResponse apiResponse = new ApiResponse();\r
-        try {\r
-            apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class);\r
-            baseUrl = getBaseUrl(apiResponse);\r
-        } catch (IllegalArgumentException e) {\r
-            throw new CapifAccessException("Error accessing the URL :- "+capifUrl);\r
-        } catch (Exception e) {\r
-            throw new CapifAccessException("Unexpected error");\r
-        }\r
-\r
-        String helloWorldEndpoint = "";\r
-        List<String> apiSetEndpoints = getApiSetEndpoints(apiResponse, baseUrl);\r
-        if(apiSetEndpoints != null && !apiSetEndpoints.isEmpty()){\r
-            helloWorldEndpoint = apiSetEndpoints.get(0);\r
-        }\r
-\r
-        try {\r
-            String responseHelloWorld = restTemplate.getForObject(helloWorldEndpoint, String.class);\r
-            logger.info("Response :- ", responseHelloWorld);\r
-            return ResponseEntity.ok(responseHelloWorld);\r
-        } catch (IllegalArgumentException e) {\r
-            throw new CapifAccessException("Error accessing the URL :- "+helloWorldEndpoint);\r
-        } catch (Exception e) {\r
-            throw new CapifAccessException("Unexpected error");\r
-        }\r
-    }\r
-\r
-    private String logRequestPath(HttpServletRequest request) {\r
-        String path = request.getRequestURI();\r
-        logger.info("Received request for path: {}", path);\r
-        return path;\r
-    }\r
-\r
-    private String createCapifUrl() {\r
-        String appId = System.getenv("APP_ID");\r
-        if (appId != null) {\r
-            logger.info("APP_ID: " + appId);\r
-        } else {\r
-            logger.info("APP_ID environment variable is not set. ");\r
-            appId = "Invoker_App_1";\r
-            logger.info("APP_ID default value :- " + appId);\r
-        }\r
-\r
-        String smeDiscoveryEndpoint = System.getenv("SME_DISCOVERY_ENDPOINT");\r
-        if (smeDiscoveryEndpoint != null) {\r
-            logger.info("SME_DISCOVERY_ENDPOINT: " + smeDiscoveryEndpoint);\r
-        } else {\r
-            logger.info("SME_DISCOVERY_ENDPOINT environment variable is not set.");\r
-            smeDiscoveryEndpoint = "capifcore.nonrtric.svc.cluster.local:8090/service-apis/v1/allServiceAPIs";\r
-            logger.info("SME_DISCOVERY_ENDPOINT default value :- " + smeDiscoveryEndpoint);\r
-        }\r
-\r
-        String invokerId = "api_invoker_id_Invoker_App_1";\r
-        if (appId != null) {\r
-            invokerId = "api_invoker_id_" + appId;\r
-        }\r
-        logger.info("invokerId: " + invokerId);\r
-\r
-        String capifUrl =\r
-            "http://capifcore.nonrtric.svc.cluster.local:8090/service-apis/v1/allServiceAPIs" + "?api-invoker-id=" +\r
-                invokerId;\r
-        if (smeDiscoveryEndpoint != null) {\r
-            capifUrl = smeDiscoveryEndpoint + "?api-invoker-id=" + invokerId;\r
-        }\r
-        logger.info("capifUrl: " + capifUrl);\r
-\r
-        return capifUrl;\r
-    }\r
-\r
-    private static String getBaseUrl(ApiResponse apiResponse) {\r
-        if (apiResponse != null &&\r
-            apiResponse.getServiceAPIDescriptions() != null &&\r
-            !apiResponse.getServiceAPIDescriptions().isEmpty()) {\r
-\r
-            ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
-\r
-            if (serviceAPIDescription.getAefProfiles() != null &&\r
-                !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
-\r
-                ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
-\r
-                if (aefProfile.getInterfaceDescriptions() != null &&\r
-                    !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
-                    ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0);\r
-                    return "http://" + interfaceDescription.getIpv4Addr() + ":" + interfaceDescription.getPort();\r
-                }\r
-            }\r
-        }\r
-        return "";\r
-    }\r
-\r
-    private static List<String> getApiSetEndpoints(ApiResponse apiResponse, String baseUrl){\r
-\r
-        String helloWorldEndpoint = "";\r
-        List<String> apiSetEndpoints = new ArrayList<>(5);\r
-\r
-        if (apiResponse != null &&\r
-            apiResponse.getServiceAPIDescriptions() != null &&\r
-            !apiResponse.getServiceAPIDescriptions().isEmpty()) {\r
-\r
-            ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
-\r
-            if (serviceAPIDescription.getAefProfiles() != null &&\r
-                !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
-\r
-                ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
-\r
-                if (aefProfile.getInterfaceDescriptions() != null &&\r
-                    !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
-\r
-                    ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0);\r
-\r
-                    if (aefProfile.getVersions() != null &&\r
-                        !aefProfile.getVersions().isEmpty()) {\r
-\r
-                        ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0);\r
-\r
-                        if (apiVersion.getResources() != null &&\r
-                            !apiVersion.getResources().isEmpty()) {\r
-\r
-                            for(ApiResponse.Resource resource : apiVersion.getResources()) {\r
-                                helloWorldEndpoint = baseUrl + resource.getUri();\r
-                                logger.info("Complete URL for resource " + helloWorldEndpoint);\r
-                                apiSetEndpoints.add(helloWorldEndpoint);\r
-                            }\r
-                        }\r
-                    }\r
-                }\r
-            }\r
-        }\r
-        return apiSetEndpoints;\r
-    }\r
-\r
-}\r