2 * ========================LICENSE_START=================================
\r
5 * Copyright (C) 2024 OpenInfra Foundation Europe.
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.oransc.nonrtric.sample.rest;
\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
36 public class HelloWorldSmeInvokerController {
\r
38 private final RestTemplate restTemplate;
\r
39 private static final Logger logger = LoggerFactory.getLogger(HelloWorldSmeInvokerController.class);
\r
41 public HelloWorldSmeInvokerController(RestTemplate restTemplate) {
\r
42 this.restTemplate = restTemplate;
\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
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
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
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
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
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
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
97 private String logRequestPath(HttpServletRequest request) {
\r
98 String path = request.getRequestURI();
\r
99 logger.info("Received request for path: {}", path);
\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
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
113 String smeDiscoveryEndpoint = System.getenv("SME_DISCOVERY_ENDPOINT");
\r
114 if (smeDiscoveryEndpoint != null) {
\r
115 logger.info("SME_DISCOVERY_ENDPOINT: " + smeDiscoveryEndpoint);
\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
122 String invokerId = "api_invoker_id_Invoker_App_1";
\r
123 if (appId != null) {
\r
124 invokerId = "api_invoker_id_" + appId;
\r
126 logger.info("invokerId: " + invokerId);
\r
129 "http://capifcore.nonrtric.svc.cluster.local:8090/service-apis/v1/allServiceAPIs" + "?api-invoker-id=" +
\r
131 if (smeDiscoveryEndpoint != null) {
\r
132 capifUrl = smeDiscoveryEndpoint + "?api-invoker-id=" + invokerId;
\r
134 logger.info("capifUrl: " + capifUrl);
\r
139 private static String getBaseUrl(ApiResponse apiResponse) {
\r
140 if (apiResponse != null &&
\r
141 apiResponse.getServiceAPIDescriptions() != null &&
\r
142 !apiResponse.getServiceAPIDescriptions().isEmpty()) {
\r
144 ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);
\r
146 if (serviceAPIDescription.getAefProfiles() != null &&
\r
147 !serviceAPIDescription.getAefProfiles().isEmpty()) {
\r
149 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);
\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
161 private static List<String> getApiSetEndpoints(ApiResponse apiResponse, String baseUrl){
\r
163 String helloWorldEndpoint = "";
\r
164 List<String> apiSetEndpoints = new ArrayList<>(5);
\r
166 if (apiResponse != null &&
\r
167 apiResponse.getServiceAPIDescriptions() != null &&
\r
168 !apiResponse.getServiceAPIDescriptions().isEmpty()) {
\r
170 ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);
\r
172 if (serviceAPIDescription.getAefProfiles() != null &&
\r
173 !serviceAPIDescription.getAefProfiles().isEmpty()) {
\r
175 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);
\r
177 if (aefProfile.getInterfaceDescriptions() != null &&
\r
178 !aefProfile.getInterfaceDescriptions().isEmpty()) {
\r
180 ApiResponse.InterfaceDescription interfaceDescription = aefProfile.getInterfaceDescriptions().get(0);
\r
182 if (aefProfile.getVersions() != null &&
\r
183 !aefProfile.getVersions().isEmpty()) {
\r
185 ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0);
\r
187 if (apiVersion.getResources() != null &&
\r
188 !apiVersion.getResources().isEmpty()) {
\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
200 return apiSetEndpoints;
\r