f06599857264c2e84e0466a9060eacb39e885dca
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Modifications Copyright (C) 2025 OpenInfra Foundation Europe
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.oran.smo.ncmp_to_teiv_adapter;
21
22 import com.fasterxml.jackson.core.type.TypeReference;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import lombok.extern.slf4j.Slf4j;
25 import okhttp3.HttpUrl;
26 import okhttp3.MediaType;
27 import okhttp3.OkHttpClient;
28 import okhttp3.Request;
29 import okhttp3.RequestBody;
30 import okhttp3.Response;
31 import org.oran.smo.ncmp_to_teiv_adapter.models.ManagedElementWrapper;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.stereotype.Component;
34
35 import java.util.List;
36
37 @Slf4j
38 @Component
39 public class NcmpPollingClient {
40
41     @Value("${polling.base-url}")
42     private String baseUrl;
43
44     @Value("${polling.searches-url}")
45     private String searchesUrl;
46
47     @Value("${polling.data-store-url}")
48     private String dataStoreUrl;
49
50     @Value("${polling.include-descendants}")
51     private String includeDescendants;
52
53     private static final OkHttpClient client = new OkHttpClient();
54     private static final ObjectMapper objectMapper = new ObjectMapper();
55
56     public List<String> getAllCmHandlesFromNcmp() {
57         try {
58             HttpUrl url = HttpUrl.parse(baseUrl + searchesUrl).newBuilder().build();
59             String requestBody = "{\"conditions\": [{\"name\": \"hasAllModules\"}]}";
60             RequestBody body = RequestBody.create(requestBody, MediaType.parse("application/json"));
61
62             Request request = new Request.Builder().url(url).post(body).addHeader("Content-Type", "application/json")
63                     .build();
64             log.info("Polling {}", url);
65             try (Response response = client.newCall(request).execute()) {
66                 if (response.isSuccessful()) {
67                     String responseBody = response.body().string();
68                     List<String> cmHandles = objectMapper.readValue(responseBody, new TypeReference<>() {
69                     });
70                     log.info("Parsed List: {}", cmHandles);
71                     return cmHandles;
72                 } else {
73                     log.error("Request failed for: {}, {}", url, response.code());
74                 }
75             }
76         } catch (Exception e) {
77             log.error("Error polling API: {}", e.getMessage());
78         }
79         return List.of();
80     }
81
82     public ManagedElementWrapper getAllManagedElementsFromNcmp(String cmHandle) {
83         try {
84             HttpUrl url = HttpUrl.parse(baseUrl + "/" + cmHandle + dataStoreUrl).newBuilder().addQueryParameter(
85                     "resourceIdentifier", "/").addQueryParameter("include-descendants", includeDescendants)
86                     .addQueryParameter("options", "(fields=_3gpp-common-managed-element:ManagedElement)").build();
87             Request request = new Request.Builder().url(url).get().addHeader("Content-Type", "application/json").build();
88             try (Response response = client.newCall(request).execute()) {
89                 if (response.isSuccessful()) {
90                     String responseBody = response.body().string();
91                     log.info("Raw Response for: {}, {}", url, responseBody);
92                     return objectMapper.readValue(responseBody, ManagedElementWrapper.class);
93                 } else {
94                     log.error("Request failed for: {}, {}", url, response.code());
95                 }
96             }
97         } catch (Exception e) {
98             log.error("Error polling API: {}", e.getMessage());
99         }
100         return new ManagedElementWrapper();
101     }
102 }