2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
4 * Modifications Copyright (C) 2022-2024 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.stream.Collectors;
30 import lombok.Builder;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.ncmp.api.NcmpResponseStatus;
34 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
39 public class CmHandleRegistrationResponse {
41 private final String cmHandle;
42 private final Status status;
43 private NcmpResponseStatus ncmpResponseStatus;
44 private String errorText;
47 * Creates a failure response based on exception.
49 * @param cmHandleId cmHandleId
50 * @param exception exception
51 * @return CmHandleRegistrationResponse
53 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
54 final Exception exception) {
55 return CmHandleRegistrationResponse.builder()
57 .status(Status.FAILURE)
58 .ncmpResponseStatus(UNKNOWN_ERROR)
59 .errorText(exception.getMessage()).build();
63 * Creates a failure response based on registration error.
65 * @param cmHandleId cmHandleId
66 * @param ncmpResponseStatus registration error code and status
67 * @return CmHandleRegistrationResponse
69 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
70 final NcmpResponseStatus ncmpResponseStatus) {
71 return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
72 .status(Status.FAILURE)
73 .ncmpResponseStatus(ncmpResponseStatus)
74 .errorText(ncmpResponseStatus.getMessage())
79 * Create a failure response of cm handle registration based on xpath and registration error.
81 * - the xpath should be valid according to the cps path, otherwise xpath is not included in the response.
83 * @param failedXpaths the failed xpaths
84 * @param ncmpResponseStatus type of the registration error
85 * @return collection of cm handle registration response
87 public static List<CmHandleRegistrationResponse> createFailureResponsesFromXpaths(
88 final Collection<String> failedXpaths, final NcmpResponseStatus ncmpResponseStatus) {
89 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
90 for (final String xpath : failedXpaths) {
92 final String cmHandleId = YangDataConverter.extractCmHandleIdFromXpath(xpath);
93 cmHandleRegistrationResponses
94 .add(CmHandleRegistrationResponse.createFailureResponse(cmHandleId, ncmpResponseStatus));
95 } catch (IllegalArgumentException | IllegalStateException e) {
96 log.warn("Unexpected xpath {}", xpath);
99 return cmHandleRegistrationResponses;
103 * Create a failure response of cm handle registration based on cm handle id and registration error.
105 * @param failedCmHandleIds the failed cm handle ids
106 * @param ncmpResponseStatus type of the registration error
107 * @return collection of cm handle registration response
109 public static List<CmHandleRegistrationResponse> createFailureResponses(
110 final Collection<String> failedCmHandleIds, final NcmpResponseStatus ncmpResponseStatus) {
111 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
112 new ArrayList<>(failedCmHandleIds.size());
113 for (final String failedCmHandleId : failedCmHandleIds) {
114 cmHandleRegistrationResponses.add(
115 CmHandleRegistrationResponse.createFailureResponse(failedCmHandleId, ncmpResponseStatus));
117 return cmHandleRegistrationResponses;
121 * Creates a failure response based on other exception.
123 * @param cmHandleIds list of failed cmHandleIds
124 * @param exception exception caught during the registration
125 * @return CmHandleRegistrationResponse
127 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
128 final Exception exception) {
129 return cmHandleIds.stream()
130 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
134 public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
135 return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
136 .status(Status.SUCCESS).build();
139 public static List<CmHandleRegistrationResponse> createSuccessResponses(final Collection<String> cmHandleIds) {
140 return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
141 .collect(Collectors.toList());