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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.oran.smo.ncmp_to_teiv_adapter;
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;
35 import java.util.List;
39 public class NcmpPollingClient {
41 @Value("${polling.base-url}")
42 private String baseUrl;
44 @Value("${polling.searches-url}")
45 private String searchesUrl;
47 @Value("${polling.data-store-url}")
48 private String dataStoreUrl;
50 @Value("${polling.include-descendants}")
51 private String includeDescendants;
53 private static final OkHttpClient client = new OkHttpClient();
54 private static final ObjectMapper objectMapper = new ObjectMapper();
56 public List<String> getAllCmHandlesFromNcmp() {
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"));
62 Request request = new Request.Builder().url(url).post(body).addHeader("Content-Type", "application/json")
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<>() {
70 log.info("Parsed List: {}", cmHandles);
73 log.error("Request failed for: {}, {}", url, response.code());
76 } catch (Exception e) {
77 log.error("Error polling API: {}", e.getMessage());
82 public ManagedElementWrapper getAllManagedElementsFromNcmp(String cmHandle) {
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);
94 log.error("Request failed for: {}, {}", url, response.code());
97 } catch (Exception e) {
98 log.error("Error polling API: {}", e.getMessage());
100 return new ManagedElementWrapper();