7d76b4f97e14043ffc17eba6dc11989f16f7d811
[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             //TODO The below should be uncommented once SME Manager provides an accessible URI\r
64 \r
65 //            String helloWorldEndpoint = "";\r
66 //            List<String> apiSetEndpoints = getApiSetEndpoints(apiResponse, baseUrl);\r
67 //            if (apiSetEndpoints != null && !apiSetEndpoints.isEmpty()) {\r
68 //                helloWorldEndpoint = apiSetEndpoints.get(0);\r
69 //            }\r
70 //\r
71 //            if (helloWorldEndpoint != null && !helloWorldEndpoint.isEmpty()) {\r
72 //                try {\r
73 //                    String responseHelloWorld = restTemplate.getForObject(helloWorldEndpoint, String.class);\r
74 //                    logger.info("Response :- ", responseHelloWorld);\r
75 //                } catch (IllegalArgumentException e) {\r
76 //                    throw new CapifAccessException("Error accessing the URL :- " + helloWorldEndpoint);\r
77 //                } catch (Exception e) {\r
78 //                    throw new CapifAccessException("Unexpected error");\r
79 //                }\r
80 //            }\r
81         }\r
82     }\r
83 \r
84     private String createCapifUrl() {\r
85         String appId = System.getenv("APP_ID");\r
86         String invokerId = "";\r
87         String capifUrl = "";\r
88         if (appId != null) {\r
89             logger.info("APP_ID: " + appId);\r
90             invokerId = "api_invoker_id_" + appId;\r
91             logger.info("invokerId: " + invokerId);\r
92         } else {\r
93             logger.info("APP_ID environment variable is not set. ");\r
94         }\r
95 \r
96         String smeDiscoveryEndpoint = System.getenv("SME_DISCOVERY_ENDPOINT");\r
97         if (smeDiscoveryEndpoint != null) {\r
98             logger.info("SME_DISCOVERY_ENDPOINT: " + smeDiscoveryEndpoint);\r
99             capifUrl = smeDiscoveryEndpoint + "?api-invoker-id=" + invokerId;\r
100             logger.info("capifUrl: " + capifUrl);\r
101         } else {\r
102             logger.info("SME_DISCOVERY_ENDPOINT environment variable is not set.");\r
103         }\r
104         return capifUrl;\r
105     }\r
106 \r
107     private static String getBaseUrl(ApiResponse apiResponse) {\r
108         if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions()\r
109                 .isEmpty()) {\r
110 \r
111             ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
112 \r
113             if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
114 \r
115                 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
116 \r
117                 if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
118                     ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0);\r
119                     return "http://" + interfaceDescription.getIpv4Addr() + ":" + interfaceDescription.getPort();\r
120                 }\r
121             }\r
122         }\r
123         return "";\r
124     }\r
125 \r
126     private static List<String> getApiSetEndpoints(ApiResponse apiResponse, String baseUrl) {\r
127 \r
128         String helloWorldEndpoint = "";\r
129         List<String> apiSetEndpoints = new ArrayList<>(5);\r
130 \r
131         if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions()\r
132                 .isEmpty()) {\r
133 \r
134             ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);\r
135 \r
136             if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) {\r
137 \r
138                 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);\r
139 \r
140                 if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) {\r
141 \r
142                     if (aefProfile.getVersions() != null && !aefProfile.getVersions().isEmpty()) {\r
143 \r
144                         ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0);\r
145 \r
146                         if (apiVersion.getResources() != null && !apiVersion.getResources().isEmpty()) {\r
147 \r
148                             for (ApiResponse.Resource resource : apiVersion.getResources()) {\r
149                                 helloWorldEndpoint = baseUrl + resource.getUri();\r
150                                 logger.info("Complete URL for resource " + helloWorldEndpoint);\r
151                                 apiSetEndpoints.add(helloWorldEndpoint);\r
152                             }\r
153                         }\r
154                     }\r
155                 }\r
156             }\r
157         }\r
158         return apiSetEndpoints;\r
159     }\r
160 \r
161 }\r