Sample producer consumer to get kafka broker from ICS
[nonrtric.git] / sample-services / hello-world-sme-invoker / src / main / java / org / oransc / nonrtric / sample / rest / HelloWorldSmeInvokerComponent.java
1 /*-\r
2  * ========================LICENSE_START=================================\r
3  * O-RAN-SC\r
4  * %%\r
5  * Copyright (C) 2024 OpenInfra Foundation Europe.\r
6  * %%\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ========================LICENSE_END===================================\r
19  */\r
20 \r
21 package org.oransc.nonrtric.sample.rest;\r
22 \r
23 import com.fasterxml.jackson.databind.ObjectMapper;\r
24 import java.util.ArrayList;\r
25 import java.util.List;\r
26 import org.oransc.nonrtric.sample.exception.CapifAccessException;\r
27 import org.oransc.nonrtric.sample.rest.response.ApiResponse;\r
28 import org.slf4j.Logger;\r
29 import org.slf4j.LoggerFactory;\r
30 import org.springframework.scheduling.annotation.Scheduled;\r
31 import org.springframework.stereotype.Component;\r
32 import org.springframework.web.client.RestTemplate;\r
33 \r
34 @Component\r
35 public class HelloWorldSmeInvokerComponent {\r
36 \r
37     private final RestTemplate restTemplate;\r
38     private final ObjectMapper objectMapper;\r
39     private static final Logger logger = LoggerFactory.getLogger(HelloWorldSmeInvokerComponent.class);\r
40 \r
41     public HelloWorldSmeInvokerComponent(RestTemplate restTemplate, ObjectMapper objectMapper) {\r
42         this.restTemplate = restTemplate;\r
43         this.objectMapper = objectMapper;\r
44     }\r
45 \r
46     @Scheduled(fixedRate = 5000)\r
47     public void accessHelloWorldByInvoker() {\r
48         String capifUrl = createCapifUrl();\r
49         if (capifUrl != null && !capifUrl.isEmpty()) {\r
50             String baseUrl = "";\r
51             ApiResponse apiResponse;\r
52             try {\r
53                 apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class);\r
54                 logger.info("Discovery endpoint response is {}",\r
55                         objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(apiResponse));\r
56                 baseUrl = getBaseUrl(apiResponse);\r
57             } catch (IllegalArgumentException e) {\r
58                 throw new CapifAccessException("Error accessing the URL :- " + capifUrl);\r
59             } catch (Exception e) {\r
60                 throw new CapifAccessException("Unexpected error");\r
61             }\r
62 \r
63             String helloWorldEndpoint = "";\r
64             List<String> apiSetEndpoints = getApiSetEndpoints(apiResponse, baseUrl);\r
65             if (apiSetEndpoints != null && !apiSetEndpoints.isEmpty()) {\r
66                 helloWorldEndpoint = apiSetEndpoints.get(0);\r
67             }\r
68 \r
69             if (helloWorldEndpoint != null && !helloWorldEndpoint.isEmpty()) {\r
70                 try {\r
71                     String responseHelloWorld = restTemplate.getForObject(helloWorldEndpoint, String.class);\r
72                     logger.info("rApp SME Provider Response : {}", responseHelloWorld);\r
73                 } catch (IllegalArgumentException e) {\r
74                     throw new CapifAccessException("Error accessing the URL :- " + helloWorldEndpoint);\r
75                 } catch (Exception e) {\r
76                     throw new CapifAccessException("Unexpected error");\r
77                 }\r
78             }\r
79         }\r
80     }\r
81 \r
82     private String createCapifUrl() {\r
83         String appId = System.getenv("APP_ID");\r
84         String invokerId = "";\r
85         String capifUrl = "";\r
86         if (appId != null) {\r
87             logger.info("APP_ID: " + appId);\r
88             invokerId = "api_invoker_id_" + appId;\r
89             logger.info("invokerId: " + invokerId);\r
90         } else {\r
91             logger.info("APP_ID environment variable is not set. ");\r
92         }\r
93 \r
94         String smeDiscoveryEndpoint = System.getenv("SME_DISCOVERY_ENDPOINT");\r
95         if (smeDiscoveryEndpoint != null) {\r
96             logger.info("SME_DISCOVERY_ENDPOINT: " + smeDiscoveryEndpoint);\r
97             capifUrl = smeDiscoveryEndpoint + "?api-invoker-id=" + invokerId;\r
98             logger.info("capifUrl: " + capifUrl);\r
99         } else {\r
100             logger.info("SME_DISCOVERY_ENDPOINT environment variable is not set.");\r
101         }\r
102         return capifUrl;\r
103     }\r
104 \r
105     private static String getBaseUrl(ApiResponse apiResponse) {\r
106         if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions()\r
107                 .isEmpty()) {\r
108 \r
109             ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
110 \r
111             if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
112 \r
113                 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
114 \r
115                 if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
116                     ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0);\r
117                     return "http://" + interfaceDescription.getIpv4Addr() + ":" + interfaceDescription.getPort();\r
118                 }\r
119             }\r
120         }\r
121         return "";\r
122     }\r
123 \r
124     private static List<String> getApiSetEndpoints(ApiResponse apiResponse, String baseUrl) {\r
125 \r
126         String helloWorldEndpoint = "";\r
127         List<String> apiSetEndpoints = new ArrayList<>(5);\r
128 \r
129         if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions()\r
130                 .isEmpty()) {\r
131 \r
132             ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
133 \r
134             if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
135 \r
136                 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
137 \r
138                 if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
139 \r
140                     if (aefProfile.getVersions() != null && !aefProfile.getVersions().isEmpty()) {\r
141 \r
142                         ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0);\r
143 \r
144                         if (apiVersion.getResources() != null && !apiVersion.getResources().isEmpty()) {\r
145 \r
146                             for (ApiResponse.Resource resource : apiVersion.getResources()) {\r
147                                 helloWorldEndpoint = baseUrl + resource.getUri();\r
148                                 logger.info("Complete URL for resource " + helloWorldEndpoint);\r
149                                 apiSetEndpoints.add(helloWorldEndpoint);\r
150                             }\r
151                         }\r
152                     }\r
153                 }\r
154             }\r
155         }\r
156         return apiSetEndpoints;\r
157     }\r
158 \r
159 }\r