2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
4 * Modifications Copyright (C) 2022-2023 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 static org.onap.cps.ncmp.api.NcmpResponseStatus.UNKNOWN_ERROR;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 import lombok.Builder;
33 import lombok.extern.slf4j.Slf4j;
34 import org.onap.cps.ncmp.api.NcmpResponseStatus;
39 public class CmHandleRegistrationResponse {
41 private final String cmHandle;
42 private final Status status;
43 private NcmpResponseStatus ncmpResponseStatus;
44 private String errorText;
46 private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']");
49 * Creates a failure response based on exception.
51 * @param cmHandleId cmHandleId
52 * @param exception exception
53 * @return CmHandleRegistrationResponse
55 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
56 final Exception exception) {
57 return CmHandleRegistrationResponse.builder()
59 .status(Status.FAILURE)
60 .ncmpResponseStatus(UNKNOWN_ERROR)
61 .errorText(exception.getMessage()).build();
65 * Creates a failure response based on registration error.
67 * @param cmHandleId cmHandleId
68 * @param ncmpResponseStatus registration error code and status
69 * @return CmHandleRegistrationResponse
71 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
72 final NcmpResponseStatus ncmpResponseStatus) {
73 return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
74 .status(Status.FAILURE)
75 .ncmpResponseStatus(ncmpResponseStatus)
76 .errorText(ncmpResponseStatus.getMessage())
81 * Creates a failure response based on registration error.
83 * @param failedXpaths list of failed Xpaths
84 * @param ncmpResponseStatus enum describing the type of registration error
85 * @return CmHandleRegistrationResponse
87 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> failedXpaths,
88 final NcmpResponseStatus ncmpResponseStatus) {
89 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
90 for (final String xpath : failedXpaths) {
91 final Matcher matcher = cmHandleIdInXpathPattern.matcher(xpath);
93 cmHandleRegistrationResponses.add(
94 CmHandleRegistrationResponse.createFailureResponse(matcher.group(1), ncmpResponseStatus));
96 log.warn("Unexpected xpath {}", xpath);
99 return cmHandleRegistrationResponses;
103 * Creates a failure response based on other exception.
105 * @param cmHandleIds list of failed cmHandleIds
106 * @param exception exception caught during the registration
107 * @return CmHandleRegistrationResponse
109 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
110 final Exception exception) {
111 return cmHandleIds.stream()
112 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
116 public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
117 return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
118 .status(Status.SUCCESS).build();
121 public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
122 return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse).toList();