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.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import java.util.stream.Collectors;
30 import lombok.Builder;
32 import lombok.RequiredArgsConstructor;
33 import lombok.extern.slf4j.Slf4j;
38 public class CmHandleRegistrationResponse {
40 private final String cmHandle;
41 private final Status status;
42 private RegistrationError registrationError;
43 private String errorText;
45 private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']");
48 * Creates a failure response based on exception.
50 * @param cmHandleId cmHandleId
51 * @param exception exception
52 * @return CmHandleRegistrationResponse
54 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
55 final Exception exception) {
56 return CmHandleRegistrationResponse.builder()
58 .status(Status.FAILURE)
59 .registrationError(RegistrationError.UNKNOWN_ERROR)
60 .errorText(exception.getMessage()).build();
64 * Creates a failure response based on registration error.
66 * @param cmHandleId cmHandleId
67 * @param registrationError registrationError
68 * @return CmHandleRegistrationResponse
70 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
71 final RegistrationError registrationError) {
72 return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
73 .status(Status.FAILURE)
74 .registrationError(registrationError)
75 .errorText(registrationError.errorText)
80 * Creates a failure response based on registration error.
82 * @param failedXpaths list of failed Xpaths
83 * @param registrationError enum describing the type of registration error
84 * @return CmHandleRegistrationResponse
86 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> failedXpaths,
87 final RegistrationError registrationError) {
88 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
89 for (final String xpath : failedXpaths) {
90 final Matcher matcher = cmHandleIdInXpathPattern.matcher(xpath);
92 cmHandleRegistrationResponses.add(
93 CmHandleRegistrationResponse.createFailureResponse(matcher.group(1), registrationError));
95 log.warn("Unexpected xpath {}", xpath);
98 return cmHandleRegistrationResponses;
102 * Creates a failure response based on other exception.
104 * @param cmHandleIds list of failed cmHandleIds
105 * @param exception exception caught during the registration
106 * @return CmHandleRegistrationResponse
108 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
109 final Exception exception) {
110 return cmHandleIds.stream()
111 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
112 .collect(Collectors.toList());
115 public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
116 return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
117 .status(Status.SUCCESS).build();
120 public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
121 return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
122 .collect(Collectors.toList());
129 @RequiredArgsConstructor
130 public enum RegistrationError {
131 UNKNOWN_ERROR("00", "Unknown error"),
132 CM_HANDLE_ALREADY_EXIST("01", "cm-handle already exists"),
133 CM_HANDLE_DOES_NOT_EXIST("02", "cm-handle does not exist"),
134 CM_HANDLE_INVALID_ID("03", "cm-handle has an invalid character(s) in id");
136 public final String errorCode;
137 public final String errorText;