3f499028d3191b4cdb4e9a325af74c3030173eaa
[smo/teiv.git] /
1 /*
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
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.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.decorators.api.DecoratorsService;
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;
46
47 @Slf4j
48 @RestController
49 @RequestMapping(TiesConstants.REQUEST_MAPPING)
50 @RequiredArgsConstructor
51 public class DecoratorsRestController implements DecoratorsApi {
52
53     private final DecoratorsService decoratorsService;
54     private final CustomMetrics customMetrics;
55     private final LoggerHandler loggerHandler;
56     private final HttpServletRequest context;
57
58     @Value("${consumer-data.batch-size}")
59     private int limit;
60
61     @Override
62     @Timed("ties_exposure_http_update_decorators_seconds")
63     public ResponseEntity<Void> updateDecorator(final String accept, final String contentType,
64             final OranTeivDecorator oranTeivDecorator) {
65         return runWithFailCheck(() -> {
66             if (Optional.ofNullable(oranTeivDecorator.getEntityIds()).orElseGet(Collections::emptyList).size() + Optional
67                     .ofNullable(oranTeivDecorator.getRelationshipIds()).orElseGet(Collections::emptyList).size() > limit) {
68                 throw TiesException.clientException("Limit exceeded",
69                         "Number of entities and relationships exceeded the limit");
70             }
71             runSafeMethod(() -> decoratorsService.update(oranTeivDecorator), status -> loggerHandler.logAudit(log,
72                     AuditMapper.fromDecoratorsRequest(oranTeivDecorator, status).toString(), context));
73
74             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
75         }, customMetrics::incrementNumUnsuccessfullyUpdatedDecorators);
76     }
77
78     private <T> T runWithFailCheck(final Supplier<T> supp, final Runnable runnable) {
79         try {
80             return supp.get();
81         } catch (Exception ex) {
82             log.error("Exception during service call", ex);
83             runnable.run();
84             throw ex;
85         }
86     }
87
88     protected void runSafeMethod(final Runnable runnable, final Consumer<HttpStatus> logAudit) {
89         try {
90             runnable.run();
91             logAudit.accept(HttpStatus.NO_CONTENT);
92         } catch (TiesException ex) {
93             logAudit.accept(ex.getStatus());
94             log.error("Exception during service call", ex);
95             throw ex;
96         }
97     }
98 }