Modify sme invoker to discover the endpoints by itself
[nonrtric.git] / sample-services / hello-world-sme-invoker / src / main / java / org / oransc / nonrtric / sample / rest / HelloWorldSmeInvokerComponent.java
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