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.Collection;
25 import java.util.List;
26 import java.util.stream.Collectors;
27 import lombok.Builder;
29 import lombok.RequiredArgsConstructor;
33 public class CmHandleRegistrationResponse {
35 private final String cmHandle;
36 private final Status status;
37 private RegistrationError registrationError;
38 private String errorText;
41 * Creates a failure response based on exception.
43 * @param cmHandle cmHandle
44 * @param exception exception
45 * @return CmHandleRegistrationResponse
47 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandle, final Exception exception) {
48 return CmHandleRegistrationResponse.builder()
50 .status(Status.FAILURE)
51 .registrationError(RegistrationError.UNKNOWN_ERROR)
52 .errorText(exception.getMessage()).build();
56 * Creates a failure response based on registration error.
58 * @param cmHandle cmHandle
59 * @param registrationError registrationError
60 * @return CmHandleRegistrationResponse
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)
72 * Creates a failure response based on registration error.
74 * @param cmHandleIds list of failed cmHandleIds
75 * @param registrationError enum describing the type of registration error
76 * @return CmHandleRegistrationResponse
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());
86 * Creates a failure response based on other exception.
88 * @param cmHandleIds list of failed cmHandleIds
89 * @param exception exception caught during the registration
90 * @return CmHandleRegistrationResponse
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());
99 public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
100 return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
101 .status(Status.SUCCESS).build();
104 public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
105 return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
106 .collect(Collectors.toList());
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");
120 public final String errorCode;
121 public final String errorText;