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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.models;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import lombok.Builder;
28 import lombok.RequiredArgsConstructor;
32 public class CmHandleRegistrationResponse {
34 private final String cmHandle;
35 private final Status status;
36 private RegistrationError registrationError;
37 private String errorText;
40 * Creates a failure response based on exception.
42 * @param cmHandle cmHandle
43 * @param exception exception
44 * @return CmHandleRegistrationResponse
46 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle, final Exception exception) {
47 return CmHandleRegistrationResponse.builder()
49 .status(Status.FAILURE)
50 .registrationError(RegistrationError.UNKNOWN_ERROR)
51 .errorText(exception.getMessage()).build();
55 * Creates a failure response based on registration error.
57 * @param cmHandle cmHandle
58 * @param registrationError registrationError
59 * @return CmHandleRegistrationResponse
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)
70 public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
71 return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
72 .status(Status.SUCCESS).build();
75 public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
76 return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
77 .collect(Collectors.toList());
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");
91 public final String errorCode;
92 public final String errorText;