Handle partial failure
[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.Collection;
25 import java.util.List;
26 import java.util.stream.Collectors;
27 import lombok.Builder;
28 import lombok.Data;
29 import lombok.RequiredArgsConstructor;
30
31 @Data
32 @Builder
33 public class CmHandleRegistrationResponse {
34
35     private final String cmHandle;
36     private final Status status;
37     private RegistrationError registrationError;
38     private String errorText;
39
40     /**
41      * Creates a failure response based on exception.
42      *
43      * @param cmHandle  cmHandle
44      * @param exception exception
45      * @return CmHandleRegistrationResponse
46      */
47     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle, final Exception exception) {
48         return CmHandleRegistrationResponse.builder()
49             .cmHandle(cmHandle)
50             .status(Status.FAILURE)
51             .registrationError(RegistrationError.UNKNOWN_ERROR)
52             .errorText(exception.getMessage()).build();
53     }
54
55     /**
56      * Creates a failure response based on registration error.
57      *
58      * @param cmHandle          cmHandle
59      * @param registrationError registrationError
60      * @return CmHandleRegistrationResponse
61      */
62     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle,
63         final RegistrationError registrationError) {
64         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
65             .status(Status.FAILURE)
66             .registrationError(registrationError)
67             .errorText(registrationError.errorText)
68             .build();
69     }
70
71     /**
72      * Creates a failure response based on registration error.
73      *
74      * @param cmHandleIds       list of failed cmHandleIds
75      * @param registrationError enum describing the type of registration error
76      * @return CmHandleRegistrationResponse
77      */
78     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
79             final RegistrationError registrationError) {
80         return cmHandleIds.stream()
81                 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, registrationError))
82                 .collect(Collectors.toList());
83     }
84
85     /**
86      * Creates a failure response based on other exception.
87      *
88      * @param cmHandleIds list of failed cmHandleIds
89      * @param exception   exception caught during the registration
90      * @return CmHandleRegistrationResponse
91      */
92     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
93             final Exception exception) {
94         return cmHandleIds.stream()
95                 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
96                 .collect(Collectors.toList());
97     }
98
99     public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
100         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
101             .status(Status.SUCCESS).build();
102     }
103
104     public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
105         return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
106                 .collect(Collectors.toList());
107     }
108
109     public enum Status {
110         SUCCESS, FAILURE;
111     }
112
113     @RequiredArgsConstructor
114     public enum RegistrationError {
115         UNKNOWN_ERROR("00", "Unknown error"),
116         CM_HANDLE_ALREADY_EXIST("01", "cm-handle already exists"),
117         CM_HANDLE_DOES_NOT_EXIST("02", "cm-handle does not exist"),
118         CM_HANDLE_INVALID_ID("03", "cm-handle has an invalid character(s) in id");
119
120         public final String errorCode;
121         public final String errorText;
122
123     }
124 }