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 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
35 public class HelloWorldSmeInvokerComponent {
\r
37 private final RestTemplate restTemplate;
\r
38 private final ObjectMapper objectMapper;
\r
39 private static final Logger logger = LoggerFactory.getLogger(HelloWorldSmeInvokerComponent.class);
\r
41 public HelloWorldSmeInvokerComponent(RestTemplate restTemplate, ObjectMapper objectMapper) {
\r
42 this.restTemplate = restTemplate;
\r
43 this.objectMapper = objectMapper;
\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
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
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
69 if (helloWorldEndpoint != null && !helloWorldEndpoint.isEmpty()) {
\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
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
91 logger.info("APP_ID environment variable is not set. ");
\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
100 logger.info("SME_DISCOVERY_ENDPOINT environment variable is not set.");
\r
105 private static String getBaseUrl(ApiResponse apiResponse) {
\r
106 if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions()
\r
109 ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);
\r
111 if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) {
\r
113 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);
\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
124 private static List<String> getApiSetEndpoints(ApiResponse apiResponse, String baseUrl) {
\r
126 String helloWorldEndpoint = "";
\r
127 List<String> apiSetEndpoints = new ArrayList<>(5);
\r
129 if (apiResponse != null && apiResponse.getServiceAPIDescriptions() != null && !apiResponse.getServiceAPIDescriptions()
\r
132 ApiResponse.ServiceAPIDescription serviceAPIDescription = apiResponse.getServiceAPIDescriptions().get(0);
\r
134 if (serviceAPIDescription.getAefProfiles() != null && !serviceAPIDescription.getAefProfiles().isEmpty()) {
\r
136 ApiResponse.AefProfile aefProfile = serviceAPIDescription.getAefProfiles().get(0);
\r
138 if (aefProfile.getInterfaceDescriptions() != null && !aefProfile.getInterfaceDescriptions().isEmpty()) {
\r
140 if (aefProfile.getVersions() != null && !aefProfile.getVersions().isEmpty()) {
\r
142 ApiResponse.ApiVersion apiVersion = aefProfile.getVersions().get(0);
\r
144 if (apiVersion.getResources() != null && !apiVersion.getResources().isEmpty()) {
\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
156 return apiSetEndpoints;
\r