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 lombok.Builder;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.ncmp.api.NcmpResponseStatus;
33 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
38 public class CmHandleRegistrationResponse {
40 private final String cmHandle;
41 private final Status status;
42 private NcmpResponseStatus ncmpResponseStatus;
43 private String errorText;
46 * Creates a failure response based on exception.
48 * @param cmHandleId cmHandleId
49 * @param exception exception
50 * @return CmHandleRegistrationResponse
52 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
53 final Exception exception) {
54 return CmHandleRegistrationResponse.builder()
56 .status(Status.FAILURE)
57 .ncmpResponseStatus(UNKNOWN_ERROR)
58 .errorText(exception.getMessage()).build();
62 * Creates a failure response based on registration error.
64 * @param cmHandleId cmHandleId
65 * @param ncmpResponseStatus registration error code and status
66 * @return CmHandleRegistrationResponse
68 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
69 final NcmpResponseStatus ncmpResponseStatus) {
70 return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
71 .status(Status.FAILURE)
72 .ncmpResponseStatus(ncmpResponseStatus)
73 .errorText(ncmpResponseStatus.getMessage())
78 * Creates a failure response based on registration error.
80 * @param failedXpaths list of failed Xpaths
81 * @param ncmpResponseStatus enum describing the type of registration error
82 * @return CmHandleRegistrationResponse
84 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> failedXpaths,
85 final NcmpResponseStatus ncmpResponseStatus) {
86 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
87 for (final String xpath : failedXpaths) {
89 final String cmHandleId = YangDataConverter.extractCmHandleIdFromXpath(xpath);
90 cmHandleRegistrationResponses.add(
91 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, ncmpResponseStatus));
92 } catch (IllegalArgumentException | IllegalStateException e) {
93 log.warn("Unexpected xpath {}", xpath);
96 return cmHandleRegistrationResponses;
100 * Creates a failure response based on other exception.
102 * @param cmHandleIds list of failed cmHandleIds
103 * @param exception exception caught during the registration
104 * @return CmHandleRegistrationResponse
106 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
107 final Exception exception) {
108 return cmHandleIds.stream()
109 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
113 public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
114 return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
115 .status(Status.SUCCESS).build();
118 public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
119 return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse).toList();