aboutsummaryrefslogtreecommitdiffstats
path: root/lib/src/main/java/org/onap/portalng/bff/services/ActionService.java
blob: cb7b6b4fb3ffae8707def014084cd93d1df212e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *
 * Copyright (c) 2022. Deutsche Telekom AG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 *
 */

package org.onap.portalng.bff.services;

import lombok.RequiredArgsConstructor;
import org.onap.portalng.bff.exceptions.DownstreamApiProblemException;
import org.onap.portalng.bff.openapi.client_history.api.ActionsApi;
import org.onap.portalng.bff.openapi.client_history.model.CreateActionRequestHistoryDto;
import org.onap.portalng.bff.openapi.server.model.ActionsListResponseApiDto;
import org.onap.portalng.bff.openapi.server.model.ActionsResponseApiDto;
import org.onap.portalng.bff.openapi.server.model.CreateActionRequestApiDto;
import org.onap.portalng.bff.openapi.server.model.ProblemApiDto;
import org.onap.portalng.bff.utils.Logger;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@RequiredArgsConstructor
@Service
public class ActionService {
  private final ActionsApi actionsApi;
  private final ConfigurableConversionService conversionService;

  public Mono<ActionsResponseApiDto> createAction(
      String userId, String xRequestId, CreateActionRequestApiDto createActionRequestApiDto) {
    // First map from server API model to client API model
    CreateActionRequestHistoryDto createActionRequestHistoryDto =
        new CreateActionRequestHistoryDto();
    createActionRequestHistoryDto.setUserId(createActionRequestApiDto.getUserId());
    createActionRequestHistoryDto.setAction(createActionRequestApiDto.getAction());
    createActionRequestHistoryDto.setActionCreatedAt(
        createActionRequestApiDto.getActionCreatedAt());

    return actionsApi
        .createAction(userId, xRequestId, createActionRequestHistoryDto)
        .map(
            action ->
                new ActionsResponseApiDto()
                    .action(action.getAction())
                    .actionCreatedAt(action.getActionCreatedAt()))
        .onErrorResume(
            DownstreamApiProblemException.class,
            ex -> {
              Logger.errorLog(
                  xRequestId,
                  "Create actions failed for userId",
                  userId,
                  ProblemApiDto.DownstreamSystemEnum.PORTAL_HISTORY.toString());
              return Mono.error(ex);
            });
  }

  public Mono<ActionsListResponseApiDto> getActions(
      String userId, String xRequestId, Integer page, Integer pageSize, Integer showLastHours) {

    return actionsApi
        .getActions(userId, xRequestId, page, pageSize, showLastHours)
        .map(actions -> conversionService.convert(actions, ActionsListResponseApiDto.class))
        .onErrorResume(
            DownstreamApiProblemException.class,
            ex -> {
              Logger.errorLog(
                  xRequestId,
                  "Get actions failed for userId",
                  userId,
                  ProblemApiDto.DownstreamSystemEnum.PORTAL_HISTORY.toString());
              return Mono.error(ex);
            });
  }

  public Mono<ActionsListResponseApiDto> listActions(
      String xRequestId, Integer page, Integer pageSize, Integer showLast) {
    return actionsApi
        .listActions(xRequestId, page, pageSize, showLast)
        .map(
            responseEntity ->
                conversionService.convert(responseEntity, ActionsListResponseApiDto.class))
        .onErrorResume(
            DownstreamApiProblemException.class,
            ex -> {
              Logger.errorLog(
                  xRequestId,
                  "List actions failed",
                  null,
                  ProblemApiDto.DownstreamSystemEnum.PORTAL_HISTORY.toString());
              return Mono.error(ex);
            });
  }

  public Mono<Object> deleteActions(String userId, String xRequestId, Integer deleteAfterHours) {
    return actionsApi
        .deleteActions(userId, xRequestId, deleteAfterHours)
        .onErrorResume(
            DownstreamApiProblemException.class,
            ex -> {
              Logger.errorLog(
                  xRequestId,
                  "Get actions failed for userId because actions cannot be deleted after "
                      + deleteAfterHours
                      + " hours",
                  userId,
                  ProblemApiDto.DownstreamSystemEnum.PORTAL_HISTORY.toString());
              return Mono.error(ex);
            });
  }
}