5782092713699f601e517f18756872c7cf217c69
[nonrtric.git] / sample-services / hello-world-sme-invoker / src / main / java / org / oransc / nonrtric / sample / rest / HelloWorldSmeInvokerController.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 jakarta.servlet.http.HttpServletRequest;\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.http.ResponseEntity;\r
31 import org.springframework.web.bind.annotation.RequestMapping;\r
32 import org.springframework.web.bind.annotation.RestController;\r
33 import org.springframework.web.client.RestTemplate;\r
34 \r
35 @RestController\r
36 public class HelloWorldSmeInvokerController {\r
37 \r
38     private final RestTemplate restTemplate;\r
39     private static final Logger logger = LoggerFactory.getLogger(HelloWorldSmeInvokerController.class);\r
40 \r
41     public HelloWorldSmeInvokerController(RestTemplate restTemplate) {\r
42         this.restTemplate = restTemplate;\r
43     }\r
44 \r
45     @RequestMapping("/helloworld/v1/sme")\r
46     public String helloWorld(HttpServletRequest request) {\r
47         String path = logRequestPath(request);\r
48         return "Hello from " + path;\r
49     }\r
50 \r
51     @RequestMapping("/helloworld/sme/invoker/v1/apiset")\r
52     public ResponseEntity<ApiResponse> helloWorldSmeInvoker(HttpServletRequest request) {\r
53         String path = logRequestPath(request);\r
54         String capifUrl = createCapifUrl();\r
55         try {\r
56             ApiResponse apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class);\r
57             return ResponseEntity.ok(apiResponse);\r
58         } catch (IllegalArgumentException e) {\r
59             throw new CapifAccessException("Error accessing the URL :- "+capifUrl);\r
60         } catch (Exception e) {\r
61             throw new CapifAccessException("Unexpected error");\r
62         }\r
63     }\r
64 \r
65     @RequestMapping("/helloworld/sme/invoker/v1")\r
66     public ResponseEntity<String> accessHelloWorldByInvoker(HttpServletRequest request) {\r
67         String path = logRequestPath(request);\r
68         String capifUrl = createCapifUrl();\r
69         String baseUrl = "";\r
70         ApiResponse apiResponse = new ApiResponse();\r
71         try {\r
72             apiResponse = restTemplate.getForObject(capifUrl, ApiResponse.class);\r
73             baseUrl = getBaseUrl(apiResponse);\r
74         } catch (IllegalArgumentException e) {\r
75             throw new CapifAccessException("Error accessing the URL :- "+capifUrl);\r
76         } catch (Exception e) {\r
77             throw new CapifAccessException("Unexpected error");\r
78         }\r
79 \r
80         String helloWorldEndpoint = "";\r
81         List<String> apiSetEndpoints = getApiSetEndpoints(apiResponse, baseUrl);\r
82         if(apiSetEndpoints != null && !apiSetEndpoints.isEmpty()){\r
83             helloWorldEndpoint = apiSetEndpoints.get(0);\r
84         }\r
85 \r
86         try {\r
87             String responseHelloWorld = restTemplate.getForObject(helloWorldEndpoint, String.class);\r
88             logger.info("Response :- ", responseHelloWorld);\r
89             return ResponseEntity.ok(responseHelloWorld);\r
90         } catch (IllegalArgumentException e) {\r
91             throw new CapifAccessException("Error accessing the URL :- "+helloWorldEndpoint);\r
92         } catch (Exception e) {\r
93             throw new CapifAccessException("Unexpected error");\r
94         }\r
95     }\r
96 \r
97     private String logRequestPath(HttpServletRequest request) {\r
98         String path = request.getRequestURI();\r
99         logger.info("Received request for path: {}", path);\r
100         return path;\r
101     }\r
102 \r
103     private String createCapifUrl() {\r
104         String appId = System.getenv("APP_ID");\r
105         if (appId != null) {\r
106             logger.info("APP_ID: " + appId);\r
107         } else {\r
108             logger.info("APP_ID environment variable is not set. ");\r
109             appId = "Invoker_App_1";\r
110             logger.info("APP_ID default value :- " + appId);\r
111         }\r
112 \r
113         String smeDiscoveryEndpoint = System.getenv("SME_DISCOVERY_ENDPOINT");\r
114         if (smeDiscoveryEndpoint != null) {\r
115             logger.info("SME_DISCOVERY_ENDPOINT: " + smeDiscoveryEndpoint);\r
116         } else {\r
117             logger.info("SME_DISCOVERY_ENDPOINT environment variable is not set.");\r
118             smeDiscoveryEndpoint = "capifcore.nonrtric.svc.cluster.local:8090/service-apis/v1/allServiceAPIs";\r
119             logger.info("SME_DISCOVERY_ENDPOINT default value :- " + smeDiscoveryEndpoint);\r
120         }\r
121 \r
122         String invokerId = "api_invoker_id_Invoker_App_1";\r
123         if (appId != null) {\r
124             invokerId = "api_invoker_id_" + appId;\r
125         }\r
126         logger.info("invokerId: " + invokerId);\r
127 \r
128         String capifUrl =\r
129             "http://capifcore.nonrtric.svc.cluster.local:8090/service-apis/v1/allServiceAPIs" + "?api-invoker-id=" +\r
130                 invokerId;\r
131         if (smeDiscoveryEndpoint != null) {\r
132             capifUrl = smeDiscoveryEndpoint + "?api-invoker-id=" + invokerId;\r
133         }\r
134         logger.info("capifUrl: " + capifUrl);\r
135 \r
136         return capifUrl;\r
137     }\r
138 \r
139     private static String getBaseUrl(ApiResponse apiResponse) {\r
140         if (apiResponse != null &&\r
141             apiResponse.getServiceAPIDescriptions() != null &&\r
142             !apiResponse.getServiceAPIDescriptions().isEmpty()) {\r
143 \r
144             ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
145 \r
146             if (serviceAPIDescription.getAefProfiles() != null &&\r
147                 !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
148 \r
149                 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
150 \r
151                 if (aefProfile.getInterfaceDescriptions() != null &&\r
152                     !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
153                     ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0);\r
154                     return "http://" + interfaceDescription.getIpv4Addr() + ":" + interfaceDescription.getPort();\r
155                 }\r
156             }\r
157         }\r
158         return "";\r
159     }\r
160 \r
161     private static List<String> getApiSetEndpoints(ApiResponse apiResponse, String baseUrl){\r
162 \r
163         String helloWorldEndpoint = "";\r
164         List<String> apiSetEndpoints = new ArrayList<>(5);\r
165 \r
166         if (apiResponse != null &&\r
167             apiResponse.getServiceAPIDescriptions() != null &&\r
168             !apiResponse.getServiceAPIDescriptions().isEmpty()) {\r
169 \r
170             ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
171 \r
172             if (serviceAPIDescription.getAefProfiles() != null &&\r
173                 !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
174 \r
175                 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
176 \r
177                 if (aefProfile.getInterfaceDescriptions() != null &&\r
178                     !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
179 \r
180                     ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0);\r
181 \r
182                     if (aefProfile.getVersions() != null &&\r
183                         !aefProfile.getVersions().isEmpty()) {\r
184 \r
185                         ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0);\r
186 \r
187                         if (apiVersion.getResources() != null &&\r
188                             !apiVersion.getResources().isEmpty()) {\r
189 \r
190                             for(ApiResponse.Resource resource : apiVersion.getResources()) {\r
191                                 helloWorldEndpoint = baseUrl + resource.getUri();\r
192                                 logger.info("Complete URL for resource " + helloWorldEndpoint);\r
193                                 apiSetEndpoints.add(helloWorldEndpoint);\r
194                             }\r
195                         }\r
196                     }\r
197                 }\r
198             }\r
199         }\r
200         return apiSetEndpoints;\r
201     }\r
202 \r
203 }\r