2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Ericsson
4 * Modifications Copyright (C) 2024 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 io.micrometer.core.annotation.Timed;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import jakarta.servlet.http.HttpServletRequest;
33 import org.oran.smo.teiv.CustomMetrics;
34 import org.oran.smo.teiv.api.ClassifiersApi;
35 import org.oran.smo.teiv.api.model.OranTeivClassifier;
36 import org.oran.smo.teiv.exception.TiesException;
37 import org.oran.smo.teiv.exposure.audit.AuditMapper;
38 import org.oran.smo.teiv.exposure.audit.LoggerHandler;
39 import org.oran.smo.teiv.exposure.classifiers.api.ClassifiersService;
40 import org.oran.smo.teiv.utils.TiesConstants;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RestController;
49 @RequestMapping(TiesConstants.REQUEST_MAPPING)
50 @RequiredArgsConstructor
51 public class ClassifiersRestController implements ClassifiersApi {
53 private final ClassifiersService classifiersService;
54 private final CustomMetrics customMetrics;
55 private final LoggerHandler loggerHandler;
56 private final HttpServletRequest context;
58 @Value("${consumer-data.batch-size}")
62 @Timed("ties_exposure_http_update_classifiers_seconds")
63 public ResponseEntity<Void> updateClassifier(final String accept, final String contentType,
64 final OranTeivClassifier oranTeivClassifier) {
65 return runWithFailCheck(() -> {
66 if (Optional.ofNullable(oranTeivClassifier.getEntityIds()).orElseGet(Collections::emptyList).size() + Optional
67 .ofNullable(oranTeivClassifier.getRelationshipIds()).orElseGet(Collections::emptyList).size() > limit) {
68 throw TiesException.clientException("Limit exceeded",
69 "Number of entities and relationships exceeded the limit");
71 runSafeMethod(() -> classifiersService.update(oranTeivClassifier), status -> loggerHandler.logAudit(log,
72 AuditMapper.fromClassifiersRequest(oranTeivClassifier, status).toString(), context));
74 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
75 }, customMetrics::incrementNumUnsuccessfullyUpdatedClassifiers);
78 private <T> T runWithFailCheck(final Supplier<T> supp, final Runnable runnable) {
81 } catch (Exception ex) {
82 log.error("Exception during service call", ex);
88 protected void runSafeMethod(final Runnable runnable, final Consumer<HttpStatus> logAudit) {
91 logAudit.accept(HttpStatus.NO_CONTENT);
92 } catch (TiesException ex) {
93 logAudit.accept(ex.getStatus());
94 log.error("Exception during service call", ex);