Performance Improvement: Use save batches of cmhandles
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / models / CmHandleRegistrationResponse.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada
4  *  Modifications Copyright (C) 2022 Nordix Foundation
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
22 package org.onap.cps.ncmp.api.models;
23
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import lombok.Builder;
27 import lombok.Data;
28 import lombok.RequiredArgsConstructor;
29
30 @Data
31 @Builder
32 public class CmHandleRegistrationResponse {
33
34     private final String cmHandle;
35     private final Status status;
36     private RegistrationError registrationError;
37     private String errorText;
38
39     /**
40      * Creates a failure response based on exception.
41      *
42      * @param cmHandle  cmHandle
43      * @param exception exception
44      * @return CmHandleRegistrationResponse
45      */
46     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle, final Exception exception) {
47         return CmHandleRegistrationResponse.builder()
48             .cmHandle(cmHandle)
49             .status(Status.FAILURE)
50             .registrationError(RegistrationError.UNKNOWN_ERROR)
51             .errorText(exception.getMessage()).build();
52     }
53
54     /**
55      * Creates a failure response based on registration error.
56      *
57      * @param cmHandle          cmHandle
58      * @param registrationError registrationError
59      * @return CmHandleRegistrationResponse
60      */
61     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle,
62         final RegistrationError registrationError) {
63         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
64             .status(Status.FAILURE)
65             .registrationError(registrationError)
66             .errorText(registrationError.errorText)
67             .build();
68     }
69
70     public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
71         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
72             .status(Status.SUCCESS).build();
73     }
74
75     public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
76         return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
77                 .collect(Collectors.toList());
78     }
79
80     public enum Status {
81         SUCCESS, FAILURE;
82     }
83
84     @RequiredArgsConstructor
85     public enum RegistrationError {
86         UNKNOWN_ERROR("00", "Unknown error"),
87         CM_HANDLE_ALREADY_EXIST("01", "cm-handle already exists"),
88         CM_HANDLE_DOES_NOT_EXIST("02", "cm-handle does not exist"),
89         CM_HANDLE_INVALID_ID("03", "cm-handle has an invalid character(s) in id");
90
91         public final String errorCode;
92         public final String errorText;
93
94     }
95 }