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.decorators.rest.controller;
23 import java.util.Collections;
24 import java.util.Optional;
25 import java.util.function.Consumer;
26 import java.util.function.Supplier;
27 import jakarta.servlet.http.HttpServletRequest;
29 import io.micrometer.core.annotation.Timed;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
33 import org.oran.smo.teiv.CustomMetrics;
34 import org.oran.smo.teiv.api.DecoratorsApi;
35 import org.oran.smo.teiv.api.model.OranTeivDecorator;
36 import org.oran.smo.teiv.exception.TeivException;
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.decorators.api.DecoratorsService;
40 import org.oran.smo.teiv.utils.TeivConstants;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.context.annotation.Profile;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RestController;
50 @RequestMapping(TeivConstants.REQUEST_MAPPING)
51 @RequiredArgsConstructor
53 public class DecoratorsRestController implements DecoratorsApi {
55 private final DecoratorsService decoratorsService;
56 private final CustomMetrics customMetrics;
57 private final LoggerHandler loggerHandler;
58 private final HttpServletRequest context;
60 @Value("${consumer-data.batch-size}")
64 @Timed("teiv_exposure_http_update_decorators_seconds")
65 public ResponseEntity<Void> updateDecorator(final String accept, final String contentType,
66 final OranTeivDecorator oranTeivDecorator) {
67 return runWithFailCheck(() -> {
68 if (Optional.ofNullable(oranTeivDecorator.getEntityIds()).orElseGet(Collections::emptyList).size() + Optional
69 .ofNullable(oranTeivDecorator.getRelationshipIds()).orElseGet(Collections::emptyList).size() > limit) {
70 throw TeivException.clientException("Limit exceeded",
71 "Number of entities and relationships exceeded the limit");
73 runSafeMethod(() -> decoratorsService.update(oranTeivDecorator), status -> loggerHandler.logAudit(log,
74 AuditMapper.fromDecoratorsRequest(oranTeivDecorator, status).toString(), context));
76 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
77 }, customMetrics::incrementNumUnsuccessfullyUpdatedDecorators);
80 private <T> T runWithFailCheck(final Supplier<T> supp, final Runnable runnable) {
83 } catch (Exception ex) {
84 log.error("Exception during service call", ex);
90 protected void runSafeMethod(final Runnable runnable, final Consumer<HttpStatus> logAudit) {
93 logAudit.accept(HttpStatus.NO_CONTENT);
94 } catch (TeivException ex) {
95 logAudit.accept(ex.getStatus());
96 log.error("Exception during service call", ex);