9a460f41020bd2d2ab0f659313cae4b1a9760148
[smo/teiv.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.teiv.exposure.decorators.rest.controller;
22
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;
28
29 import io.micrometer.core.annotation.Timed;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
32
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;
47
48 @Slf4j
49 @RestController
50 @RequestMapping(TeivConstants.REQUEST_MAPPING)
51 @RequiredArgsConstructor
52 @Profile("exposure")
53 public class DecoratorsRestController implements DecoratorsApi {
54
55     private final DecoratorsService decoratorsService;
56     private final CustomMetrics customMetrics;
57     private final LoggerHandler loggerHandler;
58     private final HttpServletRequest context;
59
60     @Value("${consumer-data.batch-size}")
61     private int limit;
62
63     @Override
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");
72             }
73             runSafeMethod(() -> decoratorsService.update(oranTeivDecorator), status -> loggerHandler.logAudit(log,
74                     AuditMapper.fromDecoratorsRequest(oranTeivDecorator, status).toString(), context));
75
76             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
77         }, customMetrics::incrementNumUnsuccessfullyUpdatedDecorators);
78     }
79
80     private <T> T runWithFailCheck(final Supplier<T> supp, final Runnable runnable) {
81         try {
82             return supp.get();
83         } catch (Exception ex) {
84             log.error("Exception during service call", ex);
85             runnable.run();
86             throw ex;
87         }
88     }
89
90     protected void runSafeMethod(final Runnable runnable, final Consumer<HttpStatus> logAudit) {
91         try {
92             runnable.run();
93             logAudit.accept(HttpStatus.NO_CONTENT);
94         } catch (TeivException ex) {
95             logAudit.accept(ex.getStatus());
96             log.error("Exception during service call", ex);
97             throw ex;
98         }
99     }
100 }