2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Ericsson
4 * Modifications Copyright (C) 2024-2025 OpenInfra Foundation Europe
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.teiv.exposure.classifiers.rest.controller;
23 import java.util.Collections;
24 import java.util.Optional;
25 import java.util.function.Consumer;
26 import java.util.function.Supplier;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import io.micrometer.core.annotation.Timed;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
32 import jakarta.servlet.http.HttpServletRequest;
34 import org.oran.smo.teiv.CustomMetrics;
35 import org.oran.smo.teiv.api.ClassifiersApi;
36 import org.oran.smo.teiv.api.model.OranTeivClassifier;
37 import org.oran.smo.teiv.exception.TeivException;
38 import org.oran.smo.teiv.exposure.audit.AuditMapper;
39 import org.oran.smo.teiv.exposure.audit.LoggerHandler;
40 import org.oran.smo.teiv.exposure.classifiers.api.ClassifiersService;
41 import org.oran.smo.teiv.utils.TeivConstants;
42 import org.springframework.beans.factory.annotation.Value;
43 import org.springframework.boot.web.servlet.FilterRegistrationBean;
44 import org.springframework.context.annotation.Bean;
45 import org.springframework.context.annotation.Profile;
46 import org.springframework.core.Ordered;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RestController;
52 import static org.oran.smo.teiv.utils.TeivConstants.REQUEST_MAPPING;
56 @RequestMapping(TeivConstants.REQUEST_MAPPING)
57 @RequiredArgsConstructor
59 public class ClassifiersRestController implements ClassifiersApi {
61 private final ClassifiersService classifiersService;
62 private final CustomMetrics customMetrics;
63 private final LoggerHandler loggerHandler;
64 private final HttpServletRequest context;
66 @Value("${consumer-data.batch-size}")
70 @Timed("teiv_exposure_http_update_classifiers_seconds")
71 public ResponseEntity<Void> updateClassifier(final String accept, final String contentType,
72 final OranTeivClassifier oranTeivClassifier) {
73 return runWithFailCheck(() -> {
74 if (Optional.ofNullable(oranTeivClassifier.getEntityIds()).orElseGet(Collections::emptyList).size() + Optional
75 .ofNullable(oranTeivClassifier.getRelationshipIds()).orElseGet(Collections::emptyList).size() > limit) {
76 throw TeivException.clientException("Limit exceeded",
77 "Number of entities and relationships exceeded the limit");
79 runSafeMethod(() -> classifiersService.update(oranTeivClassifier), status -> loggerHandler.logAudit(log,
80 AuditMapper.fromClassifiersRequest(oranTeivClassifier, status).toString(), context));
82 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
83 }, customMetrics::incrementNumUnsuccessfullyUpdatedClassifiers);
86 private <T> T runWithFailCheck(final Supplier<T> supp, final Runnable runnable) {
89 } catch (Exception ex) {
90 log.error("Exception during service call", ex);
96 protected void runSafeMethod(final Runnable runnable, final Consumer<HttpStatus> logAudit) {
99 logAudit.accept(HttpStatus.NO_CONTENT);
100 } catch (TeivException ex) {
101 logAudit.accept(ex.getStatus());
102 log.error("Exception during service call", ex);
108 public FilterRegistrationBean<ClassifiersRequestFilter> classifiersRequestFilter(final ObjectMapper objectMapper) {
109 FilterRegistrationBean<ClassifiersRequestFilter> registrationBean = new FilterRegistrationBean<>();
110 registrationBean.setFilter(new ClassifiersRequestFilter(loggerHandler, objectMapper, customMetrics));
111 registrationBean.addUrlPatterns(REQUEST_MAPPING + "/classifiers");
112 registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
113 return registrationBean;