Additional validation for names/identifiers
[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 lombok.Builder;
25 import lombok.Data;
26 import lombok.RequiredArgsConstructor;
27
28 @Data
29 @Builder
30 public class CmHandleRegistrationResponse {
31
32     private final String cmHandle;
33     private final Status status;
34     private RegistrationError registrationError;
35     private String errorText;
36
37     /**
38      * Creates a failure response based on exception.
39      *
40      * @param cmHandle  cmHandle
41      * @param exception exception
42      * @return CmHandleRegistrationResponse
43      */
44     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle, final Exception exception) {
45         return CmHandleRegistrationResponse.builder()
46             .cmHandle(cmHandle)
47             .status(Status.FAILURE)
48             .registrationError(RegistrationError.UNKNOWN_ERROR)
49             .errorText(exception.getMessage()).build();
50     }
51
52     /**
53      * Creates a failure response based on registration error.
54      *
55      * @param cmHandle          cmHandle
56      * @param registrationError registrationError
57      * @return CmHandleRegistrationResponse
58      */
59     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle,
60         final RegistrationError registrationError) {
61         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
62             .status(Status.FAILURE)
63             .registrationError(registrationError)
64             .errorText(registrationError.errorText)
65             .build();
66     }
67
68     public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
69         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
70             .status(Status.SUCCESS).build();
71     }
72
73     public enum Status {
74         SUCCESS, FAILURE;
75     }
76
77     @RequiredArgsConstructor
78     public enum RegistrationError {
79         UNKNOWN_ERROR("00", "Unknown error"),
80         CM_HANDLE_ALREADY_EXIST("01", "cm-handle already exists"),
81         CM_HANDLE_DOES_NOT_EXIST("02", "cm-handle does not exist"),
82         CM_HANDLE_INVALID_ID("03", "cm-handle has an invalid character(s) in id");
83
84         public final String errorCode;
85         public final String errorText;
86
87     }
88 }