Hello World SME Invoker Service
[nonrtric.git] / 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/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
new file mode 100644 (file)
index 0000000..5782092
--- /dev/null
@@ -0,0 +1,203 @@
+/*-\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