Return Registration response for updating 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  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.models;
22
23 import lombok.Builder;
24 import lombok.Data;
25 import lombok.RequiredArgsConstructor;
26
27 @Data
28 @Builder
29 public class CmHandleRegistrationResponse {
30
31     private final String cmHandle;
32     private final Status status;
33     private RegistrationError registrationError;
34     private String errorText;
35
36     /**
37      * Creates a failure response based on exception.
38      *
39      * @param cmHandle  cmHandle
40      * @param exception exception
41      * @return CmHandleRegistrationResponse
42      */
43     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle, final Exception exception) {
44         return CmHandleRegistrationResponse.builder()
45             .cmHandle(cmHandle)
46             .status(Status.FAILURE)
47             .registrationError(RegistrationError.UNKNOWN_ERROR)
48             .errorText(exception.getMessage()).build();
49     }
50
51     /**
52      * Creates a failure response based on registration error.
53      *
54      * @param cmHandle          cmHandle
55      * @param registrationError registrationError
56      * @return CmHandleRegistrationResponse
57      */
58     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle,
59         final RegistrationError registrationError) {
60         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
61             .status(Status.FAILURE)
62             .registrationError(registrationError)
63             .errorText(registrationError.errorText)
64             .build();
65     }
66
67     public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
68         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
69             .status(Status.SUCCESS).build();
70     }
71
72     public enum Status {
73         SUCCESS, FAILURE;
74     }
75
76     @RequiredArgsConstructor
77     public enum RegistrationError {
78         UNKNOWN_ERROR("00", "Unknown error"),
79         CM_HANDLE_ALREADY_EXIST("01", "cm-handle already exists"),
80         CM_HANDLE_DOES_NOT_EXIST("02", "cm-handle does not exist");
81
82         public final String errorCode;
83         public final String errorText;
84
85     }
86 }